Example usage for com.google.gson JsonStreamParser hasNext

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

Introduction

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

Prototype

public boolean hasNext() 

Source Link

Document

Returns true if a JsonElement is available on the input for consumption

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();/*w ww  .  j  a  v a2s  .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() {

        /*/* ww  w .j a v a2 s.co 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.hillview.RedoLog.java

License:Open Source License

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