-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeserializationTest.java
100 lines (85 loc) · 3.34 KB
/
DeserializationTest.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
package com.pchudzik.blog.examples;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import com.google.gson.Gson;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
public class DeserializationTest {
static class SampleObject {
public String message;
public Code code;
public String extraProperty;
enum Code {
OK,
FAIL
}
}
static class OtherObject {
public String message;
public String code;
public int extraProperty;
}
static final String SampleObjectJson = """
{
"message":"Hello World",
"code": "OK",
"extraProperty": "cool"
}
""";
static final String OtherObjectJson = """
{
"message": "Other Hello World",
"code": "All Good",
"extraProperty": 42
}
""";
private static final ObjectMapper objectMapper = new ObjectMapper();
private static final Gson gson = new Gson();
@Test
public void jackson_deserializes_objects() throws JsonProcessingException {
var sampleObject = objectMapper.readValue(SampleObjectJson, SampleObject.class);
assertEquals(SampleObject.Code.OK, sampleObject.code);
assertEquals("Hello World", sampleObject.message);
assertEquals("cool", sampleObject.extraProperty);
var otherObject = objectMapper.readValue(OtherObjectJson, OtherObject.class);
assertEquals("All Good", otherObject.code);
assertEquals("Other Hello World", otherObject.message);
assertEquals(42, otherObject.extraProperty);
}
@Test
public void gson_deserializes_objects() {
var sampleObject = gson.fromJson(SampleObjectJson, SampleObject.class);
assertEquals(SampleObject.Code.OK, sampleObject.code);
assertEquals("Hello World", sampleObject.message);
assertEquals("cool", sampleObject.extraProperty);
var otherObject = gson.fromJson(OtherObjectJson, OtherObject.class);
assertEquals("All Good", otherObject.code);
assertEquals("Other Hello World", otherObject.message);
assertEquals(42, otherObject.extraProperty);
}
@Test
public void jackson_fails_when_deserializing_wrong_object() {
assertThrows(
InvalidFormatException.class,
() -> objectMapper.readValue(OtherObjectJson, SampleObject.class)
);
}
@Test
@Disabled
public void print_jackson_error() throws JsonProcessingException {
objectMapper.readValue(OtherObjectJson, SampleObject.class);
}
@Test
public void gson_works_when_deserializing_wrong_object() {
var sampleObject = gson.fromJson(OtherObjectJson, SampleObject.class);
assertNull(sampleObject.code);
assertEquals("Other Hello World", sampleObject.message);
assertEquals("42", sampleObject.extraProperty);
assertNull(sampleObject.code);
}
}