forked from json-iterator/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestAnnotation.java
More file actions
85 lines (65 loc) · 2.32 KB
/
TestAnnotation.java
File metadata and controls
85 lines (65 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package com.jsoniter;
import com.jsoniter.annotation.*;
import junit.framework.TestCase;
import java.io.IOException;
public class TestAnnotation extends TestCase {
static {
JsoniterAnnotationSupport.enable();
}
public static class AnnotatedObject {
@JsonProperty("field-1")
public int field1;
@JsonIgnore
public int field2;
}
public void test_rename() throws IOException {
JsonIterator iter = JsonIterator.parse("{'field-1': 100}".replace('\'', '"'));
AnnotatedObject obj = iter.read(AnnotatedObject.class);
assertEquals(100, obj.field1);
}
public void test_ignore() throws IOException {
JsonIterator iter = JsonIterator.parse("{'field2': 100}".replace('\'', '"'));
AnnotatedObject obj = iter.read(AnnotatedObject.class);
assertEquals(0, obj.field2);
}
public static class NoDefaultCtor {
private int field1;
@JsonCreator
public NoDefaultCtor(@JsonProperty("field1") int field1) {
this.field1 = field1;
}
}
public void test_ctor() throws IOException {
JsonIterator iter = JsonIterator.parse("{'field1': 100}".replace('\'', '"'));
NoDefaultCtor obj = iter.read(NoDefaultCtor.class);
assertEquals(100, obj.field1);
}
public static class StaticFactory {
private int field1;
private StaticFactory() {
}
@JsonCreator
public static StaticFactory createObject(@JsonProperty("field1") int field1) {
StaticFactory obj = new StaticFactory();
obj.field1 = field1;
return obj;
}
}
public void test_static_factory() throws IOException {
JsonIterator iter = JsonIterator.parse("{'field1': 100}".replace('\'', '"'));
StaticFactory obj = iter.read(StaticFactory.class);
assertEquals(100, obj.field1);
}
public static class WithSetter {
private int field1;
@JsonSetter
public void initialize(@JsonProperty("field1") int field1) {
this.field1 = field1;
}
}
public void test_setter() throws IOException {
JsonIterator iter = JsonIterator.parse("{'field1': 100}".replace('\'', '"'));
WithSetter obj = iter.read(WithSetter.class);
assertEquals(100, obj.field1);
}
}