List of usage examples for com.google.gson JsonDeserializationContext deserialize
public <T> T deserialize(JsonElement json, Type typeOfT) throws JsonParseException;
From source file:com.javacreed.examples.gson.part2.BookDeserializer.java
License:Apache License
@Override public Book deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException { final JsonObject jsonObject = json.getAsJsonObject(); // Delegate the deserialisation to the AuthorDeserializer class final Author[] authors = context.deserialize(jsonObject.get("authors"), Author[].class); final Book book = new Book(); book.setTitle(jsonObject.get("title").getAsString()); book.setIsbn(jsonObject.get("isbn").getAsString()); book.setAuthors(authors);/*from w w w . j a v a2s.c o m*/ return book; }
From source file:com.joulespersecond.oba.JsonHelp.java
License:Apache License
static <T> T deserializeChild(JsonObject obj, String name, Type typeOfT, JsonDeserializationContext context) { JsonElement child = obj.get(name);//w ww. ja v a 2 s . c om if (child == null) { return null; } return context.deserialize(child, typeOfT); }
From source file:com.kotcrab.vis.editor.serializer.json.ArrayJsonSerializer.java
License:Apache License
@Override public Array<T> deserialize(JsonElement element, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonArray jsonArray = element.getAsJsonArray(); Array<T> array = new Array<>(jsonArray.size()); for (JsonElement jsonElement : jsonArray) { if (jsonElement.isJsonObject()) { JsonObject jsonObjectElement = jsonElement.getAsJsonObject(); if (jsonObjectElement.has(PRIMITIVE_CONTENT)) { array.add(context.deserialize(jsonObjectElement.get(PRIMITIVE_CONTENT), GsonUtils.readClassProperty(jsonObjectElement, context))); } else { array.add(context.deserialize(jsonObjectElement, GsonUtils.readClassProperty(jsonObjectElement, context))); }/*from www .j av a 2s .c o m*/ continue; } throw new UnsupportedOperationException("Invalid JsonElement type in ArrayJsonSerializer"); } return array; }
From source file:com.kotcrab.vis.editor.serializer.json.AssetComponentSerializer.java
License:Apache License
@Override public AssetReference deserialize(JsonElement j, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject json = j.getAsJsonObject().get("asset").getAsJsonObject(); AssetReference asset = new AssetReference(); asset.asset = context.deserialize(json, GsonUtils.readClassProperty(json, context)); return asset; }
From source file:com.kotcrab.vis.editor.serializer.json.GsonUtils.java
License:Apache License
public static Class<?> readClassProperty(JsonElement json, JsonDeserializationContext context, String customMemberName) { return context.deserialize(json.getAsJsonObject().get(customMemberName), Class.class); }
From source file:com.kotcrab.vis.editor.serializer.json.IntMapJsonSerializer.java
License:Apache License
@Override public IntMap<T> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonArray jsonArray = json.getAsJsonArray(); IntMap<T> intMap = new IntMap<>(jsonArray.size()); for (JsonElement element : jsonArray) { JsonObject object = element.getAsJsonObject(); Entry<String, JsonElement> entry = object.entrySet().iterator().next(); int mapKey = Integer.parseInt(entry.getKey()); Class<?> mapObjectClass = GsonUtils.readClassProperty(object, context); intMap.put(mapKey, context.deserialize(entry.getValue(), mapObjectClass)); }//w w w. j av a 2s. co m return intMap; }
From source file:com.kotcrab.vis.editor.serializer.json.ObjectMapJsonSerializer.java
License:Apache License
@Override public ObjectMap<K, V> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonArray jsonArray = json.getAsJsonArray(); ObjectMap<K, V> objMap = new ObjectMap<>(jsonArray.size()); for (JsonElement element : jsonArray) { JsonObject object = element.getAsJsonObject(); K key = context.deserialize(object.get("key"), GsonUtils.readClassProperty(object, context, PROPERTY_CLASS_KEY)); V value = context.deserialize(object.get("value"), GsonUtils.readClassProperty(object, context, PROPERTY_CLASS_VALUE)); objMap.put(key, value);//from ww w .j a v a 2 s . c o m } return objMap; }
From source file:com.kurento.kmf.content.jsonrpc.GsonUtils.java
License:Open Source License
@Override public JsonRpcResponse deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (!(json instanceof JsonObject)) { throw new JsonParseException("JonObject expected, found " + json.getClass().getSimpleName()); }/*w ww. j av a 2s .co m*/ JsonObject jObject = (JsonObject) json; if (!jObject.has("jsonrpc")) { throw new JsonParseException("Invalid JsonRpc response lacking version 'jsonrpc' field"); } if (!jObject.get("jsonrpc").getAsString().equals(JsonRpcConstants.JSON_RPC_VERSION)) { throw new JsonParseException("Invalid JsonRpc version"); } if (!jObject.has("id")) { throw new JsonParseException("Invalid JsonRpc response lacking 'id' field"); } if (jObject.has("result")) { return new JsonRpcResponse((JsonRpcResponseResult) context .deserialize(jObject.get("result").getAsJsonObject(), JsonRpcResponseResult.class), jObject.get("id").getAsInt()); } else if (jObject.has("error")) { return new JsonRpcResponse((JsonRpcResponseError) context .deserialize(jObject.get("error").getAsJsonObject(), JsonRpcResponseError.class), jObject.get("id").getAsInt()); } else { throw new JsonParseException("Invalid JsonRpc response lacking 'result' and 'error' fields"); } }
From source file:com.kurento.kmf.content.jsonrpc.GsonUtils.java
License:Open Source License
@Override public JsonRpcRequest deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (!(json instanceof JsonObject)) { throw new JsonParseException( "Invalid JsonRpc request showning JsonElement type " + json.getClass().getSimpleName()); }/* ww w . ja va2 s.c om*/ JsonObject jObject = (JsonObject) json; if (!jObject.has("jsonrpc")) { throw new JsonParseException("Invalid JsonRpc request lacking version 'jsonrpc' field"); } if (!jObject.get("jsonrpc").getAsString().equals(JsonRpcConstants.JSON_RPC_VERSION)) { throw new JsonParseException("Invalid JsonRpc version"); } if (!jObject.has("method")) { throw new JsonParseException("Invalid JsonRpc request lacking 'method' field"); } if (!jObject.has("params")) { throw new JsonParseException("Invalid JsonRpc request lacking 'params' field"); } if (!jObject.has("id")) { throw new JsonParseException("Invalid JsonRpc request lacking 'id' field"); } return new JsonRpcRequest( jObject.get("method").getAsString(), (JsonRpcRequestParams) context .deserialize(jObject.get("params").getAsJsonObject(), JsonRpcRequestParams.class), jObject.get("id").getAsInt()); }
From source file:com.kurento.kmf.jsonrpcconnector.JsonUtils.java
License:Open Source License
@Override public Response<?> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (!(json instanceof JsonObject)) { throw new JsonParseException("JonObject expected, found " + json.getClass().getSimpleName()); }//from www. java2s . c om JsonObject jObject = (JsonObject) json; if (!jObject.has(JSON_RPC_PROPERTY)) { throw new JsonParseException( "Invalid JsonRpc response lacking version '" + JSON_RPC_PROPERTY + "' field"); } if (!jObject.get(JSON_RPC_PROPERTY).getAsString().equals(JsonRpcConstants.JSON_RPC_VERSION)) { throw new JsonParseException("Invalid JsonRpc version"); } Integer id; try { id = jObject.get(ID_PROPERTY).getAsInt(); } catch (Exception e) { throw new JsonParseException("Invalid JsonRpc response. It lacks a valid '" + ID_PROPERTY + "' field"); } if (jObject.has(RESULT_PROPERTY)) { ParameterizedType parameterizedType = (ParameterizedType) typeOfT; return new Response<Object>(id, context.deserialize(jObject.get(RESULT_PROPERTY), parameterizedType.getActualTypeArguments()[0])); } else if (jObject.has(ERROR_PROPERTY)) { return new Response<Object>(id, (ResponseError) context.deserialize(jObject.get(ERROR_PROPERTY), ResponseError.class)); } else { throw new JsonParseException("Invalid JsonRpc response lacking '" + RESULT_PROPERTY + "' and '" + ERROR_PROPERTY + "' fields"); } }