Example usage for com.google.gson.stream JsonReader nextLong

List of usage examples for com.google.gson.stream JsonReader nextLong

Introduction

In this page you can find the example usage for com.google.gson.stream JsonReader nextLong.

Prototype

public long nextLong() throws IOException 

Source Link

Document

Returns the com.google.gson.stream.JsonToken#NUMBER long value of the next token, consuming it.

Usage

From source file:at.yawk.mojangapi.InstantTypeAdapter.java

License:Mozilla Public License

@Override
public Instant read(JsonReader in) throws IOException {
    if (in.peek() == JsonToken.NULL) {
        return null;
    } else {/*www  .  ja  va 2  s. co  m*/
        return Instant.ofEpochMilli(in.nextLong());
    }
}

From source file:co.cask.cdap.common.stream.StreamEventTypeAdapter.java

License:Apache License

@Override
public StreamEvent read(JsonReader in) throws IOException {
    long timestamp = -1;
    Map<String, String> headers = null;
    ByteBuffer body = null;//from  w  w w  .  j  a  v a2  s. c o m

    in.beginObject();
    while (in.peek() == JsonToken.NAME) {
        String key = in.nextName();
        if ("timestamp".equals(key)) {
            timestamp = in.nextLong();
        } else if ("headers".equals(key)) {
            headers = mapTypeAdapter.read(in);
        } else if ("body".equals(key)) {
            body = ByteBuffer.wrap(Bytes.toBytesBinary(in.nextString()));
        } else {
            in.skipValue();
        }
    }

    if (timestamp >= 0 && headers != null && body != null) {
        in.endObject();
        return new StreamEvent(headers, body, timestamp);
    }
    throw new IOException(String.format("Failed to read StreamEvent. Timestamp: %d, headers: %s, body: %s",
            timestamp, headers, body));
}

From source file:co.cask.cdap.format.StructuredRecordStringConverter.java

License:Apache License

private static Object readJson(JsonReader reader, Schema schema) throws IOException {
    switch (schema.getType()) {
    case NULL://  w  w  w .  j  av a  2s  .co m
        reader.nextNull();
        return null;
    case BOOLEAN:
        return reader.nextBoolean();
    case INT:
        return reader.nextInt();
    case LONG:
        return reader.nextLong();
    case FLOAT:
        // Force down cast
        return (float) reader.nextDouble();
    case DOUBLE:
        return reader.nextDouble();
    case BYTES:
        return readBytes(reader);
    case STRING:
        return reader.nextString();
    case ENUM:
        // Currently there is no standard container to represent enum type
        return reader.nextString();
    case ARRAY:
        return readArray(reader, schema.getComponentSchema());
    case MAP:
        return readMap(reader, schema.getMapSchema());
    case RECORD:
        return readRecord(reader, schema);
    case UNION:
        return readUnion(reader, schema);
    }

    throw new IOException("Unsupported schema: " + schema);
}

From source file:com.aliyun.openservices.odps.console.common.JobDetailInfo.java

License:Apache License

private FuxiInstance getFuxiInstanceFromJson(JsonReader reader) throws IOException {
    FuxiInstance inst = new FuxiInstance();

    reader.beginObject();//from   www  . j a v a2s  . com
    long endTime = 0;
    while (reader.hasNext()) {
        String nameInInstance = reader.nextName();
        if (nameInInstance.equals("id")) {
            inst.id = reader.nextString();
        } else if (nameInInstance.equals("logId")) {
            inst.logid = reader.nextString();
            inst.IpAndPath = this.decodeLogId(inst.logid);
        } else if (nameInInstance.equals("startTime")) {
            inst.startTime = reader.nextLong();
        } else if (nameInInstance.equals("endTime")) {
            endTime = reader.nextLong();
        } else if (nameInInstance.equals("status")) {
            inst.status = reader.nextString();
        } else {
            reader.skipValue();
        }
    }
    inst.duration = (int) (endTime - inst.startTime);
    reader.endObject();
    return inst;
}

From source file:com.bzcentre.dapiPush.ReceipientTypeAdapter.java

License:Open Source License

private MeetingPayload extractPayload(JsonReader in)
        throws IOException, NumberFormatException, IllegalStateException, JsonParseException {
    NginxClojureRT.log.debug(TAG + "TypeAdapter extracting Payload...");

    MeetingPayload meetingPayload = new MeetingPayload();
    if (in.peek() == JsonToken.NULL) {
        in.nextNull();/*from   ww  w . j a  v a2  s. com*/
        throw new JsonParseException("null Payload");
    }

    in.beginObject();
    while (in.hasNext()) {
        switch (in.nextName()) {
        case "aps":
            in.beginObject();
            while (in.hasNext()) {
                switch (in.nextName()) {
                case "badge":
                    meetingPayload.getAps().setBadge(in.nextLong());
                    break;
                case "sound":
                    meetingPayload.getAps().setSound(in.nextString());
                    break;
                case "alert":
                    in.beginObject();
                    while (in.hasNext()) {
                        switch (in.nextName()) {
                        case "title":
                            meetingPayload.getAps().getAlert().setTitle(in.nextString());
                            break;
                        case "body":
                            meetingPayload.getAps().getAlert().setBody(in.nextString());
                            break;
                        case "action-loc-key":
                            meetingPayload.getAps().getAlert().setActionLocKey(in.nextString());
                            break;
                        }
                    }
                    in.endObject();
                    break;
                }
            }
            in.endObject();
            break;
        case "dapi":
            meetingPayload.setDapi(in.nextString());
            break;
        case "acme1":
            meetingPayload.setAcme1(in.nextString());
            break;
        case "acme2":
            meetingPayload.setAcme2(in.nextLong());
            break;
        case "acme3":
            meetingPayload.setAcme3(in.nextLong());
            break;
        case "acme4":
            NginxClojureRT.log.info(TAG + "TypeAdapter Reader is reading acme4...");
            meetingPayload.setAcme4(in.nextLong());
            break;
        case "acme5":
            meetingPayload.setAcme5(in.nextLong());
            break;
        case "acme6":
            meetingPayload.setAcme6(in.nextLong());
            break;
        case "acme7":
            ArrayList<String> attendees = new ArrayList<>();
            in.beginArray();
            while (in.hasNext()) {
                attendees.add(in.nextString());
            }
            in.endArray();
            meetingPayload.setAcme7(attendees);
            break;
        case "acme8":
            meetingPayload.setAcme8(in.nextString());
            break;
        }
    }
    in.endObject();

    return meetingPayload;
}

From source file:com.epickrram.romero.testing.server.web.CompletedJobRunIdentifierTypeAdapter.java

License:Apache License

@Override
public CompletedJobRunIdentifier read(final JsonReader jsonReader) throws IOException {
    jsonReader.beginObject();//from   w w w. j  a  v a  2 s.  com
    final CompletedJobRunIdentifier.Builder builder = new CompletedJobRunIdentifier.Builder();
    while (jsonReader.hasNext()) {
        final String name = jsonReader.nextName();
        if ("jobRunIdentifier".equals(name)) {
            builder.jobRunIdentifier(jsonReader.nextString());
        } else if ("startTimestamp".equals(name)) {
            builder.startTimestamp(jsonReader.nextLong());
        }
    }
    jsonReader.endObject();
    return builder.create();
}

From source file:com.gelakinetic.mtgfam.helpers.JsonParser.java

License:Open Source License

public void readCardJsonStream(InputStream in, CardProgressReporter progReport, String setName,
        CardDbAdapter mDbHelper, Context context) throws IOException {
    String dialogText = String.format(context.getString(R.string.update_parse_cards), setName);

    JsonReader reader = new JsonReader(new InputStreamReader(in, "ISO-8859-1"));
    String s, s1, s2, ptstr;/*from w w w  . j  a v  a2 s .  c o  m*/

    int numTotalElements = 0;
    int elementsParsed = 0;

    reader.beginObject();
    s = reader.nextName();

    ArrayList<MtgSet> setsAdded = new ArrayList<MtgSet>();

    progReport.reportJsonCardProgress("determinate", "");
    reader.beginObject();
    while (reader.hasNext()) {

        s = reader.nextName();
        if (s.equalsIgnoreCase("v")) { // bdd_date
            reader.skipValue();
        }
        if (s.equalsIgnoreCase("u")) { // bdd_version
            reader.skipValue();
        }
        if (s.equalsIgnoreCase("s")) { // sets
            reader.beginObject();
            while (reader.hasNext()) {
                s1 = reader.nextName();
                if (s1.equalsIgnoreCase("b")) { // set
                    MtgSet set;

                    JsonToken jt = reader.peek();
                    if (jt.equals(JsonToken.BEGIN_OBJECT)) {
                        set = new MtgSet();
                        reader.beginObject();
                        while (reader.hasNext()) {
                            s2 = reader.nextName();
                            if (s2.equalsIgnoreCase("a")) { // name
                                set.name = reader.nextString();
                            }
                            if (s2.equalsIgnoreCase("r")) { // code_magiccards
                                set.code_magiccards = reader.nextString();
                            }
                            if (s2.equalsIgnoreCase("q")) { // code
                                set.code = reader.nextString();
                            }
                            if (s2.equalsIgnoreCase("y")) { // date
                                set.date = reader.nextLong();
                            }
                        }
                        setsAdded.add(set);
                        reader.endObject();
                    } else if (jt.equals(JsonToken.BEGIN_ARRAY)) {
                        reader.beginArray();
                        while (reader.hasNext()) {
                            set = new MtgSet();
                            reader.beginObject();
                            while (reader.hasNext()) {
                                s2 = reader.nextName();
                                if (s2.equalsIgnoreCase("a")) { // name
                                    set.name = reader.nextString();
                                }
                                if (s2.equalsIgnoreCase("r")) { // code_magiccards
                                    set.code_magiccards = reader.nextString();
                                }
                                if (s2.equalsIgnoreCase("q")) { // code
                                    set.code = reader.nextString();
                                }
                                if (s2.equalsIgnoreCase("y")) { // date
                                    set.date = reader.nextLong();
                                }
                            }
                            setsAdded.add(set);
                            reader.endObject();
                        }
                        reader.endArray();
                    }
                }
            }
            reader.endObject();
        }
        if (s.equalsIgnoreCase("p")) { // cards

            reader.beginObject();
            while (reader.hasNext()) {
                s1 = reader.nextName();
                if (s1.equalsIgnoreCase("o")) { // card
                    MtgCard c;
                    reader.beginArray();
                    while (reader.hasNext()) {

                        reader.beginObject();
                        c = new MtgCard();
                        while (reader.hasNext()) {
                            s2 = reader.nextName();
                            if (s2.equalsIgnoreCase("a")) { // name
                                c.name = reader.nextString();
                            } else if (s2.equalsIgnoreCase("b")) { // set
                                c.set = reader.nextString();
                            } else if (s2.equalsIgnoreCase("c")) { // type
                                c.type = reader.nextString();
                            } else if (s2.equalsIgnoreCase("d")) { // rarity
                                c.rarity = reader.nextString().charAt(0);
                            } else if (s2.equalsIgnoreCase("e")) { // manacost
                                c.manacost = reader.nextString();
                            } else if (s2.equalsIgnoreCase("f")) { // converted_manacost
                                try {
                                    c.cmc = reader.nextInt();
                                } catch (Exception e) {
                                    reader.skipValue();
                                }

                            } else if (s2.equalsIgnoreCase("g")) { // power
                                try {
                                    ptstr = reader.nextString();
                                    try {
                                        c.power = Integer.parseInt(ptstr);
                                    } catch (NumberFormatException e) {
                                        if (ptstr.equals("*")) {
                                            c.power = CardDbAdapter.STAR;
                                        } else if (ptstr.equals("1+*")) {
                                            c.power = CardDbAdapter.ONEPLUSSTAR;
                                        } else if (ptstr.equals("2+*")) {
                                            c.power = CardDbAdapter.TWOPLUSSTAR;
                                        } else if (ptstr.equals("7-*")) {
                                            c.power = CardDbAdapter.SEVENMINUSSTAR;
                                        } else if (ptstr.equals("*{^2}")) {
                                            c.power = CardDbAdapter.STARSQUARED;
                                        } else if (ptstr.equals("{1/2}")) {
                                            c.power = 0.5f;
                                        } else if (ptstr.equals("1{1/2}")) {
                                            c.power = 1.5f;
                                        } else if (ptstr.equals("2{1/2}")) {
                                            c.power = 2.5f;
                                        } else if (ptstr.equals("3{1/2}")) {
                                            c.power = 3.5f;
                                        }
                                    }
                                } catch (Exception e) {
                                    reader.skipValue();
                                }
                            } else if (s2.equalsIgnoreCase("h")) { // toughness
                                try {
                                    ptstr = reader.nextString();
                                    try {
                                        c.toughness = Integer.parseInt(ptstr);
                                    } catch (NumberFormatException e) {
                                        if (ptstr.equals("*")) {
                                            c.toughness = CardDbAdapter.STAR;
                                        } else if (ptstr.equals("1+*")) {
                                            c.toughness = CardDbAdapter.ONEPLUSSTAR;
                                        } else if (ptstr.equals("2+*")) {
                                            c.toughness = CardDbAdapter.TWOPLUSSTAR;
                                        } else if (ptstr.equals("7-*")) {
                                            c.toughness = CardDbAdapter.SEVENMINUSSTAR;
                                        } else if (ptstr.equals("*{^2}")) {
                                            c.toughness = CardDbAdapter.STARSQUARED;
                                        } else if (ptstr.equals("{1/2}")) {
                                            c.toughness = 0.5f;
                                        } else if (ptstr.equals("1{1/2}")) {
                                            c.toughness = 1.5f;
                                        } else if (ptstr.equals("2{1/2}")) {
                                            c.toughness = 2.5f;
                                        } else if (ptstr.equals("3{1/2}")) {
                                            c.toughness = 3.5f;
                                        }
                                    }
                                } catch (Exception e) {
                                    reader.skipValue();
                                }
                            } else if (s2.equalsIgnoreCase("i")) { // loyalty
                                try {
                                    c.loyalty = reader.nextInt();
                                } catch (Exception e) {
                                    reader.skipValue();
                                }
                            } else if (s2.equalsIgnoreCase("j")) { // ability
                                c.ability = reader.nextString();
                            } else if (s2.equalsIgnoreCase("k")) { // flavor
                                c.flavor = reader.nextString();
                            } else if (s2.equalsIgnoreCase("l")) { // artist
                                c.artist = reader.nextString();
                            } else if (s2.equalsIgnoreCase("m")) { // number
                                try {
                                    c.number = reader.nextString();
                                } catch (Exception e) {
                                    reader.skipValue();
                                }
                            } else if (s2.equalsIgnoreCase("n")) { // color
                                c.color = reader.nextString();
                            } else if (s2.equalsIgnoreCase("x")) { // multiverse id
                                c.multiverse_id = reader.nextInt();
                            }
                        }
                        mDbHelper.createCard(c);
                        elementsParsed++;
                        progReport.reportJsonCardProgress(new String[] { dialogText, dialogText,
                                "" + (int) Math.round(100 * elementsParsed / (double) numTotalElements) });
                        reader.endObject();
                    }
                    reader.endArray();
                }
            }
            reader.endObject();
        }
        if (s.equalsIgnoreCase("w")) { // num_cards
            numTotalElements = reader.nextInt();
        }
    }
    reader.endObject();
    reader.close();

    // Add the set information to the database AFTER adding the cards.
    // This way if the update fails while parsing cards, the database won't think it has the set already, when it doesnt.
    for (MtgSet set : setsAdded) {
        mDbHelper.createSet(set);
    }

    return;
}

From source file:com.gelakinetic.mtgfam.helpers.updaters.CardAndSetParser.java

License:Open Source License

/**
 * If a set has a patch, and doesn't exist in the database, this is called to parse an InputStream of JSON and add
 * it into the database./*from   w ww. j  a  va2  s  .  c  om*/
 * <p/>
 * The JSON uses single character keys, which is a silly thing I did in the name of compression. The patches are
 * zipped anyway, so it doesn't matter much, but we're stuck with it.
 * <p/>
 * There is some special processing for weird power and toughness too
 *
 * @param in               An InputStream containing valid JSON
 * @param progressReporter A percentage progress is reported through this class to be shown in the notification
 * @param cardsToAdd       An array list to place cards before adding to the database
 * @param setsToAdd        An array list to place sets before adding to the database
 * @throws IOException If something goes wrong with the InputStream, this will be thrown
 */
public void readCardJsonStream(InputStream in, CardProgressReporter progressReporter,
        ArrayList<MtgCard> cardsToAdd, ArrayList<MtgSet> setsToAdd) throws IOException {

    JsonReader reader = new JsonReader(new InputStreamReader(in, "ISO-8859-1"));
    /* Three levels of strings for parsing nested JSON */
    String s, s1, s2;
    String pouTouStr;

    int numTotalElements = 0;
    int elementsParsed = 0;

    reader.beginObject();
    reader.nextName();
    reader.beginObject();

    while (reader.hasNext()) {

        s = reader.nextName();
        if (s.equalsIgnoreCase("v")) { /* bdd_date */
            reader.skipValue();
        }
        if (s.equalsIgnoreCase("u")) { /* bdd_version */
            reader.skipValue();
        }
        if (s.equalsIgnoreCase("s")) { /* sets */
            reader.beginObject();
            while (reader.hasNext()) {
                s1 = reader.nextName();
                if (s1.equalsIgnoreCase("b")) { /* set */
                    MtgSet set;

                    JsonToken jt = reader.peek();
                    if (jt.equals(JsonToken.BEGIN_OBJECT)) {
                        set = new MtgSet();
                        reader.beginObject();
                        while (reader.hasNext()) {
                            s2 = reader.nextName();
                            if (s2.equalsIgnoreCase("a")) { /* name */
                                set.name = reader.nextString();
                            }
                            if (s2.equalsIgnoreCase("r")) { /* code_magicCards */
                                set.codeMagicCards = reader.nextString();
                            }
                            if (s2.equalsIgnoreCase("q")) { /* code */
                                set.code = reader.nextString();
                            }
                            if (s2.equalsIgnoreCase("y")) { /* date */
                                set.date = reader.nextLong();
                            }
                        }
                        setsToAdd.add(set);
                        reader.endObject();
                    } else if (jt.equals(JsonToken.BEGIN_ARRAY)) {
                        reader.beginArray();
                        while (reader.hasNext()) {
                            set = new MtgSet();
                            reader.beginObject();
                            while (reader.hasNext()) {
                                s2 = reader.nextName();
                                if (s2.equalsIgnoreCase("a")) { /* name */
                                    set.name = reader.nextString();
                                }
                                if (s2.equalsIgnoreCase("r")) { /* code_magicCards */
                                    set.codeMagicCards = reader.nextString();
                                }
                                if (s2.equalsIgnoreCase("q")) { /* code */
                                    set.code = reader.nextString();
                                }
                                if (s2.equalsIgnoreCase("y")) { /* date */
                                    set.date = reader.nextLong();
                                }
                            }
                            setsToAdd.add(set);
                            reader.endObject();
                        }
                        reader.endArray();
                    }
                }
            }
            reader.endObject();
        }
        if (s.equalsIgnoreCase("p")) { /* cards */

            reader.beginObject();
            while (reader.hasNext()) {
                s1 = reader.nextName();
                if (s1.equalsIgnoreCase("o")) { /* card */
                    MtgCard c;
                    reader.beginArray();
                    while (reader.hasNext()) {

                        reader.beginObject();
                        c = new MtgCard();
                        while (reader.hasNext()) {
                            s2 = reader.nextName();
                            if (s2.equalsIgnoreCase("a")) { /* name */
                                c.name = reader.nextString();
                            } else if (s2.equalsIgnoreCase("b")) { /* set */
                                c.set = reader.nextString();
                            } else if (s2.equalsIgnoreCase("c")) { /* type */
                                c.type = reader.nextString();
                            } else if (s2.equalsIgnoreCase("d")) { /* rarity */
                                c.rarity = reader.nextString().charAt(0);
                            } else if (s2.equalsIgnoreCase("e")) { /* manaCost */
                                c.manaCost = reader.nextString();
                            } else if (s2.equalsIgnoreCase("f")) { /* converted_manaCost */
                                try {
                                    c.cmc = reader.nextInt();
                                } catch (NumberFormatException e) {
                                    reader.skipValue();
                                }
                            } else if (s2.equalsIgnoreCase("g")) { /* power */
                                pouTouStr = reader.nextString();
                                try {
                                    c.power = Integer.parseInt(pouTouStr);
                                } catch (NumberFormatException e) {
                                    switch (pouTouStr) {
                                    case "*":
                                        c.power = CardDbAdapter.STAR;
                                        break;
                                    case "1+*":
                                        c.power = CardDbAdapter.ONE_PLUS_STAR;
                                        break;
                                    case "2+*":
                                        c.power = CardDbAdapter.TWO_PLUS_STAR;
                                        break;
                                    case "7-*":
                                        c.power = CardDbAdapter.SEVEN_MINUS_STAR;
                                        break;
                                    case "*{^2}":
                                        c.power = CardDbAdapter.STAR_SQUARED;
                                        break;
                                    case "{1/2}":
                                        c.power = 0.5f;
                                        break;
                                    case "1{1/2}":
                                        c.power = 1.5f;
                                        break;
                                    case "2{1/2}":
                                        c.power = 2.5f;
                                        break;
                                    case "3{1/2}":
                                        c.power = 3.5f;
                                        break;
                                    }
                                }
                            } else if (s2.equalsIgnoreCase("h")) { /* toughness */
                                pouTouStr = reader.nextString();
                                try {
                                    c.toughness = Integer.parseInt(pouTouStr);
                                } catch (NumberFormatException e) {
                                    switch (pouTouStr) {
                                    case "*":
                                        c.toughness = CardDbAdapter.STAR;
                                        break;
                                    case "1+*":
                                        c.toughness = CardDbAdapter.ONE_PLUS_STAR;
                                        break;
                                    case "2+*":
                                        c.toughness = CardDbAdapter.TWO_PLUS_STAR;
                                        break;
                                    case "7-*":
                                        c.toughness = CardDbAdapter.SEVEN_MINUS_STAR;
                                        break;
                                    case "*{^2}":
                                        c.toughness = CardDbAdapter.STAR_SQUARED;
                                        break;
                                    case "{1/2}":
                                        c.toughness = 0.5f;
                                        break;
                                    case "1{1/2}":
                                        c.toughness = 1.5f;
                                        break;
                                    case "2{1/2}":
                                        c.toughness = 2.5f;
                                        break;
                                    case "3{1/2}":
                                        c.toughness = 3.5f;
                                        break;
                                    }
                                }
                            } else if (s2.equalsIgnoreCase("i")) { /* loyalty */
                                try {
                                    c.loyalty = reader.nextInt();
                                } catch (NumberFormatException e) {
                                    reader.skipValue();
                                }
                            } else if (s2.equalsIgnoreCase("j")) { /* ability */
                                c.ability = reader.nextString();
                            } else if (s2.equalsIgnoreCase("k")) { /* flavor */
                                c.flavor = reader.nextString();
                            } else if (s2.equalsIgnoreCase("l")) { /* artist */
                                c.artist = reader.nextString();
                            } else if (s2.equalsIgnoreCase("m")) { /* number */
                                c.number = reader.nextString();
                            } else if (s2.equalsIgnoreCase("n")) { /* color */
                                c.color = reader.nextString();
                            } else if (s2.equalsIgnoreCase("x")) { /* multiverse id */
                                try {
                                    c.multiverseId = reader.nextInt();
                                } catch (NumberFormatException e) {
                                    reader.skipValue();
                                }
                            }
                        }
                        cardsToAdd.add(c);
                        elementsParsed++;
                        progressReporter.reportJsonCardProgress(
                                (int) Math.round(100 * elementsParsed / (double) numTotalElements));
                        reader.endObject();
                    }
                    reader.endArray();
                }
            }
            reader.endObject();
        }
        if (s.equalsIgnoreCase("w")) { /* num_cards */
            try {
                numTotalElements = reader.nextInt();
            } catch (NumberFormatException e) {
                reader.skipValue();
            }
        }
    }
    reader.endObject();
    reader.close();
}

From source file:com.github.lindenb.gatkui.Json2Xml.java

License:Open Source License

private void parse(String label, JsonReader r) throws Exception {
    if (!r.hasNext())
        return;/*from  w ww . java 2  s.com*/
    JsonToken token = r.peek();
    switch (token) {
    case NAME:
        break;
    case BEGIN_OBJECT: {
        r.beginObject();
        parseObject(label, r);
        break;
    }
    case END_OBJECT: {
        break;
    }
    case BEGIN_ARRAY: {
        r.beginArray();
        parseArray(label, r);
        break;
    }
    case END_ARRAY: {
        break;
    }
    case NULL: {
        r.nextNull();
        w.writeEmptyElement(NS, "null");
        if (label != null)
            w.writeAttribute("name", label);
        break;
    }
    case STRING: {
        w.writeStartElement(NS, "string");
        if (label != null)
            w.writeAttribute("name", label);
        w.writeCharacters(r.nextString());
        w.writeEndElement();
        break;
    }
    case NUMBER: {
        w.writeStartElement(NS, "number");
        if (label != null)
            w.writeAttribute("name", label);
        String s;
        try {
            s = String.valueOf(r.nextLong());
        } catch (Exception err) {
            s = String.valueOf(r.nextDouble());
        }

        w.writeCharacters(s);
        w.writeEndElement();
        break;
    }
    case BOOLEAN: {
        w.writeStartElement(NS, "boolean");
        if (label != null)
            w.writeAttribute("name", label);
        w.writeCharacters(String.valueOf(r.nextBoolean()));
        w.writeEndElement();
        break;
    }
    case END_DOCUMENT: {
        break;
    }
    default:
        throw new IllegalStateException(token.name());
    }

}

From source file:com.google.maps.internal.DateTimeAdapter.java

License:Open Source License

/**
 * Read a Time object from a Directions API result and convert it to a {@link DateTime}.
 *
 * <p>We are expecting to receive something akin to the following:
 * <pre>/*  ww w. j a v a2 s .  co  m*/
 * {
 *   "text" : "4:27pm",
 *   "time_zone" : "Australia/Sydney",
 *   "value" : 1406528829
 * }
 * </pre>
 */
@Override
public DateTime read(JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();
        return null;
    }

    String timeZoneId = "";
    long secondsSinceEpoch = 0L;

    reader.beginObject();
    while (reader.hasNext()) {
        String name = reader.nextName();
        if (name.equals("text")) {
            // Ignore the human readable rendering.
            reader.nextString();
        } else if (name.equals("time_zone")) {
            timeZoneId = reader.nextString();
        } else if (name.equals("value")) {
            secondsSinceEpoch = reader.nextLong();
        }

    }
    reader.endObject();

    return new DateTime(secondsSinceEpoch * 1000, DateTimeZone.forID(timeZoneId));
}