Search in sources :

Example 1 with GsonBuilder

use of io.github.wysohn.gsoncopy.GsonBuilder in project TriggerReactor by wysohn.

the class TestGsonConfigSource method testSaveAll.

@Test
public void testSaveAll() throws Exception {
    Field field = manager.getClass().getDeclaredField("cache");
    field.setAccessible(true);
    Map<String, Object> cache = (Map<String, Object>) field.get(manager);
    cache.put("string2", "teststring2");
    cache.put("number2", 123);
    cache.put("number2_2", 123.45);
    cache.put("boolean2", false);
    cache.put("null", null);
    List<String> list2 = new ArrayList<String>();
    list2.add("o");
    list2.add("p");
    list2.add("q");
    cache.put("list2", list2);
    Map<String, Object> object2 = new HashMap<String, Object>();
    object2.put("value2", "ccdd");
    cache.put("object2", object2);
    cache.put("uuid2", UUID.fromString("12b6df56-1fc0-4d8a-bed4-9469e7798dea"));
    Set<String> hashset2 = new HashSet<String>();
    hashset2.add("j");
    hashset2.add("k");
    hashset2.add("l");
    cache.put("hashset2", hashset2);
    CustomObject myobj2 = new CustomObject();
    myobj2.s = "some string2";
    myobj2.i = 33;
    myobj2.d = 99.9;
    myobj2.b = false;
    cache.put("myobj2", myobj2);
    manager.saveAll();
    String out = stringWriter.toString();
    Gson gson = new GsonBuilder().enableComplexMapKeySerialization().create();
    Map<String, Object> deser = gson.fromJson(out, new TypeToken<Map<String, Object>>() {
    }.getType());
    assertEquals("teststring2", deser.get("string2"));
    assertEquals(123, deser.get("number2"));
    assertEquals(123.45, deser.get("number2_2"));
    assertEquals(false, deser.get("boolean2"));
    assertEquals(new ArrayList<String>() {

        {
            add("o");
            add("p");
            add("q");
        }
    }, deser.get("list2"));
    assertEquals(new LinkedHashMap<String, Object>() {

        {
            put("value2", "ccdd");
        }
    }, deser.get("object2"));
    Map<String, Object> uuid2 = new LinkedTreeMap<>();
    uuid2.put(Serializer.SER_KEY, UUID.class.getName());
    uuid2.put(Serializer.SER_VALUE, "12b6df56-1fc0-4d8a-bed4-9469e7798dea");
    assertEquals(uuid2, deser.get("uuid2"));
    List<String> hashset22 = new ArrayList<>();
    hashset22.add("j");
    hashset22.add("k");
    hashset22.add("l");
    assertEquals(hashset22, deser.get("hashset2"));
    Map<String, Object> myobj2raw = new LinkedTreeMap<>();
    myobj2raw.put("s", "some string2");
    myobj2raw.put("i", 33);
    myobj2raw.put("d", 99.9);
    myobj2raw.put("b", false);
    assertEquals(myobj2raw, deser.get("myobj2"));
}
Also used : LinkedTreeMap(io.github.wysohn.gsoncopy.internal.LinkedTreeMap) GsonBuilder(io.github.wysohn.gsoncopy.GsonBuilder) Gson(io.github.wysohn.gsoncopy.Gson) Field(java.lang.reflect.Field) TypeToken(io.github.wysohn.gsoncopy.reflect.TypeToken) LinkedTreeMap(io.github.wysohn.gsoncopy.internal.LinkedTreeMap) Test(org.junit.Test)

Aggregations

Gson (io.github.wysohn.gsoncopy.Gson)1 GsonBuilder (io.github.wysohn.gsoncopy.GsonBuilder)1 LinkedTreeMap (io.github.wysohn.gsoncopy.internal.LinkedTreeMap)1 TypeToken (io.github.wysohn.gsoncopy.reflect.TypeToken)1 Field (java.lang.reflect.Field)1 Test (org.junit.Test)1