Skip to content

Commit fa1acbb

Browse files
committed
Add failing test for #112
1 parent 8d5be9e commit fa1acbb

File tree

4 files changed

+74
-6
lines changed

4 files changed

+74
-6
lines changed

jr-objects/src/test/java/com/fasterxml/jackson/jr/failing/ReadEnumMap21Test.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void testMapWithEnumKey() throws Exception
2929
WithEnumMap input = new WithEnumMap(DEF.E, "bar");
3030
// verify serialization, should be ok:
3131
String json = JSON.std.asString(input);
32-
assertEquals(aposToQuotes("{'values':{'E':'bar'}}"), json);
32+
assertEquals(a2q("{'values':{'E':'bar'}}"), json);
3333

3434
// and then get it back too
3535
WithEnumMap result = JSON.std.beanFrom(WithEnumMap.class, json);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.fasterxml.jackson.jr.failing;
2+
3+
import java.io.File;
4+
import java.io.IOException;
5+
import java.nio.file.Path;
6+
import java.nio.file.Paths;
7+
8+
import com.fasterxml.jackson.core.JsonGenerator;
9+
10+
import com.fasterxml.jackson.jr.ob.*;
11+
import com.fasterxml.jackson.jr.ob.api.*;
12+
import com.fasterxml.jackson.jr.ob.impl.JSONWriter;
13+
14+
public class ValueWriterModifier112Test extends TestBase
15+
{
16+
static class TestBean112 {
17+
public Path p1;
18+
public Path p2;
19+
}
20+
21+
static class PathWriter implements ValueWriter {
22+
@Override
23+
public void writeValue(JSONWriter context, JsonGenerator g, Object value) throws IOException {
24+
g.writeString(((Path) value).toString().replace(File.separatorChar, '/'));
25+
}
26+
27+
@Override
28+
public Class<?> valueType() {
29+
return Path.class;
30+
}
31+
}
32+
33+
// [jackson-jr#112]
34+
public void testMultipleFieldOverrides() throws Exception
35+
{
36+
TestBean112 input = new TestBean112();
37+
input.p1 = Paths.get("some/path");
38+
input.p2 = Paths.get("some/other/path");
39+
40+
JSON writer = JSON.builder()
41+
.register(new JacksonJrExtension() {
42+
@Override
43+
protected void register(ExtensionContext ctxt) {
44+
ctxt.insertModifier(new ReaderWriterModifier() {
45+
@Override
46+
public ValueWriter overrideStandardValueWriter(JSONWriter writeContext, Class<?> type, int stdTypeId) {
47+
if (type == Path.class) {
48+
return new PathWriter();
49+
}
50+
return super.overrideStandardValueWriter(writeContext, type, stdTypeId);
51+
}
52+
});
53+
ctxt.insertProvider(new ReaderWriterProvider() {
54+
@Override
55+
public ValueWriter findValueWriter(JSONWriter writeContext, Class<?> type) {
56+
if (Path.class.isAssignableFrom(type)) {
57+
return new PathWriter();
58+
}
59+
return super.findValueWriter(writeContext, type);
60+
}
61+
});
62+
}
63+
}).build();
64+
String json = writer.asString(input);
65+
assertEquals(a2q("{'p1':'some/path','p2':'some/other/path'}"), json);
66+
}
67+
}

jr-objects/src/test/java/com/fasterxml/jackson/jr/ob/TestBase.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,12 @@ protected String quote(String str) {
5757
}
5858

5959
protected String a2q(String json) {
60-
return aposToQuotes(json);
60+
return json.replace('\'', '"');
6161
}
6262

63+
@Deprecated
6364
protected String aposToQuotes(String json) {
64-
return json.replace("'", "\"");
65+
return a2q(json);
6566
}
6667

6768
protected JSON jsonWithModifier(final ReaderWriterModifier modifier) {

jr-objects/src/test/java/com/fasterxml/jackson/jr/ob/impl/ValueWriterModifierTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public Class<?> valueType() {
136136
String json = jsonWithModifier(mod).asString(input);
137137
assertEquals(quote("Foo-Bar"), json);
138138
// but also verify that no caching occurs wrt global standard variant:
139-
assertEquals(aposToQuotes("{'first':'Foo','last':'Bar'}"),
139+
assertEquals(a2q("{'first':'Foo','last':'Bar'}"),
140140
JSON.std.asString(input));
141141
}
142142

@@ -145,10 +145,10 @@ public void testPOJOWriterDelegatingReplacement() throws Exception
145145
final NameBean input = new NameBean("Foo", "Bar");
146146
String json = jsonWithModifier(new ArrayingWriterModifier())
147147
.asString(input);
148-
assertEquals(aposToQuotes("[{'first':'Foo','last':'Bar'}]"), json);
148+
assertEquals(a2q("[{'first':'Foo','last':'Bar'}]"), json);
149149

150150
// but also verify that no caching occurs wrt global standard variant:
151-
assertEquals(aposToQuotes("{'first':'Foo','last':'Bar'}"),
151+
assertEquals(a2q("{'first':'Foo','last':'Bar'}"),
152152
JSON.std.asString(input));
153153
}
154154
}

0 commit comments

Comments
 (0)