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

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

Introduction

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

Prototype

public void skipValue() throws IOException 

Source Link

Document

Skips the next value recursively.

Usage

From source file:com.nridge.core.io.gson.RelationshipJSON.java

License:Open Source License

/**
 * Parses an JSON stream and loads it into a relationship and returns
 * an instance to it./*from ww  w . j ava  2 s .  c om*/
 *
 * @param aReader Json reader stream instance.
 *
 * @throws java.io.IOException I/O related exception.
 */
public Relationship loadRelationship(JsonReader aReader) throws IOException {
    String jsonName, jsonValue;

    Relationship relationship = new Relationship();

    aReader.beginObject();
    while (aReader.hasNext()) {
        jsonName = aReader.nextName();
        if (StringUtils.equals(jsonName, IO.JSON_TYPE_MEMBER_NAME))
            relationship.setType(aReader.nextString());
        else if (StringUtils.equals(jsonName, IO.JSON_FEATURES_ARRAY_NAME)) {
            aReader.beginObject();
            while (aReader.hasNext()) {
                jsonName = aReader.nextName();
                jsonValue = aReader.nextString();
                relationship.addFeature(jsonName, jsonValue);
            }
            aReader.endObject();
        } else if (StringUtils.equals(jsonName, IO.JSON_DOCUMENTS_ARRAY_NAME))
            loadDocuments(aReader, relationship);
        else
            aReader.skipValue();
    }
    aReader.endObject();

    return relationship;
}

From source file:com.nridge.ds.solr.SolrCollection.java

License:Open Source License

private void parseReply(String aMessage) throws DSException, IOException {
    String jsonName;//from   w  ww.j  ava  2  s .co m

    Logger appLogger = mAppMgr.getLogger(this, "parseReply");

    appLogger.trace(mAppMgr.LOGMSG_TRACE_ENTER);

    int msgCode = 0;
    String msgString = StringUtils.EMPTY;
    StringReader stringReader = new StringReader(aMessage);
    JsonReader jsonReader = new JsonReader(stringReader);
    jsonReader.beginObject();
    while (jsonReader.hasNext()) {
        jsonName = jsonReader.nextName();
        if (StringUtils.equals(jsonName, "exception")) {
            jsonReader.beginObject();
            while (jsonReader.hasNext()) {
                jsonName = jsonReader.nextName();
                if (StringUtils.equals(jsonName, "msg"))
                    msgString = jsonReader.nextString();
                else if (StringUtils.equals(jsonName, "rspCode"))
                    msgCode = jsonReader.nextInt();
                else
                    jsonReader.skipValue();
            }
            jsonReader.endObject();
        } else
            jsonReader.skipValue();
    }
    jsonReader.endObject();

    if (StringUtils.isNotEmpty(msgString))
        throw new DSException(String.format("Solr Exception [%d]: %s", msgCode, msgString));

    appLogger.trace(mAppMgr.LOGMSG_TRACE_DEPART);
}

From source file:com.nridge.ds.solr.SolrConfigJSON.java

License:Open Source License

private String nextValueAsString(JsonReader aReader) throws IOException {
    String valueString = StringUtils.EMPTY;

    JsonToken jsonToken = aReader.peek();
    switch (jsonToken) {
    case BOOLEAN:
        valueString = StrUtl.booleanToString(aReader.nextBoolean());
        break;//w w  w  .j a  v a  2s .  c  o  m
    case NUMBER:
    case STRING:
        valueString = Solr.cleanContent(aReader.nextString());
        break;
    default:
        aReader.skipValue();
        break;
    }

    return valueString;
}

From source file:com.nridge.ds.solr.SolrConfigJSON.java

License:Open Source License

/**
 * Parses an JSON string representing the Solr schema and loads it into a document
 * and returns an instance to it.//from w  ww .  jav a  2s . c o m
 *
 * @param aConfigString JSON string representation of the Solr schema.
 *
 * @return Document instance containing the parsed data.
 *
 * @throws IOException I/O related exception.
 */
public Document loadDocument(String aConfigString) throws IOException {
    String jsonName, jsonValue;
    Document componentDocument;

    Document configDocument = new Document(Solr.DOCUMENT_SCHEMA_TYPE);
    DataBag configBag = configDocument.getBag();

    StringReader stringReader = new StringReader(aConfigString);
    JsonReader jsonReader = new JsonReader(stringReader);

    jsonReader.beginObject();
    while (jsonReader.hasNext()) {
        jsonName = jsonReader.nextName();
        if (StringUtils.equals(jsonName, OBJECT_RESPONSE_HEADER)) {
            Document headerDocument = new Document(Solr.RESPONSE_SCHEMA_HEADER);
            DataBag headerBag = headerDocument.getBag();
            jsonReader.beginObject();
            while (jsonReader.hasNext()) {
                jsonName = jsonReader.nextName();
                jsonValue = nextValueAsString(jsonReader);
                headerBag.add(new DataTextField(jsonName, Field.nameToTitle(jsonName), jsonValue));
            }
            jsonReader.endObject();
            configDocument.addRelationship(Solr.RESPONSE_SCHEMA_HEADER, headerDocument);
        } else if (StringUtils.equals(jsonName, OBJECT_RESPONSE_CONFIG)) {
            jsonReader.beginObject();
            while (jsonReader.hasNext()) {
                jsonName = jsonReader.nextName();
                if (StringUtils.equals(jsonName, OBJECT_RESPONSE_CONFIG_UPDATE_HANDLER))
                    populateCfgUpdateHandler(jsonReader, configDocument, Solr.RESPONSE_CONFIG_UPDATE_HANDLER);
                else if (StringUtils.equals(jsonName, OBJECT_RESPONSE_CONFIG_QUERY))
                    populateCfgQuery(jsonReader, configDocument, Solr.RESPONSE_CONFIG_QUERY);
                else if (StringUtils.equals(jsonName, OBJECT_RESPONSE_CONFIG_REQUEST_HANDLER))
                    populateCfgRequestHandler(jsonReader, configDocument, Solr.RESPONSE_CONFIG_REQUEST_HANDLER);
                else if (StringUtils.equals(jsonName, OBJECT_RESPONSE_CONFIG_RESPONSE_WRITER))
                    populateQueryResponseWriterHandler(jsonReader, configDocument,
                            Solr.RESPONSE_CONFIG_QUERY_RESPONSE_WRITER);
                else if (StringUtils.equals(jsonName, OBJECT_RESPONSE_CONFIG_SEARCH_COMPONENT))
                    populateSearchComponent(jsonReader, configDocument, Solr.RESPONSE_CONFIG_SEARCH_COMPONENT);
                else if (StringUtils.equals(jsonName, OBJECT_RESPONSE_CONFIG_INIT_PARAMS)) {
                    componentDocument = new Document(Solr.RESPONSE_CONFIG_INIT_PARAMS);
                    addObjectToDocument(jsonReader, componentDocument, Solr.RESPONSE_CONFIG_INIT_PARAMETERS,
                            jsonName);
                    configDocument.addRelationship(Solr.RESPONSE_CONFIG_INIT_PARAMS, componentDocument);
                } else if (StringUtils.equals(jsonName, OBJECT_RESPONSE_CONFIG_LISTENER)) {
                    componentDocument = new Document(Solr.RESPONSE_CONFIG_LISTENER);
                    jsonReader.beginArray();
                    while (jsonReader.hasNext())
                        addObjectToDocument(jsonReader, componentDocument, Solr.RESPONSE_CONFIG_LISTENER_EVENTS,
                                jsonName);
                    jsonReader.endArray();
                    configDocument.addRelationship(Solr.RESPONSE_CONFIG_LISTENER, componentDocument);
                } else if (StringUtils.equals(jsonName, OBJECT_RESPONSE_CONFIG_DIRECTORY_FACTORY)) {
                    componentDocument = new Document(Solr.RESPONSE_CONFIG_DIRECTORY_FACTORY);
                    addObjectToDocument(jsonReader, componentDocument, Solr.RESPONSE_CONFIG_DIRECTORY_ENTRIES,
                            jsonName);
                    configDocument.addRelationship(Solr.RESPONSE_CONFIG_DIRECTORY_FACTORY, componentDocument);
                } else if (StringUtils.equals(jsonName, OBJECT_RESPONSE_CONFIG_CODEC_FACTORY)) {
                    componentDocument = new Document(Solr.RESPONSE_CONFIG_CODEC_FACTORY);
                    addObjectToDocument(jsonReader, componentDocument, Solr.RESPONSE_CONFIG_CODEC_ENTRIES,
                            jsonName);
                    configDocument.addRelationship(Solr.RESPONSE_CONFIG_CODEC_FACTORY, componentDocument);
                } else if (StringUtils.equals(jsonName, OBJECT_RESPONSE_CONFIG_UPDATE_HU_LOG)) {
                    componentDocument = new Document(Solr.RESPONSE_CONFIG_UPDATE_HANDLER_ULOG);
                    addObjectToDocument(jsonReader, componentDocument, Solr.RESPONSE_CONFIG_UHUL_ENTRIES,
                            jsonName);
                    configDocument.addRelationship(Solr.RESPONSE_CONFIG_UPDATE_HANDLER_ULOG, componentDocument);
                } else if (StringUtils.equals(jsonName, OBJECT_RESPONSE_CONFIG_REQUEST_DISPATCHER)) {
                    componentDocument = new Document(Solr.RESPONSE_CONFIG_REQUEST_DISPATCHER);
                    addObjectToDocument(jsonReader, componentDocument, Solr.RESPONSE_CONFIG_RD_ENTRIES,
                            jsonName);
                    configDocument.addRelationship(Solr.RESPONSE_CONFIG_REQUEST_DISPATCHER, componentDocument);
                } else if (StringUtils.equals(jsonName, OBJECT_RESPONSE_CONFIG_INDEX_CONFIG)) {
                    componentDocument = new Document(Solr.RESPONSE_CONFIG_REQUEST_DISPATCHER);
                    addObjectToDocument(jsonReader, componentDocument, Solr.RESPONSE_CONFIG_INDEX_CONFIG,
                            jsonName);
                    configDocument.addRelationship(Solr.RESPONSE_CONFIG_IC_ENTRIES, componentDocument);
                } else if (StringUtils.equals(jsonName, OBJECT_RESPONSE_CONFIG_PEER_SYNC)) {
                    componentDocument = new Document(Solr.RESPONSE_CONFIG_PEER_SYNC);
                    addObjectToDocument(jsonReader, componentDocument, Solr.RESPONSE_CONFIG_PS_ENTRIES,
                            jsonName);
                    configDocument.addRelationship(Solr.RESPONSE_CONFIG_PEER_SYNC, componentDocument);
                } else {
                    jsonValue = nextValueAsString(jsonReader);
                    addBagTextField(configBag, jsonName, jsonValue);
                }
            }
            jsonReader.endObject();
        } else
            jsonReader.skipValue();
    }
    jsonReader.endObject();
    jsonReader.close();

    return configDocument;
}

From source file:com.nridge.ds.solr.SolrSchemaJSON.java

License:Open Source License

private DataTable createSchemaFieldTable(String aSchemaString, String aFieldName, String aTitle)
        throws IOException {
    String jsonName;/*from   ww w  .  jav a2  s.  c  o  m*/
    DataField dataField;

    DataTable dataTable = new DataTable(aTitle);
    DataBag dataBag = dataTable.getColumnBag();

    StringReader stringReader = new StringReader(aSchemaString);
    JsonReader jsonReader = new JsonReader(stringReader);

    jsonReader.beginObject();
    while (jsonReader.hasNext()) {
        jsonName = jsonReader.nextName();
        if (StringUtils.equals(jsonName, OBJECT_SCHEMA)) {
            jsonReader.beginObject();
            while (jsonReader.hasNext()) {
                jsonName = jsonReader.nextName();
                if (StringUtils.equals(jsonName, aFieldName)) {
                    jsonReader.beginArray();
                    while (jsonReader.hasNext()) {
                        jsonReader.beginObject();
                        while (jsonReader.hasNext()) {
                            jsonName = jsonReader.nextName();
                            dataField = dataBag.getFieldByName(jsonName);
                            if (dataField == null) {
                                dataField = new DataTextField(jsonName, Field.nameToTitle(jsonName));
                                dataField.addFeature(Field.FEATURE_IS_STORED, StrUtl.STRING_TRUE);
                                dataBag.add(dataField);
                            }
                            jsonReader.skipValue();
                        }
                        jsonReader.endObject();
                    }
                    jsonReader.endArray();
                    ;
                } else
                    jsonReader.skipValue();
            }
            jsonReader.endObject();
        } else
            jsonReader.skipValue();
    }
    jsonReader.endObject();
    jsonReader.close();

    return dataTable;
}

From source file:com.nridge.ds.solr.SolrSchemaJSON.java

License:Open Source License

private String nextValueAsString(JsonReader aReader) throws IOException {
    String valueString = StringUtils.EMPTY;

    JsonToken jsonToken = aReader.peek();
    switch (jsonToken) {
    case BOOLEAN:
        valueString = StrUtl.booleanToString(aReader.nextBoolean());
        break;/*from   w w w.jav  a  2 s  .c  o m*/
    case NUMBER:
    case STRING:
        valueString = aReader.nextString();
        break;
    default:
        aReader.skipValue();
        break;
    }

    return valueString;
}

From source file:com.nridge.ds.solr.SolrSchemaJSON.java

License:Open Source License

/**
 * Parses an JSON string representing the Solr schema and loads it into a document
 * and returns an instance to it.//ww w.  j  a v  a2s.c  om
 *
 * @param aSchemaString JSON string representation of the Solr schema.
 *
 * @return Document instance containing the parsed data.
 *
 * @throws java.io.IOException I/O related exception.
 */
public Document loadDocument(String aSchemaString) throws IOException {
    String jsonName, jsonValue;

    Document schemaDocument = new Document(Solr.DOCUMENT_SCHEMA_TYPE);
    DataBag schemaBag = schemaDocument.getBag();

    // Parse a subset of the JSON payload to identify the table columns before fully processing it.

    SolrSchema solrSchema = new SolrSchema(mAppMgr);
    DataTable fieldsTable = new DataTable(solrSchema.createFieldsBag());
    DataTable dynamicFieldsTable = createSchemaFieldTable(aSchemaString, OBJECT_SCHEMA_DYNAMIC_FIELDS,
            "Solr Schema Dynamic Fields");
    DataTable copyFieldsTable = createSchemaFieldTable(aSchemaString, OBJECT_SCHEMA_COPY_FIELDS,
            "Solr Schema Copy Fields");

    StringReader stringReader = new StringReader(aSchemaString);
    JsonReader jsonReader = new JsonReader(stringReader);

    jsonReader.beginObject();
    while (jsonReader.hasNext()) {
        jsonName = jsonReader.nextName();
        if (StringUtils.equals(jsonName, OBJECT_RESPONSE_HEADER)) {
            Document headerDocument = new Document(Solr.RESPONSE_SCHEMA_HEADER);
            headerDocument.setName(jsonName);
            DataBag headerBag = headerDocument.getBag();
            jsonReader.beginObject();
            while (jsonReader.hasNext()) {
                jsonName = jsonReader.nextName();
                jsonValue = nextValueAsString(jsonReader);
                headerBag.add(new DataTextField(jsonName, Field.nameToTitle(jsonName), jsonValue));
            }
            jsonReader.endObject();
            schemaDocument.addRelationship(Solr.RESPONSE_SCHEMA_HEADER, headerDocument);
        } else if (StringUtils.equals(jsonName, OBJECT_SCHEMA)) {
            jsonReader.beginObject();
            while (jsonReader.hasNext()) {
                jsonName = jsonReader.nextName();
                if (StringUtils.equals(jsonName, "name")) {
                    jsonValue = nextValueAsString(jsonReader);
                    schemaBag.add(jsonName, Field.nameToTitle(jsonName), jsonValue);
                } else if (StringUtils.equals(jsonName, "version")) {
                    jsonValue = nextValueAsString(jsonReader);
                    schemaBag.add(jsonName, Field.nameToTitle(jsonName), jsonValue);
                } else if (StringUtils.equals(jsonName, "uniqueKey")) {
                    jsonValue = nextValueAsString(jsonReader);
                    schemaBag.add(jsonName, Field.nameToTitle(jsonName), jsonValue);
                } else if (StringUtils.equals(jsonName, OBJECT_SCHEMA_FIELD_TYPES)) {
                    populateSchemaFieldTypes(jsonReader, schemaDocument);
                } else if (StringUtils.equals(jsonName, OBJECT_SCHEMA_FIELDS)) {
                    populateSchemaFieldTable(jsonReader, fieldsTable);
                    Document fieldsDocument = new Document(Solr.RESPONSE_SCHEMA_FIELD_NAMES, fieldsTable);
                    schemaDocument.addRelationship(Solr.RESPONSE_SCHEMA_FIELD_NAMES, fieldsDocument);
                } else if (StringUtils.equals(jsonName, OBJECT_SCHEMA_DYNAMIC_FIELDS)) {
                    populateSchemaFieldTable(jsonReader, dynamicFieldsTable);
                    Document dynamicFieldsDocument = new Document(Solr.RESPONSE_SCHEMA_DYNAMIC_FIELDS,
                            dynamicFieldsTable);
                    schemaDocument.addRelationship(Solr.RESPONSE_SCHEMA_DYNAMIC_FIELDS, dynamicFieldsDocument);
                } else if (StringUtils.equals(jsonName, OBJECT_SCHEMA_COPY_FIELDS)) {
                    populateSchemaFieldTable(jsonReader, copyFieldsTable);
                    Document copyFieldsDocument = new Document(Solr.RESPONSE_SCHEMA_COPY_FIELDS,
                            copyFieldsTable);
                    schemaDocument.addRelationship(Solr.RESPONSE_SCHEMA_COPY_FIELDS, copyFieldsDocument);
                } else
                    jsonReader.skipValue();
            }
            jsonReader.endObject();
        } else
            jsonReader.skipValue();
    }
    jsonReader.endObject();
    jsonReader.close();

    return schemaDocument;
}

From source file:com.owncloud.android.lib.resources.activities.models.RichElementTypeAdapter.java

License:Open Source License

private RichObject readObject(String tag, JsonReader in) throws IOException {
    in.beginObject();/*  w  w  w. j av a2s  . c  o m*/
    RichObject richObject = new RichObject();
    richObject.setTag(tag);
    while (in.hasNext()) {
        String name;
        try {
            name = in.nextName();
        } catch (IllegalStateException e) {
            name = "";
        }

        switch (name) {
        case "type":
            richObject.setType(getNextString(in));
            break;
        case "id":
            richObject.setId(getNextString(in));
            break;
        case "name":
            richObject.setName(getNextString(in));
            break;
        case "path":
            richObject.setPath(getNextString(in));
            break;
        case "link":
            richObject.setLink(getNextString(in));
            break;
        case "server":
            richObject.setLink(getNextString(in));
            break;
        default:
            in.skipValue(); // ignore value
            break;
        }
    }
    in.endObject();
    return richObject;
}

From source file:com.patloew.countries.util.CountryTypeAdapter.java

License:Apache License

@Override
public Country read(JsonReader in) throws IOException {
    try {/*  w  w  w . j a  v a2s.c  o m*/
        Country country = new Country();

        in.beginObject();

        while (in.hasNext()) {
            String name = in.nextName();

            switch (name) {
            case "alpha2Code": {
                // cannot be null, because it is the primary key
                country.alpha2Code = in.nextString();
                break;
            }
            case "alpha3Code": {
                country.alpha3Code = JsonUtils.readNullSafeString(in);
                break;
            }
            case "name": {
                country.name = JsonUtils.readNullSafeString(in);
                break;
            }
            case "nativeName": {
                country.nativeName = JsonUtils.readNullSafeString(in);
                break;
            }
            case "region": {
                country.region = JsonUtils.readNullSafeString(in);
                break;
            }
            case "capital": {
                country.capital = JsonUtils.readNullSafeString(in);
                break;
            }
            case "population": {
                country.population = JsonUtils.readNullSafeInteger(in);
                break;
            }
            case "latlng": {
                if (in.peek() == JsonToken.NULL) {
                    in.nextNull();
                } else {
                    in.beginArray();
                    if (in.hasNext()) {
                        country.lat = JsonUtils.readNullSafeFloat(in);
                    }
                    if (in.hasNext()) {
                        country.lng = JsonUtils.readNullSafeFloat(in);
                    }
                    in.endArray();
                }
                break;
            }
            case "currencies": {
                country.currencies = RealmStringListTypeAdapter.INSTANCE.read(in);
                break;
            }
            case "borders": {
                country.borders = RealmStringListTypeAdapter.INSTANCE.read(in);
                break;
            }
            case "languages": {
                country.languages = RealmStringListTypeAdapter.INSTANCE.read(in);
                break;
            }
            case "translations": {
                country.translations = RealmStringMapEntryListTypeAdapter.INSTANCE.read(in);
                break;
            }
            default:
                in.skipValue();
                break;
            }
        }

        in.endObject();

        return country;
    } catch (Exception e) {
        throw new IOException(e);
    }
}

From source file:com.ptapp.sync.PTAppDataHandler.java

License:Open Source License

/**
 * Processes a conference data body and calls the appropriate data type handlers
 * to process each of the objects represented therein.
 *
 * @param dataBody The body of data to process
 * @throws java.io.IOException If there is an error parsing the data.
 *///ww w  .  j a v  a  2s .  c  om
private void processDataBody(String dataBody) throws IOException {
    JsonReader reader = new JsonReader(new StringReader(dataBody));
    JsonParser parser = new JsonParser();
    try {
        reader.setLenient(true); // To err is human

        // the whole file is a single JSON object
        reader.beginObject();

        while (reader.hasNext()) {
            // the key is "educators", "courses", "students", "events" etc.
            String key = reader.nextName();
            if (mHandlerForKey.containsKey(key)) {
                // pass the value to the corresponding handler
                mHandlerForKey.get(key).process(parser.parse(reader));
            } else {
                LOGW(TAG, "Skipping unknown key in ptapp data json: " + key);
                reader.skipValue();
            }
        }
        reader.endObject();
    } finally {
        reader.close();
    }
}