@Test public void propagateGsonAttributes() { Gson gson = new GsonBuilder() .serializeNulls() .disableHtmlEscaping() .setPrettyPrinting() .create(); GsonOptions options = new GsonOptions(gson, true); JsonReader reader = new JsonReader(new StringReader("")); options.setReaderOptions(reader); check(reader.isLenient()); JsonWriter writer = new JsonWriter(new StringWriter()); options.setWriterOptions(writer); check(writer.isLenient()); check(!writer.isHtmlSafe()); check(writer.getSerializeNulls()); // checks pretty printing check(gson.toJson(Collections.singletonMap("k", "v"))).is("{\n \"k\": \"v\"\n}"); } }
/** * Returns the next value from the JSON stream as a parse tree. * * @throws JsonParseException if there is an IOException or if the specified * text is not valid JSON * @since 1.6 */ public JsonElement parse(JsonReader json) throws JsonIOException, JsonSyntaxException { boolean lenient = json.isLenient(); json.setLenient(true); try { return Streams.parse(json); } catch (StackOverflowError e) { throw new JsonParseException("Failed parsing JSON source: " + json + " to Json", e); } catch (OutOfMemoryError e) { throw new JsonParseException("Failed parsing JSON source: " + json + " to Json", e); } finally { json.setLenient(lenient); } } }
public <T> T fromJson(JsonReader reader, Type typeOfT) throws JsonIOException, JsonSyntaxException { boolean isEmpty = true; boolean oldLenient = reader.isLenient(); reader.setLenient(true); try {
public <T> T fromJson(JsonReader reader, Type typeOfT) throws JsonIOException, JsonSyntaxException { boolean isEmpty = true; boolean oldLenient = reader.isLenient(); reader.setLenient(true); <-- always true try { reader.peek(); isEmpty = false; TypeToken<T> typeToken = (TypeToken<T>) TypeToken.get(typeOfT); TypeAdapter<T> typeAdapter = getAdapter(typeToken); T object = typeAdapter.read(reader); return object; } catch (EOFException e) { ...
public static Object invokeServiceClass(JsonReader jsonReader, Object service, Method operation , Class[] paramClasses , int paramCount ) throws InvocationTargetException, IllegalAccessException, IOException { Object[] methodParam = new Object[paramCount]; Gson gson = new Gson(); String[] argNames = new String[paramCount]; if( ! jsonReader.isLenient()){ jsonReader.setLenient(true); } jsonReader.beginObject(); String messageName=jsonReader.nextName(); // get message name from input json stream jsonReader.beginArray(); int i = 0; for (Class paramType : paramClasses) { jsonReader.beginObject(); argNames[i] = jsonReader.nextName(); methodParam[i] = gson.fromJson(jsonReader, paramType); // gson handle all types well and return an object from it jsonReader.endObject(); i++; } jsonReader.endArray(); jsonReader.endObject(); return operation.invoke(service, methodParam); }
public static Object invokeServiceClass(JsonReader jsonReader, Object service, Method operation , Class[] paramClasses , int paramCount ) throws InvocationTargetException, IllegalAccessException, IOException { Object[] methodParam = new Object[paramCount]; Gson gson = new Gson(); String[] argNames = new String[paramCount]; if( ! jsonReader.isLenient()){ jsonReader.setLenient(true); } jsonReader.beginObject(); String messageName=jsonReader.nextName(); // get message name from input json stream jsonReader.beginArray(); int i = 0; for (Class paramType : paramClasses) { jsonReader.beginObject(); argNames[i] = jsonReader.nextName(); methodParam[i] = gson.fromJson(jsonReader, paramType); // gson handle all types well and return an object from it jsonReader.endObject(); i++; } jsonReader.endArray(); jsonReader.endObject(); return operation.invoke(service, methodParam); }
/** * Returns the next value from the JSON stream as a parse tree. * * @throws JsonParseException if there is an IOException or if the specified * text is not valid JSON * @since 1.6 */ public JsonElement parse(JsonReader json) throws JsonIOException, JsonSyntaxException { boolean lenient = json.isLenient(); json.setLenient(true); try { return Streams.parse(json); } catch (StackOverflowError e) { throw new JsonParseException("Failed parsing JSON source: " + json + " to Json", e); } catch (OutOfMemoryError e) { throw new JsonParseException("Failed parsing JSON source: " + json + " to Json", e); } finally { json.setLenient(lenient); } } }
/** * Returns the next value from the JSON stream as a parse tree. * * @throws JsonParseException if there is an IOException or if the specified * text is not valid JSON * @since 1.6 */ public JsonElement parse(JsonReader json) throws JsonIOException, JsonSyntaxException { boolean lenient = json.isLenient(); json.setLenient(true); try { return Streams.parse(json); } catch (StackOverflowError e) { throw new JsonParseException("Failed parsing JSON source: " + json + " to Json", e); } catch (OutOfMemoryError e) { throw new JsonParseException("Failed parsing JSON source: " + json + " to Json", e); } finally { json.setLenient(lenient); } } }
/** * Returns the next value from the JSON stream as a parse tree. * * @throws JsonParseException if there is an IOException or if the specified * text is not valid JSON * @since 1.6 */ public JsonElement parse(JsonReader json) throws JsonIOException, JsonSyntaxException { boolean lenient = json.isLenient(); json.setLenient(true); try { return Streams.parse(json); } catch (StackOverflowError e) { throw new JsonParseException("Failed parsing JSON source: " + json + " to Json", e); } catch (OutOfMemoryError e) { throw new JsonParseException("Failed parsing JSON source: " + json + " to Json", e); } finally { json.setLenient(lenient); } } }
/** * Returns the next value from the JSON stream as a parse tree. * * @throws JsonParseException if there is an IOException or if the specified * text is not valid JSON * @since 1.6 */ public JsonElement parse(JsonReader json) throws JsonIOException, JsonSyntaxException { boolean lenient = json.isLenient(); json.setLenient(true); try { return Streams.parse(json); } catch (StackOverflowError e) { throw new JsonParseException("Failed parsing JSON source: " + json + " to Json", e); } catch (OutOfMemoryError e) { throw new JsonParseException("Failed parsing JSON source: " + json + " to Json", e); } finally { json.setLenient(lenient); } } }
/** * Returns the next value from the JSON stream as a parse tree. * * @throws JsonParseException if there is an IOException or if the specified * text is not valid JSON * @since 1.6 */ public JsonElement parse(JsonReader json) throws JsonIOException, JsonSyntaxException { boolean lenient = json.isLenient(); json.setLenient(true); try { return Streams.parse(json); } catch (StackOverflowError e) { throw new JsonParseException("Failed parsing JSON source: " + json + " to Json", e); } catch (OutOfMemoryError e) { throw new JsonParseException("Failed parsing JSON source: " + json + " to Json", e); } finally { json.setLenient(lenient); } } }
/** * Reads the next JSON value from {@code reader} and convert it to an object * of type {@code typeOfT}. * Since Type is not parameterized by T, this method is type unsafe and should be used carefully * * @throws JsonIOException if there was a problem writing to the Reader * @throws JsonSyntaxException if json is not a valid representation for an object of type */ @SuppressWarnings("unchecked") public <T> T fromJson(JsonReader reader, Type typeOfT) throws JsonIOException, JsonSyntaxException { boolean oldLenient = reader.isLenient(); reader.setLenient(true); try { JsonElement root = Streams.parse(reader); return (T) fromJson(root, typeOfT); } finally { reader.setLenient(oldLenient); } }
/** * Returns the next value from the JSON stream as a parse tree. * * @throws JsonParseException if there is an IOException or if the specified * text is not valid JSON * @since 1.6 */ public JsonElement parse(JsonReader json) throws JsonIOException, JsonSyntaxException { boolean lenient = json.isLenient(); json.setLenient(true); try { return Streams.parse(json); } catch (StackOverflowError e) { throw new JsonParseException("Failed parsing JSON source: " + json + " to Json", e); } catch (OutOfMemoryError e) { throw new JsonParseException("Failed parsing JSON source: " + json + " to Json", e); } catch (JsonParseException e) { if (e.getCause() instanceof EOFException) { return JsonNull.INSTANCE; } throw e; } finally { json.setLenient(lenient); } } }
/** * Returns the next value from the JSON stream as a parse tree. * * @throws JsonParseException if there is an IOException or if the specified * text is not valid JSON * @since 1.6 */ public JsonElement parse(JsonReader json) throws JsonIOException, JsonSyntaxException { boolean lenient = json.isLenient(); json.setLenient(true); try { return Streams.parse(json); } catch (StackOverflowError e) { throw new JsonParseException("Failed parsing JSON source: " + json + " to Json", e); } catch (OutOfMemoryError e) { throw new JsonParseException("Failed parsing JSON source: " + json + " to Json", e); } catch (JsonParseException e) { if (e.getCause() instanceof EOFException) { return JsonNull.INSTANCE; } throw e; } finally { json.setLenient(lenient); } } }
public JsonParserStream parse(final JsonReader reader) { // code copied from gson's JsonParser and Stream classes final boolean lenient = reader.isLenient(); reader.setLenient(true); 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); } }
public JsonParserStream parse(final JsonReader reader) { // code copied from gson's JsonParser and Stream classes final boolean lenient = reader.isLenient(); reader.setLenient(true); 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); } }
public <T> T fromJson(JsonReader reader, Type typeOfT) throws JsonIOException, JsonSyntaxException { boolean isEmpty = true; boolean oldLenient = reader.isLenient(); reader.setLenient(true); try {
public <T> T fromJson(JsonReader reader, Type typeOfT) throws JsonIOException, JsonSyntaxException { boolean isEmpty = true; boolean oldLenient = reader.isLenient(); reader.setLenient(true); try {
public <T> T fromJson(JsonReader reader, Type typeOfT) throws JsonIOException, JsonSyntaxException { boolean isEmpty = true; boolean oldLenient = reader.isLenient(); reader.setLenient(true); try {
public <T> T fromJson(JsonReader reader, Type typeOfT) throws JsonIOException, JsonSyntaxException { boolean isEmpty = true; boolean oldLenient = reader.isLenient(); reader.setLenient(true); try {