Java Code Examples for com.google.gson.stream.JsonReader#setLenient()

The following examples show how to use com.google.gson.stream.JsonReader#setLenient() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JSON.java    From ariADDna with Apache License 2.0 6 votes vote down vote up
/**
 * Deserialize the given JSON string to Java object.
 *
 * @param <T> Type
 * @param body The JSON string
 * @param returnType The type to deserialize into
 * @return The deserialized Java object
 */
@SuppressWarnings("unchecked")
public <T> T deserialize(String body, Type returnType) {
    try {
        if (apiClient.isLenientOnJson()) {
            JsonReader jsonReader = new JsonReader(new StringReader(body));
            // see https://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/stream/JsonReader.html#setLenient(boolean)
            jsonReader.setLenient(true);
            return gson.fromJson(jsonReader, returnType);
        } else {
            return gson.fromJson(body, returnType);
        }
    } catch (JsonParseException e) {
        // Fallback processing when failed to parse JSON form response body:
        //   return the response body string directly for the String return type;
        //   parse response body into date or datetime for the Date return type.
        if (returnType.equals(String.class)) {
            return (T) body;
        } else if (returnType.equals(Date.class)) {
            return (T) apiClient.parseDateOrDatetime(body);
        } else {
            throw (e);
        }
    }
}
 
Example 2
Source File: JsonHandler.java    From dhis2-android-datacapture with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static JsonArray buildJsonArray(String json) throws ParsingException {
    if (json == null) {
        throw new ParsingException("Cannot parse empty json");
    }

    JsonReader reader = new JsonReader(new StringReader(json));
    reader.setLenient(true);

    try {
        JsonElement jRawSource = new JsonParser().parse(reader);

        if (jRawSource != null && jRawSource.isJsonArray()) {
            return jRawSource.getAsJsonArray();
        } else {
            throw new ParsingException("The incoming Json is bad/malicious");
        }
    } catch (JsonParseException e) {
        throw new ParsingException("The incoming Json is bad/malicious");
    }
}
 
Example 3
Source File: JsonParserTest.java    From gson with Apache License 2.0 6 votes vote down vote up
public void testReadWriteTwoObjects() throws Exception {
  Gson gson = new Gson();
  CharArrayWriter writer = new CharArrayWriter();
  BagOfPrimitives expectedOne = new BagOfPrimitives(1, 1, true, "one");
  writer.write(gson.toJson(expectedOne).toCharArray());
  BagOfPrimitives expectedTwo = new BagOfPrimitives(2, 2, false, "two");
  writer.write(gson.toJson(expectedTwo).toCharArray());
  CharArrayReader reader = new CharArrayReader(writer.toCharArray());

  JsonReader parser = new JsonReader(reader);
  parser.setLenient(true);
  JsonElement element1 = Streams.parse(parser);
  JsonElement element2 = Streams.parse(parser);
  BagOfPrimitives actualOne = gson.fromJson(element1, BagOfPrimitives.class);
  assertEquals("one", actualOne.stringValue);
  BagOfPrimitives actualTwo = gson.fromJson(element2, BagOfPrimitives.class);
  assertEquals("two", actualTwo.stringValue);
}
 
Example 4
Source File: JSON.java    From huaweicloud-cs-sdk with Apache License 2.0 6 votes vote down vote up
/**
 * Deserialize the given JSON string to Java object.
 *
 * @param <T>        Type
 * @param body       The JSON string
 * @param returnType The type to deserialize into
 * @return The deserialized Java object
 */
@SuppressWarnings("unchecked")
public <T> T deserialize(String body, Type returnType) {
    try {
        if (isLenientOnJson) {
            JsonReader jsonReader = new JsonReader(new StringReader(body));
            // see https://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/stream/JsonReader.html#setLenient(boolean)
            jsonReader.setLenient(true);
            return gson.fromJson(jsonReader, returnType);
        } else {
            return gson.fromJson(body, returnType);
        }
    } catch (JsonParseException e) {
        // Fallback processing when failed to parse JSON form response body:
        // return the response body string directly for the String return type;
        if (returnType.equals(String.class))
            return (T) body;
        else throw (e);
    }
}
 
Example 5
Source File: JSON.java    From nifi-swagger-client with Apache License 2.0 6 votes vote down vote up
/**
 * Deserialize the given JSON string to Java object.
 *
 * @param <T>        Type
 * @param body       The JSON string
 * @param returnType The type to deserialize into
 * @return The deserialized Java object
 */
@SuppressWarnings("unchecked")
public <T> T deserialize(String body, Type returnType) {
    try {
        if (isLenientOnJson) {
            JsonReader jsonReader = new JsonReader(new StringReader(body));
            // see https://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/stream/JsonReader.html#setLenient(boolean)
            jsonReader.setLenient(true);
            return gson.fromJson(jsonReader, returnType);
        } else {
            return gson.fromJson(body, returnType);
        }
    } catch (JsonParseException e) {
        // Fallback processing when failed to parse JSON form response body:
        // return the response body string directly for the String return type;
        if (returnType.equals(String.class))
            return (T) body;
        else throw (e);
    }
}
 
Example 6
Source File: JSON.java    From director-sdk with Apache License 2.0 6 votes vote down vote up
/**
 * Deserialize the given JSON string to Java object.
 *
 * @param <T>        Type
 * @param body       The JSON string
 * @param returnType The type to deserialize into
 * @return The deserialized Java object
 */
@SuppressWarnings("unchecked")
public <T> T deserialize(String body, Type returnType) {
    try {
        if (isLenientOnJson) {
            JsonReader jsonReader = new JsonReader(new StringReader(body));
            // see https://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/stream/JsonReader.html#setLenient(boolean)
            jsonReader.setLenient(true);
            return gson.fromJson(jsonReader, returnType);
        } else {
            return gson.fromJson(body, returnType);
        }
    } catch (JsonParseException e) {
        // Fallback processing when failed to parse JSON form response body:
        // return the response body string directly for the String return type;
        if (returnType.equals(String.class))
            return (T) body;
        else throw e;
    }
}
 
Example 7
Source File: JSON.java    From java with Apache License 2.0 6 votes vote down vote up
/**
 * Deserialize the given JSON string to Java object.
 *
 * @param <T>        Type
 * @param body       The JSON string
 * @param returnType The type to deserialize into
 * @return The deserialized Java object
 */
@SuppressWarnings("unchecked")
public <T> T deserialize(String body, Type returnType) {
    try {
        if (isLenientOnJson) {
            JsonReader jsonReader = new JsonReader(new StringReader(body));
            // see https://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/stream/JsonReader.html#setLenient(boolean)
            jsonReader.setLenient(true);
            return gson.fromJson(jsonReader, returnType);
        } else {
            return gson.fromJson(body, returnType);
        }
    } catch (JsonParseException e) {
        // Fallback processing when failed to parse JSON form response body:
        // return the response body string directly for the String return type;
        if (returnType.equals(String.class)) {
            return (T) body;
        } else {
            throw (e);
        }
    }
}
 
Example 8
Source File: JsonIngestFlattener.java    From datawave with Apache License 2.0 6 votes vote down vote up
private void run() throws IOException {
    
    JsonReader reader = new JsonReader(new FileReader(jsonInputFile));
    reader.setLenient(true);
    setupIterator(reader);
    
    try {
        while (true) {
            
            if (!jsonIterator.hasNext()) {
                
                if (reader.peek() == JsonToken.END_DOCUMENT) {
                    // If we're here, then we're done. No more objects left in the file
                    return;
                }
                setupIterator(reader);
            }
            
            if (jsonIterator.hasNext()) {
                flattenAndPrint(jsonIterator.next().getAsJsonObject());
            }
        }
    } finally {
        reader.close();
    }
}
 
Example 9
Source File: JSON.java    From swagger-aem with Apache License 2.0 6 votes vote down vote up
/**
 * Deserialize the given JSON string to Java object.
 *
 * @param <T>        Type
 * @param body       The JSON string
 * @param returnType The type to deserialize into
 * @return The deserialized Java object
 */
@SuppressWarnings("unchecked")
public <T> T deserialize(String body, Type returnType) {
    try {
        if (isLenientOnJson) {
            JsonReader jsonReader = new JsonReader(new StringReader(body));
            // see https://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/stream/JsonReader.html#setLenient(boolean)
            jsonReader.setLenient(true);
            return gson.fromJson(jsonReader, returnType);
        } else {
            return gson.fromJson(body, returnType);
        }
    } catch (JsonParseException e) {
        // Fallback processing when failed to parse JSON form response body:
        // return the response body string directly for the String return type;
        if (returnType.equals(String.class))
            return (T) body;
        else throw (e);
    }
}
 
Example 10
Source File: JSON.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
/**
 * Deserialize the given JSON string to Java object.
 *
 * @param <T>        Type
 * @param body       The JSON string
 * @param returnType The type to deserialize into
 * @return The deserialized Java object
 */
@SuppressWarnings("unchecked")
public <T> T deserialize(String body, Type returnType) {
    try {
        if (isLenientOnJson) {
            JsonReader jsonReader = new JsonReader(new StringReader(body));
            // see https://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/stream/JsonReader.html#setLenient(boolean)
            jsonReader.setLenient(true);
            return gson.fromJson(jsonReader, returnType);
        } else {
            return gson.fromJson(body, returnType);
        }
    } catch (JsonParseException e) {
        // Fallback processing when failed to parse JSON form response body:
        // return the response body string directly for the String return type;
        if (returnType.equals(String.class)) {
            return (T) body;
        } else {
            throw (e);
        }
    }
}
 
Example 11
Source File: JSON.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
/**
 * Deserialize the given JSON string to Java object.
 *
 * @param <T>        Type
 * @param body       The JSON string
 * @param returnType The type to deserialize into
 * @return The deserialized Java object
 */
@SuppressWarnings("unchecked")
public <T> T deserialize(String body, Type returnType) {
    try {
        if (isLenientOnJson) {
            JsonReader jsonReader = new JsonReader(new StringReader(body));
            // see https://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/stream/JsonReader.html#setLenient(boolean)
            jsonReader.setLenient(true);
            return gson.fromJson(jsonReader, returnType);
        } else {
            return gson.fromJson(body, returnType);
        }
    } catch (JsonParseException e) {
        // Fallback processing when failed to parse JSON form response body:
        // return the response body string directly for the String return type;
        if (returnType.equals(String.class)) {
            return (T) body;
        } else {
            throw (e);
        }
    }
}
 
Example 12
Source File: JSON.java    From openapi-generator with Apache License 2.0 6 votes vote down vote up
/**
 * Deserialize the given JSON string to Java object.
 *
 * @param <T>        Type
 * @param body       The JSON string
 * @param returnType The type to deserialize into
 * @return The deserialized Java object
 */
@SuppressWarnings("unchecked")
public <T> T deserialize(String body, Type returnType) {
    try {
        if (isLenientOnJson) {
            JsonReader jsonReader = new JsonReader(new StringReader(body));
            // see https://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/stream/JsonReader.html#setLenient(boolean)
            jsonReader.setLenient(true);
            return gson.fromJson(jsonReader, returnType);
        } else {
            return gson.fromJson(body, returnType);
        }
    } catch (JsonParseException e) {
        // Fallback processing when failed to parse JSON form response body:
        // return the response body string directly for the String return type;
        if (returnType.equals(String.class)) {
            return (T) body;
        } else {
            throw (e);
        }
    }
}
 
Example 13
Source File: GsonUtils.java    From DevUtils with Apache License 2.0 5 votes vote down vote up
/**
 * JSON String 缩进处理
 * @param json JSON String
 * @param gson {@link Gson}
 * @return JSON String
 */
public static String toJsonIndent(final String json, final Gson gson) {
    if (gson != null) {
        try {
            JsonReader reader = new JsonReader(new StringReader(json));
            reader.setLenient(true);
            JsonParser jsonParser = new JsonParser();
            JsonElement jsonElement = jsonParser.parse(reader);
            return gson.toJson(jsonElement);
        } catch (Exception e) {
            JCLogUtils.eTag(TAG, e, "toJsonIndent");
        }
    }
    return null;
}
 
Example 14
Source File: MixedStreamTest.java    From gson with Apache License 2.0 5 votes vote down vote up
public void testReaderDoesNotMutateState() throws IOException {
  Gson gson = new Gson();
  JsonReader jsonReader = new JsonReader(new StringReader(CARS_JSON));
  jsonReader.beginArray();

  jsonReader.setLenient(false);
  gson.fromJson(jsonReader, Car.class);
  assertFalse(jsonReader.isLenient());

  jsonReader.setLenient(true);
  gson.fromJson(jsonReader, Car.class);
  assertTrue(jsonReader.isLenient());
}
 
Example 15
Source File: Request.java    From cosmic with Apache License 2.0 5 votes vote down vote up
public Command[] getCommands() {
    if (_cmds == null) {
        try {
            final StringReader reader = new StringReader(_content);
            final JsonReader jsonReader = new JsonReader(reader);
            jsonReader.setLenient(true);
            _cmds = s_gson.fromJson(jsonReader, Command[].class);
        } catch (final RuntimeException | NoClassDefFoundError e) {
            s_logger.error("Caught problem with " + _content, e);
            throw e;
        }
    }
    return _cmds;
}
 
Example 16
Source File: JsonRecordReader.java    From datawave with Apache License 2.0 4 votes vote down vote up
protected void setupReader(InputStream is) {
    countingInputStream = new CountingInputStream(is);
    reader = new JsonReader(new InputStreamReader(countingInputStream));
    reader.setLenient(true);
    setupIterator(reader);
}
 
Example 17
Source File: GsonParser.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
GsonParser(GsonFactory factory, JsonReader reader) {
  this.factory = factory;
  this.reader = reader;
  // lenient to allow top-level values of any type
  reader.setLenient(true);
}
 
Example 18
Source File: Extractor.java    From ParsingPlayer with GNU Lesser General Public License v2.1 4 votes vote down vote up
protected JsonObject parseResponse(String response) {
    JsonReader reader = new JsonReader(new StringReader(response));
    reader.setLenient(true);
    JsonParser parser = new JsonParser();
    return parser.parse(reader).getAsJsonObject();
}
 
Example 19
Source File: TypeAdapter.java    From letv with Apache License 2.0 4 votes vote down vote up
final T fromJson(Reader in) throws IOException {
    JsonReader reader = new JsonReader(in);
    reader.setLenient(true);
    return read(reader);
}
 
Example 20
Source File: JsonStreamParser.java    From MiBandDecompiled with Apache License 2.0 4 votes vote down vote up
public JsonStreamParser(Reader reader)
{
    a = new JsonReader(reader);
    a.setLenient(true);
    b = new Object();
}