Search in sources :

Example 6 with JsonToken

use of io.github.wysohn.gsoncopy.stream.JsonToken in project TriggerReactor by wysohn.

the class JsonTreeReader method nextLong.

@Override
public long nextLong() throws IOException {
    JsonToken token = peek();
    if (token != JsonToken.NUMBER && token != JsonToken.STRING) {
        throw new IllegalStateException("Expected " + JsonToken.NUMBER + " but was " + token + locationString());
    }
    long result = ((JsonPrimitive) peekStack()).getAsLong();
    popStack();
    if (stackSize > 0) {
        pathIndices[stackSize - 1]++;
    }
    return result;
}
Also used : JsonToken(io.github.wysohn.gsoncopy.stream.JsonToken)

Example 7 with JsonToken

use of io.github.wysohn.gsoncopy.stream.JsonToken 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

JsonToken (io.github.wysohn.gsoncopy.stream.JsonToken)7 LinkedTreeMap (io.github.wysohn.gsoncopy.internal.LinkedTreeMap)3 ArrayList (java.util.ArrayList)2