/
Case4Primitives.java
115 lines (85 loc) · 3 KB
/
Case4Primitives.java
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package jug.ua.meetup;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.owlike.genson.Genson;
import org.junit.Test;
import java.io.IOException;
import static org.junit.Assert.assertEquals;
/**
* Created by Olena_Syrota on 4/19/2015.
*
* discussion:
* - ser/deser simple value
* - ser/deser Optional types
*/
public class Case4Primitives {
//GSON
private Gson gson = new Gson();
//JACKSON
private ObjectMapper jackson = new ObjectMapper()
.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, true);
//GENSON
private Genson genson = new Genson();
@Test
public void serializeIntPrimitive() throws IOException {
int val = 1;
String exp = "1";
String gsonRes = gson.toJson(val);
assertEquals(exp, gsonRes);
String jacksonRes = jackson.writeValueAsString(val);
assertEquals(exp, jacksonRes);
String gensonRes = genson.serialize(val);
assertEquals(exp, gensonRes);
}
@Test
public void deserializeIntPrimitive() throws IOException {
String val = "1";
int exp = 1;
int gsonRes = gson.fromJson(val, int.class);
assertEquals(exp, gsonRes);
int jacksonRes = jackson.readValue(val, int.class);
assertEquals(exp, jacksonRes);
int gensonRes = genson.deserialize(val, int.class);
assertEquals(exp, gensonRes);
}
@Test
public void deserializeBoolTrue() throws IOException {
String val = "true";
boolean exp = true;
boolean gsonRes = gson.fromJson(val, boolean.class);
assertEquals(exp, gsonRes);
boolean jacksonRes = jackson.readValue(val, boolean.class);
assertEquals(exp, jacksonRes);
boolean gensonRes = genson.deserialize(val, boolean.class);
assertEquals(exp, gensonRes);
}
@Test
public void gsonDeserNullPrimitive() {
String val = "{\"primitive\":null}";
ClassWithPrimitive ins = gson.fromJson(val, ClassWithPrimitive.class);
assertEquals(0, ins.getPrimitive());
}
@Test(expected = JsonMappingException.class)
public void jacksonDeserNullPrimitive() throws IOException {
String val = "{\"primitive\":null}";
ClassWithPrimitive ins = jackson.readValue(val, ClassWithPrimitive.class);
assertEquals(0, ins.getPrimitive());
}
@Test
public void gensonDeserNullPrimitive() throws IOException {
String val = "{\"primitive\":null}";
ClassWithPrimitive ins = genson.deserialize(val, ClassWithPrimitive.class);
assertEquals(0, ins.getPrimitive());
}
public static class ClassWithPrimitive {
private int primitive;
public int getPrimitive() {
return primitive;
}
public void setPrimitive(int primitive) {
this.primitive = primitive;
}
}
}