List of usage examples for com.google.gson.stream JsonReader hasNext
public boolean hasNext() throws IOException
From source file:org.eclipse.packagedrone.repo.channel.apm.ChannelReader.java
License:Open Source License
private List<ValidationMessage> readValidationMessages(final JsonReader jr) throws IOException { final List<ValidationMessage> result = new LinkedList<>(); jr.beginArray();//from w w w . ja va 2s . c om while (jr.hasNext()) { result.add(readValidationMessage(jr)); } jr.endArray(); return result; }
From source file:org.eclipse.packagedrone.repo.channel.apm.ChannelReader.java
License:Open Source License
private ValidationMessage readValidationMessage(final JsonReader jr) throws IOException { String aspectId = null;/*from w w w. ja va 2s .c o m*/ Severity severity = null; String message = null; Set<String> artifactIds = Collections.emptySet(); jr.beginObject(); while (jr.hasNext()) { final String name = jr.nextName(); switch (name) { case "aspectId": aspectId = jr.nextString(); break; case "severity": severity = Severity.valueOf(jr.nextString()); break; case "message": message = jr.nextString(); break; case "artifactIds": artifactIds = readSet(jr); break; } } jr.endObject(); if (aspectId == null || severity == null || message == null) { throw new IOException("Missing values in validation message"); } return new ValidationMessage(aspectId, severity, message, artifactIds); }
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();/* ww w . java 2s.c om*/ while (jr.hasNext()) { result.add(jr.nextString()); } jr.endArray(); return result; }
From source file:org.eclipse.packagedrone.repo.channel.apm.ChannelReader.java
License:Open Source License
private Map<MetaKey, CacheEntryInformation> readCacheEntries(final JsonReader jr) throws IOException { final Map<MetaKey, CacheEntryInformation> result = new HashMap<>(); jr.beginObject();/*from www . j a v a2 s . com*/ while (jr.hasNext()) { final String entryName = jr.nextName(); jr.beginObject(); String name = null; Long size = null; String mimeType = null; Instant timestamp = null; while (jr.hasNext()) { final String ele = jr.nextName(); switch (ele) { case "name": name = jr.nextString(); break; case "size": size = jr.nextLong(); break; case "mimeType": mimeType = jr.nextString(); break; case "timestamp": timestamp = readTime(jr); break; default: jr.skipValue(); break; } } if (name == null || size == null || mimeType == null || timestamp == null) { throw new IOException("Invalid format"); } jr.endObject(); final MetaKey key = MetaKey.fromString(entryName); result.put(key, new CacheEntryInformation(key, name, size, mimeType, timestamp)); } jr.endObject(); return result; }
From source file:org.eclipse.packagedrone.repo.MetaKeys.java
License:Open Source License
public static Map<MetaKey, String> readJson(final Reader input) throws IOException { @SuppressWarnings("resource") final JsonReader reader = new JsonReader(input); final Map<MetaKey, String> result = new HashMap<>(); reader.beginObject();//from ww w . j a v a2s .c o m while (reader.hasNext()) { final String name = reader.nextName(); if (reader.peek() == JsonToken.NULL) { reader.nextNull(); } else { result.put(MetaKey.fromString(name), reader.nextString()); } } reader.endObject(); 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 a v a2 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.automation.parser.gson.internal.RuleGSONParser.java
License:Open Source License
@Override public Set<Rule> parse(InputStreamReader reader) throws ParsingException { JsonReader jr = new JsonReader(reader); try {// ww w.ja va 2s. c o m if (jr.hasNext()) { JsonToken token = jr.peek(); if (JsonToken.BEGIN_ARRAY.equals(token)) { Rule[] rules = gson.fromJson(jr, Rule[].class); return new HashSet<Rule>(Arrays.asList(rules)); } else { Rule rule = gson.fromJson(jr, Rule.class); Set<Rule> rules = new HashSet<Rule>(); rules.add(rule); return rules; } } } catch (Exception e) { throw new ParsingException(new ParsingNestedException(ParsingNestedException.RULE, null, e)); } finally { try { jr.close(); } catch (IOException e) { } } return Collections.emptySet(); }
From source file:org.eclipse.smarthome.automation.parser.gson.internal.TemplateGSONParser.java
License:Open Source License
@Override public Set<Template> parse(InputStreamReader reader) throws ParsingException { JsonReader jr = new JsonReader(reader); try {//from w w w .j a v a 2 s .c o m if (jr.hasNext()) { JsonToken token = jr.peek(); if (JsonToken.BEGIN_ARRAY.equals(token)) { Template[] templates = gson.fromJson(jr, RuleTemplate[].class); return new HashSet<Template>(Arrays.asList(templates)); } else { Template template = gson.fromJson(jr, RuleTemplate.class); Set<Template> templates = new HashSet<Template>(); templates.add(template); return templates; } } } catch (Exception e1) { throw new ParsingException(new ParsingNestedException(ParsingNestedException.TEMPLATE, null, e1)); } finally { try { jr.close(); } catch (IOException e) { } } return Collections.emptySet(); }
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();// w w w . j a va 2s . co m 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
public List<CordovaRegistryPluginInfo> retrievePluginInfos(IProgressMonitor monitor) throws CoreException { if (monitor == null) monitor = new NullProgressMonitor(); monitor.beginTask("Retrieve plug-in registry catalog", 10); DefaultHttpClient theHttpClient = new DefaultHttpClient(); HttpUtil.setupProxy(theHttpClient);//w ww .jav a2 s .c om HttpClient client = new CachingHttpClient(theHttpClient, new HeapResourceFactory(), new BundleHttpCacheStorage(HybridCore.getContext().getBundle()), getCacheConfig()); JsonReader reader = null; try { if (monitor.isCanceled()) { return null; } String url = REGISTRY_URL + "-/_view/byKeyword?startkey=%5B%22ecosystem:cordova%22%5D&endkey=%5B%22ecosystem:cordova1%22%5D&group_level=3"; HttpGet get = new HttpGet(URI.create(url)); HttpResponse response = client.execute(get); HttpEntity entity = response.getEntity(); InputStream stream = entity.getContent(); monitor.worked(7); reader = new JsonReader(new InputStreamReader(stream)); reader.beginObject();//start the Registry final ArrayList<CordovaRegistryPluginInfo> plugins = new ArrayList<CordovaRegistryPluginInfo>(); while (reader.hasNext()) { JsonToken token = reader.peek(); switch (token) { case BEGIN_ARRAY: reader.beginArray(); break; case BEGIN_OBJECT: plugins.add(parseCordovaRegistryPluginInfo(reader)); break; default: reader.skipValue(); break; } } return plugins; } catch (ClientProtocolException e) { throw new CoreException( new Status(IStatus.ERROR, HybridCore.PLUGIN_ID, "Can not retrieve plugin catalog", e)); } catch (IOException e) { throw new CoreException( new Status(IStatus.ERROR, HybridCore.PLUGIN_ID, "Can not retrieve plugin catalog", e)); } finally { if (reader != null) try { reader.close(); } catch (IOException e) { /*ignored*/ } monitor.done(); } }