Skip to content

Commit b06f9c4

Browse files
committed
json-iterator#70 allow private class in reflection mode
1 parent 020d6a3 commit b06f9c4

4 files changed

Lines changed: 32 additions & 17 deletions

File tree

src/main/java/com/jsoniter/output/Codegen.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,14 @@ private static synchronized Encoder gen(final String cacheKey, Type type) {
7070
}
7171
addPlaceholderEncoderToSupportRecursiveStructure(cacheKey);
7272
try {
73-
type = chooseAccessibleSuper(type);
73+
EncodingMode mode = JsoniterSpi.getCurrentConfig().encodingMode();
74+
if (mode != EncodingMode.REFLECTION_MODE) {
75+
type = chooseAccessibleSuper(type);
76+
}
7477
ClassInfo classInfo = new ClassInfo(type);
7578
if (Map.class.isAssignableFrom(classInfo.clazz) && classInfo.typeArgs.length > 1) {
7679
DefaultMapKeyEncoder.registerOrGetExisting(classInfo.typeArgs[0]);
7780
}
78-
EncodingMode mode = JsoniterSpi.getCurrentConfig().encodingMode();
7981
if (mode == EncodingMode.REFLECTION_MODE) {
8082
encoder = ReflectionEncoderFactory.create(classInfo);
8183
return encoder;

src/test/java/com/jsoniter/output/TestNested.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ public void test_object_of_array() throws IOException {
7070
stream.writeVal(obj);
7171
stream.close();
7272
assertEquals("{\n" +
73-
" \"objs\":[\n" +
73+
" \"objs\": [\n" +
7474
" {\n" +
75-
" \"field1\":\"1\",\n" +
76-
" \"field2\":\"2\"\n" +
75+
" \"field1\": \"1\",\n" +
76+
" \"field2\": \"2\"\n" +
7777
" }\n" +
7878
" ]\n" +
7979
"}".replace('\'', '"'), baos.toString());
@@ -97,9 +97,9 @@ public void test_map_of_objects() throws IOException {
9797
}});
9898
stream.close();
9999
assertEquals("{\n" +
100-
" \"hello\":{\n" +
101-
" \"field1\":\"1\",\n" +
102-
" \"field2\":\"2\"\n" +
100+
" \"hello\": {\n" +
101+
" \"field1\": \"1\",\n" +
102+
" \"field2\": \"2\"\n" +
103103
" }\n" +
104104
"}".replace('\'', '"'), baos.toString());
105105
} finally {

src/test/java/com/jsoniter/output/TestObject.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.jsoniter.annotation.JsonIgnore;
44
import com.jsoniter.annotation.JsonProperty;
5+
import com.jsoniter.spi.Config;
56
import com.jsoniter.spi.TypeLiteral;
67
import junit.framework.TestCase;
78

@@ -138,9 +139,13 @@ public static class TestObject8 {
138139
public void test_not_nullable() {
139140
TestObject8 obj = new TestObject8();
140141
obj.field1 = new String[]{"hello"};
141-
assertEquals("{\"field1\":[\"hello\"]}", JsonStream.serialize(obj));
142+
Config config = new Config.Builder()
143+
.encodingMode(EncodingMode.DYNAMIC_MODE)
144+
.build();
145+
assertEquals("{\"field1\":[\"hello\"]}",
146+
JsonStream.serialize(config, obj));
142147
try {
143-
JsonStream.serialize(new TestObject8());
148+
JsonStream.serialize(config, new TestObject8());
144149
fail();
145150
} catch (NullPointerException e) {
146151
}
@@ -162,10 +167,13 @@ public void test_collection_value_not_nullable() {
162167
obj.field1 = new String[]{"hello"};
163168
assertEquals("{\"field1\":[\"hello\"]}", JsonStream.serialize(obj));
164169

170+
Config config = new Config.Builder()
171+
.encodingMode(EncodingMode.DYNAMIC_MODE)
172+
.build();
165173
obj = new TestObject9();
166174
obj.field1 = new String[]{null};
167175
try {
168-
JsonStream.serialize(obj);
176+
JsonStream.serialize(config, obj);
169177
fail();
170178
} catch (NullPointerException e) {
171179
}
@@ -174,7 +182,7 @@ public void test_collection_value_not_nullable() {
174182
obj.field2 = new ArrayList();
175183
obj.field2.add(null);
176184
try {
177-
JsonStream.serialize(obj);
185+
JsonStream.serialize(config, obj);
178186
fail();
179187
} catch (NullPointerException e) {
180188
}
@@ -183,7 +191,7 @@ public void test_collection_value_not_nullable() {
183191
obj.field3 = new HashSet<String>();
184192
obj.field3.add(null);
185193
try {
186-
JsonStream.serialize(obj);
194+
JsonStream.serialize(config, obj);
187195
fail();
188196
} catch (NullPointerException e) {
189197
}
@@ -192,7 +200,7 @@ public void test_collection_value_not_nullable() {
192200
obj.field4 = new HashMap<String, String>();
193201
obj.field4.put("hello", null);
194202
try {
195-
JsonStream.serialize(obj);
203+
JsonStream.serialize(config, obj);
196204
fail();
197205
} catch (NullPointerException e) {
198206
}

src/test/java/com/jsoniter/suite/AllTestCases.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@
2020
TestAnnotation.class,
2121
com.jsoniter.output.TestGenerics.class,
2222
TestCustomizeType.class, TestDemo.class,
23-
TestExisting.class, TestGenerics.class, TestGenerics.class, TestIO.class, TestNested.class,
24-
TestObject.class, TestReadAny.class, TestSkip.class, TestSlice.class,
23+
TestExisting.class, TestGenerics.class, TestGenerics.class, TestIO.class,
24+
TestNested.class,
25+
com.jsoniter.output.TestNested.class,
26+
TestObject.class,
27+
com.jsoniter.output.TestObject.class,
28+
TestReadAny.class, TestSkip.class, TestSlice.class,
2529
TestString.class, TestWhatIsNext.class,
2630
TestAny.class,
2731
com.jsoniter.output.TestArray.class,
@@ -30,7 +34,8 @@
3034
TestSpiPropertyEncoder.class,
3135
com.jsoniter.TestMap.class,
3236
com.jsoniter.output.TestMap.class,
33-
TestNative.class, TestNested.class, TestObject.class, TestBoolean.class, TestFloat.class,
37+
TestNative.class,
38+
TestBoolean.class, TestFloat.class,
3439
TestList.class,
3540
com.jsoniter.output.TestJackson.class,
3641
com.jsoniter.TestJackson.class,

0 commit comments

Comments
 (0)