List of usage examples for com.google.gson.stream JsonReader nextString
public String nextString() throws IOException
From source file:org.mule.runtime.extension.internal.persistence.MuleVersionTypeAdapter.java
License:Open Source License
@Override public MuleVersion read(JsonReader in) throws IOException { return new MuleVersion(in.nextString()); }
From source file:org.netscan.core.json.CredentialJsonAdapter.java
License:Open Source License
@Override public Credential read(JsonReader in) throws IOException { in.beginObject();// ww w.ja va 2 s .c o m in.nextName(); String[] str = in.nextString().split(":"); in.endObject(); //fixme personalize add on credential list to avoid problematic credential config. return new Credential(str[0], str[1], str[2]); }
From source file:org.netscan.core.json.FilterJsonAdapter.java
License:Open Source License
@Override public Filter read(JsonReader in) throws IOException { in.beginObject();//from www. j a v a 2s. c om in.nextName(); String[] filter = in.nextString().replaceAll("\\s*", "").split(","); in.endObject(); return new Filter(Stream.of(filter).collect(Collectors.toList())); }
From source file:org.netscan.core.json.IPv4JsonAdapter.java
License:Open Source License
@Override public IPv4 read(JsonReader in) throws IOException { in.beginObject();// w w w.j ava2s. c o m in.nextName(); String ip = in.nextString(); in.endObject(); return new IPv4(ip); }
From source file:org.netxms.websvc.json.adapters.InetAddressAdapter.java
License:Open Source License
@Override public InetAddress read(JsonReader reader) throws IOException { if (reader.peek() == JsonToken.NULL) { reader.nextNull();/*from w w w .j av a 2 s . c o m*/ return null; } try { return InetAddress.getByName(reader.nextString()); } catch (UnknownHostException e) { return null; } }
From source file:org.netxms.websvc.json.adapters.MacAddressAdapter.java
License:Open Source License
@Override public MacAddress read(JsonReader reader) throws IOException { if (reader.peek() == JsonToken.NULL) { reader.nextNull();// w w w. jav a2 s . c o m return null; } try { return MacAddress.parseMacAddress(reader.nextString()); } catch (MacAddressFormatException e) { return null; } }
From source file:org.objectpocket.gson.CustomTypeAdapterFactory.java
License:Apache License
@Override public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type); // @Entity//from www. ja va 2 s . co m if (type.getRawType().getAnnotation(Entity.class) != null) { return new TypeAdapter<T>() { // SERIALIZE public void write(JsonWriter out, T obj) throws IOException { if (obj != null) { String id = objectPocket.getIdForObject(obj); // normalize if (!objectPocket.isSerializeAsRoot(obj)) { gson.toJson(new ProxyOut(obj.getClass().getTypeName(), id), ProxyOut.class, out); return; } else { objectPocket.setSerializeAsRoot(obj, false); } } // default serialization delegate.write(out, obj); }; // DESERIALIZE @SuppressWarnings("unchecked") @Override public T read(JsonReader in) throws IOException { if (in.getPath().length() > 2) { in.beginObject(); in.nextName(); StringBuilder sb = new StringBuilder(in.nextString()); String id = sb.substring(0, sb.indexOf("@")); in.endObject(); T obj = null; try { obj = (T) ReflectionUtil.instantiateDefaultConstructor(type.getRawType()); } catch (InstantiationException | IllegalAccessException | ClassNotFoundException | NoSuchMethodException | InvocationTargetException e) { throw new IOException("Could not instantiate class " + type.getRawType().getName() + "\n" + "Might be that the class has no default constructor!", e); } objectPocket.addIdFromReadObject(obj, id); return obj; } else { T obj = delegate.read(in); return obj; } } }; } // All other else { return delegate; } }
From source file:org.ohmage.OhmageApi.java
License:Apache License
private Response parseStreamingReadResponse(String url, HttpResponse response, StreamingResponseListener listener) { Result result = Result.HTTP_ERROR; String[] errorCodes = null;// ww w .j a va2 s . co m // the response object that will be returned; its type is decided by outputType // it's also populated by a call to populateFromJSON() which transforms the server response into the Response object Response candidate = new Response(); CountingInputStream inputstream = null; if (response != null) { Log.v(TAG, response.getStatusLine().toString()); if (response.getStatusLine().getStatusCode() == 200) { HttpEntity responseEntity = response.getEntity(); if (responseEntity != null) { try { JsonParser parser = new JsonParser(); JsonReader reader = new JsonReader(new InputStreamReader( new CountingInputStream(responseEntity.getContent()), "UTF-8")); reader.beginObject(); // expecting: {result: "<status>", data: [{},{},{}...]} while (reader.hasNext()) { String name = reader.nextName(); if ("result".equals(name)) { if (reader.nextString().equalsIgnoreCase("success")) { result = Result.SUCCESS; } else { result = Result.FAILURE; } } else if ("data".equals(name)) { reader.beginArray(); // do pre-read init listener.beforeRead(); while (reader.hasNext()) { JsonElement elem = parser.parse(reader); listener.readObject(elem.getAsJsonObject()); } // do post-read cleanup listener.afterRead(); reader.endArray(); } else if ("errors".equals(name)) { reader.beginArray(); List<String> errorList = new ArrayList<String>(); while (reader.hasNext()) { // read off each of the errors and stick it // into the error array JsonElement elem = parser.parse(reader); errorList.add(elem.getAsJsonObject().get("code").getAsString()); } errorCodes = errorList.toArray(new String[errorList.size()]); reader.endArray(); } else { reader.skipValue(); } } reader.endObject(); // and we're done! } catch (IOException e) { Log.e(TAG, "Problem reading response body", e); result = Result.INTERNAL_ERROR; } try { responseEntity.consumeContent(); } catch (IOException e) { Log.e(TAG, "Error consuming content", e); } } else { Log.e(TAG, "No response entity in response"); result = Result.HTTP_ERROR; } } else { Log.e(TAG, "Returned status code: " + String.valueOf(response.getStatusLine().getStatusCode())); result = Result.HTTP_ERROR; } } else { Log.e(TAG, "Response is null"); result = Result.HTTP_ERROR; } if (inputstream != null && result == Result.SUCCESS) Analytics.network(mContext, url, inputstream.amountRead()); listener.readResult(result, errorCodes); candidate.setResponseStatus(result, errorCodes); return candidate; }
From source file:org.onos.yangtools.yang.data.codec.gson.JsonParserStream.java
License:Open Source License
public void read(final JsonReader in, AbstractNodeDataWithSchema parent) throws IOException { switch (in.peek()) { case STRING:// ww w . ja va 2s .c o m case NUMBER: setValue(parent, in.nextString()); break; case BOOLEAN: setValue(parent, Boolean.toString(in.nextBoolean())); break; case NULL: in.nextNull(); setValue(parent, null); break; case BEGIN_ARRAY: in.beginArray(); while (in.hasNext()) { if (parent instanceof LeafNodeDataWithSchema) { read(in, parent); } else { final AbstractNodeDataWithSchema newChild = newArrayEntry(parent); read(in, newChild); } } in.endArray(); return; case BEGIN_OBJECT: final Set<String> namesakes = new HashSet<>(); in.beginObject(); /* * This allows parsing of incorrectly /as showcased/ * in testconf nesting of list items - eg. * lists with one value are sometimes serialized * without wrapping array. * */ if (isArray(parent)) { parent = newArrayEntry(parent); } while (in.hasNext()) { final String jsonElementName = in.nextName(); final NamespaceAndName namespaceAndName = resolveNamespace(jsonElementName, parent.getSchema()); final String localName = namespaceAndName.getName(); addNamespace(namespaceAndName.getUri()); if (namesakes.contains(jsonElementName)) { throw new JsonSyntaxException("Duplicate name " + jsonElementName + " in JSON input."); } namesakes.add(jsonElementName); final Deque<DataSchemaNode> childDataSchemaNodes = findSchemaNodeByNameAndNamespace( parent.getSchema(), localName, getCurrentNamespace()); if (childDataSchemaNodes.isEmpty()) { throw new IllegalStateException("Schema for node with name " + localName + " and namespace " + getCurrentNamespace() + " doesn't exist."); } final AbstractNodeDataWithSchema newChild = ((CompositeNodeDataWithSchema) parent) .addChild(childDataSchemaNodes); /* * FIXME:anyxml data shouldn't be skipped but should be loaded somehow. * will be able to load anyxml which conforms to YANG data using these * parser, for other anyxml will be harder. */ if (newChild instanceof AnyXmlNodeDataWithSchema) { in.skipValue(); } else { read(in, newChild); } removeNamespace(); } in.endObject(); return; case END_DOCUMENT: case NAME: case END_OBJECT: case END_ARRAY: break; } }
From source file:org.opendaylight.controller.sal.rest.gson.JsonParser.java
License:Open Source License
public JsonElement read(JsonReader in) throws IOException { switch (in.peek()) { case STRING:/*from w ww. jav a 2 s . c o m*/ return new JsonPrimitive(in.nextString()); case NUMBER: String number = in.nextString(); return new JsonPrimitive(new LazilyParsedNumber(number)); case BOOLEAN: return new JsonPrimitive(in.nextBoolean()); case NULL: in.nextNull(); return JsonNull.INSTANCE; case BEGIN_ARRAY: JsonArray array = new JsonArray(); in.beginArray(); while (in.hasNext()) { array.add(read(in)); } in.endArray(); return array; case BEGIN_OBJECT: JsonObject object = new JsonObject(); in.beginObject(); while (in.hasNext()) { final String childName = in.nextName(); if (object.has(childName)) { throw new JsonSyntaxException("Duplicate name " + childName + " in JSON input."); } object.add(childName, read(in)); } in.endObject(); return object; case END_DOCUMENT: case NAME: case END_OBJECT: case END_ARRAY: default: throw new IllegalArgumentException(); } }