Skip to content

Commit 292f05b

Browse files
committed
Follow-up to #112 to resolve fundamental problem
1 parent 4486d21 commit 292f05b

File tree

4 files changed

+104
-13
lines changed

4 files changed

+104
-13
lines changed

jr-objects/src/main/java/com/fasterxml/jackson/jr/ob/impl/ValueWriterLocator.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,27 @@ protected BeanPropertyWriter[] _resolveBeanForSer(Class<?> raw, POJODefinition b
309309
f.setAccessible(true);
310310
}
311311
}
312+
// NOTE: cannot just call `findSerializationType()` due to
313+
// cyclic type definitions.
314+
315+
int typeId;
316+
Integer I = _knownSerTypes.get(new ClassKey(type, _features));
317+
318+
if (I != null) {
319+
typeId = I.intValue();
320+
} else {
321+
typeId = _findSimpleType(type, true);
322+
if ((_writerModifier != null) && typeId != 0) {
323+
ValueWriter w = _writerModifier.overrideStandardValueWriter(_writeContext, type, typeId);
324+
if (w != null) {
325+
typeId = _registerWriter(type, w);
326+
}
327+
}
328+
// But what if none found? Discovered dynamically later on?
329+
}
330+
331+
// 16-Feb-2024, tatu: Code pre-2.17 -- remove from 2.18 if not needed
332+
/*
312333
int typeId = _findSimpleType(type, true);
313334
// Give plugin the opportunity to override standard value writer
314335
if (_writerModifier != null && typeId != 0) {
@@ -318,8 +339,11 @@ protected BeanPropertyWriter[] _resolveBeanForSer(Class<?> raw, POJODefinition b
318339
if (w != null) {
319340
typeId = _registerWriter(type, w);
320341
}
342+
} else {
343+
typeId = I.intValue();
321344
}
322345
}
346+
*/
323347
props.add(new BeanPropertyWriter(typeId, rawProp.name, rawProp.field, m));
324348
}
325349
int plen = props.size();

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,24 @@ public StringBeanBean(StringBean b) {
5555
bean = b;
5656
}
5757
}
58+
59+
static class TreeNodeBean {
60+
public int id;
61+
public String name;
62+
63+
public TreeNodeBean nextLeft, nextRight;
64+
65+
public TreeNodeBean(int id, String name) {
66+
this.id = id;
67+
this.name = name;
68+
}
69+
}
70+
71+
/*
72+
/**********************************************************************
73+
/* Test methods
74+
/**********************************************************************
75+
*/
5876

5977
public void testBinary() throws Exception
6078
{
@@ -128,4 +146,19 @@ public void testBeanNulls() throws Exception
128146
assertEquals(a2q("{'bean':{'value':null}}"),
129147
withNulls.asString(new StringBeanBean(new StringBean(null))));
130148
}
149+
150+
public void testBeanWithRecursiveType() throws Exception
151+
{
152+
TreeNodeBean root = new TreeNodeBean(1, "root");
153+
TreeNodeBean left = new TreeNodeBean(2, "left");
154+
TreeNodeBean right = new TreeNodeBean(3, "right");
155+
root.nextLeft = left;
156+
root.nextRight = right;
157+
158+
assertEquals(a2q("{'id':1,'name':'root',"
159+
+"'nextLeft':{'id':2,'name':'left'},"
160+
+"'nextRight':{'id':3,'name':'right'}"
161+
+"}"),
162+
JSON.std.asString(root));
163+
}
131164
}

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

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
import com.fasterxml.jackson.jr.ob.*;
1111
import com.fasterxml.jackson.jr.ob.api.*;
12-
import com.fasterxml.jackson.jr.ob.impl.JSONWriter;
1312

1413
public class ValueWriterModifier112Test extends TestBase
1514
{
@@ -18,6 +17,12 @@ static class TestBean112 {
1817
public Path p2;
1918
}
2019

20+
static class StringBean112 {
21+
public String s1;
22+
public String s2;
23+
public String s3;
24+
}
25+
2126
static class PathWriter implements ValueWriter {
2227
@Override
2328
public void writeValue(JSONWriter context, JsonGenerator g, Object value) throws IOException {
@@ -30,24 +35,32 @@ public Class<?> valueType() {
3035
}
3136
}
3237

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");
38+
static class UpperCaseWriter implements ValueWriter {
39+
@Override
40+
public void writeValue(JSONWriter context, JsonGenerator g, Object value) throws IOException {
41+
g.writeString(String.valueOf(value).toUpperCase());
42+
}
43+
44+
@Override
45+
public Class<?> valueType() {
46+
return String.class;
47+
}
48+
}
3949

40-
JSON writer = JSON.builder()
50+
private final JSON WRITER = JSON.builder()
4151
.register(new JacksonJrExtension() {
4252
@Override
4353
protected void register(ExtensionContext ctxt) {
4454
ctxt.insertModifier(new ReaderWriterModifier() {
4555
@Override
4656
public ValueWriter overrideStandardValueWriter(JSONWriter writeContext, Class<?> type, int stdTypeId) {
47-
if (type == Path.class) {
57+
if (Path.class.isAssignableFrom(type)) {
4858
return new PathWriter();
4959
}
50-
return super.overrideStandardValueWriter(writeContext, type, stdTypeId);
60+
if (type == String.class) {
61+
return new UpperCaseWriter();
62+
}
63+
return null;
5164
}
5265
});
5366
ctxt.insertProvider(new ReaderWriterProvider() {
@@ -56,12 +69,29 @@ public ValueWriter findValueWriter(JSONWriter writeContext, Class<?> type) {
5669
if (Path.class.isAssignableFrom(type)) {
5770
return new PathWriter();
5871
}
59-
return super.findValueWriter(writeContext, type);
72+
return null;
6073
}
6174
});
6275
}
6376
}).build();
64-
String json = writer.asString(input);
77+
78+
// [jackson-jr#112]
79+
public void testMultipleFieldOverrides() throws Exception
80+
{
81+
TestBean112 input = new TestBean112();
82+
input.p1 = Paths.get("some/path");
83+
input.p2 = Paths.get("some/other/path");
84+
String json = WRITER.asString(input);
6585
assertEquals(a2q("{'p1':'some/path','p2':'some/other/path'}"), json);
6686
}
87+
88+
public void testMultipleStringFieldOverrides() throws Exception
89+
{
90+
StringBean112 input = new StringBean112();
91+
input.s1 = "abc";
92+
input.s2 = "def";
93+
input.s3 = "g";
94+
String json = WRITER.asString(input);
95+
assertEquals(a2q("{'s1':'ABC','s2':'DEF','s3':'G'}"), json);
96+
}
6797
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,12 @@ public Class<?> valueType() {
112112
assertEquals(quote("foobar"), JSON.std.asString(input));
113113

114114
// And then also applicable for multiple POJO properties
115-
assertEquals(a2q("{'first':'Bob','last':'Hope'}"),
115+
assertEquals(a2q("{'first':'BOB','last':'HOPE'}"),
116116
jsonWithMod.asString(new Name("Bob", "Hope")));
117+
118+
// .. and not global standard variant
119+
assertEquals(a2q("{'first':'Bob','last':'Hope'}"),
120+
JSON.std.asString(new Name("Bob", "Hope")));
117121
}
118122

119123
public void testPOJOWriterReplacement() throws Exception

0 commit comments

Comments
 (0)