List of usage examples for com.google.gson JsonDeserializationContext deserialize
public <T> T deserialize(JsonElement json, Type typeOfT) throws JsonParseException;
From source file:org.kurento.jsonrpc.JsonUtils.java
License:Apache 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()); }// ww w . j a v a2 s . c o m 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 = null; JsonElement idAsJsonElement = jObject.get(ID_PROPERTY); if (idAsJsonElement != null) { try { id = Integer.valueOf(idAsJsonElement.getAsInt()); } catch (Exception e) { throw new JsonParseException("Invalid format in '" + ID_PROPERTY + "' field in request " + json); } } if (jObject.has(ERROR_PROPERTY)) { return new Response<>(id, (ResponseError) context.deserialize(jObject.get(ERROR_PROPERTY), ResponseError.class)); } else { if (jObject.has(RESULT_PROPERTY)) { ParameterizedType parameterizedType = (ParameterizedType) typeOfT; return new Response<>(id, context.deserialize(jObject.get(RESULT_PROPERTY), parameterizedType.getActualTypeArguments()[0])); } else { log.warn("Invalid JsonRpc response: " + json + " It lacks a valid '" + RESULT_PROPERTY + "' or '" + ERROR_PROPERTY + "' field"); return new Response<>(id, null); } } }
From source file:org.kurento.jsonrpc.JsonUtils.java
License:Apache License
@Override public Request<?> 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()); }/*from ww w .j a v a 2 s. co m*/ JsonObject jObject = (JsonObject) json; // FIXME: Enable again when KMS sends jsonrpc field in register message // if (!jObject.has(JSON_RPC_PROPERTY)) { // throw new JsonParseException( // "Invalid JsonRpc request lacking version '" // + JSON_RPC_PROPERTY + "' field"); // } // // if (!jObject.get("jsonrpc").getAsString().equals(JSON_RPC_VERSION)) { // throw new JsonParseException("Invalid JsonRpc version"); // } if (!jObject.has(METHOD_PROPERTY)) { throw new JsonParseException("Invalid JsonRpc request lacking '" + METHOD_PROPERTY + "' field"); } Integer id = null; if (jObject.has(ID_PROPERTY)) { id = Integer.valueOf(jObject.get(ID_PROPERTY).getAsInt()); } ParameterizedType parameterizedType = (ParameterizedType) typeOfT; return new Request<>(id, jObject.get(METHOD_PROPERTY).getAsString(), context.deserialize(jObject.get(PARAMS_PROPERTY), parameterizedType.getActualTypeArguments()[0])); }
From source file:org.kurento.modulecreator.json.RemoteClassAdapter.java
License:Apache License
@Override public RemoteClass deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject object = (JsonObject) json; String name = null;/* w w w .j a v a 2s .c o m*/ String doc = null; boolean abstractValue = false; TypeRef extendsValue = null; Method constructor = null; List<Method> methods = new ArrayList<Method>(); List<Property> properties = new ArrayList<Property>(); List<TypeRef> events = new ArrayList<TypeRef>(); if (object.get("name") != null) { name = object.get("name").getAsString(); } if (object.get("doc") != null) { doc = object.get("doc").getAsString(); } if (object.get("abstract") != null) { abstractValue = object.get("abstract").getAsBoolean(); } if (object.get("extends") != null) { extendsValue = context.deserialize(object.get("extends"), TypeRef.class); } if (object.get("constructor") != null) { constructor = context.deserialize(object.get("constructor"), new TypeToken<Method>() { }.getType()); } if (object.get("methods") != null) { methods = context.deserialize(object.get("methods"), new TypeToken<List<Method>>() { }.getType()); } if (object.get("properties") != null) { properties = context.deserialize(object.get("properties"), new TypeToken<List<Property>>() { }.getType()); } if (object.get("events") != null) { events = context.deserialize(object.get("events"), new TypeToken<List<TypeRef>>() { }.getType()); } RemoteClass remoteClass = new RemoteClass(name, doc, extendsValue, constructor, methods, properties, events); remoteClass.setAbstract(abstractValue); return remoteClass; }
From source file:org.lanternpowered.server.script.function.action.json.ConditionalActionJsonSerializer.java
License:MIT License
@Override public ConditionalAction deserialize(JsonElement element, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { final JsonObject obj = element.getAsJsonObject(); final Condition condition = context.deserialize(obj.get(CONDITION), Condition.class); final Action action = context.deserialize(obj.get(ACTION), Action.class); return Action.conditional(condition, action); }
From source file:org.lanternpowered.server.script.function.action.json.MultiActionJsonSerializer.java
License:MIT License
@Override public MultiAction deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { final Action[] actions; if (!json.isJsonArray()) { actions = new Action[] { context.deserialize(json, Action.class) }; } else {/*w w w . j a v a 2s . com*/ actions = context.deserialize(json, Action[].class); } return Action.multi(actions); }
From source file:org.lanternpowered.server.script.function.condition.json.AndConditionJsonSerializer.java
License:MIT License
@Override public AndCondition deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { return Condition.and((Condition[]) context.deserialize(json, Condition[].class)); }
From source file:org.lanternpowered.server.script.function.condition.json.OrConditionJsonSerializer.java
License:MIT License
@Override public OrCondition deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { return Condition.or((Condition[]) context.deserialize(json, Condition[].class)); }
From source file:org.lanternpowered.server.text.gson.JsonTextBaseSerializer.java
License:MIT License
public static void deserialize(JsonObject json, Text.Builder builder, JsonDeserializationContext context, @Nullable JsonArray children) throws JsonParseException { JsonElement element;// ww w .j a v a 2 s . com if ((element = json.get(COLOR)) != null) { Sponge.getRegistry().getType(TextColor.class, element.getAsString()).ifPresent(builder::color); } TextStyle style = builder.getStyle(); if ((element = json.get(BOLD)) != null) { style = style.bold(element.getAsBoolean()); } if ((element = json.get(ITALIC)) != null) { style = style.italic(element.getAsBoolean()); } if ((element = json.get(UNDERLINE)) != null) { style = style.underline(element.getAsBoolean()); } if ((element = json.get(STRIKETHROUGH)) != null) { style = style.strikethrough(element.getAsBoolean()); } if ((element = json.get(OBFUSCATED)) != null) { style = style.obfuscated(element.getAsBoolean()); } builder.style(style); if (children != null) { builder.append((Text[]) context.deserialize(children, Text[].class)); } if ((element = json.get(CLICK_EVENT)) != null) { final JsonObject jsonClickEvent = element.getAsJsonObject(); if (jsonClickEvent != null) { final JsonPrimitive jsonEventAction = jsonClickEvent.getAsJsonPrimitive(EVENT_ACTION); final JsonPrimitive jsonEventValue = jsonClickEvent.getAsJsonPrimitive(EVENT_VALUE); if (jsonEventAction != null && jsonEventValue != null) { final String action = jsonEventAction.getAsString(); final String value = jsonEventValue.getAsString(); final ClickAction<?> clickAction = LanternTextHelper.parseClickAction(action, value); if (clickAction != null) { builder.onClick(clickAction); } } } } if ((element = json.get(HOVER_EVENT)) != null) { final JsonObject jsonHoverEvent = element.getAsJsonObject(); if (jsonHoverEvent != null) { final JsonPrimitive jsonEventAction = jsonHoverEvent.getAsJsonPrimitive(EVENT_ACTION); final JsonPrimitive jsonEventValue = jsonHoverEvent.getAsJsonPrimitive(EVENT_VALUE); if (jsonEventAction != null && jsonEventValue != null) { final String action = jsonEventAction.getAsString(); final String value = jsonEventValue.getAsString(); final HoverAction<?> hoverAction = LanternTextHelper.parseHoverAction(action, value); if (hoverAction != null) { builder.onHover(hoverAction); } } } } if ((element = json.get(INSERTION)) != null) { builder.onShiftClick(TextActions.insertText(element.getAsString())); } }
From source file:org.lanternpowered.server.text.gson.JsonTextSerializer.java
License:MIT License
@Override public Text deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (json.isJsonPrimitive()) { return context.deserialize(json, LiteralText.class); }/*from w w w . java 2 s . c o m*/ if (json.isJsonArray()) { final Text.Builder builder = Text.builder(); builder.append(context.<Text[]>deserialize(json, Text[].class)); return builder.build(); } final JsonObject obj = json.getAsJsonObject(); if (obj.has(TEXT)) { return context.deserialize(json, LiteralText.class); } else if (obj.has(TRANSLATABLE)) { return context.deserialize(json, TranslatableText.class); } else if (obj.has(SCORE_VALUE)) { return context.deserialize(json, ScoreText.class); } else if (obj.has(SELECTOR)) { return context.deserialize(json, SelectorText.class); } else { throw new JsonParseException("Unknown text format: " + json.toString()); } }
From source file:org.lanternpowered.server.text.gson.JsonTextTranslatableSerializer.java
License:MIT License
@Override public TranslatableText deserialize(JsonElement element, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { final JsonObject obj = element.getAsJsonObject(); final String name = obj.get(TRANSLATABLE).getAsString(); final Translation translation = this.translationManager.get(name); final Object[] arguments; if ((element = obj.get(TRANSLATABLE_ARGS)) != null) { final Text[] with = context.deserialize(element, Text[].class); arguments = new Object[with.length]; System.arraycopy(with, 0, arguments, 0, with.length); } else {/*from w w w . j a v a2 s. co m*/ arguments = new Object[0]; } final TranslatableText.Builder builder = Text.builder(translation, arguments); deserialize(obj, builder, context); return builder.build(); }