List of usage examples for com.google.gson JsonDeserializationContext deserialize
public <T> T deserialize(JsonElement json, Type typeOfT) throws JsonParseException;
From source file:org.openhab.binding.unifi.internal.api.util.UniFiClientDeserializer.java
License:Open Source License
@Override public UniFiClient deserialize(JsonElement json, Type type, JsonDeserializationContext context) throws JsonParseException { JsonObject jsonObject = json.getAsJsonObject(); JsonElement isWiredElement = jsonObject.get(PROPERTY_IS_WIRED); // mgb: if the "is_wired "property is missing, the client is unknown if (isWiredElement == null) { return context.deserialize(json, UniFiUnknownClient.class); }/*from w w w . ja v a 2s . co m*/ boolean isWired = isWiredElement.getAsBoolean(); if (isWired) { return context.deserialize(json, UniFiWiredClient.class); } return context.deserialize(json, UniFiWirelessClient.class); }
From source file:org.openhab.io.neeo.internal.serialization.NeeoDeviceChannelSerializer.java
License:Open Source License
@Override public NeeoDeviceChannel deserialize(@Nullable JsonElement elm, @Nullable Type type, @Nullable JsonDeserializationContext context) throws JsonParseException { Objects.requireNonNull(elm, "elm cannot be null"); Objects.requireNonNull(type, "type cannot be null"); Objects.requireNonNull(context, "context cannot be null"); if (!(elm instanceof JsonObject)) { throw new JsonParseException("Element not an instance of JsonObject: " + elm); }//w w w . j a va 2 s .c o m final JsonObject jo = (JsonObject) elm; final String itemName = NeeoUtil.getString(jo, "itemName"); if (itemName == null || StringUtils.isEmpty(itemName)) { throw new JsonParseException("Element requires an itemName attribute: " + elm); } final ItemSubType itemSubType = jo.has("subType") ? context.deserialize(jo.get("subType"), ItemSubType.class) : ItemSubType.NONE; final String label = NeeoUtil.getString(jo, "label"); final String value = NeeoUtil.getString(jo, "value"); final Integer channelNbr = NeeoUtil.getInt(jo, "channelNbr"); if (channelNbr == null) { throw new JsonParseException("Channel Number is not a valid integer"); } final NeeoCapabilityType capType = context.deserialize(jo.get("type"), NeeoCapabilityType.class); final NeeoDeviceChannelRange range = jo.has("range") ? context.deserialize(jo.get("range"), NeeoDeviceChannelRange.class) : null; final NeeoDeviceChannelKind kind = jo.has("kind") ? context.deserialize(jo.get("kind"), NeeoDeviceChannelKind.class) : NeeoDeviceChannelKind.ITEM; try { if (capType == NeeoCapabilityType.TEXTLABEL) { final boolean labelVisible = jo.has("labelVisible") ? jo.get("labelVisible").getAsBoolean() : true; return new NeeoDeviceChannelText(kind, itemName, channelNbr, capType, itemSubType, label == null || StringUtils.isEmpty(label) ? NeeoUtil.NOTAVAILABLE : label, value, range, labelVisible); } else if (capType == NeeoCapabilityType.DIRECTORY) { final NeeoDeviceChannelDirectoryListItem[] listItems = jo.has("listItems") ? context.deserialize(jo.get("listItems"), NeeoDeviceChannelDirectoryListItem[].class) : new NeeoDeviceChannelDirectoryListItem[0]; return new NeeoDeviceChannelDirectory(kind, itemName, channelNbr, capType, itemSubType, label == null || StringUtils.isEmpty(label) ? NeeoUtil.NOTAVAILABLE : label, value, range, listItems); } else { return new NeeoDeviceChannel(kind, itemName, channelNbr, capType, itemSubType, label == null || StringUtils.isEmpty(label) ? NeeoUtil.NOTAVAILABLE : label, value, range); } } catch (NullPointerException | IllegalArgumentException e) { throw new JsonParseException(e); } }
From source file:org.openhab.io.neeo.internal.serialization.NeeoDeviceSerializer.java
License:Open Source License
@Override public NeeoDevice deserialize(@Nullable JsonElement elm, @Nullable Type type, @Nullable JsonDeserializationContext jsonContext) throws JsonParseException { Objects.requireNonNull(elm, "elm cannot be null"); Objects.requireNonNull(type, "type cannot be null"); Objects.requireNonNull(jsonContext, "jsonContext cannot be null"); if (!(elm instanceof JsonObject)) { throw new JsonParseException("Element not an instance of JsonObject: " + elm); }/*from w ww .j ava2 s.c om*/ final JsonObject jo = (JsonObject) elm; final NeeoThingUID uid = jsonContext.deserialize(jo.get("uid"), NeeoThingUID.class); final NeeoDeviceType devType = jsonContext.deserialize(jo.get("type"), NeeoDeviceType.class); final String manufacturer = NeeoUtil.getString(jo, "manufacturer"); final String name = NeeoUtil.getString(jo, "name"); final NeeoDeviceChannel[] channels = jsonContext.deserialize(jo.get("channels"), NeeoDeviceChannel[].class); final NeeoDeviceTiming timing = jo.has("timing") ? jsonContext.deserialize(jo.get("timing"), NeeoDeviceTiming.class) : null; final String[] deviceCapabilities = jo.has("deviceCapabilities") ? jsonContext.deserialize(jo.get("deviceCapabilities"), String[].class) : null; final String specificName = jo.has("specificName") ? jo.get("specificName").getAsString() : null; final String iconName = jo.has("iconName") ? jo.get("iconName").getAsString() : null; final int driverVersion = jo.has("driverVersion") ? jo.get("driverVersion").getAsInt() : 0; try { return new NeeoDevice(uid, driverVersion, devType, manufacturer == null || StringUtils.isEmpty(manufacturer) ? NeeoUtil.NOTAVAILABLE : manufacturer, name, Arrays.asList(channels), timing, deviceCapabilities == null ? null : Arrays.asList(deviceCapabilities), specificName, iconName); } catch (NullPointerException | IllegalArgumentException e) { throw new JsonParseException(e); } }
From source file:org.ppojo.data.ArtifactSerializer.java
License:Apache License
@Override public ArtifactData deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (!json.isJsonObject()) throw new JsonParseException("Invalid element type for artifact, expected JsonObject got " + JsonElementTypes.getType(json) + ", in " + getDeserializeFilePath()); String artifactName = readStringProperty(json, "", "name"); String type = readStringProperty(json, artifactName, "type"); ArtifactTypes artifactTypes = ArtifactTypes.Parse(type); if (artifactTypes == ArtifactTypes.Unknown) throw new InvalidArtifactType("Invalid artifact type " + type + " at " + artifactName + " in template " + getDeserializeFilePath()); ArtifactMetaData artifactMetaData = ArtifactMetaData.getArtifactMetaData(artifactTypes); Map<String, Object> localOptions = null; for (Map.Entry<String, JsonElement> entry : json.getAsJsonObject().entrySet()) { JsonElementTypes elementType = JsonElementTypes.getType(entry.getValue()); String propertyName = entry.getKey(); ArtifactOptions.Fields optionsProperty = ArtifactOptions.Fields.Parse(propertyName); if (optionsProperty != ArtifactOptions.Fields.Unknown) { ArtifactMetaData.validateOptionsProperty(artifactMetaData, optionsProperty, elementType, artifactName, getDeserializeFilePath()); Object optionValue = context.deserialize(entry.getValue(), optionsProperty.getOptionType()); if (optionsProperty.getOptionType().isEnum() && optionValue == null) { throw new EnumParseException(entry.getValue().getAsString(), propertyName, artifactName, getDeserializeFilePath()); }//from ww w.j a va 2 s. c o m if (localOptions == null) localOptions = new HashMap<>(); localOptions.put(optionsProperty.toString(), optionValue); } else ArtifactMetaData.validateArtifactProperty(artifactMetaData, propertyName, elementType, artifactName, getDeserializeFilePath()); if (elementType == JsonElementTypes.JsonArray) { for (JsonElement arrayElement : entry.getValue().getAsJsonArray()) { JsonElementTypes arrayElementType = JsonElementTypes.getType(arrayElement); if (arrayElementType != JsonElementTypes.String && arrayElementType != JsonElementTypes.JsonObject) throw new JsonArrayMixedItemTypes( "Invalid json array property " + entry.getKey() + " of artifact " + artifactName + ". Item types must all be String o JsonObject items. in " + getDeserializeFilePath()); } } } ArtifactData result; result = _gson.fromJson(json, artifactMetaData.getArtifactClass()); result.options = localOptions; return result; }
From source file:org.ppojo.data.TemplateSerializer.java
License:Apache License
@Override public TemplateFileData deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (!json.isJsonObject()) throw new JsonParseException("Invalid element type for template, expected JsonObject got " + JsonElementTypes.getType(json) + ", in " + getDeserializeFilePath()); ArtifactData[] artifacts = null;//from w w w. jav a 2 s.co m Map<String, Object> options = null; for (Map.Entry<String, JsonElement> entry : json.getAsJsonObject().entrySet()) { JsonElementTypes elementType = JsonElementTypes.getType(entry.getValue()); String propertyName = entry.getKey(); validateTemplateProperty(propertyName, elementType, getDeserializeFilePath()); switch (propertyName) { case TemplateFileData.Fields.ARTIFACTS: artifacts = context.deserialize(entry.getValue(), ArtifactData[].class); break; case TemplateFileData.Fields.OPTIONS: options = deserializeOptions(entry.getValue(), context); break; } } TemplateFileData result = _gson.fromJson(json, TemplateFileData.class); result.artifacts = artifacts; result.options = options; return result; }
From source file:org.ppojo.data.TemplateSerializer.java
License:Apache License
private Map<String, Object> deserializeOptions(JsonElement optionsElement, JsonDeserializationContext context) { Map<String, Object> optionsResult = new HashMap<>(); for (Map.Entry<String, JsonElement> entry : optionsElement.getAsJsonObject().entrySet()) { String propertyName = entry.getKey(); ArtifactOptions.Fields optionsProperty = ArtifactOptions.Fields.Parse(propertyName); if (optionsProperty == ArtifactOptions.Fields.Unknown) throw new UnsupportedTemplateOption(propertyName, getDeserializeFilePath()); JsonElementTypes elementType = JsonElementTypes.getType(entry.getValue()); JsonElementTypes expectedElementType = Serializer.validateJsonSupportedOption(optionsProperty, TemplateFileData.class); if (expectedElementType != elementType) throw new TemplateElementTypeMismatch(propertyName, expectedElementType, elementType, getDeserializeFilePath()); Object optionValue = context.deserialize(entry.getValue(), optionsProperty.getOptionType()); if (optionsProperty.getOptionType().isEnum() && optionValue == null) { throw new EnumParseException(entry.getValue().getAsString(), propertyName, getDeserializeFilePath()); }//from www.j ava 2 s . c om optionsResult.put(optionsProperty.toString(), optionValue); } return optionsResult; }
From source file:org.project.openbaton.nubomedia.paas.model.openshift.PodDeserializer.java
License:Apache License
@Override public Pod deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject obj = json.getAsJsonObject(); String phase = "", hostIP = "", podIP = ""; Metadata metadata = context.deserialize(obj.get("metadata"), Metadata.class); if (obj.has("status")) { JsonObject status = obj.get("status").getAsJsonObject(); phase = status.get("phase").getAsString(); hostIP = status.get("hostIP").getAsString(); podIP = status.get("podIP").getAsString(); }/*from w w w.j a va2 s.c o m*/ return new Pod(metadata, new Pod.PodStatus(phase, hostIP, podIP)); }
From source file:org.project.openbaton.nubomedia.paas.model.openshift.TriggerDeserializer.java
License:Apache License
@Override public Trigger deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject asObj = json.getAsJsonObject(); String type = asObj.get("type").getAsString(); if (type.equals("ConfigChange")) { return context.deserialize(json, ConfigChangeTrigger.class); } else {//from w w w . j av a 2s. c o m return context.deserialize(json, ImageChangeTrigger.class); } }
From source file:org.quartzpowered.protocol.data.chat.component.serialize.BaseComponentSerializer.java
License:Open Source License
protected void deserialize(JsonObject object, BaseComponent component, JsonDeserializationContext context) { if (object.has("color")) { component.setColor(ChatColor.valueOf(object.get("color").getAsString().toUpperCase())); }/*from ww w .j a v a2s . c o m*/ if (object.has("bold")) { component.setBold(object.get("bold").getAsBoolean()); } if (object.has("italic")) { component.setItalic(object.get("italic").getAsBoolean()); } if (object.has("underlined")) { component.setUnderlined(object.get("underlined").getAsBoolean()); } if (object.has("strikethrough")) { component.setStrikethrough(object.get("strikethrough").getAsBoolean()); } if (object.has("obfuscated")) { component.setObfuscated(object.get("obfuscated").getAsBoolean()); } if (object.has("extra")) { component.setExtra(Arrays.<BaseComponent>asList( context.<BaseComponent[]>deserialize(object.get("extra"), BaseComponent[].class))); } //Events if (object.has("clickEvent")) { JsonObject event = object.getAsJsonObject("clickEvent"); component.setClickEvent( new ClickEvent(ClickEvent.Action.valueOf(event.get("action").getAsString().toUpperCase()), event.get("value").getAsString())); } if (object.has("hoverEvent")) { JsonObject event = object.getAsJsonObject("hoverEvent"); BaseComponent[] res; if (event.get("value").isJsonArray()) { res = context.deserialize(event.get("value"), BaseComponent[].class); } else { res = new BaseComponent[] { context.<BaseComponent>deserialize(event.get("value"), BaseComponent.class) }; } component.setHoverEvent(new HoverEvent( HoverEvent.Action.valueOf(event.get("action").getAsString().toUpperCase()), res)); } }
From source file:org.quartzpowered.protocol.data.chat.component.serialize.ComponentSerializer.java
License:Open Source License
@Override public BaseComponent deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (json.isJsonPrimitive()) { return new TextComponent(json.getAsString()); }//from www .j a va 2 s . c o m JsonObject object = json.getAsJsonObject(); if (object.has("translate")) { return context.deserialize(json, TranslatableComponent.class); } return context.deserialize(json, TextComponent.class); }