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

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

Introduction

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

Prototype

public void beginArray() throws IOException 

Source Link

Document

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

Usage

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

License:Open Source License

private ArrayList<String> load(InputStream anIS) throws IOException {
    String jsonName, jsonValue;// w w  w.j av a 2s  . c  om

    ArrayList<String> configSetList = new ArrayList<>();
    String configSetString = IOUtils.toString(anIS, StrUtl.CHARSET_UTF_8);
    StringReader stringReader = new StringReader(configSetString);
    JsonReader jsonReader = new JsonReader(stringReader);
    jsonReader.beginObject();
    while (jsonReader.hasNext()) {
        jsonName = jsonReader.nextName();
        if (StringUtils.equals(jsonName, "configSets")) {
            jsonReader.beginArray();
            while (jsonReader.hasNext()) {
                jsonValue = jsonReader.nextString();
                configSetList.add(jsonValue);
            }
            jsonReader.endArray();
        }
    }
    jsonReader.endObject();

    return configSetList;
}

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;//ww  w .  java  2 s .c  om
    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 void populateSchemaFieldTable(JsonReader aReader, DataTable aTable) throws IOException {
    String jsonName, jsonValue;/*from www . j a  va2  s.  c o m*/

    aReader.beginArray();
    while (aReader.hasNext()) {
        aTable.newRow();
        aReader.beginObject();
        while (aReader.hasNext()) {
            jsonName = aReader.nextName();
            jsonValue = nextValueAsString(aReader);
            aTable.setValueByName(jsonName, jsonValue);
        }
        aReader.endObject();
        aTable.addRow();
    }
    aReader.endArray();
}

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  ww .java 2s  . c om*/
    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   w ww. ja  va 2  s  .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.oscarsalguero.smartystreetsautocomplete.json.GsonSmartyStreetsApiJsonParser.java

License:Apache License

@Override
public List<Address> readHistoryJson(final InputStream in) throws JsonParsingException {
    try {/* w  ww. j av  a  2  s . co 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();
    int count = 0;
    while (in.hasNext()) {
        if (count == 0) {
            richElement.setRichSubject(in.nextString());
        } else {/*from  ww w  . j  ava 2s. c om*/
            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.patloew.countries.util.CountryTypeAdapter.java

License:Apache License

@Override
public Country read(JsonReader in) throws IOException {
    try {/*  w  w  w  .  ja v a  2s  .  c om*/
        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();

    while (in.hasNext()) {
        if (in.peek() == JsonToken.NULL) {
            in.nextNull();//from   w w w  .  j  a  v  a 2 s .  c o m
        } else {
            RealmString realmString = new RealmString();
            realmString.value = in.nextString();
            realmStrings.add(realmString);
        }
    }

    in.endArray();

    return realmStrings;
}

From source file:com.quarterfull.newsAndroid.reader.owncloud.OwnCloudReaderMethods.java

License:Open Source License

/**
 * can parse json like {"items":[{"id":6782}]}
 * @param in//from  w  w  w  .  java  2  s. c  om
 * @param iJoBj
 * @return count all, count new items
 * @throws IOException
 */
public static int[] readJsonStreamV2(InputStream in, IHandleJsonObject iJoBj) throws IOException {
    List<String> allowedArrays = Arrays.asList("feeds", "folders", "items");

    int count = 0;
    int newItemsCount = 0;
    JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
    reader.beginObject();

    String currentName;
    while (reader.hasNext() && (currentName = reader.nextName()) != null) {
        if (allowedArrays.contains(currentName))
            break;
        else
            reader.skipValue();
    }

    reader.beginArray();
    while (reader.hasNext()) {
        JSONObject e = getJSONObjectFromReader(reader);

        if (iJoBj.performAction(e))
            newItemsCount++;

        count++;
    }

    if (iJoBj instanceof InsertItemIntoDatabase)
        ((InsertItemIntoDatabase) iJoBj).performDatabaseBatchInsert(); //Save pending buffer

    //reader.endArray();
    //reader.endObject();
    reader.close();

    return new int[] { count, newItemsCount };
}