List of usage examples for com.google.gson JsonElement getAsJsonPrimitive
public JsonPrimitive getAsJsonPrimitive()
From source file:rpc.server.data.JSONSerializer.java
License:Open Source License
private Object fromJsonElement(JsonElement jsonElement, Type expected) throws NoSuitableSerializableFactory { if (jsonElement == null) { return null; }/*from ww w .j av a2s. c om*/ // Null if (jsonElement.isJsonNull()) { return null; } // Boolean // Integer // Long // Float // Double // String // Enum if (jsonElement.isJsonPrimitive()) { JsonPrimitive asJsonPrimitive = jsonElement.getAsJsonPrimitive(); if (asJsonPrimitive.isBoolean()) { return asJsonPrimitive.getAsBoolean(); } if (asJsonPrimitive.isNumber()) { if (expected.isInteger()) { return asJsonPrimitive.getAsInt(); } if (expected.isLong()) { return asJsonPrimitive.getAsLong(); } if (expected.isFloat()) { return asJsonPrimitive.getAsFloat(); } if (expected.isDouble()) { return asJsonPrimitive.getAsDouble(); } return asJsonPrimitive.getAsNumber(); } if (asJsonPrimitive.isString()) { if (expected.isEnum()) { String value = asJsonPrimitive.getAsString(); return Enum.valueOf((Class) expected.getTypeClass(), value); } else { return asJsonPrimitive.getAsString(); } } } // Map // Serializable if (jsonElement.isJsonObject()) { JsonObject asJsonObject = jsonElement.getAsJsonObject(); if (expected.isMap()) { Map<Object, Object> map = new HashMap<Object, Object>(); Type keyType = expected.getParameterized(0); Type valueType = expected.getParameterized(1); if (!(keyType.isString() || keyType.isEnum())) { return null; } for (Map.Entry<String, JsonElement> entry : asJsonObject.entrySet()) { String key = entry.getKey(); JsonElement value = entry.getValue(); if (keyType.isString()) { map.put(entry.getKey(), fromJsonElement(value, valueType)); } if (keyType.isEnum()) { map.put(Enum.valueOf((Class) keyType.getTypeClass(), key), fromJsonElement(value, valueType)); } } return map; } else { if (provider == null) { throw new NoSuitableSerializableFactory(); } Serializable object = provider.make(expected); for (Map.Entry<String, Type> entry : object.fields().entrySet()) { String field = entry.getKey(); Type fieldType = entry.getValue(); JsonElement value = asJsonObject.get(field); object.set(field, fromJsonElement(value, fieldType)); } return object; } } // List if (jsonElement.isJsonArray()) { JsonArray asJsonArray = jsonElement.getAsJsonArray(); int size = asJsonArray.size(); List<Object> list = new ArrayList<Object>(); Type itemType = expected.getParameterized(0); for (int i = 0; i < size; i++) { JsonElement value = asJsonArray.get(i); list.add(fromJsonElement(value, itemType)); } return list; } return null; }
From source file:tk.breezy64.pantex.core.ConfigManager.java
private static Object jsonElementToObject(JsonElement e) { if (e.isJsonObject()) { return jsonObjectToMap(e.getAsJsonObject()); } else if (e.isJsonPrimitive()) { JsonPrimitive p = e.getAsJsonPrimitive(); if (p.isNumber()) { return p.getAsInt(); } else if (p.isString()) { return p.getAsString(); } else if (p.isBoolean()) { return p.getAsBoolean(); }//from w ww . j ava 2 s . c o m } else if (e.isJsonArray()) { List<Object> list = new ArrayList<>(); e.getAsJsonArray().forEach((x) -> list.add(jsonElementToObject(x))); return list; } return null; }
From source file:vazkii.botania.client.model.SpecialFlowerModel.java
License:Open Source License
private ModelResourceLocation getLocation(String json) { JsonElement e = new JsonParser().parse(json); if (e.isJsonPrimitive() && e.getAsJsonPrimitive().isString()) { return new ModelResourceLocation(e.getAsString()); }// w ww . j a va2 s .c om Botania.LOGGER.error("Expect ModelResourceLocation, got: ", json); return new ModelResourceLocation("builtin/missing", "missing"); }