Skip to content

Commit 17cc3bd

Browse files
committed
fix #136, field with only getter is also considered as java bean property, so that @JsonIgnore on the field should be propagated to getter
1 parent 81b5eed commit 17cc3bd

File tree

9 files changed

+52
-31
lines changed

9 files changed

+52
-31
lines changed

src/main/java/com/jsoniter/CodegenImplObjectHash.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ public static String genObjectUsingHash(ClassDescriptor desc) {
4444
append(lines, "} // end of if end");
4545
append(lines, "} else { com.jsoniter.CodegenAccess.unreadByte(iter); }// end of if not quote");
4646
for (Binding field : desc.fields) {
47+
if (field.fromNames.length == 0) {
48+
continue;
49+
}
4750
appendVarDef(lines, field);
4851
}
4952
for (Binding setter : desc.setters) {
@@ -94,6 +97,9 @@ public int compare(String o1, String o2) {
9497
append(lines, "} while (com.jsoniter.CodegenAccess.nextTokenIsComma(iter));");
9598
append(lines, CodegenImplNative.getTypeName(clazz) + " obj = {{newInst}};");
9699
for (Binding field : desc.fields) {
100+
if (field.fromNames.length == 0) {
101+
continue;
102+
}
97103
append(lines, String.format("obj.%s = _%s_;", field.field.getName(), field.name));
98104
}
99105
for (Binding setter : desc.setters) {

src/main/java/com/jsoniter/CodegenImplObjectStrict.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ public static String genObjectUsingStrict(ClassDescriptor desc) {
6868
}
6969
append(lines, "}");
7070
for (Binding field : desc.fields) {
71+
if (field.fromNames.length == 0) {
72+
continue;
73+
}
7174
appendVarDef(lines, field);
7275
}
7376
for (Binding setter : desc.setters) {
@@ -91,6 +94,9 @@ public static String genObjectUsingStrict(ClassDescriptor desc) {
9194
if (desc.ctor.parameters.isEmpty()) {
9295
// if not field or setter, the value will set to temp variable
9396
for (Binding field : desc.fields) {
97+
if (field.fromNames.length == 0) {
98+
continue;
99+
}
94100
rendered = updateBindingSetOp(rendered, field);
95101
}
96102
for (Binding setter : desc.setters) {
@@ -128,6 +134,9 @@ public static String genObjectUsingStrict(ClassDescriptor desc) {
128134
if (!desc.ctor.parameters.isEmpty()) {
129135
append(lines, String.format("%s obj = {{newInst}};", CodegenImplNative.getTypeName(desc.clazz)));
130136
for (Binding field : desc.fields) {
137+
if (field.fromNames.length == 0) {
138+
continue;
139+
}
131140
append(lines, String.format("obj.%s = _%s_;", field.field.getName(), field.name));
132141
}
133142
for (Binding setter : desc.setters) {

src/main/java/com/jsoniter/spi/ClassDescriptor.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -205,20 +205,25 @@ private static ConstructorDescriptor getCtor(Class clazz) {
205205

206206
private static List<Binding> getFields(Map<String, Type> lookup, ClassInfo classInfo, boolean includingPrivate) {
207207
ArrayList<Binding> bindings = new ArrayList<Binding>();
208-
for (Field field : getAllFields(classInfo.clazz, includingPrivate)) {
208+
for (Field field : getAllFields(classInfo.clazz)) {
209209
if (Modifier.isStatic(field.getModifiers())) {
210210
continue;
211211
}
212-
if (!includingPrivate && !Modifier.isPublic(field.getType().getModifiers())) {
213-
continue;
214-
}
215212
if (includingPrivate) {
216213
field.setAccessible(true);
217214
}
218215
if (isTransient(field.getModifiers())) {
219216
continue;
220217
}
221218
Binding binding = createBindingFromField(lookup, classInfo, field);
219+
if (!includingPrivate && !Modifier.isPublic(field.getModifiers())) {
220+
binding.toNames = new String[0];
221+
binding.fromNames = new String[0];
222+
}
223+
if (!includingPrivate && !Modifier.isPublic(field.getType().getModifiers())) {
224+
binding.toNames = new String[0];
225+
binding.fromNames = new String[0];
226+
}
222227
bindings.add(binding);
223228
}
224229
return bindings;
@@ -240,15 +245,12 @@ private static Binding createBindingFromField(Map<String, Type> lookup, ClassInf
240245
}
241246
}
242247

243-
private static List<Field> getAllFields(Class clazz, boolean includingPrivate) {
244-
List<Field> allFields = Arrays.asList(clazz.getFields());
245-
if (includingPrivate) {
246-
allFields = new ArrayList<Field>();
247-
Class current = clazz;
248-
while (current != null) {
249-
allFields.addAll(Arrays.asList(current.getDeclaredFields()));
250-
current = current.getSuperclass();
251-
}
248+
private static List<Field> getAllFields(Class clazz) {
249+
ArrayList<Field> allFields = new ArrayList<Field>();
250+
Class current = clazz;
251+
while (current != null) {
252+
allFields.addAll(Arrays.asList(current.getDeclaredFields()));
253+
current = current.getSuperclass();
252254
}
253255
return allFields;
254256
}

src/main/java/com/jsoniter/spi/Config.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,8 @@ private void updateBindings(ClassDescriptor desc) {
436436
setter.toNames = new String[0];
437437
}
438438
}
439+
}
440+
if (desc.getters != null) {
439441
for (Binding getter : desc.getters) {
440442
if (binding.field.getName().equals(getter.name)) {
441443
getter.fromNames = new String[0];

src/test/java/com/jsoniter/TestAnnotation.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
11
package com.jsoniter;
22

3-
import com.jsoniter.annotation.*;
4-
import com.jsoniter.any.Any;
5-
import com.jsoniter.fuzzy.StringIntDecoder;
3+
import com.jsoniter.annotation.JsonCreator;
4+
import com.jsoniter.annotation.JsonProperty;
65
import com.jsoniter.spi.JsonException;
7-
import com.jsoniter.spi.JsoniterSpi;
86
import junit.framework.TestCase;
97

108
import java.io.IOException;
11-
import java.util.Arrays;
12-
import java.util.LinkedList;
13-
import java.util.List;
14-
import java.util.Map;
159

1610
public class TestAnnotation extends TestCase {
1711

1812
static {
19-
// JsonIterator.setMode(DecodingMode.DYNAMIC_MODE_AND_MATCH_FIELD_WITH_HASH);
13+
// JsonIterator.setMode(DecodingMode.DYNAMIC_MODE_AND_MATCH_FIELD_STRICTLY);
2014
// JsonIterator.setMode(DecodingMode.REFLECTION_MODE);
2115
}
2216

src/test/java/com/jsoniter/TestAnnotationJsonWrapper.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.jsoniter.annotation.JsonProperty;
55
import com.jsoniter.annotation.JsonWrapper;
66
import com.jsoniter.annotation.JsonWrapperType;
7+
import com.jsoniter.spi.DecodingMode;
78
import junit.framework.TestCase;
89

910
import java.io.IOException;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.jsoniter.annotation.JsonProperty;
44
import com.jsoniter.any.Any;
55
import com.jsoniter.fuzzy.MaybeEmptyArrayDecoder;
6+
import com.jsoniter.spi.DecodingMode;
67
import com.jsoniter.spi.EmptyExtension;
78
import com.jsoniter.spi.JsonException;
89
import com.jsoniter.spi.JsoniterSpi;

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import junit.framework.TestCase;
55

66
import java.io.IOException;
7-
import java.io.Serializable;
87

98
public class TestAnnotationJsonIgnore extends TestCase {
109

@@ -29,4 +28,19 @@ public void test_ignore_decoding_only() throws IOException {
2928
obj.field1 = 100;
3029
assertEquals("{\"field1\":100}", JsonStream.serialize(obj));
3130
}
31+
32+
public static class TestPrivateVariables {
33+
@JsonIgnore
34+
private String field1;
35+
36+
public String getField1() {
37+
return field1;
38+
}
39+
}
40+
41+
public void test_private_serialize() throws IOException {
42+
TestPrivateVariables obj = new TestPrivateVariables();
43+
obj.field1 = "hello";
44+
assertEquals("{}", JsonStream.serialize(obj));
45+
}
3246
}

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,6 @@ public String getField1() {
4747
}
4848
}
4949

50-
public void test_getter() throws IOException {
51-
TestObject2 obj = new TestObject2();
52-
obj.field1 = "hello";
53-
stream.writeVal(obj);
54-
stream.close();
55-
assertEquals("{'field1':'hello'}".replace('\'', '"'), baos.toString());
56-
}
57-
5850
public void test_null() throws IOException {
5951
stream.writeVal(new TypeLiteral<TestObject2>() {
6052
}, null);

0 commit comments

Comments
 (0)