-
Notifications
You must be signed in to change notification settings - Fork 0
/
GsonNullExplainedTest.java
70 lines (60 loc) · 1.86 KB
/
GsonNullExplainedTest.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
package eu.toolchain.scribe;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;
import eu.toolchain.scribe.gson.NonNullDeserializer;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
/**
* Ambiguous handling of null causes really bad issues in services.
* <p>
* This test examines how hard it is to make Gson disallow null values.
* <p>
* Note that I have yet to find a way for Gson to deal with primitive defaults on absent and null.
* <p>
* Oh yeah, and note that {@link Person} doesn't even have a callable constructor?
*/
public class GsonNullExplainedTest {
/**
* Just an empty object.
* <p>
* Note how name is null, and age is assigned {@code 0}.
*/
@Test
public void test1() throws Exception {
final Gson gson = new Gson();
final Person person = gson.fromJson("{}", Person.class);
assertNull(person.getName());
assertEquals(0, person.getAge());
}
/**
* So let's try to figure out how to disallow null.
* <p>
* Seriously, Gson?
* http://stackoverflow.com/questions/21626690/gson-optional-and-required-fields
* <p>
* Works for name at least, but age is never null. So I guess it's back to boxed types
* everywhere?
*/
@Test(expected = JsonParseException.class)
public void test2() throws Exception {
final Gson gson = new GsonBuilder()
.registerTypeAdapter(Person.class, new NonNullDeserializer<Person>())
.create();
gson.fromJson("{}", Person.class);
}
public static class Person {
private final String name;
private final int age;
public Person(final String name, final int age) {
throw new IllegalStateException("lol, who cares right?");
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
}