|
| 1 | +// Copyright (c) 2015 ServiceStack LLC. All rights reserved. |
| 2 | + |
| 3 | +package net.servicestack.android.checkweb; |
| 4 | + |
| 5 | +import com.google.gson.Gson; |
| 6 | + |
| 7 | +import junit.framework.TestCase; |
| 8 | + |
| 9 | +import net.servicestack.android.checkweb.dtos.*; |
| 10 | + |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.List; |
| 13 | + |
| 14 | +public class GsonTests extends TestCase { |
| 15 | + |
| 16 | + public GsonTests() { |
| 17 | + |
| 18 | + } |
| 19 | + |
| 20 | + public void test_Gson() { |
| 21 | + System.out.println("=========== HELLO JSON ============"); |
| 22 | + |
| 23 | + String json = "{\n" + |
| 24 | + " \"posts\": [\n" + |
| 25 | + " {\n" + |
| 26 | + " \"post\": {\n" + |
| 27 | + " \"username\": \"John\",\n" + |
| 28 | + " \"message\": \"I'm back\",\n" + |
| 29 | + " \"time\": \"2010-5-6 7:00:34\"\n" + |
| 30 | + " }\n" + |
| 31 | + " },\n" + |
| 32 | + " {\n" + |
| 33 | + " \"post\": {\n" + |
| 34 | + " \"username\": \"Smith\",\n" + |
| 35 | + " \"message\": \"I've been waiting\",\n" + |
| 36 | + " \"time\": \"2010-4-6 10:30:26\"\n" + |
| 37 | + " }\n" + |
| 38 | + " }\n" + |
| 39 | + " ]\n" + |
| 40 | + "}"; |
| 41 | + |
| 42 | + Gson gson = new Gson(); |
| 43 | + PostList list = gson.fromJson(json, PostList.class); |
| 44 | + |
| 45 | + System.out.println("JSON: " + gson.toJson(list)); |
| 46 | + } |
| 47 | + |
| 48 | + public class PostList { |
| 49 | + private List<PostContainer> posts = new ArrayList<>(); |
| 50 | + |
| 51 | + public List<PostContainer> getPostContainterList() { |
| 52 | + return posts; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + class PostContainer { |
| 57 | + Posts post; |
| 58 | + public Posts getPost() { |
| 59 | + return post; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + public class Posts { |
| 64 | + String message; |
| 65 | + String time; |
| 66 | + String username; |
| 67 | + } |
| 68 | + |
| 69 | + public static class dto |
| 70 | + { |
| 71 | + public static class Foo { |
| 72 | + Integer fooId; |
| 73 | + String fooName; |
| 74 | + } |
| 75 | + public static class Bar { |
| 76 | + Integer barId; |
| 77 | + String barName; |
| 78 | + Foo foo; |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + class NestedPojo { |
| 83 | + dto.Foo foo; |
| 84 | + dto.Bar bar; |
| 85 | + public Integer version = 1; |
| 86 | + } |
| 87 | + |
| 88 | + class NestedList extends ArrayList<NestedPojo> { |
| 89 | + |
| 90 | + } |
| 91 | + public static interface MetadataTestChild |
| 92 | + { |
| 93 | + public String name = null; |
| 94 | +// ArrayList<MetadataTestNestedChild> results; |
| 95 | + } |
| 96 | + |
| 97 | + public void test_Can_serialize_nested_classes() { |
| 98 | + NestedPojo o = new NestedPojo(); |
| 99 | + o.foo = new dto.Foo(); |
| 100 | + o.foo.fooId = 1; |
| 101 | + o.foo.fooName = "foo"; |
| 102 | + |
| 103 | + o.bar = new dto.Bar(); |
| 104 | + o.bar.barId = 2; |
| 105 | + o.bar.barName = "bar"; |
| 106 | + |
| 107 | + List<NestedPojo> list = new ArrayList<>(); |
| 108 | + list.add(o); |
| 109 | + list.add(o); |
| 110 | + list.add(o); |
| 111 | + |
| 112 | + Gson gson = new Gson(); |
| 113 | + System.out.println("JSON LIST: " + gson.toJson(list)); |
| 114 | + |
| 115 | + Class a = NestedPojo.class; |
| 116 | + } |
| 117 | + |
| 118 | + public void test_Can_deserialize_Hello() { |
| 119 | + String json = "{\"Result\":\"World\"}\n"; |
| 120 | + |
| 121 | + Gson gson = new Gson(); |
| 122 | + |
| 123 | + HelloResponse response = gson.fromJson(json, HelloResponse.class); |
| 124 | + |
| 125 | + assertEquals("World", response.getResult()); |
| 126 | + } |
| 127 | +} |
0 commit comments