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:org.apache.orc.tools.json.JsonShredder.java

License:Apache License

public static void main(String[] args) throws Exception {
    int count = 0;
    JsonShredder shredder = new JsonShredder();
    for (String filename : args) {
        System.out.println("Reading " + filename);
        System.out.flush();//from  www . j a  va 2 s.  c o m
        java.io.Reader reader;
        FileInputStream inStream = new FileInputStream(filename);
        if (filename.endsWith(".gz")) {
            reader = new InputStreamReader(new GZIPInputStream(inStream), StandardCharsets.UTF_8);
        } else {
            reader = new InputStreamReader(inStream, StandardCharsets.UTF_8);
        }
        JsonStreamParser parser = new JsonStreamParser(reader);
        while (parser.hasNext()) {
            count += 1;
            JsonElement item = parser.next();
            shredder.shredObject("root", item);
        }
    }
    shredder.close();
    System.out.println(count + " records read");
    System.out.println();
}

From source file:org.ebayopensource.scc.track.TrackerClient.java

License:Apache License

public void start() {
    Runnable thread = new Runnable() {

        /*//from  www .j  a  v a  2s.c o  m
         * (non-Javadoc)
         * 
         * @see java.lang.Thread#run()
         */
        @Override
        public void run() {
            Tracker tracker = new Tracker(getUser(), getHostName(), getIp(), getOs(),
                    System.currentTimeMillis(), getInitiator());

            AutoRetryHttpClient client = new AutoRetryHttpClient(
                    new DefaultServiceUnavailableRetryStrategy(3, 3000));
            HttpPost request = createRequest(tracker);
            HttpHost httpHost = createHttpHost();

            try {
                HttpResponse resp = client.execute(httpHost, request);
                int code = resp.getStatusLine().getStatusCode();
                if (HttpResponseStatus.OK.code() == code) {
                    InputStream is = null;
                    try {
                        is = resp.getEntity().getContent();
                        is = new BufferedInputStream(is);
                        InputStreamReader reader = new InputStreamReader(is);
                        JsonStreamParser parser = new JsonStreamParser(reader);
                        while (parser.hasNext()) {
                            JsonElement json = parser.next();
                            LOGGER.debug("Tracking data sent: " + json);
                        }
                    } finally {
                        if (is != null) {
                            is.close();
                        }
                    }

                } else {
                    LOGGER.error(ERROR_FAILED_TO_SEND);
                    LOGGER.debug("Response code: " + code);
                }
            } catch (IOException e) {
                LOGGER.warn(ERROR_FAILED_TO_SEND);
            }
        }

    };

    m_scheduledService.scheduleAtFixedRate(thread, 0, m_interval, TimeUnit.SECONDS);
}

From source file:org.eclim.plugin.core.command.admin.SettingsUpdateCommand.java

License:Open Source License

@Override
public Object execute(CommandLine commandLine) throws Exception {
    String settings = commandLine.getValue(Options.SETTINGS_OPTION);

    FileReader in = null;//  w ww  .ja  v  a2 s  . co  m
    File file = new File(settings);
    ArrayList<Error> errors = new ArrayList<Error>();
    try {
        in = new FileReader(file);
        JsonStreamParser parser = new JsonStreamParser(in);
        JsonObject obj = (JsonObject) parser.next();

        Preferences preferences = getPreferences();
        for (Map.Entry<String, JsonElement> entry : obj.entrySet()) {
            String name = entry.getKey();
            String value = entry.getValue().getAsString();
            try {
                preferences.setValue(name, value);
            } catch (IllegalArgumentException iae) {
                errors.add(new Error(iae.getMessage(), null, 0, 0));
            }
        }
    } finally {
        IOUtils.closeQuietly(in);
        try {
            file.delete();
        } catch (Exception e) {
            logger.warn("Error deleting settings temp file: " + file, e);
        }
    }

    if (errors.size() > 0) {
        return errors;
    }
    return Services.getMessage("settings.updated");
}

From source file:org.eclim.plugin.core.command.project.ProjectUpdateCommand.java

License:Open Source License

/**
 * Updates the projects settings.//from   w  ww  . j a  va 2 s.c  o  m
 *
 * @param project The project.
 * @param settings The temp settings file.
 * @return List of errors or an empty List if none.
 */
private List<Error> updateSettings(IProject project, String settings) throws Exception {
    FileReader in = null;
    File file = new File(settings);
    ArrayList<Error> errors = new ArrayList<Error>();
    try {
        in = new FileReader(file);
        JsonStreamParser parser = new JsonStreamParser(in);
        JsonObject obj = (JsonObject) parser.next();

        Preferences preferences = getPreferences();
        for (Map.Entry<String, JsonElement> entry : obj.entrySet()) {
            String name = entry.getKey();
            String value = entry.getValue().getAsString();
            try {
                preferences.setValue(project, name, value);
            } catch (IllegalArgumentException iae) {
                errors.add(new Error(iae.getMessage(), null, 0, 0));
            }
        }
    } finally {
        IOUtils.closeQuietly(in);
        try {
            file.delete();
        } catch (Exception e) {
            logger.warn("Error deleting project settings temp file: " + file, e);
        }
    }
    return errors;
}

From source file:org.eclipse.che.infrastructure.docker.client.JsonMessageReader.java

License:Open Source License

/**
 * @param source source of messages in JSON format
 * @param messageClass class of the message object where JSON messages should be parsed. Because
 *     of erasure of generic information in runtime in some cases we can't get parameter class of
 *     current class.// w ww . j  a  va 2  s . c om
 */
public JsonMessageReader(InputStream source, Class<T> messageClass) {
    // we need to push back only 1 char, read more further
    this.reader = new PushbackReader(new InputStreamReader(source), 1);
    this.streamParser = new JsonStreamParser(reader);
    this.messageClass = messageClass;
}

From source file:org.eclipse.che.plugin.docker.client.ProgressStatusReader.java

License:Open Source License

ProgressStatusReader(InputStream source) {
    streamParser = new JsonStreamParser(new InputStreamReader(source));
}

From source file:org.hillview.RedoLog.java

License:Open Source License

private void reload() {
    if (this.backupFile == null)
        return;/*from w  w  w.  ja v  a 2  s  .  c om*/
    HillviewLogger.instance.info("Replaying redo log");
    try (FileReader fr = new FileReader(this.backupFile)) {
        JsonStreamParser parser = new JsonStreamParser(fr);
        while (parser.hasNext()) {
            JsonElement elem = parser.next();
            DestAndRequest dar = IJson.gsonInstance.fromJson(elem, DestAndRequest.class);
            this.addGenerated(dar.resultId, dar.request);
        }
    } catch (IOException ex) {
        HillviewLogger.instance.error("Cannot read hillview redo log", ex);
    }
}

From source file:org.sourcepit.osgifier.core.util.JsonUtils.java

License:Apache License

public static JsonElement parse(Reader reader) {
    return (JsonArray) new JsonStreamParser(reader).next();
}

From source file:org.terrier.indexing.TwitterJSONCollection.java

License:Mozilla Public License

public void loadJSON(String file) throws IOException {
    currentTweetStream = new BufferedReader(
            new InputStreamReader(new GZIPInputStream(new FileInputStream(file)), "UTF-8"));
    JSONStream = new JsonStreamParser(currentTweetStream);
}

From source file:org.uorm.serializer.DefaultJsonSerializer.java

License:Apache License

@Override
public Map<String, Object> deserialize2(Class<?> cls, String json) throws Exception {
    JsonStreamParser parser = new JsonStreamParser(json);
    if (parser.hasNext()) {
        JsonObject jsonobj = parser.next().getAsJsonObject();
        Set<Entry<String, JsonElement>> jset = jsonobj.entrySet();
        if (!jset.isEmpty()) {
            Map<String, PropertyDescriptor> propMap = ObjectMappingCache.getInstance()
                    .getObjectPropertyMap(cls);
            Map<String, Object> instance = new HashMap<String, Object>();
            for (Entry<String, JsonElement> entry : jset) {
                String name = entry.getKey();
                JsonElement val = entry.getValue();
                if (!val.isJsonNull()) {
                    PropertyDescriptor descriptor = propMap.get(name);
                    if (descriptor != null) {
                        Class<?> memberType = descriptor.getPropertyType();
                        if (memberType == String.class) {
                            instance.put(name, val.getAsString());
                        } else if (memberType == Integer.class || memberType == Integer.TYPE) {
                            instance.put(name, val.getAsInt());
                        } else if (memberType == Byte.class || memberType == Byte.TYPE) {
                            instance.put(name, val.getAsByte());
                        } else if (memberType == Double.class || memberType == Double.TYPE) {
                            instance.put(name, val.getAsDouble());
                        } else if (memberType == Float.class || memberType == Float.TYPE) {
                            instance.put(name, val.getAsFloat());
                        } else if (memberType == Long.class || memberType == Long.TYPE) {
                            instance.put(name, val.getAsLong());
                        } else if (memberType == Short.class || memberType == Short.TYPE) {
                            instance.put(name, val.getAsShort());
                        } else if (memberType == Boolean.class || memberType == Boolean.TYPE) {
                            instance.put(name, val.getAsBoolean());
                        } else if (memberType == BigDecimal.class) {
                            instance.put(name, val.getAsBigDecimal());
                        } else if (memberType == BigInteger.class) {
                            instance.put(name, val.getAsBigInteger());
                        } else if (memberType == byte[].class) {
                            instance.put(name, val.getAsString().getBytes());
                        } else if (memberType == java.sql.Timestamp.class) {
                            Long time = parserDate(val.getAsString());
                            if (time == null) {
                                instance.put(name, null);
                            } else {
                                instance.put(name, new java.sql.Timestamp(time));
                            }/*from  ww w.j a  va2 s.  c om*/
                        } else if (memberType == java.sql.Date.class) {
                            Long time = parserDate(val.getAsString());
                            if (time == null) {
                                instance.put(name, null);
                            } else {
                                instance.put(name, new java.sql.Date(time));
                            }
                        } else if (memberType == java.util.Date.class) {
                            Long time = parserDate(val.getAsString());
                            if (time == null) {
                                instance.put(name, null);
                            } else {
                                instance.put(name, new java.util.Date(time));
                            }
                        } else {//default String
                            instance.put(name, val.getAsString());
                        }
                    } else {//String
                        instance.put(name, val.getAsString());
                    }
                }
            }
            return instance;
        }
    }
    return null;
}