List of usage examples for com.google.gson.stream JsonReader nextName
public String nextName() throws IOException
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./*from w w w. ja v a2 s .co 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.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)); }// ww w . j a v a2 s. c o 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 w w. j av a 2 s . co 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 {//from w ww. j av 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 a 2 s .c o m*/ 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.pocketbeer.presentation.flow.GsonParceler.java
License:Apache License
private Object decode(String json) throws IOException { JsonReader reader = new JsonReader(new StringReader(json)); try {/*from w ww . ja v a 2 s .c om*/ reader.beginObject(); Class<?> type = Class.forName(reader.nextName()); return mGson.fromJson(reader, type); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } finally { reader.close(); } }
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 . java 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;// w w w.jav a 2 s . c o m 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
/** * can parse json like {"items":[{"id":6782}]} * @param in/*from w w w.j av a 2 s . co m*/ * @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 }; }
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 ww.j a v a 2 s . c om * @param iJoBj * @return new int[] { count, newItemsCount } * @throws IOException */ public static int[] readJsonStreamV1(InputStream in, IHandleJsonObject iJoBj) throws IOException { int count = 0; int newItemsCount = 0; JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8")); reader.beginObject();//{ reader.nextName();//"ocs" reader.beginObject();//{ reader.nextName();//meta getJSONObjectFromReader(reader);//skip status etc. reader.nextName();//data reader.beginObject();//{ reader.nextName();//folders etc.. reader.beginArray(); while (reader.hasNext()) { //reader.beginObject(); JSONObject e = getJSONObjectFromReader(reader); if (iJoBj.performAction(e)) newItemsCount++; //reader.endObject(); count++; } if (iJoBj instanceof InsertItemIntoDatabase) ((InsertItemIntoDatabase) iJoBj).performDatabaseBatchInsert(); //Save pending buffer //reader.endArray(); //reader.endObject(); reader.close(); return new int[] { count, newItemsCount }; }