List of usage examples for com.google.gson JsonSyntaxException JsonSyntaxException
public JsonSyntaxException(Throwable cause)
From source file:com.ibm.og.util.json.type.CaseInsensitiveEnumTypeAdapterFactory.java
License:Open Source License
@Override public <T> TypeAdapter<T> create(final Gson gson, final TypeToken<T> type) { @SuppressWarnings("unchecked") final Class<T> rawType = (Class<T>) type.getRawType(); if (!rawType.isEnum()) { return null; }//from w w w . j a v a2s . c o m return new TypeAdapter<T>() { @Override public void write(final JsonWriter out, final T value) throws IOException { out.value(value.toString().toLowerCase(Locale.US)); } @Override @SuppressWarnings("unchecked") public T read(final JsonReader in) throws IOException { final String s = in.nextString().toUpperCase(Locale.US); for (final Object enumEntry : rawType.getEnumConstants()) { if (enumEntry.toString().equals(s)) { return (T) enumEntry; } } throw new JsonSyntaxException(String.format("Could not parse into enum [%s]", s)); } }.nullSafe(); }
From source file:com.ouyangzn.github.json.DoubleAdapter.java
License:Apache License
@Override public Number read(JsonReader jsonReader) throws IOException { if (jsonReader.peek() == JsonToken.NULL) { jsonReader.nextNull();//from w ww .j a va 2 s.com return null; } try { String value = jsonReader.nextString(); if ("".equals(value)) { return null; } return Double.parseDouble(value); } catch (NumberFormatException e) { throw new JsonSyntaxException(e); } }
From source file:com.ouyangzn.github.json.IntegerAdapter.java
License:Apache License
@Override public Number read(JsonReader jsonReader) throws IOException { if (jsonReader.peek() == JsonToken.NULL) { jsonReader.nextNull();/*from ww w . j a va 2 s.c om*/ return null; } try { String value = jsonReader.nextString(); if ("".equals(value)) { return null; } return Integer.parseInt(value); } catch (NumberFormatException e) { throw new JsonSyntaxException(e); } }
From source file:com.ouyangzn.github.json.LongAdapter.java
License:Apache License
@Override public Number read(JsonReader jsonReader) throws IOException { if (jsonReader.peek() == JsonToken.NULL) { jsonReader.nextNull();/*w ww . j a va2s . c om*/ return null; } try { String value = jsonReader.nextString(); if ("".equals(value)) { return null; } return Long.parseLong(value); } catch (NumberFormatException e) { throw new JsonSyntaxException(e); } }
From source file:com.redhat.jenkins.nodesharing.transport.Entity.java
License:Open Source License
/** * Read entity from stream.//w w w . j ava2 s . c om * * @return The entity created. * @throws JsonIOException if there was a problem reading from the Reader. * @throws JsonSyntaxException if json is not a valid representation for an object of type. */ public static @Nonnull <T> T fromInputStream(@Nonnull InputStream inputStream, @Nonnull Class<T> type) throws JsonSyntaxException, JsonIOException { T out = GSON.fromJson(new InputStreamReader(inputStream, TRANSPORT_CHARSET), type); if (out == null) throw new JsonSyntaxException("There was nothing in the stream"); return out; }
From source file:com.redhat.thermostat.gateway.common.mongodb.response.LongTypeAdapter.java
License:Open Source License
@Override public Long read(JsonReader in) throws IOException { in.beginObject();//from w ww.j ava2 s. c om String name = in.nextName(); if (!name.equals(NUMBER_LONG_IDENTIFIER)) { throw new JsonSyntaxException("Unexpected name: " + name); } long returnValue = in.nextLong(); in.endObject(); return returnValue; }
From source file:com.scvngr.levelup.core.model.factory.json.GsonModelFactory.java
License:Apache License
/** * Parse a model from the JSON object passed, un-nesting from a root element if necessary. * * @param json the JSON representation of the model to parse. * @return a model instance.//w w w . j a v a 2 s . co m * @throws JsonParseException if there is a problem decoding the JSON structure. */ @NonNull public final T from(@NonNull final String json) throws JsonParseException { final JsonParser p = new JsonParser(); final JsonElement root = p.parse(json); if (!root.isJsonObject()) { throw new JsonSyntaxException(NullUtils.format("JSON data must be a JSON object. Type is '%s'.", root.getClass().getSimpleName())); } return from(NullUtils.nonNullContract(root.getAsJsonObject())); }
From source file:com.scvngr.levelup.core.model.factory.json.GsonModelFactory.java
License:Apache License
/** * Parse a list of models from the JSON array passed. * * @param jsonArray the {@link JsonArray} containing a list of models. * @return a {@link List} of model instances. * @throws JsonParseException if there is a problem decoding the JSON structure. *//*from www. j ava 2 s.c o m*/ @NonNull public final List<T> fromList(@NonNull final JsonArray jsonArray) throws JsonParseException { final int count = jsonArray.size(); final List<T> objectList = new ArrayList<T>(count); for (final JsonElement object : jsonArray) { if (object.isJsonObject()) { objectList.add(from(NullUtils.nonNullContract(object.getAsJsonObject()))); } else { throw new JsonSyntaxException(NullUtils.format("Element in array was a '%s', not an object.", object.getClass().getSimpleName())); } } return objectList; }
From source file:com.scvngr.levelup.core.model.factory.json.GsonModelFactory.java
License:Apache License
/** * Parse a list of models from the JSON array passed. * * @param json the JSON representation of the list of models to parse. * @return a {@link List} of model instances. * @throws JsonParseException if there is a problem decoding the JSON structure. *///from w ww.j ava2 s . c om @NonNull public final List<T> fromList(@NonNull final String json) throws JsonParseException { final JsonElement root = new JsonParser().parse(json); if (!root.isJsonArray()) { throw new JsonSyntaxException(NullUtils.format("JSON data must be a JSON array. Type is '%s'.", root.getClass().getSimpleName())); } return fromList(NullUtils.nonNullContract(root.getAsJsonArray())); }
From source file:cosmos.records.JsonRecords.java
License:Apache License
public static List<MapRecord> parseAsMap(String json, DocIdGenerator generator) throws JsonSyntaxException { Preconditions.checkNotNull(json);//from w w w . j a v a 2s . c o m Preconditions.checkNotNull(generator); JsonParser parser = new JsonParser(); JsonElement topLevelElement = parser.parse(json); if (topLevelElement.isJsonNull()) { return Collections.emptyList(); } else if (!topLevelElement.isJsonArray()) { throw new JsonSyntaxException("Expected a list of dictionaries"); } final LinkedList<MapRecord> records = Lists.newLinkedList(); // Walk the top level list JsonArray list = topLevelElement.getAsJsonArray(); for (JsonElement e : list) { // Make sure that it's a Json Object if (!e.isJsonObject()) { throw new JsonSyntaxException("Expected a Json Object"); } // Parse each "Object" (map) JsonObject map = e.getAsJsonObject(); records.add(asMapRecord(map, generator)); } return records; }