List of usage examples for com.google.gson.stream JsonReader endArray
public void endArray() throws IOException
From source file:com.nridge.core.io.gson.DSCriteriaJSON.java
License:Open Source License
/** * Parses an JSON stream and loads it into a bag/table. * * @param aReader Json reader stream instance. * * @throws java.io.IOException I/O related exception. *//* w ww. j a v a 2 s . co m*/ public void load(JsonReader aReader) throws IOException { DataField dataField; String jsonName, jsonValue, logicalOperator; mDSCriteria.reset(); aReader.beginObject(); while (aReader.hasNext()) { jsonName = aReader.nextName(); if (StringUtils.equals(jsonName, IO.JSON_NAME_MEMBER_NAME)) mDSCriteria.setName(aReader.nextString()); else if (StringUtils.equals(jsonName, IO.JSON_FEATURES_ARRAY_NAME)) { aReader.beginObject(); while (aReader.hasNext()) { jsonName = aReader.nextName(); jsonValue = aReader.nextString(); mDSCriteria.addFeature(jsonName, jsonValue); } aReader.endObject(); } else if (StringUtils.equals(jsonName, IO.JSON_FIELDS_ARRAY_NAME)) { aReader.beginArray(); while (aReader.hasNext()) { dataField = mDataFieldJSON.load(aReader); if (dataField != null) { logicalOperator = dataField.getFeature(IO.JSON_OPERATOR_MEMBER_NAME); if (StringUtils.isEmpty(logicalOperator)) logicalOperator = Field.operatorToString(Field.Operator.EQUAL); mDSCriteria.add(dataField, Field.stringToOperator(logicalOperator)); } } aReader.endArray(); } else aReader.skipValue(); } aReader.endObject(); }
From source file:com.nridge.core.io.gson.JSONDocument.java
License:Open Source License
/** * Parses an JSON stream and loads it into an internally managed * document instance./*from w w w.ja v a2 s . c o m*/ * * @param aReader Json reader stream instance. * * @throws IOException I/O related exception. */ private void loadDocument(JsonReader aReader, Document aDocument) throws IOException { DataBag dataBag; JsonToken jsonToken; DataField dataField; String jsonName, jsonValue, jsonTitle; aReader.beginObject(); jsonToken = aReader.peek(); while (jsonToken == JsonToken.NAME) { jsonName = aReader.nextName(); jsonTitle = Field.nameToTitle(jsonName); jsonToken = aReader.peek(); switch (jsonToken) { case BOOLEAN: dataBag = aDocument.getBag(); dataField = new DataField(jsonName, jsonTitle, aReader.nextBoolean()); dataBag.add(dataField); break; case NUMBER: dataBag = aDocument.getBag(); jsonValue = aReader.nextString(); if (StringUtils.contains(jsonValue, StrUtl.CHAR_DOT)) dataField = new DataField(jsonName, jsonTitle, Double.valueOf(jsonValue)); else dataField = new DataField(jsonName, jsonTitle, Long.valueOf(jsonValue)); dataBag.add(dataField); break; case STRING: dataBag = aDocument.getBag(); jsonValue = aReader.nextString(); Date dateValue = DatUtl.detectCreateDate(jsonValue); if (dateValue != null) dataField = new DataField(jsonName, jsonTitle, dateValue); else dataField = new DataField(Field.Type.Text, jsonName, jsonTitle, jsonValue); dataBag.add(dataField); break; case NULL: dataBag = aDocument.getBag(); aReader.nextNull(); dataField = new DataField(Field.Type.Text, jsonName, jsonTitle); dataBag.add(dataField); break; case BEGIN_ARRAY: aReader.beginArray(); if (isNextTokenAnObject(aReader)) { Document childDocument = new Document(jsonTitle); childDocument.setName(jsonName); aDocument.addRelationship(jsonTitle, childDocument); loadDocumentArray(aReader, childDocument); } else { dataBag = aDocument.getBag(); dataField = new DataField(Field.Type.Text, jsonName, jsonTitle); jsonToken = aReader.peek(); while (jsonToken != JsonToken.END_ARRAY) { jsonValue = aReader.nextString(); dataField.addValue(jsonValue); jsonToken = aReader.peek(); } dataBag.add(dataField); } aReader.endArray(); break; case BEGIN_OBJECT: Document childDocument = new Document(jsonTitle); childDocument.setName(jsonName); aDocument.addRelationship(jsonTitle, childDocument); loadDocument(aReader, childDocument); break; default: aReader.skipValue(); break; } jsonToken = aReader.peek(); } aReader.endObject(); }
From source file:com.nridge.core.io.gson.RelationshipJSON.java
License:Open Source License
private void loadDocuments(JsonReader aReader, Relationship aRelationship) throws IOException { Document relationshipDocument; DocumentJSON documentJSON = new DocumentJSON(); ArrayList<Document> documentList = aRelationship.getDocuments(); aReader.beginArray();//from w w w.j a v a2 s . c o m while (aReader.hasNext()) { relationshipDocument = documentJSON.loadDocument(aReader); documentList.add(relationshipDocument); } aReader.endArray(); }
From source file:com.nridge.ds.solr.SolrCollection.java
License:Open Source License
private ArrayList<String> load(InputStream anIS) throws IOException { String jsonName, jsonValue;/*from w ww . jav a 2s.co m*/ 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, "collections")) { jsonReader.beginArray(); while (jsonReader.hasNext()) { jsonValue = jsonReader.nextString(); configSetList.add(jsonValue); } jsonReader.endArray(); } } jsonReader.endObject(); return configSetList; }
From source file:com.nridge.ds.solr.SolrConfigJSON.java
License:Open Source License
private void addFieldToDocument(JsonReader aReader, Document aDocument, String aName) throws IOException { DataBag childBag;/* w ww .ja v a2s .co m*/ JsonToken jsonToken; boolean isArrayArray; Document childDocument; String jsonName, jsonValue; DataTextField dataTextField; DataBag dataBag = aDocument.getBag(); if (isNextTokenAnArray(aReader)) { aReader.beginArray(); dataTextField = new DataTextField(aName, Field.nameToTitle(aName)); dataTextField.addFeature(Field.FEATURE_IS_STORED, StrUtl.STRING_TRUE); jsonToken = aReader.peek(); if (jsonToken == JsonToken.BEGIN_ARRAY) { isArrayArray = true; aReader.beginArray(); jsonToken = aReader.peek(); } else isArrayArray = false; while (jsonToken != JsonToken.END_ARRAY) { jsonValue = nextValueAsString(aReader); dataTextField.addValue(jsonValue); jsonToken = aReader.peek(); } aReader.endArray(); if (isArrayArray) aReader.endArray(); dataBag.add(dataTextField); } else if (isNextTokenAnObject(aReader)) { childDocument = new Document(aName); childDocument.setName(aName); childBag = childDocument.getBag(); aReader.beginObject(); jsonToken = aReader.peek(); while (jsonToken != JsonToken.END_OBJECT) { jsonName = aReader.nextName(); jsonValue = nextValueAsString(aReader); addBagTextField(childBag, jsonName, jsonValue); jsonToken = aReader.peek(); } aReader.endObject(); aDocument.addRelationship(aName, childDocument); } else { jsonValue = nextValueAsString(aReader); addBagTextField(dataBag, aName, jsonValue); } }
From source file:com.nridge.ds.solr.SolrConfigJSON.java
License:Open Source License
private void addObjectToDocument(JsonReader aReader, Document aParentDocument, String aType, String aName) throws IOException { String jsonValue;/*from w w w . j av a2s . c o m*/ JsonToken jsonToken; boolean isArrayArray; Document childDocument; DataBag dataBag = aParentDocument.getBag(); if (isNextTokenAnArray(aReader)) { aReader.beginArray(); jsonToken = aReader.peek(); if (jsonToken == JsonToken.BEGIN_ARRAY) { isArrayArray = true; aReader.beginArray(); jsonToken = aReader.peek(); } else isArrayArray = false; while (jsonToken != JsonToken.END_ARRAY) { childDocument = new Document(aType); childDocument.setName(aName); aReader.beginObject(); while (aReader.hasNext()) addFieldToDocument(aReader, childDocument); aReader.endObject(); aParentDocument.addRelationship(aType, childDocument); jsonToken = aReader.peek(); } aReader.endArray(); if (isArrayArray) aReader.endArray(); } else if (isNextTokenAnObject(aReader)) { childDocument = new Document(aType); childDocument.setName(aName); aReader.beginObject(); while (aReader.hasNext()) addFieldToDocument(aReader, childDocument); aReader.endObject(); aParentDocument.addRelationship(aType, childDocument); } else { jsonValue = nextValueAsString(aReader); addBagTextField(dataBag, aName, jsonValue); } }
From source file:com.nridge.ds.solr.SolrConfigJSON.java
License:Open Source License
private void populateSearchComponent(JsonReader aReader, Document aCfgDocument, String aDocType) throws IOException { DataBag scBag;/*from w w w . j av a 2 s .co m*/ Document scDocument; JsonToken jsonToken; boolean isArrayArray; Document childDocument; String jsonName, jsonValue; Document searchComponentDocument = new Document(aDocType); aReader.beginObject(); while (aReader.hasNext()) { jsonName = aReader.nextName(); scDocument = new Document(Solr.RESPONSE_CONFIG_SEARCH_COMPONENT); scDocument.setName(jsonName); scBag = scDocument.getBag(); DataTextField operationField = new DataTextField(Solr.CONFIG_OPERATION_FIELD_NAME, Field.nameToTitle(Solr.CONFIG_OPERATION_FIELD_NAME)); operationField.addFeature(Field.FEATURE_IS_STORED, StrUtl.STRING_FALSE); scBag.add(operationField); aReader.beginObject(); while (aReader.hasNext()) { jsonName = aReader.nextName(); if (isNextTokenAnArray(aReader)) { aReader.beginArray(); jsonToken = aReader.peek(); if (jsonToken == JsonToken.BEGIN_ARRAY) { isArrayArray = true; aReader.beginArray(); jsonToken = aReader.peek(); } else isArrayArray = false; while (jsonToken != JsonToken.END_ARRAY) { childDocument = new Document(Field.nameToTitle(jsonName)); childDocument.setName(jsonName); aReader.beginObject(); while (aReader.hasNext()) addFieldToDocument(aReader, childDocument); aReader.endObject(); scDocument.addRelationship(Field.nameToTitle(jsonName), childDocument); jsonToken = aReader.peek(); } aReader.endArray(); if (isArrayArray) aReader.endArray(); } else if (isNextTokenAnObject(aReader)) { childDocument = new Document(Field.nameToTitle(jsonName)); childDocument.setName(jsonName); aReader.beginObject(); while (aReader.hasNext()) addFieldToDocument(aReader, childDocument); aReader.endObject(); scDocument.addRelationship(Field.nameToTitle(jsonName), childDocument); } else { jsonValue = nextValueAsString(aReader); addBagTextField(scBag, jsonName, jsonValue); } } aReader.endObject(); assignSearchComponentType(scDocument); searchComponentDocument.addRelationship(scDocument.getType(), scDocument); } aCfgDocument.addRelationship(aDocType, searchComponentDocument); }
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 w w . ja va 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.SolrConfigSet.java
License:Open Source License
private ArrayList<String> load(InputStream anIS) throws IOException { String jsonName, jsonValue;/*from w ww .j a v a 2s . c o m*/ 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;/*w ww . j ava2s . 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; }