List of usage examples for com.google.gson.stream JsonToken BEGIN_OBJECT
JsonToken BEGIN_OBJECT
To view the source code for com.google.gson.stream JsonToken BEGIN_OBJECT.
Click Source Link
From source file:com.seleritycorp.common.base.coreservices.RawCoreServiceClient.java
License:Apache License
private void pushToWriter(JsonReader reader, JsonWriter writer) throws IOException { int count = 0; boolean isObject = reader.peek() == JsonToken.BEGIN_OBJECT; do {//from ww w . ja v a2s. co m JsonToken jsToken = reader.peek(); switch (jsToken) { case BEGIN_ARRAY: reader.beginArray(); writer.beginArray(); if (!isObject) { count++; } break; case END_ARRAY: reader.endArray(); writer.endArray(); if (!isObject) { count--; } break; case BEGIN_OBJECT: reader.beginObject(); writer.beginObject(); if (isObject) { count++; } break; case END_OBJECT: reader.endObject(); writer.endObject(); if (isObject) { count--; } break; case NAME: String name = reader.nextName(); writer.name(name); break; case STRING: String stringValue = reader.nextString(); writer.value(stringValue); break; case NUMBER: String numValue = reader.nextString(); writer.value(new BigDecimal(numValue)); break; case BOOLEAN: boolean boolValue = reader.nextBoolean(); writer.value(boolValue); break; case NULL: reader.nextNull(); writer.nullValue(); break; case END_DOCUMENT: return; default: return; } } while (count != 0); }
From source file:com.splunk.ResultsReaderJson.java
License:Apache License
boolean advanceIntoNextSetBeforeEvent() throws IOException { // jsonReader will be set to null once the end is reached. if (jsonReader == null) return false; // In Splunk 5.0 from the export endpoint, // each result is in its own top level object. // In Splunk 5.0 not from the export endpoint, the results are // an array at that object's key "results". // In Splunk 4.3, the // array was the top level returned. So if we find an object // at top level, we step into it until we find the right key, // then leave it in that state to iterate over. try {//w ww . ja v a 2 s . co m // Json single-reader depends on 'isExport' flag to function. // It does not support a stream from a file saved from // a stream from an export endpoint. // Json multi-reader assumes export format thus does not support // a stream from none export endpoints. if (exportHelper != null) { if (jsonReader.peek() == JsonToken.BEGIN_ARRAY) throw new UnsupportedOperationException("A stream from an export endpoint of " + "a Splunk 4.x server in the JSON output format " + "is not supported by this class. " + "Use the XML search output format, " + "and an XML result reader instead."); /* * We're on a stream from an export endpoint * Below is an example of an input stream. * {"preview":true,"offset":0,"lastrow":true,"result":{"host":"Andy-PC","count":"62"}} * {"preview":true,"offset":0,"result":{"host":"Andy-PC","count":"1682"}} */ // Read into first result object of the next set. while (true) { boolean endPassed = exportHelper.lastRow; exportHelper.skipRestOfRow(); if (!exportHelper.readIntoRow()) return false; if (endPassed) break; } return true; } // Single-reader not from an export endpoint if (jsonReader.peek() == JsonToken.BEGIN_OBJECT) { /* * We're on Splunk 5 with a single-reader not from * an export endpoint * Below is an example of an input stream. * {"preview":false,"init_offset":0,"messages":[{"type":"DEBUG","text":"base lispy: [ AND index::_internal ]"},{"type":"DEBUG","text":"search context: user=\"admin\", app=\"search\", bs-pathname=\"/Users/fross/splunks/splunk-5.0/etc\""}],"results":[{"sum(kb)":"14372242.758775","series":"twitter"},{"sum(kb)":"267802.333926","series":"splunkd"},{"sum(kb)":"5979.036338","series":"splunkd_access"}]} */ jsonReader.beginObject(); String key; while (true) { key = jsonReader.nextName(); if (key.equals("preview")) readPreviewFlag(); else if (key.equals("results")) { jsonReader.beginArray(); return true; } else { skipEntity(); } } } else { // We're on Splunk 4.x, and we just need to start the array. /* * Below is an example of an input stream * [ * { * "sum(kb)":"14372242.758775", * "series":"twitter" * }, * { * "sum(kb)":"267802.333926", * "series":"splunkd" * }, * { * "sum(kb)":"5979.036338", * "series":"splunkd_access" * } * ] */ jsonReader.beginArray(); return true; } } catch (EOFException e) { return false; } }
From source file:com.splunk.ResultsReaderJson.java
License:Apache License
/** * Skip the next value, whether it is atomic or compound, in the JSON * stream./* w w w . j a v a 2 s . c o m*/ */ private void skipEntity() throws IOException { if (jsonReader.peek() == JsonToken.STRING) { jsonReader.nextString(); } else if (jsonReader.peek() == JsonToken.BOOLEAN) { jsonReader.nextBoolean(); } else if (jsonReader.peek() == JsonToken.NUMBER) { jsonReader.nextDouble(); } else if (jsonReader.peek() == JsonToken.NULL) { jsonReader.nextNull(); } else if (jsonReader.peek() == JsonToken.NAME) { jsonReader.nextName(); } else if (jsonReader.peek() == JsonToken.BEGIN_ARRAY) { jsonReader.beginArray(); while (jsonReader.peek() != JsonToken.END_ARRAY) { skipEntity(); } jsonReader.endArray(); } else if (jsonReader.peek() == JsonToken.BEGIN_OBJECT) { jsonReader.beginObject(); while (jsonReader.peek() != JsonToken.END_OBJECT) { skipEntity(); } jsonReader.endObject(); } }
From source file:com.splunk.ResultsReaderJson.java
License:Apache License
private Event readEvent() throws IOException { Event returnData = null;/*from w w w . j ava 2s . c om*/ String name = null; List<String> values = new ArrayList<String>(); if (jsonReader == null) return null; // Events are almost flat, so no need for a true general parser // solution. But the Gson parser is a little unintuitive here. Nested // objects, have their own relative notion of hasNext. This // means that for every object or array start, hasNext() returns false // and one must consume the closing (END) object to get back to the // previous object. while (jsonReader.hasNext()) { if (returnData == null) { returnData = new Event(); } if (jsonReader.peek() == JsonToken.BEGIN_OBJECT) { jsonReader.beginObject(); } if (jsonReader.peek() == JsonToken.BEGIN_ARRAY) { jsonReader.beginArray(); // The Gson parser is a little unintuitive here. Nested objects, // have their own relative notion of hasNext; when hasNext() // is done, it is only for this array. while (jsonReader.hasNext()) { JsonToken jsonToken2 = jsonReader.peek(); if (jsonToken2 == JsonToken.STRING) { values.add(jsonReader.nextString()); } } jsonReader.endArray(); String[] valuesArray = values.toArray(new String[values.size()]); returnData.putArray(name, valuesArray); values.clear(); } if (jsonReader.peek() == JsonToken.NAME) { name = jsonReader.nextName(); } if (jsonReader.peek() == JsonToken.STRING) { String delimitedValues = jsonReader.nextString(); returnData.putSingleOrDelimited(name, delimitedValues); } if (jsonReader.peek() == JsonToken.END_OBJECT) { jsonReader.endObject(); break; } if (jsonReader.peek() == JsonToken.END_ARRAY) { jsonReader.endArray(); } } return returnData; }
From source file:com.splunk.sdk.ResultsReaderJson.java
License:Apache License
/** * Class constructor.//from w ww . j ava 2s . c om * * Constructs a streaming JSON reader for the event stream. You should only * attempt to parse a JSON stream with the JSON reader. Using a non-JSON * stream yields unpredictable results. * * @param inputStream The stream to parse. * @throws Exception On exception. */ public ResultsReaderJson(InputStream inputStream) throws IOException { super(inputStream); jsonReader = new JsonReader(new InputStreamReader(inputStream, "UTF8")); // if stream is empty, return a null reader. try { if (jsonReader.peek() == JsonToken.BEGIN_OBJECT) { // In Splunk 5.0, JSON output is an object, and the results are // an array at that object's key "results". In Splunk 4.3, the // array was the top level returned. So if we find an object // at top level, we step into it until we find the right key, // then leave it in that state to iterate over. jsonReader.beginObject(); String key; while (true) { key = jsonReader.nextName(); if (key.equals("results")) { jsonReader.beginArray(); return; } else { skipEntity(); } } } else { // We're on Splunk 4.x, and we just need to start the array. jsonReader.beginArray(); return; } } catch (EOFException e) { jsonReader = null; return; } }
From source file:com.splunk.sdk.ResultsReaderJson.java
License:Apache License
/** {@inheritDoc} */ @Override/*from w ww . ja va 2 s . c o m*/ public Event getNextEvent() throws IOException { Event returnData = null; String name = null; List<String> values = new ArrayList<String>(); if (jsonReader == null) return null; // Events are almost flat, so no need for a true general parser // solution. But the Gson parser is a little unintuitive here. Nested // objects, have their own relative notion of hasNext. This // means that for every object or array start, hasNext() returns false // and one must consume the closing (END) object to get back to the // previous object. while (jsonReader.hasNext()) { if (returnData == null) { returnData = new Event(); } if (jsonReader.peek() == JsonToken.BEGIN_OBJECT) { jsonReader.beginObject(); } if (jsonReader.peek() == JsonToken.BEGIN_ARRAY) { jsonReader.beginArray(); // The Gson parser is a little unintuitive here. Nested objects, // have their own relative notion of hasNext; when hasNext() // is done, it is only for this array. while (jsonReader.hasNext()) { JsonToken jsonToken2 = jsonReader.peek(); if (jsonToken2 == JsonToken.STRING) { values.add(jsonReader.nextString()); } } jsonReader.endArray(); String[] valuesArray = values.toArray(new String[values.size()]); returnData.putArray(name, valuesArray); values.clear(); } if (jsonReader.peek() == JsonToken.NAME) { name = jsonReader.nextName(); } if (jsonReader.peek() == JsonToken.STRING) { String delimitedValues = jsonReader.nextString(); returnData.putSingleOrDelimited(name, delimitedValues); } if (jsonReader.peek() == JsonToken.END_OBJECT) { jsonReader.endObject(); break; } if (jsonReader.peek() == JsonToken.END_ARRAY) { jsonReader.endArray(); } } return returnData; }
From source file:controllers.ParseController.java
@RequestMapping("/addFile") public String addFile(Map<String, Object> model, @RequestParam("newFile") MultipartFile file) throws IOException { File newFile = new File("/usr/local/etc/newFile"); if (!newFile.exists()) { FileUtils.writeByteArrayToFile(newFile, file.getBytes()); } else {//from w w w.j av a 2 s . c o m newFile.delete(); FileUtils.writeByteArrayToFile(newFile, file.getBytes()); } if (newFile.exists()) { JsonReader reader = new JsonReader(new FileReader(newFile)); try { reader.beginObject(); while (reader.hasNext()) { String objectName = reader.nextName(); if (objectName.equals("colors")) { reader.beginObject(); while (reader.hasNext()) { String colorNameId = reader.nextName(); reader.beginObject(); Long oldColorId = Long.valueOf("0"); String title = "0"; String titleEng = "0"; while (reader.hasNext()) { String objectField = reader.nextName(); if (objectField.equals("id")) { oldColorId = reader.nextLong(); } if (objectField.equals("title")) { title = reader.nextString(); } if (objectField.equals("title_eng")) { titleEng = reader.nextString(); } if (objectField.equals("aliases")) { String oldId = ""; String name = ""; if (reader.peek().equals(JsonToken.BEGIN_ARRAY)) { reader.beginArray(); reader.endArray(); oldId = "0"; name = "? "; Color c = new Color(); c.setName(name); c.setTitle(title); c.setTitleEng(titleEng); c.setOldGroupId(oldColorId); c.setOldId(Long.valueOf(oldId)); colorService.createColor(c); //keys.add(oldColorId + " - " + title + " - " + titleEng + " - " + oldId + " - " + name); } else { reader.beginObject(); while (reader.hasNext()) { oldId = reader.nextName(); name = reader.nextString(); Color c = new Color(); c.setName(name); c.setTitle(title); c.setTitleEng(titleEng); c.setOldGroupId(oldColorId); c.setOldId(Long.valueOf(oldId)); colorService.createColor(c); //keys.add(oldColorId + " - " + title + " - " + titleEng + " - " + oldId + " - " + name); } reader.endObject(); } } } reader.endObject(); } reader.endObject(); } else if (objectName.equals("color_groups")) { reader.beginObject(); while (reader.hasNext()) { String colorGroupNameId = reader.nextName(); reader.beginObject(); Long oldGroupId = Long.valueOf("0"); String title = ""; while (reader.hasNext()) { String objectField = reader.nextName(); if (objectField.equals("id")) { oldGroupId = reader.nextLong(); } else if (objectField.equals("title")) { title = reader.nextString(); } else if (objectField.equals("aliases")) { reader.beginObject(); while (reader.hasNext()) { String oldId = reader.nextName(); String name = reader.nextString(); ColorGroup cg = new ColorGroup(); cg.setName(name); cg.setTitle(title); cg.setOldGroupId(oldGroupId); cg.setOldId(Long.valueOf(oldId)); colorGroupService.createColorGroup(cg); //keys.add(oldGroupId + " - " + title + " - " + oldId + " - " + name); } reader.endObject(); } } reader.endObject(); } reader.endObject(); //keys.add(name); } else if (objectName.equals("features")) { reader.beginObject(); while (reader.hasNext()) { String featureNameId = reader.nextName(); reader.beginObject(); Long featureId = Long.valueOf("0"); String title = ""; String description = ""; Long cmgId = null; Long ccoId = null; String media = ""; while (reader.hasNext()) { String objectField = reader.nextName(); if (objectField.equals("id")) { featureId = reader.nextLong(); } else if (objectField.equals("title")) { title = reader.nextString(); } else if (objectField.equals("description")) { description = reader.nextString(); } else if (objectField.equals("car_model_generation_id")) { if (!reader.peek().equals(JsonToken.NULL)) { cmgId = reader.nextLong(); } else { reader.skipValue(); } } else if (objectField.equals("car_completion_option_id")) { if (!reader.peek().equals(JsonToken.NULL)) { ccoId = reader.nextLong(); } else { reader.skipValue(); } } else if (objectField.equals("media")) { //reader.skipValue(); if (reader.peek().equals(JsonToken.BEGIN_OBJECT)) { reader.beginObject(); while (reader.hasNext()) { if (reader.peek().equals(JsonToken.NAME)) { String mediaName = reader.nextName(); media += mediaName + " : [ "; reader.beginArray(); while (reader.hasNext()) { media += " { "; reader.beginObject(); while (reader.hasNext()) { String mediaParamName = reader.nextName(); media += mediaParamName + " : "; if (!mediaParamName.equals("url")) { if (!reader.peek().equals(JsonToken.NULL)) { media += reader.nextString(); } else { reader.skipValue(); media += "null"; } } else { reader.beginObject(); media += "{"; while (reader.hasNext()) { media += reader.nextName() + " : " + reader.nextString(); } media += "}"; reader.endObject(); } } reader.endObject(); media += " } "; } reader.endArray(); media += " ] "; } } reader.endObject(); } else { reader.skipValue(); } } } reader.endObject(); Feature f = new Feature(); f.setOldId(featureId); f.setTitle(title); f.setDescription(description); f.setCmgId(cmgId); f.setCcoId(ccoId); f.setMedia(media); //keys.add(featureId + " - " + title + " - " + description + " - " + cmgId + " - " + ccoId + " - " + media); featureService.createFeature(f); } reader.endObject(); } else { reader.skipValue(); } } reader.endObject(); reader.close(); } catch (Exception e) { model.put("error", "? ? :" + StringAdapter.getStackTraceException(e)); reader.close(); return "ParseShow"; } } return "redirect:/ParseController/show"; }
From source file:ddt.dtool.dub.DubDescribeParser.java
License:Open Source License
@Override protected void readData(JsonReaderExt jsonParser) throws IOException { jsonParser.consumeExpected(JsonToken.BEGIN_OBJECT); while (jsonParser.hasNext()) { JsonToken tokenType = jsonParser.peek(); if (tokenType == JsonToken.NAME) { String propertyName = jsonParser.nextName(); if (propertyName.equals("mainPackage")) { bundleName = jsonParser.consumeStringValue(); } else if (propertyName.equals("packages")) { bundles = readDescribedBundles(jsonParser); } else { jsonParser.skipValue();/*from w w w .ja v a 2s .c o m*/ } } else { jsonParser.errorUnexpected(tokenType); } } jsonParser.consumeExpected(JsonToken.END_OBJECT); }
From source file:ddt.dtool.dub.DubDescribeParser.java
License:Open Source License
protected static ArrayList<DubBundle> readDescribedBundles(JsonReaderExt jsonParser) throws IOException { jsonParser.consumeExpected(JsonToken.BEGIN_ARRAY); ArrayList<DubBundle> bundles = new ArrayList<>(); while (jsonParser.hasNext()) { JsonToken tokenType = jsonParser.peek(); if (tokenType == JsonToken.BEGIN_OBJECT) { DubBundle bundle = new DubManifestParser().readBundle(jsonParser).createBundle(null, false); bundles.add(bundle);//from w w w. j av a 2s .c om } else { jsonParser.errorUnexpected(tokenType); } } jsonParser.consumeExpected(JsonToken.END_ARRAY); return bundles; }
From source file:ddt.dtool.dub.DubManifestParser.java
License:Open Source License
protected DubManifestParser readBundle(JsonReaderExt jsonReader) throws IOException { jsonReader.consumeExpected(JsonToken.BEGIN_OBJECT); while (jsonReader.hasNext()) { JsonToken tokenType = jsonReader.peek(); if (tokenType == JsonToken.NAME) { String propertyName = jsonReader.nextName(); if (propertyName.equals("name")) { bundleName = jsonReader.consumeStringValue(); } else if (propertyName.equals("version")) { version = jsonReader.consumeStringValue(); } else if (propertyName.equals("path")) { pathString = jsonReader.consumeStringValue(); } else if (propertyName.equals("importPaths")) { sourceFolders = parseSourcePaths(jsonReader); } else if (propertyName.equals("dependencies")) { dependencies = parseDependencies(jsonReader); } else if (propertyName.equals("files")) { bundleFiles = parseFiles(jsonReader); } else if (propertyName.equals("targetName")) { targetName = jsonReader.consumeStringValue(); } else if (propertyName.equals("targetPath")) { targetPath = jsonReader.consumeStringValue(); } else { jsonReader.skipValue();//from w ww.j a v a 2s .co m } } else { jsonReader.errorUnexpected(tokenType); } } jsonReader.consumeExpected(JsonToken.END_OBJECT); return this; }