Search in sources :

Example 1 with LinkedTreeMap

use of io.github.wysohn.gsoncopy.internal.LinkedTreeMap in project TriggerReactor by wysohn.

the class GsonHelper method readJson.

public static Map<String, Object> readJson(JsonReader jsonReader, Gson gson) throws IOException {
    Map<String, Object> map = new LinkedTreeMap<>();
    if (!jsonReader.hasNext())
        return null;
    JsonToken token = jsonReader.peek();
    if (token != JsonToken.BEGIN_OBJECT)
        return null;
    jsonReader.beginObject();
    readJson(map, jsonReader, gson);
    jsonReader.endObject();
    return map;
}
Also used : LinkedTreeMap(io.github.wysohn.gsoncopy.internal.LinkedTreeMap) JsonToken(io.github.wysohn.gsoncopy.stream.JsonToken)

Example 2 with LinkedTreeMap

use of io.github.wysohn.gsoncopy.internal.LinkedTreeMap 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)

Example 3 with LinkedTreeMap

use of io.github.wysohn.gsoncopy.internal.LinkedTreeMap in project TriggerReactor by wysohn.

the class ObjectTypeAdapter method read.

@Override
public Object read(JsonReader in) throws IOException {
    JsonToken token = in.peek();
    switch(token) {
        case BEGIN_ARRAY:
            List<Object> list = new ArrayList<Object>();
            in.beginArray();
            while (in.hasNext()) {
                list.add(read(in));
            }
            in.endArray();
            return list;
        case BEGIN_OBJECT:
            Map<String, Object> map = new LinkedTreeMap<String, Object>();
            in.beginObject();
            while (in.hasNext()) {
                map.put(in.nextName(), read(in));
            }
            in.endObject();
            return map;
        case STRING:
            return in.nextString();
        case NUMBER:
            String value = in.nextString();
            if (value.contains(".")) {
                return Double.parseDouble(value);
            } else {
                try {
                    return Integer.parseInt(value);
                } catch (NumberFormatException ex) {
                    return Long.parseLong(value);
                }
            }
        case BOOLEAN:
            return in.nextBoolean();
        case NULL:
            in.nextNull();
            return null;
        default:
            throw new IllegalStateException();
    }
}
Also used : LinkedTreeMap(io.github.wysohn.gsoncopy.internal.LinkedTreeMap) ArrayList(java.util.ArrayList) JsonToken(io.github.wysohn.gsoncopy.stream.JsonToken)

Example 4 with LinkedTreeMap

use of io.github.wysohn.gsoncopy.internal.LinkedTreeMap in project TriggerReactor by wysohn.

the class GsonHelper method convert.

public static Object convert(JsonReader jsonReader, Gson gson) throws IOException {
    JsonToken token = jsonReader.peek();
    switch(token) {
        case BEGIN_OBJECT:
            jsonReader.beginObject();
            Map<String, Object> object = new LinkedTreeMap<>();
            TypeAdapter<?> adapter = null;
            while (jsonReader.hasNext()) {
                String key = jsonReader.nextName();
                if (CustomSerializer.SER_KEY.equals(key)) {
                    // custom serializer found
                    String className = jsonReader.nextString();
                    try {
                        adapter = gson.getAdapter(Class.forName(className));
                    } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                    }
                    continue;
                }
                if (adapter != null) {
                    if (!CustomSerializer.SER_VALUE.equals(key))
                        throw new RuntimeException("Found serializable key but field name of value is not " + CustomSerializer.SER_VALUE);
                    final Object read = adapter.read(jsonReader);
                    if (jsonReader.hasNext())
                        throw new RuntimeException("Finished deserialization, yet there are still more fields to read.");
                    jsonReader.endObject();
                    return read;
                } else {
                    // just a json object
                    object.put(key, convert(jsonReader, gson));
                }
            }
            jsonReader.endObject();
            return object;
        case BEGIN_ARRAY:
            List<Object> list = new ArrayList<>();
            jsonReader.beginArray();
            while (jsonReader.hasNext()) {
                list.add(convert(jsonReader, gson));
            }
            jsonReader.endArray();
            return list;
        case STRING:
            return jsonReader.nextString();
        case NUMBER:
            String value = jsonReader.nextString();
            if (value.contains(".")) {
                return Double.parseDouble(value);
            } else {
                try {
                    return Integer.parseInt(value);
                } catch (NumberFormatException ex) {
                    return Long.parseLong(value);
                }
            }
        case BOOLEAN:
            return jsonReader.nextBoolean();
        case NULL:
            jsonReader.nextNull();
            return null;
        default:
            throw new IllegalStateException();
    }
}
Also used : LinkedTreeMap(io.github.wysohn.gsoncopy.internal.LinkedTreeMap) ArrayList(java.util.ArrayList) JsonToken(io.github.wysohn.gsoncopy.stream.JsonToken)

Aggregations

LinkedTreeMap (io.github.wysohn.gsoncopy.internal.LinkedTreeMap)4 JsonToken (io.github.wysohn.gsoncopy.stream.JsonToken)3 ArrayList (java.util.ArrayList)2 Gson (io.github.wysohn.gsoncopy.Gson)1 GsonBuilder (io.github.wysohn.gsoncopy.GsonBuilder)1 TypeToken (io.github.wysohn.gsoncopy.reflect.TypeToken)1 Field (java.lang.reflect.Field)1 Test (org.junit.Test)1