MCPcopy
hub / github.com/google/gson / ObjectTest

Class ObjectTest

src/test/java/com/google/gson/functional/ObjectTest.java:48–461  ·  view source on GitHub ↗

Functional tests for Json serialization and deserialization of regular classes. @author Inderjeet Singh @author Joel Leitch

Source from the content-addressed store, hash-verified

46 * @author Joel Leitch
47 */
48public class ObjectTest extends TestCase {
49 private Gson gson;
50
51 @Override
52 protected void setUp() throws Exception {
53 super.setUp();
54 gson = new Gson();
55 }
56
57 public void testJsonInSingleQuotesDeserialization() {
58 String json = "{'stringValue':'no message','intValue':10,'longValue':20}";
59 BagOfPrimitives target = gson.fromJson(json, BagOfPrimitives.class);
60 assertEquals("no message", target.stringValue);
61 assertEquals(10, target.intValue);
62 assertEquals(20, target.longValue);
63 }
64
65 public void testJsonInMixedQuotesDeserialization() {
66 String json = "{\"stringValue\":'no message','intValue':10,'longValue':20}";
67 BagOfPrimitives target = gson.fromJson(json, BagOfPrimitives.class);
68 assertEquals("no message", target.stringValue);
69 assertEquals(10, target.intValue);
70 assertEquals(20, target.longValue);
71 }
72
73 public void testBagOfPrimitivesSerialization() throws Exception {
74 BagOfPrimitives target = new BagOfPrimitives(10, 20, false, "stringValue");
75 assertEquals(target.getExpectedJson(), gson.toJson(target));
76 }
77
78 public void testBagOfPrimitivesDeserialization() throws Exception {
79 BagOfPrimitives src = new BagOfPrimitives(10, 20, false, "stringValue");
80 String json = src.getExpectedJson();
81 BagOfPrimitives target = gson.fromJson(json, BagOfPrimitives.class);
82 assertEquals(json, target.getExpectedJson());
83 }
84
85 public void testBagOfPrimitiveWrappersSerialization() throws Exception {
86 BagOfPrimitiveWrappers target = new BagOfPrimitiveWrappers(10L, 20, false);
87 assertEquals(target.getExpectedJson(), gson.toJson(target));
88 }
89
90 public void testBagOfPrimitiveWrappersDeserialization() throws Exception {
91 BagOfPrimitiveWrappers target = new BagOfPrimitiveWrappers(10L, 20, false);
92 String jsonString = target.getExpectedJson();
93 target = gson.fromJson(jsonString, BagOfPrimitiveWrappers.class);
94 assertEquals(jsonString, target.getExpectedJson());
95 }
96
97 public void testClassWithTransientFieldsSerialization() throws Exception {
98 ClassWithTransientFields<Long> target = new ClassWithTransientFields<Long>(1L);
99 assertEquals(target.getExpectedJson(), gson.toJson(target));
100 }
101
102 @SuppressWarnings("unchecked")
103 public void testClassWithTransientFieldsDeserialization() throws Exception {
104 String json = "{\"longValue\":[1]}";
105 ClassWithTransientFields target = gson.fromJson(json, ClassWithTransientFields.class);

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected