Example usage for com.google.gson.stream JsonReader setLenient

List of usage examples for com.google.gson.stream JsonReader setLenient

Introduction

In this page you can find the example usage for com.google.gson.stream JsonReader setLenient.

Prototype

public final void setLenient(boolean lenient) 

Source Link

Document

Configure this parser to be liberal in what it accepts.

Usage

From source file:com.magnet.android.mms.request.JsonUtils.java

License:Open Source License

private static boolean isJson(Reader reader) {
    boolean result = false;
    try {/*from  w  ww  .  j a v a 2  s.c om*/
        JsonReader jr = new JsonReader(reader);
        jr.setLenient(true);
        JsonToken token = jr.peek();
        result = token.equals(JsonToken.BEGIN_OBJECT) || token.equals(JsonToken.BEGIN_ARRAY);
    } catch (Exception e) {
        Log.w(LOG_TAG, "JsonReader exception:" + e.getMessage());
    }
    return result;
}

From source file:com.meltmedia.cadmium.core.history.HistoryManager.java

License:Apache License

private void readHistoryFile() throws Exception {
    if (contentRoot != null) {
        String path = FileSystemManager.getFileIfCanRead(contentRoot, HISTORY_FILE_NAME);

        if (path != null) {
            Gson gson = new Gson();
            JsonReader reader = null;
            try {
                reader = new JsonReader(new FileReader(path));
                reader.setLenient(true);
                List<HistoryEntry> entries = gson.fromJson(reader, new TypeToken<List<HistoryEntry>>() {
                }.getType());//from   w w  w . j a  v a2  s  .  co m

                if (entries != null) {
                    history.addAll(entries);
                }
            } finally {
                log.info("Read in {} history entries", history.size());
                if (reader != null) {
                    reader.close();
                }
            }
        }
    }
}

From source file:com.miki.webapp.webservicerestful.MikiWsJsonTools.java

public List<Map<String, String>> lectureJson(List<String> listeAttribut, final JsonReader reader)
        throws IOException {
    List<Map<String, String>> resultat2 = new ArrayList<>();
    reader.setLenient(true);
    reader.beginArray();//from  w  w  w.  jav a 2  s .  com
    while (reader.hasNext()) {
        resultat2.add(lecture(listeAttribut, reader));
    }
    reader.endArray();
    return resultat2;
}

From source file:com.oneops.inductor.Listener.java

License:Apache License

private CmsWorkOrderSimpleBase getWorkOrderOf(String msgText, Class c) {
    CmsWorkOrderSimpleBase wo;// w  w w  .j a v a 2 s  . c  o m
    JsonReader reader = new JsonReader(new StringReader(msgText));
    reader.setLenient(true);
    wo = gson.fromJson(reader, c);
    return wo;
}

From source file:com.oneops.inductor.WorkOrderExecutor.java

License:Apache License

/**
 * getRepoList: gets list of repos from a json string
 *
 * @param jsonReposArray String/*w ww  .  j  a  v a2  s .  c o  m*/
 */
private ArrayList<String> getRepoList(String jsonReposArray) {
    JsonReader reader = new JsonReader(new StringReader(jsonReposArray));
    reader.setLenient(true);
    ArrayList<String> repos = gson.fromJson(reader, ArrayList.class);
    if (repos == null) {
        repos = new ArrayList<>();
    }
    return repos;
}

From source file:com.pets.core.petstore.data.store.JSONSerializer.java

License:Apache License

/**
 * Deserialize the given JSON string to Java object.
 *
 * @param <T> Type/*from   ww  w.j a va2s  . co m*/
 * @param body The JSON string
 * @param returnType The type to deserialize inot
 * @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 from response body:
        // return the raw body
        return (T) body;
    }
}

From source file:com.ptapp.sync.PTAppDataHandler.java

License:Open Source License

/**
 * Processes a conference data body and calls the appropriate data type handlers
 * to process each of the objects represented therein.
 *
 * @param dataBody The body of data to process
 * @throws java.io.IOException If there is an error parsing the data.
 *///from ww w  . jav a2s . co  m
private void processDataBody(String dataBody) throws IOException {
    JsonReader reader = new JsonReader(new StringReader(dataBody));
    JsonParser parser = new JsonParser();
    try {
        reader.setLenient(true); // To err is human

        // the whole file is a single JSON object
        reader.beginObject();

        while (reader.hasNext()) {
            // the key is "educators", "courses", "students", "events" etc.
            String key = reader.nextName();
            if (mHandlerForKey.containsKey(key)) {
                // pass the value to the corresponding handler
                mHandlerForKey.get(key).process(parser.parse(reader));
            } else {
                LOGW(TAG, "Skipping unknown key in ptapp data json: " + key);
                reader.skipValue();
            }
        }
        reader.endObject();
    } finally {
        reader.close();
    }
}

From source file:com.razza.apps.iosched.sync.ConferenceDataHandler.java

License:Open Source License

/**
 * Processes a conference data body and calls the appropriate data type handlers
 * to process each of the objects represented therein.
 *
 * @param dataBody The body of data to process
 * @throws IOException If there is an error parsing the data.
 *//*from  ww w.j  ava  2s  .com*/
private void processDataBody(String dataBody) throws IOException {
    JsonReader reader = new JsonReader(new StringReader(dataBody));
    JsonParser parser = new JsonParser();
    try {
        reader.setLenient(true); // To err is human

        // the whole file is a single JSON object
        reader.beginObject();

        while (reader.hasNext()) {
            // the key is "rooms", "speakers", "tracks", etc.
            String key = reader.nextName();
            if (mHandlerForKey.containsKey(key)) {
                // pass the value to the corresponding handler
                mHandlerForKey.get(key).process(parser.parse(reader));
            } else {
                LogUtils.LOGW(TAG, "Skipping unknown key in conference data json: " + key);
                reader.skipValue();
            }
        }
        reader.endObject();
    } finally {
        reader.close();
    }
}

From source file:com.rw.legion.input.JsonRecordReader.java

License:Apache License

/**
 * Takes a line in JSON format, parses it, and builds a
 * <code>LegionRecord</code> with JSON paths mapped to the data found at
 * that path./*from w  w w.  ja  v  a 2  s.com*/
 * 
 * @return A legion record containing JSON paths mapped to the data found
 *         at that path.
 */
protected LegionRecord makeRecord() {
    String lineString = currentLine.toString();

    record = new LegionRecord();
    record.setField("file_name", fileName);
    record.setField("file_line", Long.toString(currentLineNumber));

    try {
        JsonReader jsonReader = new JsonReader(new StringReader(lineString));
        jsonReader.setLenient(true);
        JsonParser jsonParser = new JsonParser();
        JsonElement mainElement = jsonParser.parse(jsonReader);
        traverseJson("$", mainElement);
    } catch (JsonSyntaxException e) {
        return null;
    }

    return record;
}

From source file:com.seagate.kinetic.example.heartbeat.rest.HeartbeatCollector.java

License:Open Source License

@Override
public void onMessage(byte[] data) {

    try {/*from ww  w .  j  a  va2 s  .c om*/

        String message = new String(data, "UTF8");

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

        Gson gson = new Gson();

        HeartbeatMessage hbm = gson.fromJson(reader, HeartbeatMessage.class);

        String key = hbm.getNetworkInterfaces().get(0).getIpV4Address() + ":" + hbm.getPort();

        MessageContainer container = new MessageContainer(hbm, System.currentTimeMillis());

        this.drives.put(key, container);

        logger.fine("received heart beat: " + key);

    } catch (Exception e) {
        logger.log(Level.WARNING, e.getMessage(), e);
    }

}