List of usage examples for com.google.gson.stream JsonReader peek
public JsonToken peek() throws IOException
From source file:org.gradle.api.internal.artifacts.ivyservice.ivyresolve.parser.ModuleMetadataParser.java
License:Apache License
private ImmutableList<ExcludeMetadata> consumeExcludes(JsonReader reader) throws IOException { ImmutableList.Builder<ExcludeMetadata> builder = new ImmutableList.Builder<ExcludeMetadata>(); reader.beginArray();//from w w w. j a va2 s.co m while (reader.peek() != END_ARRAY) { reader.beginObject(); String group = null; String module = null; 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 { consumeAny(reader); } } reader.endObject(); ExcludeMetadata exclude = excludeRuleConverter.createExcludeRule(group, module); builder.add(exclude); } reader.endArray(); return builder.build(); }
From source file:org.gradle.api.internal.artifacts.ivyservice.ivyresolve.parser.ModuleMetadataParser.java
License:Apache License
private List<ModuleFile> consumeFiles(JsonReader reader) throws IOException { List<ModuleFile> files = new ArrayList<ModuleFile>(); reader.beginArray();/*from w w w .ja va 2s .c om*/ while (reader.peek() != END_ARRAY) { reader.beginObject(); String fileName = null; String fileUri = null; while (reader.peek() != END_OBJECT) { String name = reader.nextName(); if (name.equals("name")) { fileName = reader.nextString(); } else if (name.equals("url")) { fileUri = reader.nextString(); } else { consumeAny(reader); } } reader.endObject(); files.add(new ModuleFile(fileName, fileUri)); } reader.endArray(); return files; }
From source file:org.gradle.api.internal.artifacts.ivyservice.ivyresolve.parser.ModuleMetadataParser.java
License:Apache License
private ImmutableAttributes consumeAttributes(JsonReader reader) throws IOException { ImmutableAttributes attributes = ImmutableAttributes.EMPTY; reader.beginObject();/*from w w w. j av a2 s .c o m*/ while (reader.peek() != END_OBJECT) { String attrName = reader.nextName(); if (reader.peek() == BOOLEAN) { boolean attrValue = reader.nextBoolean(); attributes = attributesFactory.concat(attributes, Attribute.of(attrName, Boolean.class), attrValue); } else { String attrValue = reader.nextString(); attributes = attributesFactory.concat(attributes, Attribute.of(attrName, String.class), new CoercingStringValueSnapshot(attrValue, instantiator)); } } reader.endObject(); return attributes; }
From source file:org.gw2InfoViewer.services.json.JsonConversionService.java
License:Open Source License
public static EventList parseEventListWithoutNames(InputStream json) throws IOException { List<Event> events; events = new ArrayList<Event>(); JsonReader reader = new JsonReader(new InputStreamReader(json)); reader.beginObject();/*from w ww . ja va 2 s .c o m*/ if (reader.nextName().equals("events")) { reader.beginArray(); while (reader.peek() != JsonToken.END_ARRAY) { Event event = gsonBuilder.create().fromJson(reader, Event.class); events.add(event); } reader.endArray(); reader.endObject(); } return new EventList(events); }
From source file:org.gw2InfoViewer.services.json.typeadaptors.EventAdapter.java
License:Open Source License
@Override public Event read(JsonReader reader) throws IOException { if (reader.peek() == JsonToken.NULL) { reader.nextNull();/*ww w. j a v a 2 s. c o m*/ return null; } Event event = new Event(); reader.beginObject(); reader.nextName(); //world id event.setWorldId(reader.nextInt()); reader.nextName(); //map id event.setMapId(reader.nextInt()); reader.nextName(); //event id event.setEventId(reader.nextString()); reader.nextName(); event.setState(EventState.valueOf(reader.nextString())); //state reader.endObject(); return event; }
From source file:org.hibernate.search.backend.elasticsearch.document.model.impl.esnative.ElasticsearchFormatJsonAdapter.java
License:LGPL
@Override public List<String> read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { in.nextNull();//from w ww . j a v a2s. c o m return null; } String joinedFormats = in.nextString(); List<String> formats = Arrays.asList(joinedFormats.split(FORMAT_SEPARATOR_REGEX)); return formats; }
From source file:org.hibernate.search.backend.elasticsearch.document.model.impl.esnative.ElasticsearchRoutingTypeJsonAdapter.java
License:LGPL
@Override public RoutingType read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { in.nextNull();/* w w w .ja va 2 s . c o m*/ return null; } RoutingType value = null; in.beginObject(); while (in.hasNext()) { String name = in.nextName(); switch (name) { case "required": if (in.nextBoolean()) { value = RoutingType.REQUIRED; } else { value = RoutingType.OPTIONAL; } break; default: throw new AssertionFailure( "Unexpected property for attribute of type " + RoutingType.class + ": " + name); } } return value; }
From source file:org.hibernate.search.backend.elasticsearch.gson.impl.AbstractExtraPropertiesJsonAdapter.java
License:LGPL
@Override public T read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { in.nextNull();//from w ww. j a v a2s . c o m return null; } T instance = createInstance(); try { in.beginObject(); while (in.hasNext()) { String name = in.nextName(); FieldAdapter<? super T> fieldAdapter = fieldAdapters.get(name); if (fieldAdapter == null) { extraPropertyAdapter.readOne(in, name, instance); } else { fieldAdapter.read(in, instance); } } in.endObject(); } catch (IllegalStateException e) { throw new JsonSyntaxException(e); } return instance; }
From source file:org.hillview.table.rows.GuessSchema.java
License:Open Source License
/** * Throws if the value is not valid JSON * The gson parser is not strict enough: it parses * unquoted strings as JSON.String, so we have to do this manually. * Returns true if this is a complex json value, false otherwise. *///from ww w . j a va 2 s . c o m private static boolean isJsonValid(final JsonReader jsonReader) throws IOException { JsonToken token; boolean isComplex = false; loop: while ((token = jsonReader.peek()) != JsonToken.END_DOCUMENT && token != null) { switch (token) { case BEGIN_ARRAY: isComplex = true; jsonReader.beginArray(); break; case END_ARRAY: isComplex = true; jsonReader.endArray(); break; case BEGIN_OBJECT: isComplex = true; jsonReader.beginObject(); break; case END_OBJECT: isComplex = true; jsonReader.endObject(); break; case NAME: jsonReader.nextName(); break; case STRING: case NUMBER: case BOOLEAN: case NULL: jsonReader.skipValue(); break; case END_DOCUMENT: break loop; default: throw new AssertionError(token); } } return isComplex; }
From source file:org.jboss.tools.aerogear.hybrid.core.plugin.registry.CordovaPluginRegistryManager.java
License:Open Source License
public List<CordovaRegistryPluginInfo> retrievePluginInfos(IProgressMonitor monitor) throws CoreException { if (monitor == null) monitor = new NullProgressMonitor(); HttpClient client = new DefaultHttpClient(); String url = registry.endsWith("/") ? registry + "-/all" : registry + "/-/all"; HttpGet get = new HttpGet(url); HttpResponse response;//from www .j a v a 2 s . c o m try { if (monitor.isCanceled()) { return null; } response = client.execute(get); HttpEntity entity = response.getEntity(); InputStream stream = entity.getContent(); monitor.worked(9); JsonReader reader = new JsonReader(new InputStreamReader(stream)); reader.beginObject();//start the Registry plugins = new ArrayList<CordovaRegistryPluginInfo>(); while (reader.hasNext()) { JsonToken token = reader.peek(); switch (token) { case BEGIN_OBJECT: CordovaRegistryPluginInfo info = new CordovaRegistryPluginInfo(); readPluginInfo(reader, info); plugins.add(info); break; case NAME: String name = reader.nextName(); if (name.equals("_updated")) { long newUpdate = reader.nextLong(); if (newUpdate == this.updated) {//No changes return plugins; } } break; default: Assert.isTrue(false, "Unexpected token: " + token); break; } } reader.endObject(); 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 { monitor.done(); } }