use of com.google.gson.JsonNull in project maple-ir by LLVM-but-worse.
the class JsonTreeReader method peek.
@Override
public JsonToken peek() throws IOException {
if (stack.isEmpty()) {
return JsonToken.END_DOCUMENT;
}
Object o = peekStack();
if (o instanceof Iterator) {
boolean isObject = stack.get(stack.size() - 2) instanceof JsonObject;
Iterator<?> iterator = (Iterator<?>) o;
if (iterator.hasNext()) {
if (isObject) {
return JsonToken.NAME;
} else {
stack.add(iterator.next());
return peek();
}
} else {
return isObject ? JsonToken.END_OBJECT : JsonToken.END_ARRAY;
}
} else if (o instanceof JsonObject) {
return JsonToken.BEGIN_OBJECT;
} else if (o instanceof JsonArray) {
return JsonToken.BEGIN_ARRAY;
} else if (o instanceof JsonPrimitive) {
JsonPrimitive primitive = (JsonPrimitive) o;
if (primitive.isString()) {
return JsonToken.STRING;
} else if (primitive.isBoolean()) {
return JsonToken.BOOLEAN;
} else if (primitive.isNumber()) {
return JsonToken.NUMBER;
} else {
throw new AssertionError();
}
} else if (o instanceof JsonNull) {
return JsonToken.NULL;
} else if (o == SENTINEL_CLOSED) {
throw new IllegalStateException("JsonReader is closed");
} else {
throw new AssertionError();
}
}
use of com.google.gson.JsonNull in project structr by structr.
the class WebSocketDataGSONAdapter method deserialize.
@Override
public WebSocketMessage deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
WebSocketMessage webSocketData = new WebSocketMessage();
if (json instanceof JsonObject) {
JsonObject root = json.getAsJsonObject();
JsonObject nodeData = root.getAsJsonObject("data");
JsonObject relData = root.getAsJsonObject("relData");
if (root.has("command")) {
webSocketData.setCommand(root.getAsJsonPrimitive("command").getAsString());
}
if (root.has("id")) {
webSocketData.setId(root.getAsJsonPrimitive("id").getAsString());
}
if (root.has("pageId")) {
webSocketData.setPageId(root.getAsJsonPrimitive("pageId").getAsString());
}
if (root.has("sessionId")) {
JsonPrimitive sessionId = root.getAsJsonPrimitive("sessionId");
if (sessionId != null) {
webSocketData.setSessionId(sessionId.getAsString());
}
}
if (root.has("callback")) {
webSocketData.setCallback(root.getAsJsonPrimitive("callback").getAsString());
}
if (root.has("button")) {
webSocketData.setButton(root.getAsJsonPrimitive("button").getAsString());
}
if (root.has("parent")) {
webSocketData.setParent(root.getAsJsonPrimitive("parent").getAsString());
}
if (root.has("view")) {
webSocketData.setView(root.getAsJsonPrimitive("view").getAsString());
}
if (root.has("sort")) {
webSocketData.setSortKey(root.getAsJsonPrimitive("sort").getAsString());
}
if (root.has("order")) {
webSocketData.setSortOrder(root.getAsJsonPrimitive("order").getAsString());
}
if (root.has("pageSize")) {
webSocketData.setPageSize(root.getAsJsonPrimitive("pageSize").getAsInt());
}
if (root.has("page")) {
webSocketData.setPage(root.getAsJsonPrimitive("page").getAsInt());
}
if (nodeData != null) {
JsonInputGSONAdapter adapter = new JsonInputGSONAdapter();
for (Entry<String, JsonElement> entry : nodeData.entrySet()) {
final JsonElement obj = entry.getValue();
Object value = null;
if (obj instanceof JsonPrimitive) {
value = adapter.fromPrimitive(obj.getAsJsonPrimitive());
} else if (obj instanceof JsonObject) {
value = adapter.deserialize(obj, typeOfT, context);
} else if (obj instanceof JsonArray) {
final JsonArray array = obj.getAsJsonArray();
final List list = new LinkedList();
for (JsonElement element : array) {
if (element.isJsonPrimitive()) {
list.add(fromPrimitive((element.getAsJsonPrimitive())));
} else if (element.isJsonObject()) {
// create map of values
list.add(JsonInputGSONAdapter.deserialize(element, context));
}
}
value = list;
} else if (obj instanceof JsonNull) {
value = null;
} else if (obj != null) {
value = obj.getAsString();
}
webSocketData.setNodeData(entry.getKey(), value);
}
}
if (relData != null) {
for (Entry<String, JsonElement> entry : relData.entrySet()) {
JsonElement obj = entry.getValue();
if (obj instanceof JsonNull || obj.isJsonNull()) {
webSocketData.setRelData(entry.getKey(), null);
} else {
try {
webSocketData.setRelData(entry.getKey(), obj.getAsString());
} catch (Throwable t) {
webSocketData.setRelData(entry.getKey(), null);
}
}
}
}
}
return webSocketData;
}
Aggregations