List of usage examples for com.google.gson JsonSyntaxException JsonSyntaxException
public JsonSyntaxException(Throwable cause)
From source file:org.opendaylight.controller.sal.rest.gson.JsonParser.java
License:Open Source License
public JsonElement parse(JsonReader reader) throws JsonIOException, JsonSyntaxException { // code copied from gson's JsonParser and Stream classes boolean lenient = reader.isLenient(); reader.setLenient(true);/* w w w . j a v a 2 s. co m*/ boolean isEmpty = true; try { reader.peek(); isEmpty = false; return read(reader); } catch (EOFException e) { if (isEmpty) { return JsonNull.INSTANCE; } // The stream ended prematurely so it is likely a syntax error. throw new JsonSyntaxException(e); } catch (MalformedJsonException e) { throw new JsonSyntaxException(e); } catch (IOException e) { throw new JsonIOException(e); } catch (NumberFormatException e) { throw new JsonSyntaxException(e); } catch (StackOverflowError | OutOfMemoryError e) { throw new JsonParseException("Failed parsing JSON source: " + reader + " to Json", e); } finally { reader.setLenient(lenient); } }
From source file:org.opendaylight.controller.sal.rest.gson.JsonParser.java
License:Open Source License
public JsonElement read(JsonReader in) throws IOException { switch (in.peek()) { case STRING:// w w w . j a v a 2 s .c om return new JsonPrimitive(in.nextString()); case NUMBER: String number = in.nextString(); return new JsonPrimitive(new LazilyParsedNumber(number)); case BOOLEAN: return new JsonPrimitive(in.nextBoolean()); case NULL: in.nextNull(); return JsonNull.INSTANCE; case BEGIN_ARRAY: JsonArray array = new JsonArray(); in.beginArray(); while (in.hasNext()) { array.add(read(in)); } in.endArray(); return array; case BEGIN_OBJECT: JsonObject object = new JsonObject(); in.beginObject(); while (in.hasNext()) { final String childName = in.nextName(); if (object.has(childName)) { throw new JsonSyntaxException("Duplicate name " + childName + " in JSON input."); } object.add(childName, read(in)); } in.endObject(); return object; case END_DOCUMENT: case NAME: case END_OBJECT: case END_ARRAY: default: throw new IllegalArgumentException(); } }
From source file:org.opendaylight.yangtools.yang.data.codec.gson.JsonParserStream.java
License:Open Source License
public JsonParserStream parse(final JsonReader reader) { // code copied from gson's JsonParser and Stream classes final boolean lenient = reader.isLenient(); reader.setLenient(true);/*from w w w .java 2 s. c om*/ boolean isEmpty = true; try { reader.peek(); isEmpty = false; final CompositeNodeDataWithSchema compositeNodeDataWithSchema = new CompositeNodeDataWithSchema( parentNode); read(reader, compositeNodeDataWithSchema); compositeNodeDataWithSchema.write(writer); return this; } catch (final EOFException e) { if (isEmpty) { return this; } // The stream ended prematurely so it is likely a syntax error. throw new JsonSyntaxException(e); } catch (final MalformedJsonException | NumberFormatException e) { throw new JsonSyntaxException(e); } catch (final IOException e) { throw new JsonIOException(e); } catch (StackOverflowError | OutOfMemoryError e) { throw new JsonParseException("Failed parsing JSON source: " + reader + " to Json", e); } finally { reader.setLenient(lenient); } }
From source file:org.opendaylight.yangtools.yang.data.codec.gson.JsonParserStream.java
License:Open Source License
public void read(final JsonReader in, AbstractNodeDataWithSchema parent) throws IOException { switch (in.peek()) { case STRING:/*from w w w . j ava 2s. c o m*/ case NUMBER: setValue(parent, in.nextString()); break; case BOOLEAN: setValue(parent, Boolean.toString(in.nextBoolean())); break; case NULL: in.nextNull(); setValue(parent, null); break; case BEGIN_ARRAY: in.beginArray(); while (in.hasNext()) { if (parent instanceof LeafNodeDataWithSchema) { read(in, parent); } else { final AbstractNodeDataWithSchema newChild = newArrayEntry(parent); read(in, newChild); } } in.endArray(); return; case BEGIN_OBJECT: final Set<String> namesakes = new HashSet<>(); in.beginObject(); /* * This allows parsing of incorrectly /as showcased/ * in testconf nesting of list items - eg. * lists with one value are sometimes serialized * without wrapping array. * */ if (isArray(parent)) { parent = newArrayEntry(parent); } while (in.hasNext()) { final String jsonElementName = in.nextName(); DataSchemaNode parentSchema = parent.getSchema(); if (parentSchema instanceof YangModeledAnyXmlSchemaNode) { parentSchema = ((YangModeledAnyXmlSchemaNode) parentSchema).getSchemaOfAnyXmlData(); } final NamespaceAndName namespaceAndName = resolveNamespace(jsonElementName, parentSchema); final String localName = namespaceAndName.getName(); addNamespace(namespaceAndName.getUri()); if (namesakes.contains(jsonElementName)) { throw new JsonSyntaxException("Duplicate name " + jsonElementName + " in JSON input."); } namesakes.add(jsonElementName); final Deque<DataSchemaNode> childDataSchemaNodes = ParserStreamUtils .findSchemaNodeByNameAndNamespace(parentSchema, localName, getCurrentNamespace()); if (childDataSchemaNodes.isEmpty()) { throw new IllegalStateException("Schema for node with name " + localName + " and namespace " + getCurrentNamespace() + " doesn't exist."); } final AbstractNodeDataWithSchema newChild = ((CompositeNodeDataWithSchema) parent) .addChild(childDataSchemaNodes); if (newChild instanceof AnyXmlNodeDataWithSchema) { readAnyXmlValue(in, (AnyXmlNodeDataWithSchema) newChild, jsonElementName); } else { read(in, newChild); } removeNamespace(); } in.endObject(); return; case END_DOCUMENT: case NAME: case END_OBJECT: case END_ARRAY: break; } }
From source file:org.sprintapi.hyperdata.gson.HyperDataTypeAdapter.java
License:Apache License
@SuppressWarnings({ "unchecked", "rawtypes" }) @Override// www . jav a 2 s. c o m public Object read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { in.nextNull(); return null; } Object instance = constructor.construct(); BoundField metadataField = null; if (metadataAccess != null) { metadataField = boundFields.get(metadataAccess.fieldName); } Object meta = null; Map<String, BoundField> metaBoundFields = null; try { in.beginObject(); while (in.hasNext()) { String name = in.nextName(); if ((name != null) && (metadataField != null) && (name.startsWith(Constants.META_CHAR))) { if (meta == null) { meta = constructorConstructor.get(metadataField.type).construct(); if (!Map.class.isAssignableFrom(meta.getClass())) { metaBoundFields = reflectiveFactory.getBoundFields(gson, metadataField.type, metadataField.type.getRawType()); } } if (metaBoundFields != null) { BoundField field = metaBoundFields.get(name.substring(1)); if (field == null || !field.deserialized) { in.skipValue(); } else { field.read(in, meta); } } else { TypeAdapter<Object> ta = gson.getAdapter(Object.class); Object value = ta.read(in); ((Map) meta).put(name.substring(1), value); } } else if ((name != null) && (!name.equals(metadataAccess.fieldName))) { BoundField field = boundFields.get(name); if (field == null || !field.deserialized) { in.skipValue(); } else { field.read(in, instance); } } } if (metadataAccess.setter != null) { metadataAccess.setter.invoke(instance, meta); //TODO } } catch (IllegalStateException e) { throw new JsonSyntaxException(e); } catch (IllegalAccessException e) { throw new AssertionError(e); } catch (IllegalArgumentException e) { throw new AssertionError(e); } catch (InvocationTargetException e) { throw new AssertionError(e); } in.endObject(); return instance; }
From source file:org.structr.core.rest.JsonInputGSONAdapter.java
License:Open Source License
@Override public IJsonInput deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException { IJsonInput jsonInput = null;/*from w w w . ja va 2 s . c o m*/ JsonInput wrapper = null; if (json.isJsonObject()) { jsonInput = new JsonSingleInput(); wrapper = deserialize(json, context); jsonInput.add(wrapper); } else if (json.isJsonArray()) { jsonInput = new JsonSingleInput(); JsonArray array = json.getAsJsonArray(); for (final JsonElement elem : array) { wrapper = deserialize(elem, context); jsonInput.add(wrapper); } } else { // when we arrive here, the input element was // not one of the expected types => error throw new JsonSyntaxException("Invalid JSON, expecting object or array"); } return jsonInput; }
From source file:org.structr.core.rest.JsonInputGSONAdapter.java
License:Open Source License
public static JsonInput deserialize(final JsonElement json, final JsonDeserializationContext context) throws JsonParseException { final JsonInput wrapper = new JsonInput(); if (json.isJsonObject()) { final JsonObject obj = json.getAsJsonObject(); for (final Entry<String, JsonElement> entry : obj.entrySet()) { final String key = entry.getKey(); final JsonElement elem = entry.getValue(); if (elem.isJsonNull()) { wrapper.add(key, null);/* www.j av a 2s . co m*/ } else if (elem.isJsonObject()) { wrapper.add(key, deserialize(elem, context)); } else if (elem.isJsonArray()) { final JsonArray array = elem.getAsJsonArray(); final List list = new LinkedList(); for (final JsonElement element : array) { if (element.isJsonPrimitive()) { list.add(fromPrimitive((element.getAsJsonPrimitive()))); } else if (element.isJsonObject()) { // create map of values list.add(deserialize(element, context)); } } wrapper.add(key, list); } else if (elem.isJsonPrimitive()) { // wrapper.add(key, elem.getAsString()); wrapper.add(key, fromPrimitive(elem.getAsJsonPrimitive())); } } } else if (json.isJsonArray()) { final JsonArray array = json.getAsJsonArray(); for (final JsonElement elem : array) { if (elem.isJsonPrimitive()) { wrapper.add(elem.toString(), fromPrimitive(elem.getAsJsonPrimitive())); } else if (elem.isJsonObject()) { wrapper.add(elem.toString(), deserialize(elem, context)); } else if (elem.isJsonArray()) { wrapper.add(elem.toString(), deserialize(elem, context)); } } } else { // when we arrive here, the input element was // not one of the expected types => error throw new JsonSyntaxException("Invalid JSON, expecting object or array"); } return wrapper; }
From source file:org.syphr.lametrictime.api.common.impl.typeadapters.imported.RuntimeTypeAdapterFactory.java
License:Apache License
/** * Takes a reader in any state and returns the next value as a JsonElement. *//*from ww w. ja v a 2 s . c o m*/ private static JsonElement parse(JsonReader reader) throws JsonParseException { boolean isEmpty = true; try { reader.peek(); isEmpty = false; return RuntimeTypeAdapterFactory.JSON_ELEMENT.read(reader); } catch (EOFException e) { /* * For compatibility with JSON 1.5 and earlier, we return a JsonNull for * empty documents instead of throwing. */ if (isEmpty) { return JsonNull.INSTANCE; } // The stream ended prematurely so it is likely a syntax error. throw new JsonSyntaxException(e); } catch (MalformedJsonException e) { throw new JsonSyntaxException(e); } catch (IOException e) { throw new JsonIOException(e); } catch (NumberFormatException e) { throw new JsonSyntaxException(e); } }
From source file:org.thingsboard.server.common.transport.adaptor.JsonConverter.java
License:Apache License
public static PostTelemetryMsg convertToTelemetryProto(JsonElement jsonObject) throws JsonSyntaxException { long systemTs = System.currentTimeMillis(); PostTelemetryMsg.Builder builder = PostTelemetryMsg.newBuilder(); if (jsonObject.isJsonObject()) { parseObject(builder, systemTs, jsonObject); } else if (jsonObject.isJsonArray()) { jsonObject.getAsJsonArray().forEach(je -> { if (je.isJsonObject()) { parseObject(builder, systemTs, je.getAsJsonObject()); } else { throw new JsonSyntaxException(CAN_T_PARSE_VALUE + je); }/*from w ww .jav a2 s . co m*/ }); } else { throw new JsonSyntaxException(CAN_T_PARSE_VALUE + jsonObject); } return builder.build(); }
From source file:org.thingsboard.server.common.transport.adaptor.JsonConverter.java
License:Apache License
public static PostAttributeMsg convertToAttributesProto(JsonElement jsonObject) throws JsonSyntaxException { if (jsonObject.isJsonObject()) { PostAttributeMsg.Builder result = PostAttributeMsg.newBuilder(); List<KeyValueProto> keyValueList = parseProtoValues(jsonObject.getAsJsonObject()); result.addAllKv(keyValueList);// w w w.ja va 2s. com return result.build(); } else { throw new JsonSyntaxException(CAN_T_PARSE_VALUE + jsonObject); } }