List of usage examples for com.google.gson.stream JsonToken STRING
JsonToken STRING
To view the source code for com.google.gson.stream JsonToken STRING.
Click Source Link
From source file:org.apache.olingo.odata2.core.ep.consumer.JsonPropertyConsumer.java
License:Apache License
private Object readSimpleProperty(final JsonReader reader, final EntityPropertyInfo entityPropertyInfo, final Object typeMapping, final EntityProviderReadProperties readProperties) throws EdmException, EntityProviderException, IOException { final EdmSimpleType type = (EdmSimpleType) entityPropertyInfo.getType(); Object value = null;//w ww .ja v a 2 s . c o m final JsonToken tokenType = reader.peek(); if (tokenType == JsonToken.NULL) { reader.nextNull(); } else { switch (EdmSimpleTypeKind.valueOf(type.getName())) { case Boolean: if (tokenType == JsonToken.BOOLEAN) { value = reader.nextBoolean(); value = value.toString(); } else { throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE .addContent(entityPropertyInfo.getName())); } break; case Byte: case SByte: case Int16: case Int32: if (tokenType == JsonToken.NUMBER) { value = reader.nextInt(); value = value.toString(); } else { throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE .addContent(entityPropertyInfo.getName())); } break; case Single: case Double: if (tokenType == JsonToken.STRING) { value = reader.nextString(); } else if (tokenType == JsonToken.NUMBER) { value = reader.nextDouble(); value = value.toString(); } else { throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE .addContent(entityPropertyInfo.getName())); } break; default: if (tokenType == JsonToken.STRING) { value = reader.nextString(); } else { throw new EntityProviderException(EntityProviderException.INVALID_PROPERTY_VALUE .addContent(entityPropertyInfo.getName())); } break; } } final Class<?> typeMappingClass = typeMapping == null ? type.getDefaultType() : (Class<?>) typeMapping; final EdmFacets facets = readProperties == null || readProperties.isValidatingFacets() ? entityPropertyInfo.getFacets() : null; return type.valueOfString((String) value, EdmLiteralKind.JSON, facets, typeMappingClass); }
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/* w w w.j a v a 2 s .c om*/ 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.arl.fjage.remote.GenericValueAdapterFactory.java
License:BSD License
public <T> TypeAdapter<T> create(final Gson gson, TypeToken<T> type) { final Class<T> rawType = (Class<T>) type.getRawType(); if (!rawType.equals(GenericValue.class)) return null; final GenericValueAdapterFactory parent = this; return new TypeAdapter<T>() { @Override// www . j a v a 2s.c o m public void write(JsonWriter out, T value) throws IOException { if (value == null) { out.nullValue(); return; } Class type = ((GenericValue) value).getType(); if (type == null) { out.nullValue(); return; } if (Number.class.isAssignableFrom(type)) out.value((Number) ((GenericValue) value).getValue()); else if (type.equals(String.class)) out.value((String) ((GenericValue) value).getValue()); else if (type.equals(Boolean.class)) out.value((Boolean) ((GenericValue) value).getValue()); else { out.beginObject(); out.name("clazz").value(type.getName()); out.name("data"); TypeAdapter delegate = gson.getAdapter(TypeToken.get(type)); Object v = ((GenericValue) value).getValue(); delegate.write(out, v); 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.NUMBER) { String s = in.nextString(); try { if (s.contains(".")) return (T) new GenericValue(Double.parseDouble(s)); else return (T) new GenericValue(Long.parseLong(s)); } catch (NumberFormatException ex) { return (T) new GenericValue(null); } } if (tok == JsonToken.STRING) return (T) new GenericValue(in.nextString()); if (tok == JsonToken.BOOLEAN) return (T) new GenericValue(in.nextBoolean()); if (tok != JsonToken.BEGIN_OBJECT) return null; TypeToken tt = null; GenericValue rv = null; in.beginObject(); while (in.hasNext()) { String name = in.nextName(); if (name.equals("clazz")) { try { Class<?> cls = Class.forName(in.nextString()); tt = TypeToken.get(cls); } catch (Exception ex) { // do nothing } } else if (name.equals("data") && tt != null) { TypeAdapter delegate = gson.getAdapter(tt); rv = new GenericValue(delegate.read(in)); } else in.skipValue(); } in.endObject(); return (T) rv; } }; }
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 {/* w ww .jav a 2 s . c om*/ 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.packagedrone.repo.channel.apm.ChannelReader.java
License:Open Source License
private Map<String, String> readAspects(final JsonReader jr) throws IOException { final Map<String, String> result = new HashMap<>(); jr.beginObject();/* w w w .j a va 2 s . c o m*/ while (jr.hasNext()) { switch (jr.nextName()) { case "map": jr.beginObject(); while (jr.hasNext()) { final String id = jr.nextName(); String value = null; if (jr.peek() == JsonToken.STRING) { value = jr.nextString(); } else { jr.skipValue(); } result.put(id, value); } jr.endObject(); break; } } jr.endObject(); return result; }
From source file:org.eclipse.packagedrone.repo.channel.apm.ChannelReader.java
License:Open Source License
private Instant readTime(final JsonReader jr) throws IOException { final JsonToken peek = jr.peek(); if (peek == JsonToken.NUMBER) { return Instant.ofEpochMilli(jr.nextLong()); } else if (peek == JsonToken.NULL) { jr.nextNull();//from ww w.ja v a 2 s .c o m return null; } else if (peek == JsonToken.STRING) { final String str = jr.nextString(); try { return Instant.ofEpochMilli(Long.parseLong(str)); } catch (final NumberFormatException e) { try { return this.dateFormat.parse(str).toInstant(); } catch (final ParseException e2) { throw new IOException(e2); } } } else { throw new IOException(String.format("Invalid timestamp token: %s", peek)); } }
From source file:org.eclipse.skalli.core.rest.JSONRestReader.java
License:Open Source License
@Override public boolean isValue() throws IOException { if (state == STATE_FINAL) { return false; }/*from ww w . jav a2 s . c o m*/ if (isKey("value")) { //$NON-NLS-1$ return true; } JsonToken next = json.peek(); return lookAhead == null && (next == JsonToken.STRING || next == JsonToken.NUMBER || next == JsonToken.BOOLEAN || next == JsonToken.BEGIN_ARRAY || next == JsonToken.BEGIN_OBJECT || next == JsonToken.NULL); }
From source file:org.eclipse.thym.core.plugin.registry.CordovaPluginRegistryManager.java
License:Open Source License
private String safeReadStringValue(JsonReader reader) throws IOException { if (reader.peek() == JsonToken.STRING) { return reader.nextString(); }//from w ww . j a v a 2s. co m reader.skipValue(); return ""; }
From source file:org.gradle.api.internal.artifacts.ivyservice.ivyresolve.parser.ModuleMetadataParser.java
License:Apache License
public void parse(final LocallyAvailableExternalResource resource, final MutableModuleComponentResolveMetadata metadata) { resource.withContent(new Transformer<Void, InputStream>() { @Override/*from ww w.j a va 2s . c o m*/ public Void transform(InputStream inputStream) { try { JsonReader reader = new JsonReader(new InputStreamReader(inputStream, "utf-8")); reader.beginObject(); if (reader.peek() != JsonToken.NAME) { throw new RuntimeException("Module metadata should contain a 'formatVersion' value."); } String name = reader.nextName(); if (!name.equals("formatVersion")) { throw new RuntimeException(String.format( "The 'formatVersion' value should be the first value in a module metadata. Found '%s' instead.", name)); } if (reader.peek() != JsonToken.STRING) { throw new RuntimeException("The 'formatVersion' value should have a string value."); } String version = reader.nextString(); if (!version.equals(FORMAT_VERSION)) { throw new RuntimeException(String.format( "Unsupported format version '%s' specified in module metadata. This version of Gradle supports format version %s only.", version, FORMAT_VERSION)); } consumeTopLevelElements(reader, metadata); return null; } catch (Exception e) { throw new MetaDataParseException("module metadata", resource, e); } } }); }
From source file:org.immutables.gson.stream.JsonParserReader.java
License:Apache License
private static JsonToken toGsonToken(com.fasterxml.jackson.core.JsonToken token) { switch (token) { case START_ARRAY: return JsonToken.BEGIN_ARRAY; case END_ARRAY: return JsonToken.END_ARRAY; case START_OBJECT: return JsonToken.BEGIN_OBJECT; case END_OBJECT: return JsonToken.END_OBJECT; case FIELD_NAME: return JsonToken.NAME; case VALUE_FALSE: return JsonToken.BOOLEAN; case VALUE_TRUE: return JsonToken.BOOLEAN; case VALUE_NULL: return JsonToken.NULL; case VALUE_NUMBER_INT: return JsonToken.NUMBER; case VALUE_NUMBER_FLOAT: return JsonToken.NUMBER; case VALUE_STRING: return JsonToken.STRING; default: // Not semantically equivalent return JsonToken.NULL; }//from w ww . j a v a 2s. co m }