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

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

Introduction

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

Prototype

public int nextInt() throws IOException 

Source Link

Document

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

Usage

From source file:com.dangdang.ddframe.job.util.json.AbstractJobConfigurationGsonTypeAdapter.java

License:Apache License

@Override
public T read(final JsonReader in) throws IOException {
    String jobName = "";
    String cron = "";
    int shardingTotalCount = 0;
    String shardingItemParameters = "";
    String jobParameter = "";
    boolean failover = false;
    boolean misfire = failover;
    String description = "";
    JobProperties jobProperties = new JobProperties();
    JobType jobType = null;//from www  .  j  a  v  a  2  s .c o m
    String jobClass = "";
    boolean streamingProcess = false;
    String scriptCommandLine = "";
    Map<String, Object> customizedValueMap = new HashMap<>(32, 1);
    in.beginObject();
    while (in.hasNext()) {
        String jsonName = in.nextName();
        switch (jsonName) {
        case "jobName":
            jobName = in.nextString();
            break;
        case "cron":
            cron = in.nextString();
            break;
        case "shardingTotalCount":
            shardingTotalCount = in.nextInt();
            break;
        case "shardingItemParameters":
            shardingItemParameters = in.nextString();
            break;
        case "jobParameter":
            jobParameter = in.nextString();
            break;
        case "failover":
            failover = in.nextBoolean();
            break;
        case "misfire":
            misfire = in.nextBoolean();
            break;
        case "description":
            description = in.nextString();
            break;
        case "jobProperties":
            jobProperties = getJobProperties(in);
            break;
        case "jobType":
            jobType = JobType.valueOf(in.nextString());
            break;
        case "jobClass":
            jobClass = in.nextString();
            break;
        case "streamingProcess":
            streamingProcess = in.nextBoolean();
            break;
        case "scriptCommandLine":
            scriptCommandLine = in.nextString();
            break;
        default:
            addToCustomizedValueMap(jsonName, in, customizedValueMap);
            break;
        }
    }
    in.endObject();
    JobCoreConfiguration coreConfig = getJobCoreConfiguration(jobName, cron, shardingTotalCount,
            shardingItemParameters, jobParameter, failover, misfire, description, jobProperties);
    JobTypeConfiguration typeConfig = getJobTypeConfiguration(coreConfig, jobType, jobClass, streamingProcess,
            scriptCommandLine);
    return getJobRootConfiguration(typeConfig, customizedValueMap);
}

From source file:com.facebook.buck.worker.WorkerProcessProtocolZero.java

License:Apache License

private static void receiveHandshake(JsonReader reader, int messageId, Optional<Path> stdErr)
        throws IOException {
    int id = -1;/*from   w w w. j  a va2 s .co m*/
    String type = "";
    String protocolVersion = "";

    try {
        reader.beginArray();
        reader.beginObject();
        while (reader.hasNext()) {
            String property = reader.nextName();
            if (property.equals("id")) {
                id = reader.nextInt();
            } else if (property.equals("type")) {
                type = reader.nextString();
            } else if (property.equals("protocol_version")) {
                protocolVersion = reader.nextString();
            } else if (property.equals("capabilities")) {
                try {
                    reader.beginArray();
                    reader.endArray();
                } catch (IllegalStateException e) {
                    throw new HumanReadableException(
                            "Expected handshake response's \"capabilities\" to " + "be an empty array.");
                }
            } else {
                reader.skipValue();
            }
        }
        reader.endObject();
    } catch (IOException e) {
        throw new HumanReadableException(e, "Error receiving handshake response from external process.\n"
                + "Stderr from external process:\n%s", getStdErrorOutput(stdErr));
    }

    if (id != messageId) {
        throw new HumanReadableException(String.format(
                "Expected handshake response's \"id\" value " + "to be \"%d\", got \"%d\" instead.", messageId,
                id));
    }
    if (!type.equals(TYPE_HANDSHAKE)) {
        throw new HumanReadableException(
                String.format("Expected handshake response's \"type\" " + "to be \"%s\", got \"%s\" instead.",
                        TYPE_HANDSHAKE, type));
    }
    if (!protocolVersion.equals(PROTOCOL_VERSION)) {
        throw new HumanReadableException(String.format(
                "Expected handshake response's " + "\"protocol_version\" to be \"%s\", got \"%s\" instead.",
                PROTOCOL_VERSION, protocolVersion));
    }
}

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  ww. j  a v a2s .co 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  v a2  s.  co m*/
 * <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.google.maps.internal.DayOfWeekAdapter.java

License:Open Source License

@Override
public DayOfWeek read(JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();//from   ww w  .j a  v a  2  s . c o  m
        return null;
    }

    if (reader.peek() == JsonToken.NUMBER) {
        int day = reader.nextInt();

        switch (day) {
        case 0:
            return DayOfWeek.SUNDAY;
        case 1:
            return DayOfWeek.MONDAY;
        case 2:
            return DayOfWeek.TUESDAY;
        case 3:
            return DayOfWeek.WEDNESDAY;
        case 4:
            return DayOfWeek.THURSDAY;
        case 5:
            return DayOfWeek.FRIDAY;
        case 6:
            return DayOfWeek.SATURDAY;
        }
    }

    return DayOfWeek.UNKNOWN;
}

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

License:Open Source License

@Override
public GeolocationApi.Response read(JsonReader reader) throws IOException {

    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();/*w w  w  .jav  a2s.  c om*/
        return null;
    }
    GeolocationApi.Response response = new GeolocationApi.Response();
    LatLngAdapter latLngAdapter = new LatLngAdapter();

    reader.beginObject(); // opening {
    while (reader.hasNext()) {
        String name = reader.nextName();
        // two different objects could be returned a success object containing "location" and "accuracy"
        // keys or an error object containing an "error" key
        if (name.equals("location")) {
            // we already have a parser for the LatLng object so lets use that
            response.location = latLngAdapter.read(reader);
        } else if (name.equals("accuracy")) {
            response.accuracy = reader.nextDouble();
        } else if (name.equals("error")) {
            reader.beginObject(); // the error key leads to another object...
            while (reader.hasNext()) {
                String errName = reader.nextName();
                // ...with keys "errors", "code" and "message"
                if (errName.equals("code")) {
                    response.code = reader.nextInt();
                } else if (errName.equals("message")) {
                    response.message = reader.nextString();
                } else if (errName.equals("errors")) {
                    reader.beginArray(); // its plural because its an array of errors...
                    while (reader.hasNext()) {
                        reader.beginObject();// ...and each error array element is an object...
                        while (reader.hasNext()) {
                            errName = reader.nextName();
                            // ...with keys "reason", "domain", "debugInfo", "location", "locationType",  and "message" (again)
                            if (errName.equals("reason")) {
                                response.reason = reader.nextString();
                            } else if (errName.equals("domain")) {
                                response.domain = reader.nextString();
                            } else if (errName.equals("debugInfo")) {
                                response.debugInfo = reader.nextString();
                            } else if (errName.equals("message")) {
                                // have this already
                                reader.nextString();
                            } else if (errName.equals("location")) {
                                reader.nextString();
                            } else if (errName.equals("locationType")) {
                                reader.nextString();
                            }
                        }
                        reader.endObject();
                    }
                    reader.endArray();
                }
            }
            reader.endObject(); // closing }
        }
    }
    reader.endObject();
    return response;
}

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

License:Open Source License

@Override
public PriceLevel read(JsonReader reader) throws IOException {
    if (reader.peek() == JsonToken.NULL) {
        reader.nextNull();// ww  w .  j  av a  2s  .c o m
        return null;
    }

    if (reader.peek() == JsonToken.NUMBER) {
        int priceLevel = reader.nextInt();

        switch (priceLevel) {
        case 0:
            return PriceLevel.FREE;
        case 1:
            return PriceLevel.INEXPENSIVE;
        case 2:
            return PriceLevel.MODERATE;
        case 3:
            return PriceLevel.EXPENSIVE;
        case 4:
            return PriceLevel.VERY_EXPENSIVE;
        }
    }

    return PriceLevel.UNKNOWN;
}

From source file:com.ibasco.agql.protocols.valve.dota2.webapi.adapters.Dota2TeamInfoAdapter.java

License:Open Source License

@Override
public Dota2MatchTeamInfo read(JsonReader in) throws IOException {
    in.beginObject();/*from  ww  w . j  av a2  s  . co m*/
    Dota2MatchTeamInfo teamInfo = new Dota2MatchTeamInfo();
    while (in.hasNext()) {
        String name = in.nextName();
        if (name.startsWith("league_id_")) {
            teamInfo.getLeagueIds().add(in.nextInt());
        } else if (name.startsWith("player_")) {
            teamInfo.getPlayerAccountIds().add(in.nextLong());
        } else {
            switch (name) {
            case "name":
                teamInfo.setName(in.nextString());
                break;
            case "tag":
                teamInfo.setTag(in.nextString());
                break;
            case "time_created":
                teamInfo.setTimeCreated(in.nextLong());
                break;
            case "calibration_games_remaining":
                teamInfo.setCalibrationGamesRemaining(in.nextInt());
                break;
            case "logo":
                teamInfo.setLogo(in.nextLong());
                break;
            case "logo_sponsor":
                teamInfo.setLogoSponsor(in.nextLong());
                break;
            case "country_code":
                teamInfo.setCountryCode(in.nextString());
                break;
            case "url":
                teamInfo.setUrl(in.nextString());
                break;
            case "games_played":
                teamInfo.setGamesPlayed(in.nextInt());
                break;
            case "admin_account_id":
                teamInfo.setAdminAccountId(in.nextLong());
                break;
            default:
                break;
            }
        }
    }
    in.endObject();
    return teamInfo;
}

From source file:com.ichi2.anki.AnkiDroidProxy.java

License:Open Source License

/**
 * Parses a JSON InputStream to a list of SharedDecks efficiently 
 * @param is InputStream to parse and convert. It only works with the reply from
 *        getSharedDecks request//from  w  w w.j av  a2  s  .c om
 * @return List of SharedDecks that were parsed
 * @throws Exception 
 */
public static List<SharedDeck> parseGetSharedDecksResponce(InputStream is) throws Exception {
    BufferedReader rd = new BufferedReader(new InputStreamReader(is), 409600);
    JsonReader reader = new JsonReader(rd);
    List<SharedDeck> sharedDecks = new ArrayList<SharedDeck>();
    try {
        reader.beginArray();
        int count = 0;
        while (reader.hasNext()) {
            SharedDeck sd = new SharedDeck();
            reader.beginArray();
            sd.setId(reader.nextInt()); // SD_ID
            reader.skipValue(); // SD_USERNAME
            sd.setTitle(reader.nextString()); // SD_TITLE
            reader.skipValue(); // SD_DESCRIPTION
            reader.skipValue(); // SD_TAGS
            reader.skipValue(); // SD_VERSION
            sd.setFacts(reader.nextInt()); // SD_FACTS
            sd.setSize(reader.nextInt()); // SD_SIZE
            reader.skipValue(); // SD_COUNT
            reader.skipValue(); // SD_MODIFIED
            reader.skipValue(); // SD_FNAME
            reader.endArray();
            sharedDecks.add(sd);
            count++;
        }
        reader.endArray();
        reader.close();
        Log.d(AnkiDroidApp.TAG, "parseGetSharedDecksResponce: found " + count + " shared decks");
    } catch (Exception e) {
        Log.e(AnkiDroidApp.TAG, Log.getStackTraceString(e));
        sharedDecks.clear();
        throw e;
    }

    return sharedDecks;
}

From source file:com.javacreed.examples.gson.part3.BookTypeAdapter.java

License:Apache License

@Override
public Book read(final JsonReader in) throws IOException {
    final Book book = new Book();

    in.beginArray();/* w w  w  . j av a2 s.  co m*/
    book.setIsbn(in.nextString());
    book.setTitle(in.nextString());
    final List<Author> authors = new ArrayList<>();
    while (in.hasNext()) {
        final int id = in.nextInt();
        final String name = in.nextString();
        authors.add(new Author(id, name));
    }
    book.setAuthors(authors.toArray(new Author[authors.size()]));
    in.endArray();

    return book;
}