List of usage examples for com.google.gson.stream JsonToken BEGIN_ARRAY
JsonToken BEGIN_ARRAY
To view the source code for com.google.gson.stream JsonToken BEGIN_ARRAY.
Click Source Link
From source file:org.apache.jclouds.oneandone.rest.util.ServerFirewallPolicyAdapter.java
License:Apache License
@Override public List<T> read(JsonReader reader) throws IOException { List<ServerFirewallPolicy> list = new ArrayList<ServerFirewallPolicy>(); if (reader.peek() == JsonToken.BEGIN_OBJECT) { Type mapType = new TypeToken<Map<String, Object>>() { }.getType();/*w ww .j av a2 s.c o m*/ Map<String, String> jsonMap = gson.fromJson(reader, mapType); ServerFirewallPolicy inning = ServerFirewallPolicy.create(jsonMap.get("id"), jsonMap.get("name")); list.add(inning); } else if (reader.peek() == JsonToken.BEGIN_ARRAY) { reader.beginArray(); while (reader.hasNext()) { Type mapType = new TypeToken<Map<String, Object>>() { }.getType(); Map<String, String> jsonMap = gson.fromJson(reader, mapType); ServerFirewallPolicy inning = ServerFirewallPolicy.create(jsonMap.get("id"), jsonMap.get("name")); list.add(inning); } reader.endArray(); } else { reader.skipValue(); } return (List<T>) list; }
From source file:org.apache.jclouds.oneandone.rest.util.SnapshotAdapter.java
License:Apache License
@Override public List<T> read(JsonReader reader) throws IOException { List<Snapshot> list = new ArrayList<Snapshot>(); if (reader.peek() == JsonToken.BEGIN_OBJECT) { Type mapType = new TypeToken<Map<String, Object>>() { }.getType();// w ww . j ava 2 s .c o m Map<String, String> jsonMap = gson.fromJson(reader, mapType); Snapshot inning = Snapshot.create(jsonMap.get("id"), jsonMap.get("creation_date"), jsonMap.get("deletion_date")); list.add(inning); } else if (reader.peek() == JsonToken.BEGIN_ARRAY) { reader.beginArray(); while (reader.hasNext()) { Type mapType = new TypeToken<Map<String, Object>>() { }.getType(); Map<String, String> jsonMap = gson.fromJson(reader, mapType); Snapshot inning = Snapshot.create(jsonMap.get("id"), jsonMap.get("creation_date"), jsonMap.get("deletion_date")); list.add(inning); } reader.endArray(); } else { reader.skipValue(); } return (List<T>) list; }
From source file:org.apache.olingo.odata2.core.ep.consumer.JsonErrorDocumentConsumer.java
License:Apache License
private String readJson(final JsonReader reader) throws IOException { StringBuilder sb = new StringBuilder(); while (reader.hasNext()) { JsonToken token = reader.peek(); if (token == JsonToken.NAME) { if (sb.length() > 0) { sb.append(","); }// ww w .j a va 2 s. c om String name = reader.nextName(); sb.append("\"").append(name).append("\"").append(":"); } else if (token == JsonToken.BEGIN_OBJECT) { reader.beginObject(); sb.append("{").append(readJson(reader)).append("}"); reader.endObject(); } else if (token == JsonToken.BEGIN_ARRAY) { reader.beginArray(); sb.append("[").append(readJson(reader)).append("]"); reader.endArray(); } else { sb.append("\"").append(reader.nextString()).append("\""); } } return sb.toString(); }
From source file:org.apache.olingo.odata2.core.ep.consumer.JsonFeedConsumer.java
License:Apache License
private void readFeed() throws IOException, EdmException, EntityProviderException { JsonToken peek = reader.peek();/*from w ww . j a v a 2 s.c o m*/ if (peek == JsonToken.BEGIN_ARRAY) { readArrayContent(); } else { reader.beginObject(); final String nextName = reader.nextName(); if (FormatJson.D.equals(nextName)) { if (reader.peek() == JsonToken.BEGIN_ARRAY) { readArrayContent(); } else { reader.beginObject(); readFeedContent(); reader.endObject(); } } else { handleName(nextName); readFeedContent(); } reader.endObject(); } }
From source file:org.apache.olingo.odata2.core.ep.util.JsonUtils.java
License:Apache License
public static int startJson(final JsonReader reader) throws EntityProviderException { // The enclosing "d" and "results" are optional - so we cannot check for the presence // but we have to read over them in case they are present. JsonToken token;/*from ww w . j a v a 2s . com*/ try { token = reader.peek(); int openJsonObjects = 0; if (JsonToken.BEGIN_OBJECT == token) { reader.beginObject(); openJsonObjects++; token = reader.peek(); if (JsonToken.NAME == token) { String name = reader.nextName(); if (!("d".equals(name) ^ "results".equals(name))) { // TODO I18N throw new EntityProviderException(EntityProviderException.COMMON, name + " not expected, only d or results"); } } token = reader.peek(); if (JsonToken.BEGIN_OBJECT == token) { reader.beginObject(); openJsonObjects++; } else if (JsonToken.BEGIN_ARRAY == token) { // TODO I18N throw new EntityProviderException(EntityProviderException.COMMON, "Array not expected"); } } return openJsonObjects; } catch (IOException e) { // TODO I18N throw new EntityProviderException(EntityProviderException.COMMON, e); } }
From source file:org.arl.fjage.remote.ArrayAdapterFactory.java
License:BSD License
@SuppressWarnings("unchecked") public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) { final Class<T> rawType = (Class<T>) type.getRawType(); if (!rawType.isArray()) return null; final Class<?> compType = rawType.getComponentType(); if (compType == null) return null; if (!compType.isPrimitive()) return null; if (!compType.equals(byte.class) && !compType.equals(int.class) && !compType.equals(short.class) && !compType.equals(long.class) && !compType.equals(float.class) && !compType.equals(double.class)) return null; final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type); return new TypeAdapter<T>() { @Override//from w ww . j a va 2s.c o m public void write(JsonWriter out, T value) throws IOException { if (value == null) out.nullValue(); else { byte[] data; if (compType.equals(byte.class)) data = (byte[]) value; else { ByteBuffer buf = null; if (compType.equals(int.class)) { buf = ByteBuffer.allocate(Integer.SIZE / Byte.SIZE * ((int[]) value).length) .order(ByteOrder.LITTLE_ENDIAN); buf.asIntBuffer().put((int[]) value); } else if (compType.equals(short.class)) { buf = ByteBuffer.allocate(Short.SIZE / Byte.SIZE * ((short[]) value).length) .order(ByteOrder.LITTLE_ENDIAN); buf.asShortBuffer().put((short[]) value); } else if (compType.equals(long.class)) { buf = ByteBuffer.allocate(Long.SIZE / Byte.SIZE * ((long[]) value).length) .order(ByteOrder.LITTLE_ENDIAN); buf.asLongBuffer().put((long[]) value); } else if (compType.equals(float.class)) { buf = ByteBuffer.allocate(Float.SIZE / Byte.SIZE * ((float[]) value).length) .order(ByteOrder.LITTLE_ENDIAN); buf.asFloatBuffer().put((float[]) value); } else if (compType.equals(double.class)) { buf = ByteBuffer.allocate(Double.SIZE / Byte.SIZE * ((double[]) value).length) .order(ByteOrder.LITTLE_ENDIAN); buf.asDoubleBuffer().put((double[]) value); } data = buf.array(); } if (bare) out.value(Base64.getEncoder().encodeToString(data)); else { out.beginObject(); out.name("clazz").value(rawType.getName()); out.name("data").value(Base64.getEncoder().encodeToString(data)); out.endObject(); } } } @Override public T read(JsonReader in) throws IOException { JsonToken tok = in.peek(); if (tok == JsonToken.NULL) { in.nextNull(); return null; } if (tok == JsonToken.STRING) return decodeString(in.nextString()); if (tok == JsonToken.BEGIN_ARRAY) return delegate.read(in); if (tok != JsonToken.BEGIN_OBJECT) return null; T rv = null; in.beginObject(); while (in.hasNext()) { String name = in.nextName(); if (name.equals("data")) rv = decodeString(in.nextString()); else in.skipValue(); } in.endObject(); return rv; } private T decodeString(String s) { byte[] data = Base64.getDecoder().decode(s); if (compType.equals(byte.class)) return (T) data; ByteBuffer buf = ByteBuffer.wrap(data); if (compType.equals(int.class)) { IntBuffer buf2 = ByteBuffer.wrap(Base64.getDecoder().decode(s)).order(ByteOrder.LITTLE_ENDIAN) .asIntBuffer(); int[] array = new int[buf2.limit()]; buf2.get(array); return (T) array; } if (compType.equals(short.class)) { ShortBuffer buf2 = ByteBuffer.wrap(Base64.getDecoder().decode(s)).order(ByteOrder.LITTLE_ENDIAN) .asShortBuffer(); short[] array = new short[buf2.limit()]; buf2.get(array); return (T) array; } if (compType.equals(long.class)) { LongBuffer buf2 = ByteBuffer.wrap(Base64.getDecoder().decode(s)).order(ByteOrder.LITTLE_ENDIAN) .asLongBuffer(); long[] array = new long[buf2.limit()]; buf2.get(array); return (T) array; } if (compType.equals(float.class)) { FloatBuffer buf2 = ByteBuffer.wrap(Base64.getDecoder().decode(s)).order(ByteOrder.LITTLE_ENDIAN) .asFloatBuffer(); float[] array = new float[buf2.limit()]; buf2.get(array); return (T) array; } if (compType.equals(double.class)) { DoubleBuffer buf2 = ByteBuffer.wrap(Base64.getDecoder().decode(s)) .order(ByteOrder.LITTLE_ENDIAN).asDoubleBuffer(); double[] array = new double[buf2.limit()]; buf2.get(array); return (T) array; } return null; } }; }
From source file:org.dice_research.topicmodeling.io.json.stream.StreamBasedJsonDocumentSupplier.java
License:Open Source License
@Override public Document getNextDocument() { if (reader != null) { // If this is the first document if (documentCount == 0) { try { // It is possible that we have to read the beginning of the document array, // first. if (reader.hasNext() && JsonToken.BEGIN_ARRAY.equals(reader.peek())) { reader.beginArray(); }/*www .j a v a 2 s. c om*/ } catch (IOException e) { LOGGER.warn( "Got an IOException when trying to remove the beginning of a document array. Moving on.", e); } } document = readDocument(reader); if (document != null) { Document nextDocument = document; document = null; if (!useDocumentIdsFromFile) { nextDocument.setDocumentId(getNextDocumentId()); } ++documentCount; if (LOGGER.isInfoEnabled() && ((documentCount % 1000) == 0)) { LOGGER.info("Read the " + documentCount + "th document from JSON file."); } return nextDocument; } else { // The parser has reached the end of the file IOUtils.closeQuietly(reader); reader = null; } } return null; }
From source file:org.eclipse.lsp4j.adapters.HoverTypeAdapter.java
License:Open Source License
protected Either<List<Either<String, MarkedString>>, MarkupContent> readContents(final JsonReader in) throws IOException { final JsonToken nextToken = in.peek(); boolean _equals = Objects.equal(nextToken, JsonToken.STRING); if (_equals) { final List<Either<String, MarkedString>> value = CollectionLiterals .<Either<String, MarkedString>>newArrayList( Either.<String, MarkedString>forLeft(in.nextString())); return Either.<List<Either<String, MarkedString>>, MarkupContent>forLeft(value); } else {/* ww w. j a v a 2 s . c o m*/ boolean _equals_1 = Objects.equal(nextToken, JsonToken.BEGIN_ARRAY); if (_equals_1) { final List<Either<String, MarkedString>> value_1 = this.gson .<List<Either<String, MarkedString>>>fromJson(in, HoverTypeAdapter.LIST_STRING_MARKEDSTRING.getType()); return Either.<List<Either<String, MarkedString>>, MarkupContent>forLeft(value_1); } else { JsonElement _parse = new JsonParser().parse(in); final JsonObject object = ((JsonObject) _parse); boolean _has = object.has("language"); if (_has) { final List<Either<String, MarkedString>> value_2 = CollectionLiterals .<Either<String, MarkedString>>newArrayList(Either.<String, MarkedString>forRight( this.gson.<MarkedString>fromJson(object, MarkedString.class))); return Either.<List<Either<String, MarkedString>>, MarkupContent>forLeft(value_2); } else { return Either.<List<Either<String, MarkedString>>, MarkupContent>forRight( this.gson.<MarkupContent>fromJson(object, MarkupContent.class)); } } } }
From source file:org.eclipse.lsp4j.jsonrpc.json.adapters.CollectionTypeAdapter.java
License:Open Source License
@Override public Collection<E> read(JsonReader in) throws IOException { JsonToken peek = in.peek();//from w w w .j a v a2 s . c o m if (peek == JsonToken.NULL) { in.nextNull(); return null; } else if (peek == JsonToken.BEGIN_ARRAY) { Collection<E> collection = constructor.get(); in.beginArray(); while (in.hasNext()) { E instance = elementTypeAdapter.read(in); collection.add(instance); } in.endArray(); return collection; } else { Collection<E> collection = constructor.get(); E instance = elementTypeAdapter.read(in); collection.add(instance); return collection; } }
From source file:org.eclipse.lsp4j.jsonrpc.json.adapters.MessageTypeAdapter.java
License:Open Source License
/** * Convert the json input into the parameters object corresponding to the call * made by method.// ww w . ja v a2s.c o m * * If the method is not known until after parsing, call * {@link #parseParams(Object, String)} on the return value of this call for a * second chance conversion. * * @param in * json input to read from * @param method * method name of request * @return correctly typed object if the correct expected type can be * determined, or a JsonElement representing the parameters */ protected Object parseParams(JsonReader in, String method) throws IOException, JsonIOException { JsonToken next = in.peek(); if (next == JsonToken.NULL) { in.nextNull(); return null; } Type[] parameterTypes = getParameterTypes(method); if (parameterTypes.length == 1) { return fromJson(in, parameterTypes[0]); } if (parameterTypes.length > 1 && next == JsonToken.BEGIN_ARRAY) { List<Object> parameters = new ArrayList<Object>(parameterTypes.length); int index = 0; in.beginArray(); while (in.hasNext()) { Type parameterType = index < parameterTypes.length ? parameterTypes[index] : null; Object parameter = fromJson(in, parameterType); parameters.add(parameter); index++; } in.endArray(); while (index < parameterTypes.length) { parameters.add(null); index++; } return parameters; } return new JsonParser().parse(in); }