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;
}
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();
}
}
Aggregations