Example usage for com.google.gson JsonStreamParser next

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

Introduction

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

Prototype

public JsonElement next() throws JsonParseException 

Source Link

Document

Returns the next available JsonElement on the reader.

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 v a  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() {

        /*/*  w w w  .  jav a 2 s  .c  om*/
         * (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 a  2 s  .  c  o 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./*  w ww  . j  a  va2  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.hillview.RedoLog.java

License:Open Source License

private void reload() {
    if (this.backupFile == null)
        return;// www.  j  a v a  2s .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.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  v a 2s. c o  m
                        } 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;
}

From source file:pl.projewski.game.polan.server.data.ServerData.java

public void loadProductDefinitionsFromGsonFile(String filename) {
    productDefinitionMap = new HashMap();
    Gson gson = GSonUtil.getGSon();/*from w  w w . jav a  2  s .  c om*/
    InputStreamReader reader = null;
    try {
        final File file = new File(filename);
        if (file.exists()) {
            reader = new InputStreamReader(new FileInputStream(file));
            JsonStreamParser parser = new JsonStreamParser(reader);
            while (parser.hasNext()) {
                ProductDefinition pd = gson.fromJson(parser.next(), ProductDefinition.class);
                productDefinitionMap.put(pd.getName(), pd);
                LOG.debug("Loaded product " + pd.getName());
            }
        }
    } catch (FileNotFoundException ex) {
        Logger.getLogger(ServerData.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        IOUtils.closeQuietly(reader);
    }

}

From source file:pl.projewski.game.polan.server.data.ServerData.java

public void loadBiomeDefinitionsFromGsonFile(String filename) {
    biomeDefinitionList = new ArrayList();
    Gson gson = GSonUtil.getGSon();/* w  w w .  ja  v  a2 s . c o m*/
    InputStreamReader reader = null;
    try {
        final File file = new File(filename);
        if (file.exists()) {
            reader = new InputStreamReader(new FileInputStream(file));
            JsonStreamParser parser = new JsonStreamParser(reader);
            while (parser.hasNext()) {
                BiomeDefinition bd = gson.fromJson(parser.next(), BiomeDefinition.class);
                biomeDefinitionList.add(bd);
                LOG.debug("Loaded biome " + bd.getName());
            }
        }
    } catch (FileNotFoundException ex) {
        Logger.getLogger(ServerData.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        IOUtils.closeQuietly(reader);
    }
}