List of usage examples for com.google.gson.stream JsonReader endObject
public void endObject() throws IOException
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;// w w w.ja v a 2s . c o 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 v a 2 s.co 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./* www . j a va2 s . 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
@Override public RichElement read(JsonReader in) throws IOException { RichElement richElement = new RichElement(); in.beginArray();/*from w w w.j av 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 RichObject readObject(String tag, JsonReader in) throws IOException { in.beginObject();/*from www . ja va 2 s.com*/ 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 ww . ja v a 2s . 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.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 a2s .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; }
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. *//*from w w w .j av a 2s .co m*/ 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(); } }
From source file:com.qdeve.oilprice.configuration.GsonMoneyTypeAdapter.java
License:Apache License
@Override public Money read(JsonReader in) throws IOException { String currency = null;//ww w. j a v a 2 s . c om Double price = null; in.beginObject(); while (in.hasNext()) { switch (in.nextName()) { case CURRENTY_TAG: currency = in.nextString(); break; case PRICE_TAG: price = in.nextDouble(); break; } } in.endObject(); return MonetaryUtils.newMoneyFrom(price, currency); }
From source file:com.quarterfull.newsAndroid.reader.owncloud.OwnCloudReaderMethods.java
License:Open Source License
private static JSONObject getJSONObjectFromReader(JsonReader jsonReader) { JSONObject jObj = new JSONObject(); JsonToken tokenInstance;//from w ww . j av a 2 s . c o m try { tokenInstance = jsonReader.peek(); if (tokenInstance == JsonToken.BEGIN_OBJECT) jsonReader.beginObject(); else if (tokenInstance == JsonToken.BEGIN_ARRAY) jsonReader.beginArray(); while (jsonReader.hasNext()) { JsonToken token; String name; try { name = jsonReader.nextName(); token = jsonReader.peek(); //Log.d(TAG, token.toString()); switch (token) { case NUMBER: jObj.put(name, jsonReader.nextLong()); break; case NULL: jsonReader.skipValue(); break; case BOOLEAN: jObj.put(name, jsonReader.nextBoolean()); break; case BEGIN_OBJECT: //jsonReader.beginObject(); jObj.put(name, getJSONObjectFromReader(jsonReader)); //jsonReader.endObject(); break; case BEGIN_ARRAY: jsonReader.skipValue(); break; default: jObj.put(name, jsonReader.nextString()); } } catch (Exception ex) { ex.printStackTrace(); jsonReader.skipValue(); } } if (tokenInstance == JsonToken.BEGIN_OBJECT) jsonReader.endObject(); else if (tokenInstance == JsonToken.BEGIN_ARRAY) jsonReader.endArray(); return jObj; } catch (Exception e) { e.printStackTrace(); } return null; }