List of usage examples for com.google.gson.stream JsonToken BEGIN_ARRAY
JsonToken BEGIN_ARRAY
To view the source code for com.google.gson.stream JsonToken BEGIN_ARRAY.
Click Source Link
From source file:com.nridge.ds.solr.SolrConfigJSON.java
License:Open Source License
private void populateSearchComponent(JsonReader aReader, Document aCfgDocument, String aDocType) throws IOException { DataBag scBag;// w ww .j a v a2 s . c o m Document scDocument; JsonToken jsonToken; boolean isArrayArray; Document childDocument; String jsonName, jsonValue; Document searchComponentDocument = new Document(aDocType); aReader.beginObject(); while (aReader.hasNext()) { jsonName = aReader.nextName(); scDocument = new Document(Solr.RESPONSE_CONFIG_SEARCH_COMPONENT); scDocument.setName(jsonName); scBag = scDocument.getBag(); DataTextField operationField = new DataTextField(Solr.CONFIG_OPERATION_FIELD_NAME, Field.nameToTitle(Solr.CONFIG_OPERATION_FIELD_NAME)); operationField.addFeature(Field.FEATURE_IS_STORED, StrUtl.STRING_FALSE); scBag.add(operationField); aReader.beginObject(); while (aReader.hasNext()) { jsonName = aReader.nextName(); if (isNextTokenAnArray(aReader)) { aReader.beginArray(); jsonToken = aReader.peek(); if (jsonToken == JsonToken.BEGIN_ARRAY) { isArrayArray = true; aReader.beginArray(); jsonToken = aReader.peek(); } else isArrayArray = false; while (jsonToken != JsonToken.END_ARRAY) { childDocument = new Document(Field.nameToTitle(jsonName)); childDocument.setName(jsonName); aReader.beginObject(); while (aReader.hasNext()) addFieldToDocument(aReader, childDocument); aReader.endObject(); scDocument.addRelationship(Field.nameToTitle(jsonName), childDocument); jsonToken = aReader.peek(); } aReader.endArray(); if (isArrayArray) aReader.endArray(); } else if (isNextTokenAnObject(aReader)) { childDocument = new Document(Field.nameToTitle(jsonName)); childDocument.setName(jsonName); aReader.beginObject(); while (aReader.hasNext()) addFieldToDocument(aReader, childDocument); aReader.endObject(); scDocument.addRelationship(Field.nameToTitle(jsonName), childDocument); } else { jsonValue = nextValueAsString(aReader); addBagTextField(scBag, jsonName, jsonValue); } } aReader.endObject(); assignSearchComponentType(scDocument); searchComponentDocument.addRelationship(scDocument.getType(), scDocument); } aCfgDocument.addRelationship(aDocType, searchComponentDocument); }
From source file:com.quarterfull.newsAndroid.reader.owncloud.OwnCloudReaderMethods.java
License:Open Source License
private static JSONObject getJSONObjectFromReader(JsonReader jsonReader) { JSONObject jObj = new JSONObject(); JsonToken tokenInstance;/* ww w . j av a2s . c om*/ try { tokenInstance = jsonReader.peek(); if (tokenInstance == JsonToken.BEGIN_OBJECT) jsonReader.beginObject(); else if (tokenInstance == JsonToken.BEGIN_ARRAY) jsonReader.beginArray(); while (jsonReader.hasNext()) { JsonToken token; String name; try { name = jsonReader.nextName(); token = jsonReader.peek(); //Log.d(TAG, token.toString()); switch (token) { case NUMBER: jObj.put(name, jsonReader.nextLong()); break; case NULL: jsonReader.skipValue(); break; case BOOLEAN: jObj.put(name, jsonReader.nextBoolean()); break; case BEGIN_OBJECT: //jsonReader.beginObject(); jObj.put(name, getJSONObjectFromReader(jsonReader)); //jsonReader.endObject(); break; case BEGIN_ARRAY: jsonReader.skipValue(); break; default: jObj.put(name, jsonReader.nextString()); } } catch (Exception ex) { ex.printStackTrace(); jsonReader.skipValue(); } } if (tokenInstance == JsonToken.BEGIN_OBJECT) jsonReader.endObject(); else if (tokenInstance == JsonToken.BEGIN_ARRAY) jsonReader.endArray(); return jObj; } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:com.sap.core.odata.core.ep.consumer.JsonFeedConsumer.java
License:Apache License
private void readFeed() throws IOException, EdmException, EntityProviderException { if (reader.peek() == JsonToken.BEGIN_ARRAY) { readArrayContent();//from ww w . j a v a2 s. com } else { reader.beginObject(); final String nextName = reader.nextName(); if (FormatJson.D.equals(nextName)) { if (reader.peek() == JsonToken.BEGIN_ARRAY) { readArrayContent(); } else { reader.beginObject(); readFeedContent(); reader.endObject(); } } else { handleName(nextName); readFeedContent(); } reader.endObject(); } }
From source file:com.sap.core.odata.core.ep.consumer.JsonLinkConsumer.java
License:Apache License
/** * Reads a collection of links, optionally wrapped in a "d" object, * and optionally wrapped in an "results" object, where an additional "__count" * object could appear on the same level as the "results". * @param reader//w w w .j a v a 2s . co m * @param entitySet * @return links as List of Strings * @throws EntityProviderException */ public List<String> readLinks(final JsonReader reader, final EdmEntitySet entitySet) throws EntityProviderException { List<String> links = null; int openedObjects = 0; try { String nextName; if (reader.peek() == JsonToken.BEGIN_ARRAY) { nextName = FormatJson.RESULTS; } else { reader.beginObject(); openedObjects++; nextName = reader.nextName(); } if (FormatJson.D.equals(nextName)) { if (reader.peek() == JsonToken.BEGIN_ARRAY) { nextName = FormatJson.RESULTS; } else { reader.beginObject(); openedObjects++; nextName = reader.nextName(); } } FeedMetadataImpl feedMetadata = new FeedMetadataImpl(); if (FormatJson.COUNT.equals(nextName)) { JsonFeedConsumer.readInlineCount(reader, feedMetadata); nextName = reader.nextName(); } if (FormatJson.RESULTS.equals(nextName)) { links = readLinksArray(reader); } else { throw new EntityProviderException(EntityProviderException.INVALID_CONTENT .addContent(FormatJson.RESULTS).addContent(nextName)); } if (reader.hasNext() && reader.peek() == JsonToken.NAME) { if (FormatJson.COUNT.equals(reader.nextName())) { JsonFeedConsumer.readInlineCount(reader, feedMetadata); } else { throw new EntityProviderException(EntityProviderException.INVALID_CONTENT .addContent(FormatJson.COUNT).addContent(nextName)); } } for (; openedObjects > 0; openedObjects--) { reader.endObject(); } reader.peek(); // to assert end of document } catch (final IOException e) { throw new EntityProviderException( EntityProviderException.EXCEPTION_OCCURRED.addContent(e.getClass().getSimpleName()), e); } catch (final IllegalStateException e) { throw new EntityProviderException( EntityProviderException.EXCEPTION_OCCURRED.addContent(e.getClass().getSimpleName()), e); } return links; }
From source file:com.sap.core.odata.core.ep.util.JsonUtils.java
License:Apache License
public static int startJson(final JsonReader reader) throws EntityProviderException { //The enclosing "d" and "results" are optional - so we cannot check for the presence //but we have to read over them in case they are present. JsonToken token;/*ww w. j a v a2 s . co m*/ try { token = reader.peek(); int openJsonObjects = 0; if (JsonToken.BEGIN_OBJECT == token) { reader.beginObject(); openJsonObjects++; token = reader.peek(); if (JsonToken.NAME == token) { String name = reader.nextName(); if (!("d".equals(name) ^ "results".equals(name))) { //TODO I18N throw new EntityProviderException(EntityProviderException.COMMON, name + " not expected, only d or results"); } } token = reader.peek(); if (JsonToken.BEGIN_OBJECT == token) { reader.beginObject(); openJsonObjects++; } else if (JsonToken.BEGIN_ARRAY == token) { //TODO I18N throw new EntityProviderException(EntityProviderException.COMMON, "Array not expected"); } } return openJsonObjects; } catch (IOException e) { //TODO I18N throw new EntityProviderException(EntityProviderException.COMMON, e); } }
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 {//from w w w. j av a2 s . c o 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 ww . j a v a 2 s .co 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 ww. j av a 2 s. com*/ 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
/** {@inheritDoc} */ @Override/*from ww w. j a v a 2s. co 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 {// w ww .jav 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"; }