List of usage examples for com.google.gson JsonDeserializationContext deserialize
public <T> T deserialize(JsonElement json, Type typeOfT) throws JsonParseException;
From source file:net.daporkchop.toobeetooteebot.text.JsonUtils.java
License:Open Source License
public static <T> T deserializeClass(JsonElement json, String memberName, JsonDeserializationContext context, Class<? extends T> adapter) { if (json != null) { return (T) context.deserialize(json, adapter); } else {//from ww w . j ava2 s .c o m throw new JsonSyntaxException("Missing " + memberName); } }
From source file:net.datenstrudel.bulbs.core.application.facade.json.TypeHierarchyAdapterDtoActuatorCmd.java
@Override public DtoAbstractActuatorCmd deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject src = json.getAsJsonObject(); String type = src.get("type").getAsString(); switch (type) { case "BULB": return context.deserialize(json, DtoBulbActuatorCmd.class); case "GROUP": return context.deserialize(json, DtoGroupActuatorCmd.class); case "PRESET": return context.deserialize(json, DtoPresetActuatorCmd.class); default://from ww w. j ava 2 s.co m throw new UnsupportedOperationException( "Specific " + "sub type of DtoAbstractActuatorCmd not supported; type was: " + type); } }
From source file:net.kleditzsch.shcCore.Json.AutomationDeviceResponseSerializer.java
License:Open Source License
@Override public AutomationDeviceResponse deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException { JsonObject jo = jsonElement.getAsJsonObject(); AutomationDeviceResponse automationDeviceResponse = new AutomationDeviceResponse(); automationDeviceResponse.setSuccess(jo.get("success").getAsBoolean()); automationDeviceResponse.setMessage(jo.get("message").getAsString()); automationDeviceResponse.setErrorCode(jo.get("errorCode").getAsInt()); JsonArray ja = jo.get("devices").getAsJsonArray(); for (int i = 0; i < ja.size(); i++) { JsonObject deviceJson = ja.get(i).getAsJsonObject(); AutomationDevice automationDevice = jsonDeserializationContext.deserialize(ja.get(i), AutomationDeviceResponse.getClassForType(deviceJson.get("type").getAsInt())); automationDeviceResponse.getAutomationDevices().put(automationDevice.getHash(), automationDevice); }/*from ww w . java2 s .c om*/ JsonArray ja1 = jo.get("switchServer").getAsJsonArray(); for (int i = 0; i < ja1.size(); i++) { JsonObject switchServerJson = ja1.get(i).getAsJsonObject(); SwitchServer switchServer; if (switchServerJson.get("type").getAsInt() == SwitchServer.SWITCH_SERVER_RASPBERRY_PI) { switchServer = jsonDeserializationContext.deserialize(ja1.get(i), RaspberryPiSwitchServer.class); } else { switchServer = jsonDeserializationContext.deserialize(ja1.get(i), MicroControllerSwitchServer.class); } automationDeviceResponse.getSwitchServers().put(switchServer.getHash(), switchServer); } return automationDeviceResponse; }
From source file:net.kyori.text.serializer.gson.GsonComponentSerializer.java
License:MIT License
private BuildableComponent<?, ?> deserialize0(final JsonElement element, final JsonDeserializationContext context) throws JsonParseException { if (element.isJsonPrimitive()) { return TextComponent.of(element.getAsString()); } else if (element.isJsonArray()) { ComponentBuilder<?, ?> parent = null; for (final JsonElement childElement : element.getAsJsonArray()) { final BuildableComponent<?, ?> child = this.deserialize0(childElement, context); if (parent == null) { parent = child.toBuilder(); } else { parent.append(child);//from ww w.jav a 2 s . co m } } if (parent == null) { throw new JsonParseException("Don't know how to turn " + element + " into a Component"); } return parent.build(); } else if (!element.isJsonObject()) { throw new JsonParseException("Don't know how to turn " + element + " into a Component"); } final JsonObject object = element.getAsJsonObject(); final ComponentBuilder<?, ?> component; if (object.has(TEXT)) { component = TextComponent.builder(object.get(TEXT).getAsString()); } else if (object.has(TRANSLATE)) { final String key = object.get(TRANSLATE).getAsString(); if (!object.has(TRANSLATE_WITH)) { component = TranslatableComponent.builder(key); } else { final JsonArray with = object.getAsJsonArray(TRANSLATE_WITH); final List<Component> args = new ArrayList<>(with.size()); for (int i = 0, size = with.size(); i < size; i++) { final JsonElement argElement = with.get(i); args.add(this.deserialize0(argElement, context)); } component = TranslatableComponent.builder(key).args(args); } } else if (object.has(SCORE)) { final JsonObject score = object.getAsJsonObject(SCORE); if (!score.has(SCORE_NAME) || !score.has(SCORE_OBJECTIVE)) { throw new JsonParseException( "A score component requires a " + SCORE_NAME + " and " + SCORE_OBJECTIVE); } // score components can have a value sometimes, let's grab it if (score.has(SCORE_VALUE)) { component = ScoreComponent.builder().name(score.get(SCORE_NAME).getAsString()) .objective(score.get(SCORE_OBJECTIVE).getAsString()) .value(score.get(SCORE_VALUE).getAsString()); } else { component = ScoreComponent.builder().name(score.get(SCORE_NAME).getAsString()) .objective(score.get(SCORE_OBJECTIVE).getAsString()); } } else if (object.has(SELECTOR)) { component = SelectorComponent.builder().pattern(object.get(SELECTOR).getAsString()); } else if (object.has(KEYBIND)) { component = KeybindComponent.builder().keybind(object.get(KEYBIND).getAsString()); } else if (object.has(NBT)) { final String nbt = object.get(NBT).getAsString(); final boolean interpret = object.has(NBT_INTERPRET) && object.getAsJsonPrimitive(NBT_INTERPRET).getAsBoolean(); if (object.has(NBT_BLOCK)) { final BlockNbtComponent.Pos position = context.deserialize(object.get(NBT_BLOCK), BlockNbtComponent.Pos.class); component = BlockNbtComponent.builder().nbtPath(nbt).interpret(interpret).pos(position); } else if (object.has(NBT_ENTITY)) { component = EntityNbtComponent.builder().nbtPath(nbt).interpret(interpret) .selector(object.get(NBT_ENTITY).getAsString()); } else { throw notSureHowToDeserialize(element); } } else { throw notSureHowToDeserialize(element); } if (object.has(EXTRA)) { final JsonArray extra = object.getAsJsonArray(EXTRA); for (int i = 0, size = extra.size(); i < size; i++) { final JsonElement extraElement = extra.get(i); component.append(this.deserialize0(extraElement, context)); } } final Style style = context.deserialize(element, Style.class); if (!style.isEmpty()) { component.style(style); } return component.build(); }
From source file:net.kyori.text.serializer.gson.StyleSerializer.java
License:MIT License
private Style deserialize(final JsonObject json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException { final Style.Builder style = Style.builder(); if (json.has(COLOR)) { final TextColorWrapper color = context.deserialize(json.get(COLOR), TextColorWrapper.class); if (color.color != null) { style.color(color.color);/*from www. j a v a 2 s.co m*/ } else if (color.decoration != null) { style.decoration(color.decoration, true); } } for (final TextDecoration decoration : DECORATIONS) { final String name = TextDecoration.NAMES.name(decoration); if (json.has(name)) { style.decoration(decoration, json.get(name).getAsBoolean()); } } if (json.has(INSERTION)) { style.insertion(json.get(INSERTION).getAsString()); } if (json.has(CLICK_EVENT)) { final JsonObject clickEvent = json.getAsJsonObject(CLICK_EVENT); if (clickEvent != null) { final /* @Nullable */ JsonPrimitive rawAction = clickEvent.getAsJsonPrimitive(CLICK_EVENT_ACTION); final ClickEvent./*@Nullable*/ Action action = rawAction == null ? null : context.deserialize(rawAction, ClickEvent.Action.class); if (action != null && action.readable()) { final /* @Nullable */ JsonPrimitive rawValue = clickEvent.getAsJsonPrimitive(CLICK_EVENT_VALUE); final /* @Nullable */ String value = rawValue == null ? null : rawValue.getAsString(); if (value != null) { style.clickEvent(ClickEvent.of(action, value)); } } } } if (json.has(HOVER_EVENT)) { final JsonObject hoverEvent = json.getAsJsonObject(HOVER_EVENT); if (hoverEvent != null) { final /* @Nullable */ JsonPrimitive rawAction = hoverEvent.getAsJsonPrimitive(HOVER_EVENT_ACTION); final HoverEvent./*@Nullable*/ Action action = rawAction == null ? null : context.deserialize(rawAction, HoverEvent.Action.class); if (action != null && action.readable()) { final /* @Nullable */ JsonElement rawValue = hoverEvent.get(HOVER_EVENT_VALUE); final /* @Nullable */ Component value = rawValue == null ? null : context.deserialize(rawValue, Component.class); if (value != null) { style.hoverEvent(HoverEvent.of(action, value)); } } } } return style.build(); }
From source file:net.kyori.text.serializer.gson.TextColorWrapper.java
License:MIT License
private static TextColor deserializeColor(final JsonElement json, final JsonDeserializationContext context) { try {/*from w w w. j a v a 2 s. c o m*/ return context.deserialize(json, TextColor.class); } catch (final JsonParseException e) { return null; } }
From source file:net.kyori.text.serializer.gson.TextColorWrapper.java
License:MIT License
private static TextDecoration deserializeDecoration(final JsonElement json, final JsonDeserializationContext context) { try {//from w ww . j a v a 2 s. co m return context.deserialize(json, TextDecoration.class); } catch (final JsonParseException e) { return null; } }
From source file:net.minecraftforge.gradle.util.json.fgversion.FGVersionDeserializer.java
License:Open Source License
@Override public FGVersionWrapper deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { FGVersionWrapper wrapper = new FGVersionWrapper(); List<FGVersion> versions = context.deserialize(json, new TypeToken<List<FGVersion>>() { }.getType());/*from w ww . j a va2s .c om*/ for (int i = 0; i < versions.size(); i++) { FGVersion v = versions.get(i); v.index = i; wrapper.versions.add(v.version); wrapper.versionObjects.put(v.version, v); } return wrapper; }
From source file:net.minecraftforge.gradle.util.json.MojangManifestAdapter.java
License:Open Source License
@Override public Map<String, ManifestVersion> deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException { Map<String, ManifestVersion> out = Maps.newHashMap(); for (JsonElement element : json.getAsJsonObject().get("versions").getAsJsonArray()) { ManifestVersion version = context.deserialize(element, ManifestVersion.class); out.put(version.id, version);/* w ww .j ava2s . c o m*/ } return out; }
From source file:net.sf.uadetector.json.internal.data.deserializer.BrowserPatternDeserializer.java
License:Apache License
@Override public BrowserPattern deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) { String hash = EMPTY_HASH_CODE; Pattern pattern = null;/* ww w . j av a 2 s . com*/ // deserialize for (final Entry<String, JsonElement> entry : json.getAsJsonObject().entrySet()) { if (PATTERN.getName().equals(entry.getKey())) { pattern = context.deserialize(entry.getValue(), Pattern.class); } else if (HASH.getName().equals(entry.getKey())) { hash = entry.getValue().getAsString(); } } final int id = counter.incrementAndGet(); // create browser pattern BrowserPattern browserPattern = null; try { browserPattern = new BrowserPattern(id, pattern, id); // check hash when option is set checkHash(json, hash, browserPattern); // add pattern to map browserPatterns.put(hash, browserPattern); } catch (final Exception e) { addWarning(e.getLocalizedMessage()); } return browserPattern; }