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

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

Introduction

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

Prototype

public void beginObject() throws IOException 

Source Link

Document

Consumes the next token from the JSON stream and asserts that it is the beginning of a new object.

Usage

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

License:Open Source License

public void readLegalityJsonStream(CardDbAdapter cda, PreferencesAdapter prefAdapter, boolean reparseDatabase)
        throws IOException, FamiliarDbException {

    CardDbAdapter mDbHelper;/*from   w w w  . j ava  2s . c  o m*/
    String legalSet;
    String bannedCard;
    String restrictedCard;
    String formatName;
    String jsonArrayName;
    String jsonTopLevelName;

    URL legal = new URL("https://sites.google.com/site/mtgfamiliar/manifests/legality.json");
    InputStream in = new BufferedInputStream(legal.openStream());

    JsonReader reader = new JsonReader(new InputStreamReader(in, "ISO-8859-1"));

    mDbHelper = cda;

    reader.beginObject();
    while (reader.hasNext()) {

        jsonTopLevelName = reader.nextName();
        if (jsonTopLevelName.equalsIgnoreCase("Date")) {
            currentRulesDate = reader.nextString();

            // compare date, maybe return, update sharedprefs
            String spDate = prefAdapter.getLegalityDate();
            if (spDate != null && spDate.equals(currentRulesDate)) {
                if (!reparseDatabase) { // if we're reparsing, screw the date
                    reader.close();
                    return; // dates match, nothing new here.
                }
            }

            mDbHelper.dropLegalTables();
            mDbHelper.createLegalTables();
        } else if (jsonTopLevelName.equalsIgnoreCase("Formats")) {

            reader.beginObject();
            while (reader.hasNext()) {
                formatName = reader.nextName();

                mDbHelper.createFormat(formatName);

                reader.beginObject();
                while (reader.hasNext()) {
                    jsonArrayName = reader.nextName();

                    if (jsonArrayName.equalsIgnoreCase("Sets")) {
                        reader.beginArray();
                        while (reader.hasNext()) {
                            legalSet = reader.nextString();
                            mDbHelper.addLegalSet(legalSet, formatName);
                        }
                        reader.endArray();
                    } else if (jsonArrayName.equalsIgnoreCase("Banlist")) {
                        reader.beginArray();
                        while (reader.hasNext()) {
                            bannedCard = reader.nextString();
                            mDbHelper.addLegalCard(bannedCard, formatName, CardDbAdapter.BANNED);
                        }
                        reader.endArray();
                    } else if (jsonArrayName.equalsIgnoreCase("Restrictedlist")) {
                        reader.beginArray();
                        while (reader.hasNext()) {
                            restrictedCard = reader.nextString();
                            mDbHelper.addLegalCard(restrictedCard, formatName, CardDbAdapter.RESTRICTED);
                        }
                        reader.endArray();
                    }
                }
                reader.endObject();
            }
            reader.endObject();
        }
    }
    reader.endObject();

    reader.close();
    return;
}

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

License:Open Source License

public void readTCGNameJsonStream(PreferencesAdapter prefAdapter, CardDbAdapter mDbHelper,
        boolean reparseDatabase) throws MalformedURLException, IOException, FamiliarDbException {
    URL update;/*  www. j a va 2s .co m*/
    String label;
    String label2;
    String name = null, code = null;

    update = new URL("https://sites.google.com/site/mtgfamiliar/manifests/TCGnames.json");
    InputStreamReader isr = new InputStreamReader(update.openStream(), "ISO-8859-1");
    JsonReader reader = new JsonReader(isr);

    reader.beginObject();
    while (reader.hasNext()) {
        label = reader.nextName();

        if (label.equals("Date")) {
            String lastUpdate = prefAdapter.getLastTCGNameUpdate();
            currentTCGNamePatchDate = reader.nextString();
            if (lastUpdate.equals(currentTCGNamePatchDate) && !reparseDatabase) {
                reader.close();
                return;
            }
        } else if (label.equals("Sets")) {
            reader.beginArray();
            while (reader.hasNext()) {
                reader.beginObject();
                while (reader.hasNext()) {
                    label2 = reader.nextName();
                    if (label2.equals("Code")) {
                        code = reader.nextString();
                    } else if (label2.equals("TCGName")) {
                        name = reader.nextString();
                    }
                }
                mDbHelper.addTCGname(name, code);
                reader.endObject();
            }
            reader.endArray();
        }
    }
    reader.endObject();
    reader.close();

}

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./*  w  w  w. j av a2  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.gelakinetic.mtgfam.helpers.updaters.CardAndSetParser.java

License:Open Source License

/**
 * This method checks the hardcoded URL and downloads a list of patches to be checked
 *
 * @param prefAdapter The preference adapter is used to get the last update time
 * @return An ArrayList of String[] which contains the {Name, URL, Set Code} for each available patch
 * @throws IOException Thrown if something goes wrong with the InputStream from the web
 *//*from   w  ww .j ava2s  . c  om*/
public ArrayList<String[]> readUpdateJsonStream(PreferenceAdapter prefAdapter) throws IOException {
    ArrayList<String[]> patchInfo = new ArrayList<>();
    URL update;
    String label;
    String label2;

    update = new URL(PATCHES_URL);
    InputStreamReader isr = new InputStreamReader(update.openStream(), "ISO-8859-1");
    JsonReader reader = new JsonReader(isr);

    reader.beginObject();
    while (reader.hasNext()) {
        label = reader.nextName();

        if (label.equals("Date")) {
            String lastUpdate = prefAdapter.getLastUpdate();
            mCurrentPatchDate = reader.nextString();
            if (lastUpdate.equals(mCurrentPatchDate)) {
                reader.close();
                return null;
            }
        } else if (label.equals("Patches")) {
            reader.beginArray();
            while (reader.hasNext()) {
                reader.beginObject();
                String[] setData = new String[3];
                while (reader.hasNext()) {
                    label2 = reader.nextName();
                    switch (label2) {
                    case "Name":
                        setData[SET_NAME] = reader.nextString();
                        break;
                    case "URL":
                        setData[SET_URL] = reader.nextString();
                        break;
                    case "Code":
                        setData[SET_CODE] = reader.nextString();
                        break;
                    }
                }
                patchInfo.add(setData);
                reader.endObject();
            }
            reader.endArray();
        }
    }
    reader.endObject();
    reader.close();

    return patchInfo;
}

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

License:Open Source License

/**
 * Parses the legality file and populates the database with the different formats, their respective sets, and their
 * banned and restricted lists/*from w w  w.j a  va  2s  .  c  o  m*/
 *
 * @param prefAdapter The preference adapter is used to get the last update time
 * @return An object with all of the legal info, to be added to the database in one fell swoop
 * @throws IOException                                                 Thrown if something goes wrong with the InputStream
 * @throws com.gelakinetic.mtgfam.helpers.database.FamiliarDbException Thrown if something goes wrong with database writing
 */
public LegalInfo readLegalityJsonStream(PreferenceAdapter prefAdapter) throws IOException, FamiliarDbException {

    LegalInfo legalInfo = new LegalInfo();

    String legalSet;
    String bannedCard;
    String restrictedCard;
    String formatName;
    String jsonArrayName;
    String jsonTopLevelName;

    URL legal = new URL(LEGALITY_URL);
    InputStream in = new BufferedInputStream(legal.openStream());

    JsonReader reader = new JsonReader(new InputStreamReader(in, "ISO-8859-1"));

    reader.beginObject();
    while (reader.hasNext()) {

        jsonTopLevelName = reader.nextName();
        if (jsonTopLevelName.equalsIgnoreCase("Date")) {
            mCurrentRulesDate = reader.nextString();

            /* compare date, maybe return, update shared prefs */
            String spDate = prefAdapter.getLegalityDate();
            if (spDate != null && spDate.equals(mCurrentRulesDate)) {
                reader.close();
                return null; /* dates match, nothing new here. */
            }

        } else if (jsonTopLevelName.equalsIgnoreCase("Formats")) {

            reader.beginObject();
            while (reader.hasNext()) {
                formatName = reader.nextName();

                legalInfo.formats.add(formatName);

                reader.beginObject();
                while (reader.hasNext()) {
                    jsonArrayName = reader.nextName();

                    if (jsonArrayName.equalsIgnoreCase("Sets")) {
                        reader.beginArray();
                        while (reader.hasNext()) {
                            legalSet = reader.nextString();
                            legalInfo.legalSets.add(new NameAndMetadata(legalSet, formatName));
                        }
                        reader.endArray();
                    } else if (jsonArrayName.equalsIgnoreCase("Banlist")) {
                        reader.beginArray();
                        while (reader.hasNext()) {
                            bannedCard = reader.nextString();
                            legalInfo.bannedCards.add(new NameAndMetadata(bannedCard, formatName));
                        }
                        reader.endArray();
                    } else if (jsonArrayName.equalsIgnoreCase("Restrictedlist")) {
                        reader.beginArray();
                        while (reader.hasNext()) {
                            restrictedCard = reader.nextString();
                            legalInfo.restrictedCards.add(new NameAndMetadata(restrictedCard, formatName));
                        }
                        reader.endArray();
                    }
                }
                reader.endObject();
            }
            reader.endObject();
        }
    }
    reader.endObject();

    reader.close();

    return legalInfo;
}

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

License:Open Source License

/**
 * This method parses the mapping between set codes and the names TCGPlayer.com uses
 *
 * @param prefAdapter The preference adapter is used to get the last update time
 * @param tcgNames    A place to store tcg names before adding to the database
 * @throws IOException Thrown if something goes wrong with the InputStream
 *///from   www .  j  a  v  a 2 s .co  m
public void readTCGNameJsonStream(PreferenceAdapter prefAdapter, ArrayList<NameAndMetadata> tcgNames)
        throws IOException {
    URL update;
    String label;
    String label2;
    String name = null, code = null;

    update = new URL(TCG_NAMES_URL);
    InputStreamReader isr = new InputStreamReader(update.openStream(), "ISO-8859-1");
    JsonReader reader = new JsonReader(isr);

    reader.beginObject();
    while (reader.hasNext()) {
        label = reader.nextName();

        if (label.equals("Date")) {
            String lastUpdate = prefAdapter.getLastTCGNameUpdate();
            mCurrentTCGNamePatchDate = reader.nextString();
            if (lastUpdate.equals(mCurrentTCGNamePatchDate)) {
                reader.close();
                return;
            }
        } else if (label.equals("Sets")) {
            reader.beginArray();
            while (reader.hasNext()) {
                reader.beginObject();
                while (reader.hasNext()) {
                    label2 = reader.nextName();
                    if (label2.equals("Code")) {
                        code = reader.nextString();
                    } else if (label2.equals("TCGName")) {
                        name = reader.nextString();
                    }
                }
                tcgNames.add(new NameAndMetadata(name, code));
                reader.endObject();
            }
            reader.endArray();
        }
    }
    reader.endObject();
    reader.close();
}

From source file:com.getperka.flatpack.Unpacker.java

License:Apache License

private <T> FlatPackEntity<T> unpack(Type returnType, JsonReader reader, Principal principal)
        throws IOException {
    // Hold temporary state for deserialization
    DeserializationContext context = contexts.get();

    /*/*from   w  w w.  j  a  v  a 2 s . co m*/
     * Decoding is done as a two-pass operation since the runtime type of an allocated object cannot
     * be swizzled. The per-entity data is held as a semi-reified JsonObject to be passed off to a
     * Codex.
     */
    Map<HasUuid, JsonObject> entityData = FlatPackCollections.mapForIteration();
    // Used to populate the entityData map
    JsonParser jsonParser = new JsonParser();
    /*
     * The reader is placed in lenient mode as an aid to developers, however all output will be
     * strictly formatted.
     */
    reader.setLenient(true);

    // The return value
    @SuppressWarnings("unchecked")
    FlatPackEntity<T> toReturn = (FlatPackEntity<T>) FlatPackEntity.create(returnType, null, principal);
    // Stores the single, top-level value in the payload for two-pass reification
    JsonElement value = null;

    if (reader.peek().equals(JsonToken.NULL)) {
        return toReturn;
    }

    reader.beginObject();

    while (JsonToken.NAME.equals(reader.peek())) {
        String name = reader.nextName();
        if ("data".equals(name)) {
            // data : { "fooEntity" : [ { ... }, { ... } ]
            reader.beginObject();
            while (JsonToken.NAME.equals(reader.peek())) {
                // Turn "fooEntity" into the actual FooEntity class
                String simpleName = reader.nextName();
                Class<? extends HasUuid> clazz = typeContext.getClass(simpleName);
                if (clazz == null) {
                    if (ignoreUnresolvableTypes) {
                        reader.skipValue();
                        continue;
                    } else {
                        throw new UnsupportedOperationException("Cannot resolve type " + simpleName);
                    }
                } else if (Modifier.isAbstract(clazz.getModifiers())) {
                    throw new UnsupportedOperationException(
                            "A subclass of " + simpleName + " must be used instead");
                }
                context.pushPath("allocating " + simpleName);
                try {
                    // Find the Codex for the requested entity type
                    EntityCodex<?> codex = (EntityCodex<?>) typeContext.getCodex(clazz);

                    // Take the n-many property objects and stash them for later decoding
                    reader.beginArray();
                    while (!JsonToken.END_ARRAY.equals(reader.peek())) {
                        JsonObject chunk = jsonParser.parse(reader).getAsJsonObject();
                        HasUuid entity = codex.allocate(chunk, context);
                        toReturn.addExtraEntity(entity);
                        entityData.put(entity, chunk.getAsJsonObject());
                    }
                    reader.endArray();
                } finally {
                    context.popPath();
                }
            }
            reader.endObject();
        } else if ("errors".equals(name)) {
            // "errors" : { "path" : "problem", "path2" : "problem2" }
            reader.beginObject();
            while (JsonToken.NAME.equals(reader.peek())) {
                String path = reader.nextName();
                if (JsonToken.STRING.equals(reader.peek()) || JsonToken.NUMBER.equals(reader.peek())) {
                    toReturn.addError(path, reader.nextString());
                } else {
                    reader.skipValue();
                }
            }
            reader.endObject();
        } else if ("metadata".equals(name)) {
            reader.beginArray();

            while (!JsonToken.END_ARRAY.equals(reader.peek())) {
                EntityMetadata meta = new EntityMetadata();
                JsonObject metaElement = jsonParser.parse(reader).getAsJsonObject();
                metaCodex.get().readProperties(meta, metaElement, context);
                toReturn.addMetadata(meta);
            }

            reader.endArray();
        } else if ("value".equals(name)) {
            // Just stash the value element in case it occurs first
            value = jsonParser.parse(reader);
        } else if (JsonToken.STRING.equals(reader.peek()) || JsonToken.NUMBER.equals(reader.peek())) {
            // Save off any other simple properties
            toReturn.putExtraData(name, reader.nextString());
        } else {
            // Ignore random other entries
            reader.skipValue();
        }
    }

    reader.endObject();
    reader.close();

    for (Map.Entry<HasUuid, JsonObject> entry : entityData.entrySet()) {
        HasUuid entity = entry.getKey();
        EntityCodex<HasUuid> codex = (EntityCodex<HasUuid>) typeContext.getCodex(entity.getClass());
        codex.readProperties(entity, entry.getValue(), context);
    }

    @SuppressWarnings("unchecked")
    Codex<T> returnCodex = (Codex<T>) typeContext.getCodex(toReturn.getType());
    toReturn.withValue(returnCodex.read(value, context));

    for (Map.Entry<UUID, String> entry : context.getWarnings().entrySet()) {
        toReturn.addWarning(entry.getKey().toString(), entry.getValue());
    }

    // Process metadata
    for (EntityMetadata meta : toReturn.getMetadata()) {
        if (meta.isPersistent()) {
            HasUuid entity = context.getEntity(meta.getUuid());
            if (entity instanceof PersistenceAware) {
                ((PersistenceAware) entity).markPersistent();
            }
        }
    }

    context.runPostWork();
    context.close();

    return toReturn;
}

From source file:com.gilecode.yagson.types.TypeUtils.java

License:Apache License

private static Type readTypeAdvice(JsonReader in) throws IOException {
    in.beginObject();
    if (!in.hasNext()) {
        throw new JsonSyntaxException("BEGIN_OBJECT is not expected at path " + in.getPath());
    }/* w  w w .j a va  2  s . c  o m*/
    String name = in.nextName();
    if (!name.equals("@type")) {
        throw new JsonSyntaxException("@type is expected at path " + in.getPath());
    }
    return readTypeAdviceAfterTypeField(in);
}

From source file:com.github.gv2011.jsong.JsongAdapter.java

License:Open Source License

private JsonNode deserialize(final JsonFactoryImp jf, final JsonReader in) {
    return call(() -> {
        switch (in.peek()) {
        case STRING:
            return jf.primitive(in.nextString());
        case NUMBER:
            return jf.primitive(new BigDecimal(in.nextString()));
        case BOOLEAN:
            return jf.primitive(in.nextBoolean());
        case NULL:
            in.nextNull();//from ww  w .  j  ava  2 s. c  o  m
            return jf.jsonNull();
        case BEGIN_ARRAY:
            in.beginArray();
            final JsonList list = XStream.fromIterator(new It(jf, in)).collect(jf.toJsonList());
            in.endArray();
            return list;
        case BEGIN_OBJECT:
            in.beginObject();
            final JsonObject obj = XStream.fromIterator(new Itm(jf, in)).collect(jf.toJsonObject());
            in.endObject();
            return obj;
        case NAME:
        case END_DOCUMENT:
        case END_OBJECT:
        case END_ARRAY:
        default:
            throw new IllegalArgumentException();
        }
    });
}

From source file:com.github.kevinsawicki.halligan.Resource.java

License:Open Source License

/**
 * Fill this resource by parsing the next object in the reader
 *
 * @param reader/* w  w  w  .ja v  a2  s  .c  o  m*/
 * @return this resource
 * @throws IOException
 */
protected Resource parse(final JsonReader reader) throws IOException {
    reader.beginObject();
    while (reader.hasNext() && reader.peek() == NAME) {
        String name = reader.nextName();
        if ("_links".equals(name))
            parseLinks(reader);
        else if ("_embedded".equals(name))
            parseResources(reader);
        else
            parseProperty(reader, name);
    }
    reader.endObject();
    return this;
}