List of usage examples for com.google.gson JsonParseException JsonParseException
public JsonParseException(Throwable cause)
From source file:org.mozilla.zest.core.v1.ZestJSON.java
License:Mozilla Public License
@Override public ZestElement deserialize(JsonElement element, Type rawType, JsonDeserializationContext arg2) throws JsonParseException { if (element instanceof JsonObject) { String elementType = ((JsonObject) element).get("elementType").getAsString(); if (elementType.startsWith("Zest")) { try { //Class<?> c = Class.forName(this.getClass().getPackage().getName()"org.mozilla.zest.core.v1." + elementType); Class<?> c = Class.forName(this.getClass().getPackage().getName() + "." + elementType); return (ZestElement) getGson().fromJson(element, c); } catch (ClassNotFoundException e) { throw new JsonParseException(e); }/*w ww.j a v a 2 s .com*/ } } return null; }
From source file:org.myeslib.jdbi.storage.helpers.gson.RuntimeTypeAdapter.java
License:Apache License
public T deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonElement labelJsonElement = json.getAsJsonObject().remove(typeFieldName); if (labelJsonElement == null) { throw new JsonParseException( "cannot deserialize " + typeOfT + " because it does not define a field named " + typeFieldName); }/* w w w . ja va 2s . c o m*/ String label = labelJsonElement.getAsString(); Class<?> subtype = labelToSubtype.get(label); if (subtype == null) { throw new JsonParseException("cannot deserialize " + baseType + " subtype named " + label + "; did you forget to register a subtype?"); } @SuppressWarnings("unchecked") // registration requires that subtype extends T T result = (T) context.deserialize(json, subtype); return result; }
From source file:org.nnsoft.sameas4j.AbstractEquivalenceDeserializer.java
License:Open Source License
/** * Deserialize a single {@link org.nnsoft.sameas4j.Equivalence} * from its Json serialization./*from w w w.j a v a2 s. c om*/ * * @param json object to be deserialized * @return a not null {@link org.nnsoft.sameas4j.Equivalence} instance */ public Equivalence getEquivalence(JsonElement json) { Equivalence equivalence; String uriString = json.getAsJsonObject().getAsJsonPrimitive(URI).getAsString(); URI uri; try { uri = new URI(urlEncode(uriString)); } catch (Exception e) { throw new JsonParseException(String.format(EXCEPTION_MESSAGE, uriString)); } equivalence = new Equivalence(uri); JsonArray duplicates = json.getAsJsonObject().getAsJsonArray(DUPLICATES); for (int i = 0; i < duplicates.size(); i++) { try { equivalence.addDuplicate(new URI(urlEncode(duplicates.get(i).getAsString()))); } catch (Exception e) { // if an equivalent URI is not well-formed it's better to do not add it, let's go on continue; } } return equivalence; }
From source file:org.obsidianbox.obsidian.addon.AddonDescriptionJsonDeserializer.java
License:MIT License
@Override public AddonDescription deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { JsonObject jsonObject = json.getAsJsonObject(); final String identifier = jsonObject.get("identifier").getAsString(); final String name = jsonObject.get("name").getAsString(); final String version = jsonObject.get("version").getAsString(); final String main = jsonObject.get("main").getAsString(); final String modeRaw = jsonObject.get("mode").getAsString(); AddonMode mode;/*from w w w.j a v a 2 s . co m*/ try { mode = AddonMode.valueOf(modeRaw); } catch (Exception e) { throw new JsonParseException(modeRaw + " is not a valid addon mode [CLIENT, SERVER, BOTH]"); } return new AddonDescription(identifier, name, version, mode, main); }
From source file:org.opendaylight.sfc.tacker.util.DateDeserializer.java
License:Open Source License
@Override public Date deserialize(JsonElement jsonElement, Type typeOF, JsonDeserializationContext context) throws JsonParseException { if (jsonElement.getAsString().endsWith("Z")) { try {//from w w w . j av a2s .c om return new SimpleDateFormat(DATE_FORMAT_ZULU, Locale.US) .parse(jsonElement.getAsString().replace("Z", "+0000")); } catch (ParseException ignored) { } } else { try { // cutting off the microsecond precision return new SimpleDateFormat(DATE_FORMAT_STANDARD, Locale.US).parse( jsonElement.getAsString().substring(0, jsonElement.getAsString().length() - 3) + "+0000"); } catch (ParseException ignored) { } } throw new JsonParseException("Unparseable date: \"" + jsonElement.getAsString() + "\"."); }
From source file:org.openhab.binding.nikohomecontrol.internal.protocol.nhc1.NikoHomeControlMessageDeserializer1.java
License:Open Source License
@Override public NhcMessageBase1 deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException { final JsonObject jsonObject = json.getAsJsonObject(); try {/*from w ww .jav a2s. c o m*/ String cmd = null; String event = null; if (jsonObject.has("cmd")) { cmd = jsonObject.get("cmd").getAsString(); } if (jsonObject.has("event")) { event = jsonObject.get("event").getAsString(); } JsonElement jsonData = null; if (jsonObject.has("data")) { jsonData = jsonObject.get("data"); } NhcMessageBase1 message = null; if (jsonData != null) { if (jsonData.isJsonObject()) { message = new NhcMessageMap1(); Map<String, String> data = new HashMap<>(); for (Entry<String, JsonElement> entry : jsonData.getAsJsonObject().entrySet()) { data.put(entry.getKey(), entry.getValue().getAsString()); } ((NhcMessageMap1) message).setData(data); } else if (jsonData.isJsonArray()) { JsonArray jsonDataArray = jsonData.getAsJsonArray(); message = new NhcMessageListMap1(); List<Map<String, String>> dataList = new ArrayList<>(); for (int i = 0; i < jsonDataArray.size(); i++) { JsonObject jsonDataObject = jsonDataArray.get(i).getAsJsonObject(); Map<String, String> data = new HashMap<>(); for (Entry<String, JsonElement> entry : jsonDataObject.entrySet()) { data.put(entry.getKey(), entry.getValue().getAsString()); } dataList.add(data); } ((NhcMessageListMap1) message).setData(dataList); } } if (message != null) { message.setCmd(cmd); message.setEvent(event); } else { throw new JsonParseException("Unexpected Json type"); } return message; } catch (IllegalStateException | ClassCastException e) { throw new JsonParseException("Unexpected Json type"); } }
From source file:org.openhab.binding.nikohomecontrol.internal.protocol.NikoHomeControlMessageDeserializer.java
License:Open Source License
@Override public NhcMessageBase deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException { final JsonObject jsonObject = json.getAsJsonObject(); try {/*w w w.ja va2 s . c o m*/ String cmd = null; String event = null; if (jsonObject.has("cmd")) { cmd = jsonObject.get("cmd").getAsString(); } if (jsonObject.has("event")) { event = jsonObject.get("event").getAsString(); } JsonElement jsonData = null; if (jsonObject.has("data")) { jsonData = jsonObject.get("data"); } NhcMessageBase message = null; if (jsonData != null) { if (jsonData.isJsonObject()) { message = new NhcMessageMap(); Map<String, String> data = new HashMap<>(); for (Entry<String, JsonElement> entry : jsonData.getAsJsonObject().entrySet()) { data.put(entry.getKey(), entry.getValue().getAsString()); } ((NhcMessageMap) message).setData(data); } else if (jsonData.isJsonArray()) { JsonArray jsonDataArray = jsonData.getAsJsonArray(); message = new NhcMessageListMap(); List<Map<String, String>> dataList = new ArrayList<>(); for (int i = 0; i < jsonDataArray.size(); i++) { JsonObject jsonDataObject = jsonDataArray.get(i).getAsJsonObject(); Map<String, String> data = new HashMap<>(); for (Entry<String, JsonElement> entry : jsonDataObject.entrySet()) { data.put(entry.getKey(), entry.getValue().getAsString()); } dataList.add(data); } ((NhcMessageListMap) message).setData(dataList); } } if (message != null) { message.setCmd(cmd); message.setEvent(event); } else { throw new JsonParseException("Unexpected Json type"); } return message; } catch (IllegalStateException | ClassCastException e) { throw new JsonParseException("Unexpected Json type"); } }
From source file:org.openhab.io.hueemulation.internal.RESTApi.java
License:Open Source License
/** * Handles /api and forwards any deeper path * * @param isDebug//w w w . j av a2 s.co m */ @SuppressWarnings("null") public int handle(HttpMethod method, String body, Writer out, Path path, boolean isDebug) throws IOException, JsonParseException { if (!"api".equals(path.getName(0).toString())) { return 404; } if (path.getNameCount() == 1) { // request for API key if (method != HttpMethod.POST) { return 405; } if (!ds.config.linkbutton) { return 10403; } final HueCreateUser userRequest; userRequest = gson.fromJson(body, HueCreateUser.class); if (userRequest.devicetype == null || userRequest.devicetype.isEmpty()) { throw new JsonParseException("devicetype not given"); } String apiKey = userRequest.username; if (apiKey == null || apiKey.length() == 0) { apiKey = UUID.randomUUID().toString(); } userManagement.addUser(apiKey, userRequest.devicetype); try (JsonWriter writer = new JsonWriter(out)) { HueSuccessResponseCreateUser h = new HueSuccessResponseCreateUser(apiKey); gson.toJson(Collections.singleton(new HueResponse(h)), new TypeToken<List<?>>() { }.getType(), writer); } return 200; } updateDataStore(); Path userPath = remaining(path); return handleUser(method, body, out, userPath.getName(0).toString(), remaining(userPath), path, isDebug); }
From source file:org.openhab.io.hueemulation.internal.RESTApi.java
License:Open Source License
/** * Hue API call to set the state of a light. * Enpoint: /api/{username}/lights/{id}/state *//* w w w. j a v a 2 s. co m*/ @SuppressWarnings({ "null", "unused" }) private int handleLightChangeState(Path fullURI, HttpMethod method, String body, Writer out, int hueID, HueDevice hueDevice) throws IOException, JsonParseException { HueStateChange state = gson.fromJson(body, HueStateChange.class); if (state == null) { throw new JsonParseException("No state change data received!"); } // logger.debug("Received state change: {}", gson.toJson(state)); // Apply new state and collect success, error items Map<String, Object> successApplied = new TreeMap<>(); List<String> errorApplied = new ArrayList<>(); Command command = hueDevice.applyState(state, successApplied, errorApplied); // If a command could be created, post it to the framework now if (command != null) { logger.debug("sending {} to {}", command, hueDevice.item.getName()); eventPublisher .post(ItemEventFactory.createCommandEvent(hueDevice.item.getName(), command, "hueemulation")); } // Generate the response. The response consists of a list with an entry each for all // submitted change requests. If for example "on" and "bri" was send, 2 entries in the response are // expected. Path contextPath = fullURI.subpath(2, fullURI.getNameCount() - 1); List<HueResponse> responses = new ArrayList<>(); successApplied.forEach((t, v) -> { responses .add(new HueResponse(new HueSuccessResponseStateChanged(contextPath.resolve(t).toString(), v))); }); errorApplied.forEach(v -> { responses.add(new HueResponse(new HueErrorMessage(HueResponse.NOT_AVAILABLE, contextPath.resolve(v).toString(), "Could not set"))); }); try (JsonWriter writer = new JsonWriter(out)) { gson.toJson(responses, new TypeToken<List<?>>() { }.getType(), writer); } return 200; }
From source file:org.openhab.io.neeo.internal.serialization.ChannelUIDSerializer.java
License:Open Source License
@Override public ChannelUID 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.isJsonNull()) { throw new JsonParseException("Not a valid ChannelUID: (null)"); }/*ww w . j a v a 2s . c om*/ try { return new ChannelUID(elm.getAsString()); } catch (IllegalArgumentException e) { throw new JsonParseException("Not a valid ChannelUID: " + elm.getAsString(), e); } }