Search in sources :

Example 1 with JsonToken

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

the class JsonTreeReader method nextString.

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

Example 2 with JsonToken

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

the class JsonTreeReader method nextInt.

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

Example 3 with JsonToken

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

the class JsonTreeReader method nextDouble.

@Override
public double nextDouble() throws IOException {
    JsonToken token = peek();
    if (token != JsonToken.NUMBER && token != JsonToken.STRING) {
        throw new IllegalStateException("Expected " + JsonToken.NUMBER + " but was " + token + locationString());
    }
    double result = ((JsonPrimitive) peekStack()).getAsDouble();
    if (!isLenient() && (Double.isNaN(result) || Double.isInfinite(result))) {
        throw new NumberFormatException("JSON forbids NaN and infinities: " + result);
    }
    popStack();
    if (stackSize > 0) {
        pathIndices[stackSize - 1]++;
    }
    return result;
}
Also used : JsonToken(io.github.wysohn.gsoncopy.stream.JsonToken)

Example 4 with JsonToken

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

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

Aggregations

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