List of usage examples for com.google.gson.stream JsonReader endArray
public void endArray() throws IOException
From source file:org.eclipse.packagedrone.repo.channel.apm.ChannelReader.java
License:Open Source License
private Set<String> readSet(final JsonReader jr) throws IOException { final Set<String> result = new HashSet<>(); jr.beginArray();//from w w w. ja v a 2 s .co m while (jr.hasNext()) { result.add(jr.nextString()); } jr.endArray(); return result; }
From source file:org.eclipse.recommenders.utils.gson.MultisetTypeAdapterFactory.java
License:Open Source License
private <E> TypeAdapter<Multiset<E>> newMultisetAdapter(final TypeAdapter<E> elementAdapter) { return new TypeAdapter<Multiset<E>>() { public void write(JsonWriter out, Multiset<E> multiset) throws IOException { if (multiset == null) { out.nullValue();/*from w w w .j ava 2 s. c o m*/ return; } out.beginArray(); for (Entry<E> entry : multiset.entrySet()) { out.beginObject(); out.name("id"); elementAdapter.write(out, entry.getElement()); out.name("count"); out.value(entry.getCount()); out.endObject(); } out.endArray(); } public Multiset<E> read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { in.nextNull(); return null; } Multiset<E> result = LinkedHashMultiset.create(); in.beginArray(); while (in.hasNext()) { in.beginObject(); in.nextName(); // "id" E element = elementAdapter.read(in); in.nextName(); // "count" int count = in.nextInt(); result.add(element, count); in.endObject(); } in.endArray(); return result; } }; }
From source file:org.eclipse.smarthome.storage.json.PropertiesTypeAdapter.java
License:Open Source License
@Override public Map<String, Object> read(JsonReader in) throws IOException { // gson implementation code is modified when deserializing numbers JsonToken peek = in.peek();//from w w w . j a va2 s . c om if (peek == JsonToken.NULL) { in.nextNull(); return null; } Map<String, Object> map = constructor.get(TOKEN).construct(); if (peek == JsonToken.BEGIN_ARRAY) { in.beginArray(); while (in.hasNext()) { in.beginArray(); // entry array String key = keyAdapter.read(in); // modification Object value = getValue(in); Object replaced = map.put(key, value); if (replaced != null) { throw new JsonSyntaxException("duplicate key: " + key); } in.endArray(); } in.endArray(); } else { in.beginObject(); while (in.hasNext()) { JsonReaderInternalAccess.INSTANCE.promoteNameToValue(in); String key = keyAdapter.read(in); // modification Object value = getValue(in); Object replaced = map.put(key, value); if (replaced != null) { throw new JsonSyntaxException("duplicate key: " + key); } } in.endObject(); } return map; }
From source file:org.eclipse.thym.core.plugin.registry.CordovaPluginRegistryManager.java
License:Open Source License
private CordovaRegistryPluginInfo parseCordovaRegistryPluginInfo(JsonReader reader) throws IOException { CordovaRegistryPluginInfo info = new CordovaRegistryPluginInfo(); reader.beginObject();//w w w .j a va 2s . c o m reader.skipValue(); // name reader.beginArray(); reader.nextString(); //ecosystem:cordova info.setName(safeReadStringValue(reader)); info.setDescription(safeReadStringValue(reader)); reader.endArray(); reader.nextName(); reader.nextInt(); reader.endObject(); return info; }
From source file:org.eclipse.thym.core.plugin.registry.CordovaPluginRegistryManager.java
License:Open Source License
private void parseMaintainers(JsonReader reader, CordovaRegistryPlugin plugin) throws IOException { reader.beginArray();/*w w w . j ava2 s . co m*/ String name = null, email = null; JsonToken token = reader.peek(); while (token != JsonToken.END_ARRAY) { switch (token) { case BEGIN_OBJECT: reader.beginObject(); name = email = null; break; case END_OBJECT: reader.endObject(); plugin.addMaintainer(email, name); break; case NAME: String tagName = reader.nextName(); if ("name".equals(tagName)) { name = reader.nextString(); break; } if ("email".equals(tagName)) { email = reader.nextString(); break; } default: Assert.isTrue(false, "Unexpected token"); break; } token = reader.peek(); } reader.endArray(); }
From source file:org.eclipse.thym.core.plugin.registry.CordovaPluginRegistryManager.java
License:Open Source License
private void parseKeywords(JsonReader reader, CordovaRegistryPlugin plugin) throws IOException { reader.beginArray();//from ww w . j av a 2s .co m while (reader.hasNext()) { plugin.addKeyword(reader.nextString()); } reader.endArray(); }
From source file:org.eclipse.tm4e.core.internal.parser.json.JSONPListParser.java
License:Open Source License
public T parse(InputStream contents) throws Exception { PList<T> pList = new PList<T>(theme); JsonReader reader = new JsonReader(new InputStreamReader(contents, StandardCharsets.UTF_8)); // reader.setLenient(true); boolean parsing = true; while (parsing) { JsonToken nextToken = reader.peek(); switch (nextToken) { case BEGIN_ARRAY: pList.startElement(null, "array", null, null); reader.beginArray();//ww w . j av a2s . c o m break; case END_ARRAY: pList.endElement(null, "array", null); reader.endArray(); break; case BEGIN_OBJECT: pList.startElement(null, "dict", null, null); reader.beginObject(); break; case END_OBJECT: pList.endElement(null, "dict", null); reader.endObject(); break; case NAME: String lastName = reader.nextName(); pList.startElement(null, "key", null, null); pList.characters(lastName.toCharArray(), 0, lastName.length()); pList.endElement(null, "key", null); break; case NULL: reader.nextNull(); break; case BOOLEAN: reader.nextBoolean(); break; case NUMBER: reader.nextLong(); break; case STRING: String value = reader.nextString(); pList.startElement(null, "string", null, null); pList.characters(value.toCharArray(), 0, value.length()); pList.endElement(null, "string", null); break; case END_DOCUMENT: parsing = false; break; default: break; } } reader.close(); return pList.getResult(); }
From source file:org.fao.fenix.wds.core.datasource.DatasourcePool.java
License:Open Source License
/** * @throws FileNotFoundException// w ww . j a va2s.c o m * @throws IOException * * This method is invoked by Spring at the start-up. This function retrieves * all the files stored in the <code>datasourcePath</code> and populate the * <code>datasources</code> map where the key is the datasource name (e.g. 'FAOSTAT') * and the value is the <code>DatasourceBean</code>. */ public void init() throws FileNotFoundException, IOException { Gson g = new Gson(); File root = new File(this.datasourcePath); File[] files = root.listFiles(); for (int i = 0; i < files.length; i++) { InputStream is = new FileInputStream(files[i].getAbsoluteFile()); JsonReader reader = new JsonReader(new InputStreamReader(is, "UTF-8")); reader.beginArray(); while (reader.hasNext()) { DatasourceBean b = readMessage(reader); this.datasources.put(b.getId(), b); } reader.endArray(); } }
From source file:org.gradle.api.internal.artifacts.ivyservice.ivyresolve.parser.ModuleMetadataParser.java
License:Apache License
private void consumeVariants(JsonReader reader, MutableModuleComponentResolveMetadata metadata) throws IOException { reader.beginArray();//from w w w . ja v a2s .co m while (reader.peek() != JsonToken.END_ARRAY) { consumeVariant(reader, metadata); } reader.endArray(); }
From source file:org.gradle.api.internal.artifacts.ivyservice.ivyresolve.parser.ModuleMetadataParser.java
License:Apache License
private List<ModuleDependency> consumeDependencies(JsonReader reader) throws IOException { List<ModuleDependency> dependencies = new ArrayList<ModuleDependency>(); reader.beginArray();/*from ww w . j a v a 2s. c o m*/ while (reader.peek() != END_ARRAY) { reader.beginObject(); String group = null; String module = null; String reason = null; ImmutableAttributes attributes = ImmutableAttributes.EMPTY; VersionConstraint version = DefaultImmutableVersionConstraint.of(); ImmutableList<ExcludeMetadata> excludes = ImmutableList.of(); while (reader.peek() != END_OBJECT) { String name = reader.nextName(); if (name.equals("group")) { group = reader.nextString(); } else if (name.equals("module")) { module = reader.nextString(); } else if (name.equals("version")) { version = consumeVersion(reader); } else if (name.equals("excludes")) { excludes = consumeExcludes(reader); } else if (name.equals("reason")) { reason = reader.nextString(); } else if (name.equals("attributes")) { attributes = consumeAttributes(reader); } else { consumeAny(reader); } } dependencies.add(new ModuleDependency(group, module, version, excludes, reason, attributes)); reader.endObject(); } reader.endArray(); return dependencies; }