List of usage examples for com.google.gson.stream JsonToken NAME
JsonToken NAME
To view the source code for com.google.gson.stream JsonToken NAME.
Click Source Link
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;/*from w w w . ja va2s .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.solidfire.jsvcgen.serialization.ApiServerExceptionTypeAdapter.java
License:Open Source License
/** * {@inheritDoc}/* w w w . j a v a 2s . c o m*/ */ @Override public ApiServerException read(JsonReader in) throws IOException { String name = null; String code = null; String message = null; in.beginObject(); while (in.hasNext()) { if (in.peek() != JsonToken.NAME) { in.skipValue(); continue; } switch (in.nextName()) { case "name": name = in.nextString(); break; case "code": code = in.nextString(); break; case "message": message = in.nextString(); break; } } in.endObject(); return new ApiServerException(name, code, message); }
From source file:com.splunk.ResultsReaderJson.java
License:Apache License
/** * Skip the next value, whether it is atomic or compound, in the JSON * stream./*from w w w . j av 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 ww . j a v a2s. c o m 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/*www . java 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:com.squareup.wire.MessageTypeAdapter.java
License:Apache License
@SuppressWarnings("unchecked") @Override/* www. ja va 2 s. co m*/ public M read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { in.nextNull(); return null; } MessageAdapter<M> messageAdapter = wire.messageAdapter(type); Message.Builder<M> builder = messageAdapter.newBuilder(); in.beginObject(); while (in.peek() == JsonToken.NAME) { String name = in.nextName(); MessageAdapter.FieldInfo fieldInfo = messageAdapter.getField(name); if (fieldInfo == null) { Extension<?, ?> extension = messageAdapter.getExtension(name); if (extension == null) { parseUnknownField(in, builder, Integer.parseInt(name)); } else { Type valueType = getType(extension); Object value = parseValue(extension.getLabel(), valueType, parse(in)); ((ExtendableMessage.ExtendableBuilder) builder).setExtension(extension, value); } } else { Type valueType = getType(fieldInfo); Object value = parseValue(fieldInfo.label, valueType, parse(in)); // Use the builder setter method to ensure proper 'oneof' behavior. messageAdapter.setBuilderMethod(builder, fieldInfo, value); } } in.endObject(); return builder.build(); }
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. ja v 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 ww w. java2s. co m } } else { jsonParser.errorUnexpected(tokenType); } } jsonParser.consumeExpected(JsonToken.END_OBJECT); }
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();// w ww. j ava2s. c o m } } else { jsonReader.errorUnexpected(tokenType); } } jsonReader.consumeExpected(JsonToken.END_OBJECT); return this; }
From source file:ddt.dtool.util.JsonReaderExt.java
License:Open Source License
public String consumeExpectedPropName() throws IOException { JsonToken tokenType = jsonReader.peek(); if (tokenType != JsonToken.NAME) { jsonReader.sourceError("Expected property name, instead got: " + tokenType); }/*from w ww . j a v a2 s . c o m*/ // Note: there is a bug in nextName, it throws IllegalStateException insteand of IOE // so that's why we check peek ourselves. return jsonReader.nextName(); }