Example usage for com.google.gson.stream JsonToken NUMBER

List of usage examples for com.google.gson.stream JsonToken NUMBER

Introduction

In this page you can find the example usage for com.google.gson.stream JsonToken NUMBER.

Prototype

JsonToken NUMBER

To view the source code for com.google.gson.stream JsonToken NUMBER.

Click Source Link

Document

A JSON number represented in this API by a Java double , long , or int .

Usage

From source file:com.ibm.watson.developer_cloud.speech_to_text.v1.util.SpeechWordConfidenceTypeAdapter.java

License:Open Source License

@Override
public SpeechWordConfidence read(JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();//from w  w  w.jav a2s .c o m
        return null;
    }

    final SpeechWordConfidence speechWordConfidence = new SpeechWordConfidence();
    reader.beginArray();

    if (reader.peek() == JsonToken.STRING) {
        speechWordConfidence.setWord(reader.nextString());
    }
    if (reader.peek() == JsonToken.NUMBER) {
        speechWordConfidence.setConfidence(reader.nextDouble());
    }

    reader.endArray();
    return speechWordConfidence;
}

From source file:com.magnet.android.mms.request.marshall.GsonStreamReader.java

License:Open Source License

@Override
public String getAttributeContent(Class<?> type) throws MarshallingException {
    String result;// ww  w.j a v a  2  s  . c  o  m
    JsonToken tokenType = getJsonToken();
    if (tokenType == JsonToken.NULL) {
        try {
            jr.nextNull();
            return null;
        } catch (Exception e) {
            throw new MarshallingException(e);
        }
    }
    try {
        if (type == String.class) {
            result = jr.nextString();
        } else if (type == Boolean.class || type == boolean.class) {
            if (tokenType == JsonToken.BOOLEAN) {
                result = String.valueOf(jr.nextBoolean());
                return result;
            }
            if (tokenType == JsonToken.NUMBER) {
                result = String.valueOf(jr.nextInt());
                return result;
            }
            result = jr.nextString();
        } else if (type == int.class) {
            result = String.valueOf(jr.nextInt());
        } else if (type == long.class) {
            result = String.valueOf(jr.nextLong());
        } else if (type == double.class) {
            result = String.valueOf(jr.nextDouble());
        } else {
            result = jr.nextString();
        }
    } catch (Exception e) {
        throw new MarshallingException(e);
    }

    return result;
}

From source file:com.pcloud.sdk.internal.networking.serialization.DateTypeAdapter.java

License:Apache License

@Override
public Date read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NUMBER) {
        return new Date(in.nextLong() * 1000);
    }/*from  w  w w  .ja  v a 2s.c o m*/
    return null;
}

From source file:com.sap.core.odata.core.ep.consumer.JsonPropertyConsumer.java

License:Apache License

private Object readSimpleProperty(final JsonReader reader, final EntityPropertyInfo entityPropertyInfo,
        final Object typeMapping) throws EdmException, EntityProviderException, IOException {
    final EdmSimpleType type = (EdmSimpleType) entityPropertyInfo.getType();
    Object value = null;//from  ww  w .  j ava 2 s .  c o  m
    final JsonToken tokenType = reader.peek();
    if (tokenType == JsonToken.NULL) {
        reader.nextNull();
    } else {
        switch (EdmSimpleTypeKind.valueOf(type.getName())) {
        case Boolean:
            if (tokenType == JsonToken.BOOLEAN) {
                value = reader.nextBoolean();
                value = value.toString();
            } else {
                throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE
                        .addContent(entityPropertyInfo.getName()));
            }
            break;
        case Byte:
        case SByte:
        case Int16:
        case Int32:
            if (tokenType == JsonToken.NUMBER) {
                value = reader.nextInt();
                value = value.toString();
            } else {
                throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE
                        .addContent(entityPropertyInfo.getName()));
            }
            break;
        default:
            if (tokenType == JsonToken.STRING) {
                value = reader.nextString();
            } else {
                throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE
                        .addContent(entityPropertyInfo.getName()));
            }
            break;
        }
    }

    final Class<?> typeMappingClass = typeMapping == null ? type.getDefaultType() : (Class<?>) typeMapping;
    return type.valueOfString((String) value, EdmLiteralKind.JSON, entityPropertyInfo.getFacets(),
            typeMappingClass);
}

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  ww  . j  a  va 2  s  . com
 */
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:json.tests.reader.JSONStreamReader.java

License:Open Source License

/**
 * {@inheritDoc}//  ww w. j a  v a  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:json.tests.reader.JSONStreamReader.java

License:Open Source License

/**
 * {@inheritDoc}/*from   ww  w .  j  a va  2 s .  c o m*/
 */
@Override
public double nextDouble() throws IOException {
    if (this.currentToken == JsonToken.NUMBER) {
        double tempElementValue = Double.parseDouble((String) this.currentElementValue);
        goToNextToken(false, false);
        return tempElementValue;
    } else {
        throw new IllegalStateException("Expected a NUMBER token but found " + this.currentToken);
    }
}

From source file:json.tests.reader.JSONStreamReader.java

License:Open Source License

/**
 * {@inheritDoc}//  w w w  .  j ava  2  s  .co  m
 */
@Override
public long nextLong() throws IOException {
    if (this.currentToken == JsonToken.NUMBER) {
        long tempElementValue = Long.parseLong((String) this.currentElementValue);
        goToNextToken(false, false);
        return tempElementValue;
    } else {
        throw new IllegalStateException("Expected a NUMBER token but found " + this.currentToken);
    }
}

From source file:json.tests.reader.JSONStreamReader.java

License:Open Source License

/**
 * {@inheritDoc}//  www. jav a  2s .c  om
 */
@Override
public int nextInt() throws IOException {
    if (this.currentToken == JsonToken.NUMBER) {
        int tempElementValue = Integer.parseInt((String) this.currentElementValue);
        goToNextToken(false, false);
        return tempElementValue;
    } else {
        throw new IllegalStateException("Expected a NUMBER token but found " + this.currentToken);
    }
}

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);//  w  w w . j a  v  a2  s.co  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;
    }
}