Example usage for com.google.gson JsonStreamParser JsonStreamParser

List of usage examples for com.google.gson JsonStreamParser JsonStreamParser

Introduction

In this page you can find the example usage for com.google.gson JsonStreamParser JsonStreamParser.

Prototype

public JsonStreamParser(Reader reader) 

Source Link

Usage

From source file:be.okno.tik.tak.server.processing.WindClockProtocol.java

License:Open Source License

public WindClockProtocol(InputStreamReader isReader) {
    parser = new JsonStreamParser(isReader);
    wcLogger = new WindClockLogger();
    wtp = new WindTimeProtocol();
}

From source file:com.asakusafw.testdriver.json.JsonDataModelSource.java

License:Apache License

/**
 * Creates a new instance./*  ww w. j av a 2 s  .  c  o  m*/
 * @param id source ID (nullable)
 * @param definition the data model definition
 * @param reader source character stream
 * @throws IllegalArgumentException if some parameters were {@code null}
 */
public JsonDataModelSource(URI id, DataModelDefinition<?> definition, Reader reader) {
    if (definition == null) {
        throw new IllegalArgumentException("definition must not be null"); //$NON-NLS-1$
    }
    if (reader == null) {
        throw new IllegalArgumentException("reader must not be null"); //$NON-NLS-1$
    }
    this.id = id;
    this.definition = definition;
    this.reader = reader;
    this.parser = new JsonStreamParser(reader);
}

From source file:com.facebook.buck.apple.XctoolOutputParsing.java

License:Apache License

/**
 * Decodes a stream of JSON objects as produced by {@code xctool -reporter json-stream}
 * and invokes the callbacks in {@code eventCallback} with each event in the stream.
 *///from ww w  .j a va  2 s  .  c o  m
public static void streamOutputFromReader(Reader reader, XctoolEventCallback eventCallback) {
    Gson gson = new Gson();
    JsonStreamParser streamParser = new JsonStreamParser(reader);
    while (streamParser.hasNext()) {
        try {
            dispatchEventCallback(gson, streamParser.next(), eventCallback);
        } catch (JsonParseException e) {
            LOG.warn(e, "Couldn't parse xctool JSON stream");
        }
    }
}

From source file:com.flipkart.polyguice.config.JsonConfiguration.java

License:Apache License

private void load(Reader in) {
    configTab = new HashMap<>();
    JsonStreamParser parser = new JsonStreamParser(in);
    JsonElement root = null;//from   w  ww  .ja  v a  2  s.com
    if (parser.hasNext()) {
        root = parser.next();
    }
    if (root != null && root.isJsonObject()) {
        flatten(null, root);
    }
    LOGGER.debug("json configuration loaded: {}", configTab);
}

From source file:com.marcolotz.lung.posprocesser.core.JSONExtractor.java

License:Creative Commons License

/***
 * Opens the input File//  w  w  w .ja  va2  s.c  o m
 */
private void openFile() {

    try {
        parser = new JsonStreamParser(new FileReader(inputPath));
    } catch (FileNotFoundException e) {
        System.err.println("Error openning file.");
        e.printStackTrace();
    }
}

From source file:com.triarc.sync.SyncAdapter.java

License:Apache License

@SuppressLint("NewApi")
private void readResponse(SyncTypeCollection typeCollection, InputStream inputStream, HttpResponse response)
        throws UnsupportedEncodingException, IOException, JSONException, Exception {
    long lastUpdate = 0;
    Header header = response.getFirstHeader("X-Application-Timestamp");
    if (header != null) {
        String value = header.getValue();
        lastUpdate = Long.parseLong(value);
    }/*from  ww w  . ja  v  a  2  s  .  co  m*/

    // json is UTF-8 by default
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
    JsonStreamParser parser = new JsonStreamParser(reader);
    JsonReader jsonReader = new JsonReader(reader);
    jsonReader.beginObject();
    jsonReader.nextName();
    jsonReader.beginObject();
    while (jsonReader.hasNext()) {

        String syncName = jsonReader.nextName();
        SyncType syncType = this.GetSyncType(typeCollection, syncName);

        JsonElement fromJson = new Gson().fromJson(jsonReader, JsonElement.class);
        updateLocalTypeData(fromJson.getAsJsonObject(), syncType, typeCollection, syncResult);

        notificationQueue.add(new SyncNotificationMessage(syncType, fromJson.toString()));
        // String path = jsonReader.getPath();
        // String nextName = jsonReader.nextName();
        // jsonReader.endObject();
    }
    jsonReader.endObject();

    // String json = getStringForReader(reader);
    //
    //
    // JSONObject syncChangeSets = new JSONObject(json)
    // .getJSONObject("changeSetPerType");
    //
    // // first save all
    // for (SyncType syncType : typeCollection.getTypes()) {
    // syncResult.madeSomeProgress();
    // String name = syncType.getName();
    //
    // if (syncChangeSets.has(name)) {
    // JSONObject changeSetObject = syncChangeSets.getJSONObject(name);
    // updateLocalTypeData(changeSetObject, syncType, typeCollection,
    // syncResult);
    // notificationMap.put(syncType, changeSetObject);
    // } else {
    // Log.w(TAG, "Server does not support syncing of " + name);
    // sendLogs();
    // }
    // }

    // store collection update timestamp
    PreferenceManager.getDefaultSharedPreferences(this.getContext()).edit()
            .putLong(typeCollection.getName(), lastUpdate).commit();
    // then notify all
    notifyWebApp();

}

From source file:com.twitter.common.thrift.text.TTextProtocol.java

License:Apache License

@Override
public TStruct readStructBegin() throws TException {
    getCurrentContext().read();// ww w  .j a va  2s.com

    JsonElement structElem;
    // Reading a new top level struct if the only item on the stack
    // is the BaseContext
    if (1 == contextStack.size()) {
        structElem = parser.next();
        if (null == structElem) {
            throw new TException("parser.next() has nothing to parse!");
        }
    } else {
        structElem = getCurrentContext().getCurrentChild();
    }

    if (getCurrentContext().isMapKey()) {
        structElem = new JsonStreamParser(structElem.getAsString()).next();
    }

    if (!structElem.isJsonObject()) {
        throw new TException("Expected Json Object!");
    }

    pushContext(new StructContext(structElem.getAsJsonObject()));
    return ANONYMOUS_STRUCT;
}

From source file:com.twitter.common.thrift.text.TTextProtocol.java

License:Apache License

@Override
public TMap readMapBegin() throws TException {
    getCurrentContext().read();/*  w  ww  .j  ava  2 s.  c o  m*/

    JsonElement curElem = getCurrentContext().getCurrentChild();

    if (getCurrentContext().isMapKey()) {
        curElem = new JsonStreamParser(curElem.getAsString()).next();
    }

    if (!curElem.isJsonObject()) {
        throw new TException("Expected JSON Object!");
    }

    pushContext(new MapContext(curElem.getAsJsonObject()));

    return new TMap(UNUSED_TYPE, UNUSED_TYPE, curElem.getAsJsonObject().entrySet().size());
}

From source file:com.twitter.common.thrift.text.TTextProtocol.java

License:Apache License

/**
 * Set up the stream parser to read from the trans_ TTransport
 * buffer./*from  w  w w  .j  a  va  2s .  c o  m*/
 */
private JsonStreamParser createParser() throws IOException {
    return new JsonStreamParser(new String(ByteStreams.toByteArray(new InputStream() {
        private int index;
        private int max;
        private final byte[] buffer = new byte[READ_BUFFER_SIZE];

        @Override
        public int read() throws IOException {
            if (max == -1) {
                return -1;
            }
            if (max > 0 && index < max) {
                return buffer[index++];
            }
            try {
                max = trans_.read(buffer, 0, READ_BUFFER_SIZE);
                index = 0;
            } catch (TTransportException e) {
                if (TTransportException.END_OF_FILE != e.getType()) {
                    throw new IOException(e);
                }
                max = -1;
            }
            return read();
        }
    }), Charsets.UTF_8));
}

From source file:jGraphprocessingsystem.JsonDataFormat.java

public ArrayList<User> getData(String directory) {
    ArrayList<User> listUser = new ArrayList<>();
    try {// www  . j  a v a 2 s  .c om
        JsonStreamParser parser = new JsonStreamParser(new FileReader(directory));
        while (parser.hasNext()) {
            User user = gson.fromJson(parser.next(), User.class);
            listUser.add(user);
        }
    } catch (FileNotFoundException ex) {
        Logger.getLogger(JsonDataFormat.class.getName()).log(Level.SEVERE, null, ex);
    }
    return listUser;
}