Skip to content

Commit 33e064a

Browse files
committed
support JsonIgnore
1 parent 0825f82 commit 33e064a

File tree

6 files changed

+51
-10
lines changed

6 files changed

+51
-10
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.jsoniter.annotation.jsoniter;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Target({ElementType.ANNOTATION_TYPE, ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER})
9+
@Retention(RetentionPolicy.RUNTIME)
10+
public @interface JsonIgnore {
11+
String USE_DEFAULT_NAME = "";
12+
String value() default USE_DEFAULT_NAME;
13+
}

src/main/java/com/jsoniter/annotation/jsoniter/JsoniterAnnotationSupport.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,18 @@ public Decoder createDecoder(final Field field) {
1919

2020
@Override
2121
public String[] getAlternativeFieldNames(Field field) {
22-
JsonProperty annotation = field.getAnnotation(JsonProperty.class);
23-
if (annotation == null) {
24-
return null;
22+
JsonIgnore jsonIgnore = field.getAnnotation(JsonIgnore.class);
23+
if (jsonIgnore != null) {
24+
return new String[0];
2525
}
26-
String alternativeField = annotation.value();
27-
if (alternativeField.equals(JsonProperty.USE_DEFAULT_NAME)) {
28-
alternativeField = field.getName();
26+
JsonProperty jsonProperty = field.getAnnotation(JsonProperty.class);
27+
if (jsonProperty != null) {
28+
String alternativeField = jsonProperty.value();
29+
if (alternativeField.equals(JsonProperty.USE_DEFAULT_NAME)) {
30+
alternativeField = field.getName();
31+
}
32+
return new String[]{alternativeField};
2933
}
30-
final String[] alternativeFields = new String[]{alternativeField};
31-
return alternativeFields;
34+
return null;
3235
}
3336
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package com.jsoniter;
22

3+
import com.jsoniter.annotation.jsoniter.JsonIgnore;
34
import com.jsoniter.annotation.jsoniter.JsonProperty;
45

56
public class AnnotatedObject {
67
@JsonProperty("field-1")
78
public int field1;
89

9-
@JsonProperty("field-2")
10+
@JsonIgnore
1011
public int field2;
1112
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.jsoniter;
2+
3+
public class InheritedObject extends SimpleObject {
4+
public String inheritedField;
5+
}

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,16 @@
66
import java.io.IOException;
77

88
public class TestAnnotation extends TestCase {
9-
public void test() throws IOException {
9+
public void test_rename() throws IOException {
1010
JsoniterAnnotationSupport.enable();
1111
Jsoniter iter = Jsoniter.parse("{'field-1': 100}".replace('\'', '"'));
1212
AnnotatedObject obj = iter.read(AnnotatedObject.class);
1313
assertEquals(100, obj.field1);
1414
}
15+
public void test_ignore() throws IOException {
16+
JsoniterAnnotationSupport.enable();
17+
Jsoniter iter = Jsoniter.parse("{'field2': 100}".replace('\'', '"'));
18+
AnnotatedObject obj = iter.read(AnnotatedObject.class);
19+
assertEquals(0, obj.field2);
20+
}
1521
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.jsoniter;
2+
3+
import junit.framework.TestCase;
4+
5+
import java.io.IOException;
6+
7+
public class TestInheritance extends TestCase {
8+
public void test() throws IOException {
9+
Jsoniter iter = Jsoniter.parse("{'inheritedField': 'hello'}".replace('\'', '"'));
10+
InheritedObject inheritedObject = iter.read(InheritedObject.class);
11+
assertEquals("hello", inheritedObject.inheritedField);
12+
}
13+
}

0 commit comments

Comments
 (0)