List of usage examples for com.google.gson.stream JsonToken STRING
JsonToken STRING
To view the source code for com.google.gson.stream JsonToken STRING.
Click Source Link
From source file:ddt.dtool.util.JsonReaderExt.java
License:Open Source License
public String consumeStringValue() throws IOException { if (jsonReader.peek() != JsonToken.STRING) { throw new MalformedJsonException("Expected: " + JsonToken.STRING); }/* ww w.j a v a 2 s . c o m*/ return jsonReader.nextString(); }
From source file:ddt.dtool.util.JsonReaderExt.java
License:Open Source License
public ArrayList<String> consumeStringArray(boolean ignoreNulls) throws IOException { jsonReader.consumeExpected(JsonToken.BEGIN_ARRAY); ArrayList<String> strings = new ArrayList<>(); while (jsonReader.hasNext()) { JsonToken tokenType = jsonReader.peek(); if (ignoreNulls && tokenType == JsonToken.NULL) { jsonReader.nextNull();/*ww w . j a va 2 s . co m*/ continue; } if (tokenType != JsonToken.STRING) { sourceError("Expected String value, instead got: " + tokenType); } String entry = jsonReader.nextString(); strings.add(entry); } jsonReader.consumeExpected(JsonToken.END_ARRAY); return strings; }
From source file:json.tests.reader.JSONStreamReader.java
License:Open Source License
/** * {@inheritDoc}/*from w ww .j a va 2s. c o m*/ */ @Override public String nextString() throws IOException { if (this.currentToken == JsonToken.STRING || this.currentToken == JsonToken.NUMBER) { String tempElementValue; /* * {@link com.google.gson.JsonParser} reads all primitive values as String, * and then convert it to the corresponding type. Following block is to support * that requirement. */ if (this.currentElementValue instanceof Integer) { tempElementValue = Integer.toString((int) this.currentElementValue); } else if (this.currentElementValue instanceof Long) { tempElementValue = Long.toString((long) this.currentElementValue); } else if (this.currentElementValue instanceof Double) { tempElementValue = Double.toString((double) this.currentElementValue); } else if (this.currentElementValue instanceof Boolean) { tempElementValue = Boolean.toString((boolean) this.currentElementValue); } else { tempElementValue = (String) this.currentElementValue; } goToNextToken(false, false); return tempElementValue; } else { throw new IllegalStateException("Expected a STRING token but found " + this.currentToken); } }
From source file:net.bpiwowar.experimaestro.tasks.ClassChooserAdapter.java
License:Open Source License
@Override public Object read(JsonReader in) throws IOException { // If string, use this if (in.peek() == JsonToken.STRING) { final String type = in.nextString(); final Class<?> aClass = types.get(type); if (aClass == null) { throw new JsonParseException("No type " + type + " defined"); }//from ww w . j av a2 s . c o m return gson.fromJson(new JsonObject(), aClass); } // Get the Json object final JsonObject json; if (in instanceof JsonTreeReader) { json = ((JsonTreeReader) in).getJsonObject(); } else { json = new JsonParser().parse(in).getAsJsonObject(); } // Get the type final JsonElement _type = json.get("type"); if (_type == null) { throw new JsonParseException("No type defined"); } String type = _type.getAsString(); // Get the class of the object to create final Class<?> aClass = types.get(type); if (aClass == null) { throw new JsonParseException("No type " + type + " defined"); } return gson.fromJson(json, aClass); }
From source file:net.evecom.android.json.JsonSysOrgansData.java
License:Open Source License
public static ArrayList<MianPerson> getData(final String path, String entity_str) throws Exception { URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); byte[] entity = entity_str.getBytes(); conn.setConnectTimeout(5000);// www. j a v a2s . c o m conn.setDoOutput(true); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); conn.setRequestProperty("Content-Length", String.valueOf(entity.length)); conn.getOutputStream().write(entity); if (conn.getResponseCode() == 200) { InputStream inputstream = conn.getInputStream(); StringBuffer buffer = new StringBuffer(); byte[] b = new byte[1024]; for (int i; (i = inputstream.read(b)) != -1;) { buffer.append(new String(b, 0, i)); } StringReader reader = new StringReader(buffer.toString()); JsonReader jsonReader = new JsonReader(reader); list = new ArrayList<MianPerson>(); jsonReader.beginObject(); while (jsonReader.hasNext()) { String nextname1 = ""; if (jsonReader.peek() == JsonToken.NULL) { jsonReader.nextNull(); } else if (jsonReader.peek() == JsonToken.NUMBER) { nextname1 = jsonReader.nextInt() + ""; } else { nextname1 = jsonReader.nextName(); } if ("rows".equals(nextname1)) { jsonReader.beginArray(); while (jsonReader.hasNext()) { jsonReader.beginObject(); MianPerson person = new MianPerson(); while (jsonReader.hasNext()) { String nextName = ""; if (jsonReader.peek() == JsonToken.NULL) { jsonReader.nextNull(); } else if (jsonReader.peek() == JsonToken.NUMBER) { nextName = jsonReader.nextInt() + ""; } else if (jsonReader.peek() == JsonToken.STRING) { nextName = jsonReader.nextString() + ""; } else { nextName = jsonReader.nextName(); } String nextValue = ""; if (nextName.equals("IDCARDNO")) {// if (jsonReader.peek() == JsonToken.NULL) { jsonReader.nextNull(); } else if (jsonReader.peek() == JsonToken.NUMBER) { nextValue = jsonReader.nextInt() + ""; } else { nextValue = jsonReader.nextString(); } // nextValue = jsonReader.nextString(); person.setIDCARDNO(nextValue); } else if (nextName.equals("HOUSEHOLDID")) {// if (jsonReader.peek() == JsonToken.NULL) { jsonReader.nextNull(); } else if (jsonReader.peek() == JsonToken.NUMBER) { nextValue = jsonReader.nextInt() + ""; } else { nextValue = jsonReader.nextString(); } // nextValue = jsonReader.nextString(); person.setHOUSEHOLDID(nextValue); } else if (nextName.equals("AREAID")) {// id if (jsonReader.peek() == JsonToken.NULL) { jsonReader.nextNull(); } else if (jsonReader.peek() == JsonToken.NUMBER) { nextValue = jsonReader.nextInt() + ""; } else { nextValue = jsonReader.nextString(); } // nextValue = jsonReader.nextString(); person.setAREAID(nextValue); } else if (nextName.equals("AREANAME")) {// if (jsonReader.peek() == JsonToken.NULL) { jsonReader.nextNull(); } else if (jsonReader.peek() == JsonToken.NUMBER) { nextValue = jsonReader.nextInt() + ""; } else { nextValue = jsonReader.nextString(); } // nextValue = jsonReader.nextString(); person.setAREANAME(nextValue); } else if (nextName.equals("PERSONNAME")) {// if (jsonReader.peek() == JsonToken.NULL) { jsonReader.nextNull(); } else if (jsonReader.peek() == JsonToken.NUMBER) { nextValue = jsonReader.nextInt() + ""; } else { nextValue = jsonReader.nextString(); } // nextValue = jsonReader.nextString(); person.setPERSONNAME(nextValue); } else if (nextName.equals("MALEDICTID")) {// 1 // 2 if (jsonReader.peek() == JsonToken.NULL) { jsonReader.nextNull(); } else if (jsonReader.peek() == JsonToken.NUMBER) { nextValue = jsonReader.nextInt() + ""; } else { nextValue = jsonReader.nextString(); } // nextValue = jsonReader.nextString(); if (null != nextValue && "1".equals(nextValue)) { person.setMALEDICTID(""); } else if (null != nextValue && "2".equals(nextValue)) { person.setMALEDICTID(""); } } else if (nextName.equals("BIRTH")) {// if (jsonReader.peek() == JsonToken.NULL) { jsonReader.nextNull(); } else if (jsonReader.peek() == JsonToken.NUMBER) { nextValue = jsonReader.nextInt() + ""; } else { nextValue = jsonReader.nextString(); } // nextValue = jsonReader.nextString(); person.setBIRTH(nextValue); } else if (nextName.equals("HOUSEADDR")) {// if (jsonReader.peek() == JsonToken.NULL) { jsonReader.nextNull(); } else if (jsonReader.peek() == JsonToken.NUMBER) { nextValue = jsonReader.nextInt() + ""; } else { nextValue = jsonReader.nextString(); } // nextValue = jsonReader.nextString(); person.setHOUSEADDR(nextValue); } else if (nextName.equals("PERSONID")) {// id if (jsonReader.peek() == JsonToken.NULL) { jsonReader.nextNull(); } else if (jsonReader.peek() == JsonToken.NUMBER) { nextValue = jsonReader.nextInt() + ""; } else { nextValue = jsonReader.nextString(); } // nextValue = jsonReader.nextString(); person.setPERSONID(nextValue); } System.out.println(nextName + "=" + nextValue); } list.add(person); person = null; jsonReader.endObject(); } jsonReader.endArray(); } // } jsonReader.endObject(); // XmlPullParser xml = Xml.newPullParser(); // xml.setInput(inputstream, "UTF-8"); // int event = xml.getEventType(); // while (event != XmlPullParser.END_DOCUMENT) { // switch (event) { // // // case XmlPullParser.START_DOCUMENT: // list = new ArrayList<SysOrgan>(); // break; // case XmlPullParser.START_TAG: // // String value = xml.getName(); // if (value.equals("QY")) { // organ = new SysOrgan(); // } else if (value.equals("SO_ID")) { // organ.setSoId(xml.nextText()); // } else if (value.equals("so_Name")) { // organ.setSoName(xml.nextText()); // } // break; // case XmlPullParser.END_TAG: // if (xml.getName().equals("QY")) { // list.add(organ); // organ = null; // } // break; // default: // break; // } // // // event = xml.next(); // } return list; } else { return null; } }
From source file:org.apache.axis2.json.gson.GsonXMLStreamReader.java
License:Apache License
private void readName() throws IOException, XMLStreamException { nextName();//from w ww.j av a2 s .co m tokenType = jsonReader.peek(); if (tokenType == JsonToken.BEGIN_OBJECT) { beginObject(); state = JsonState.NameName; } else if (tokenType == JsonToken.BEGIN_ARRAY) { beginArray(); tokenType = jsonReader.peek(); if (tokenType == JsonToken.BEGIN_OBJECT) { beginObject(); state = JsonState.NameName; } else { state = JsonState.NameValue; } } else if (tokenType == JsonToken.STRING || tokenType == JsonToken.NUMBER || tokenType == JsonToken.BOOLEAN || tokenType == JsonToken.NULL) { state = JsonState.NameValue; } }
From source file:org.apache.axis2.json.gson.GsonXMLStreamReader.java
License:Apache License
private void readValue() throws IOException { nextValue();//from w w w. j a v a 2 s .co m tokenType = jsonReader.peek(); if (tokenType == JsonToken.NAME) { state = JsonState.ValueName_END; } else if (tokenType == JsonToken.STRING || tokenType == JsonToken.NUMBER || tokenType == JsonToken.BOOLEAN || tokenType == JsonToken.NULL) { state = JsonState.ValueValue_END; } else if (tokenType == JsonToken.END_ARRAY) { state = JsonState.ValueEndArray; } else if (tokenType == JsonToken.END_OBJECT) { state = JsonState.ValueEndObject_END_1; } }
From source file:org.apache.axis2.json.gson.GsonXMLStreamReader.java
License:Apache License
private String nextValue() { try {/* w w w. j a v a2 s . c om*/ tokenType = jsonReader.peek(); JsonObject peek = stackObj.peek(); String valueType = peek.getValueType(); if (!validateArgumentTypes(tokenType, valueType)) { log.error("Value type miss match, Expected value type - '" + valueType + "', but found - '" + tokenType.toString() + "'"); throw new IllegalArgumentException("Value type miss match, Expected value type - '" + valueType + "', but found - '" + tokenType.toString() + "'"); } if (tokenType == JsonToken.STRING) { value = jsonReader.nextString(); } else if (tokenType == JsonToken.BOOLEAN) { value = String.valueOf(jsonReader.nextBoolean()); } else if (tokenType == JsonToken.NUMBER) { if (valueType.equals("int")) { value = String.valueOf(jsonReader.nextInt()); } else if (valueType.equals("long")) { value = String.valueOf(jsonReader.nextLong()); } else if (valueType.equals("double")) { value = String.valueOf(jsonReader.nextDouble()); } else if (valueType.equals("float")) { value = String.valueOf(jsonReader.nextDouble()); } } else if (tokenType == JsonToken.NULL) { jsonReader.nextNull(); value = null; } else { log.error("Couldn't read the value, Illegal state exception"); throw new RuntimeException("Couldn't read the value, Illegal state exception"); } } catch (IOException e) { log.error("IO error while reading json stream"); throw new RuntimeException("IO error while reading json stream"); } return value; }
From source file:org.apache.carbondata.core.metadata.datatype.DataTypeAdapter.java
License:Apache License
@Override public Object read(JsonReader jsonReader) throws IOException { JsonToken token = jsonReader.peek(); if (token == JsonToken.STRING) { return DataTypeUtil.valueOf(jsonReader.nextString()); } else {/*from ww w . ja va 2 s. c o m*/ // use original deserializer logic return fallBack_original.fromJson(jsonReader, DataType.class); } }
From source file:org.apache.olingo.odata2.core.ep.consumer.JsonErrorDocumentConsumer.java
License:Apache License
private void parseInnerError(final JsonReader reader, final ODataErrorContext errorContext) throws IOException { JsonToken token = reader.peek();/* www .java 2 s.com*/ if (token == JsonToken.STRING) { // implementation for parse content as provided by JsonErrorDocumentProducer String innerError = reader.nextString(); errorContext.setInnerError(innerError); } else if (token == JsonToken.BEGIN_OBJECT) { // implementation for OData v2 Section 2.2.8.1.2 JSON Error Response // (RFC4627 Section 2.2 -> https://www.ietf.org/rfc/rfc4627.txt)) // currently partial provided errorContext.setInnerError(readJson(reader)); } }