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

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

Introduction

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

Prototype

public boolean hasNext() throws IOException 

Source Link

Document

Returns true if the current array or object has another element.

Usage

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

License:Open Source License

private void populateSchemaFieldTypeAnalyzer(JsonReader aReader, Document aFTDocument, String aDocType)
        throws IOException {
    String jsonName, jsonValue;//from  w  w w . j  av a2s . co m
    DataTextField dataTextField;
    DataBag tokenizerBag, filterBag;
    Document tokenizerDocument, filterDocument;

    Document analyzerDocument = new Document(aDocType);
    DataBag analyzerBag = analyzerDocument.getBag();
    aReader.beginObject();
    while (aReader.hasNext()) {
        jsonName = aReader.nextName();
        if (StringUtils.equals(jsonName, OBJECT_SCHEMA_FT_ANALYZER_TOKENIZER)) {
            tokenizerDocument = new Document(Solr.RESPONSE_SCHEMA_FTA_TOKENIZER);
            tokenizerDocument.setName(jsonName);
            tokenizerBag = tokenizerDocument.getBag();
            aReader.beginObject();
            while (aReader.hasNext()) {
                jsonName = aReader.nextName();
                jsonValue = nextValueAsString(aReader);
                dataTextField = new DataTextField(jsonName, Field.nameToTitle(jsonName), jsonValue);
                dataTextField.addFeature(Field.FEATURE_IS_STORED, StrUtl.STRING_TRUE);
                tokenizerBag.add(dataTextField);
            }
            aReader.endObject();
            analyzerDocument.addRelationship(Solr.RESPONSE_SCHEMA_FTA_TOKENIZER, tokenizerDocument);
        } else if (StringUtils.equals(jsonName, OBJECT_SCHEMA_FT_ANALYZER_FILTERS)) {
            aReader.beginArray();
            while (aReader.hasNext()) {
                filterDocument = new Document(Solr.RESPONSE_SCHEMA_FTA_FILTERS);
                filterDocument.setName(jsonName);
                filterBag = filterDocument.getBag();
                aReader.beginObject();
                while (aReader.hasNext()) {
                    jsonName = aReader.nextName();
                    jsonValue = nextValueAsString(aReader);
                    dataTextField = new DataTextField(jsonName, Field.nameToTitle(jsonName), jsonValue);
                    dataTextField.addFeature(Field.FEATURE_IS_STORED, StrUtl.STRING_TRUE);
                    filterBag.add(dataTextField);
                }
                aReader.endObject();
                analyzerDocument.addRelationship(Solr.RESPONSE_SCHEMA_FTA_FILTERS, filterDocument);
            }
            aReader.endArray();
        } else {
            jsonValue = nextValueAsString(aReader);
            if (StringUtils.equals(jsonName, "class"))
                analyzerDocument.setName(jsonName);
            dataTextField = new DataTextField(jsonName, Field.nameToTitle(jsonName), jsonValue);
            dataTextField.addFeature(Field.FEATURE_IS_STORED, StrUtl.STRING_TRUE);
            analyzerBag.add(dataTextField);
        }
    }
    aReader.endObject();

    aFTDocument.addRelationship(Solr.RESPONSE_SCHEMA_FT_ANALYZERS, analyzerDocument);
}

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

License:Open Source License

private void populateSchemaFieldTypes(JsonReader aReader, Document aSchemaDocument) throws IOException {
    DataBag fieldTypeBag;/*from ww  w.j  a  va 2s  .c o  m*/
    String jsonName, jsonValue;
    Document fieldTypeDocument;
    DataTextField dataTextField;

    aReader.beginArray();
    while (aReader.hasNext()) {
        fieldTypeDocument = new Document(Solr.RESPONSE_SCHEMA_FIELD_TYPE);
        fieldTypeBag = fieldTypeDocument.getBag();
        DataTextField operationField = new DataTextField(Solr.SCHEMA_OPERATION_FIELD_NAME,
                Field.nameToTitle(Solr.SCHEMA_OPERATION_FIELD_NAME));
        operationField.addFeature(Field.FEATURE_IS_STORED, StrUtl.STRING_FALSE);
        fieldTypeBag.add(operationField);
        aReader.beginObject();
        while (aReader.hasNext()) {
            jsonName = aReader.nextName();
            if (StringUtils.equals(jsonName, OBJECT_SCHEMA_FT_ANALYZER))
                populateSchemaFieldTypeAnalyzer(aReader, fieldTypeDocument, Solr.RESPONSE_SCHEMA_FT_ANALYZER);
            else if (StringUtils.equals(jsonName, OBJECT_SCHEMA_FT_INDEX_ANALYZER))
                populateSchemaFieldTypeAnalyzer(aReader, fieldTypeDocument,
                        Solr.RESPONSE_SCHEMA_FT_INDEX_ANALYZER);
            else if (StringUtils.equals(jsonName, OBJECT_SCHEMA_FT_QUERY_ANALYZER))
                populateSchemaFieldTypeAnalyzer(aReader, fieldTypeDocument,
                        Solr.RESPONSE_SCHEMA_FT_QUERY_ANALYZER);
            else {
                jsonValue = nextValueAsString(aReader);
                if (StringUtils.equals(jsonName, "name"))
                    fieldTypeDocument.setName(jsonValue);
                dataTextField = new DataTextField(jsonName, Field.nameToTitle(jsonName), jsonValue);
                dataTextField.addFeature(Field.FEATURE_IS_STORED, StrUtl.STRING_TRUE);
                fieldTypeBag.add(dataTextField);
            }
        }
        aReader.endObject();
        aSchemaDocument.addRelationship(Solr.RESPONSE_SCHEMA_FIELD_TYPE, fieldTypeDocument);
    }
    aReader.endArray();
}

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./* w  w  w  .j ava2  s.c o  m*/
 *
 * @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.oscarsalguero.smartystreetsautocomplete.json.GsonSmartyStreetsApiJsonParser.java

License:Apache License

@Override
public List<Address> readHistoryJson(final InputStream in) throws JsonParsingException {
    try {/*from   w  ww.  j  a  v a2s .  c o m*/
        JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
        List<Address> addresses = new ArrayList<>();
        reader.beginArray();
        while (reader.hasNext()) {
            Address message = gson.fromJson(reader, Address.class);
            addresses.add(message);
        }
        reader.endArray();
        reader.close();
        return addresses;
    } catch (Exception e) {
        throw new JsonParsingException(e);
    }
}

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

License:Open Source License

@Override
public RichElement read(JsonReader in) throws IOException {
    RichElement richElement = new RichElement();
    in.beginArray();/*from   w  w  w.  j a  v  a 2s  .  c  o  m*/
    int count = 0;
    while (in.hasNext()) {
        if (count == 0) {
            richElement.setRichSubject(in.nextString());
        } else {
            JsonToken nextType = in.peek();

            switch (nextType) {
            case BEGIN_OBJECT:
                in.beginObject();
                read(richElement, in);
                in.endObject();
                break;

            case BEGIN_ARRAY:
                in.beginArray();
                in.endArray();
                break;

            default:
                // do nothing
                break;
            }

        }
        count++;
    }

    in.endArray();

    return richElement;
}

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

License:Open Source License

private void read(RichElement richElement, JsonReader in) throws IOException {
    while (in.hasNext()) {
        String name = in.nextName();
        if (name != null && !name.isEmpty()) {
            richElement.getRichObjectList().add(readObject(name, in));
        }//w  w  w. j  ava2 s . co  m
    }
}

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();/*from w  ww .ja  v  a 2  s. c  om*/
    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 {//from www .  j  av  a2  s  . com
        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.patloew.countries.util.RealmStringListTypeAdapter.java

License:Apache License

@Override
public RealmList<RealmString> read(JsonReader in) throws IOException {
    RealmList<RealmString> realmStrings = new RealmList<>();

    in.beginArray();/*from www.j  a v a2s .c  o  m*/

    while (in.hasNext()) {
        if (in.peek() == JsonToken.NULL) {
            in.nextNull();
        } else {
            RealmString realmString = new RealmString();
            realmString.value = in.nextString();
            realmStrings.add(realmString);
        }
    }

    in.endArray();

    return realmStrings;
}

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

License:Apache License

@Override
public RealmList<RealmStringMapEntry> read(JsonReader in) throws IOException {
    RealmList<RealmStringMapEntry> mapEntries = new RealmList<>();

    in.beginObject();/*from w w w .  j  a  v  a2  s.  c  om*/

    while (in.hasNext()) {
        RealmStringMapEntry realmStringMapEntry = new RealmStringMapEntry();
        realmStringMapEntry.key = in.nextName();

        if (in.peek() == JsonToken.NULL) {
            in.nextNull();
            realmStringMapEntry.value = null;
        } else {
            realmStringMapEntry.value = in.nextString();
        }

        mapEntries.add(realmStringMapEntry);
    }

    in.endObject();

    return mapEntries;
}