@Override public Student read(JsonReader reader) throws IOException { Student student = new Student(); reader.beginObject(); String fieldname = null; while (reader.hasNext()) { JsonToken token = reader.peek(); if (token.equals(JsonToken.NAME)) { // get the current token fieldname = reader.nextName(); } if ("name".equals(fieldname)) { // move to next token token = reader.peek(); student.setName(reader.nextString()); } if ("rollNo".equals(fieldname)) { // move to next token token = reader.peek(); student.setRollNo(reader.nextInt()); } } reader.endObject(); return student; }
@Override public Date read(JsonReader jsonReader) throws IOException { if (jsonReader.peek() == JsonToken.NULL) { jsonReader.nextNull(); return null; } Date date = null; // while(jsonReader.hasNext()) { if (jsonReader.peek().equals(JsonToken.BEGIN_OBJECT)) { jsonReader.beginObject(); while (!jsonReader.peek().equals(JsonToken.END_OBJECT)) { JsonToken token = jsonReader.peek(); switch (token) { case NAME: if (jsonReader.nextName().equals("$date")) { date = getDateFromString(jsonReader.nextString()); } break; default: jsonReader.skipValue(); } } jsonReader.endObject(); } // } return date; }
public BitSet read(JsonReader jsonreader) { JsonToken jsontoken; BitSet bitset; int i; if (jsonreader.peek() == JsonToken.NULL) { jsonreader.nextNull(); return null; } bitset = new BitSet(); jsonreader.beginArray(); jsontoken = jsonreader.peek(); i = 0; _L2: boolean flag; if (jsontoken == JsonToken.END_ARRAY) { break MISSING_BLOCK_LABEL_209; } switch (..SwitchMap.com.google.gson.stream.JsonToken[jsontoken.ordinal()]) { default: throw new JsonSyntaxException((new StringBuilder()).append("Invalid bitset value type: ").append(jsontoken).toString()); case 2: // '\002' break; /* Loop/switch isn't completed */
@Override public Calendar read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { in.nextNull(); return null; } in.beginObject(); int year = 0; int month = 0; int dayOfMonth = 0; int hourOfDay = 0; int minute = 0; int second = 0; while (in.peek() != JsonToken.END_OBJECT) { String name = in.nextName(); int value = in.nextInt(); if (YEAR.equals(name)) { year = value; } else if (MONTH.equals(name)) { month = value; } else if (DAY_OF_MONTH.equals(name)) { dayOfMonth = value; } else if (HOUR_OF_DAY.equals(name)) { hourOfDay = value; } else if (MINUTE.equals(name)) { minute = value; } else if (SECOND.equals(name)) { second = value; } } in.endObject(); return new GregorianCalendar(year, month, dayOfMonth, hourOfDay, minute, second); }
@Override public Boolean read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { in.nextNull(); return null; } else if (in.peek() == JsonToken.STRING) { // support strings for compatibility with GSON 1.7 return Boolean.parseBoolean(in.nextString()); } return in.nextBoolean(); }
@Override public Dependency read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { in.nextNull(); return null; } if (in.peek() != JsonToken.BEGIN_OBJECT) { in.nextString(); return null; } return readDependency(in); }
private static Calendar read(JsonReader jsonreader) throws IOException { if (jsonreader.peek() == JsonToken.NULL) { jsonreader.nextNull(); return null; } jsonreader.beginObject(); int k1 = 0; int j1 = 0; int i1 = 0; int l = 0; int k = 0; int j = 0; do { if (jsonreader.peek() == JsonToken.END_OBJECT) { break; } String s = jsonreader.nextName(); int i = jsonreader.nextInt(); if ("year".equals(s)) { k1 = i; } else if ("month".equals(s)) { j1 = i; } else if ("dayOfMonth".equals(s)) { i1 = i; } else if ("hourOfDay".equals(s)) { l = i; } else if ("minute".equals(s)) { k = i; } else if ("second".equals(s)) { j = i; } } while (true); jsonreader.endObject(); return new GregorianCalendar(k1, j1, i1, l, k, j); }
@Override public void consume(JsonReader in, JsonSchemaObject object) throws IOException { if (in.peek() == JsonToken.BEGIN_ARRAY) { in.beginArray(); final ArrayList<JsonSchemaObject> list = new ArrayList<>(); while (in.peek() != JsonToken.END_ARRAY) { if (in.peek() == JsonToken.BEGIN_OBJECT) { list.add(readInnerObject(in)); } else in.skipValue(); } assign(list, object); in.endArray(); } else in.skipValue(); }
private static boolean validateJSON(final File testOutputFile, final List<String> failures) throws FileNotFoundException, SAXException, IOException { final JsonElement jsonElement; try (final Reader reader = new BufferedReader(new FileReader(testOutputFile)); final JsonReader jsonReader = new JsonReader(reader); ) { jsonElement = new JsonParser().parse(jsonReader); if (jsonReader.peek() != JsonToken.END_DOCUMENT) { failures.add("JSON document was not fully consumed."); } } catch (final Exception e) { failures.add(e.getMessage()); return false; } final int size; if (jsonElement.isJsonObject()) { size = jsonElement.getAsJsonObject().entrySet().size(); } else if (jsonElement.isJsonArray()) { size = jsonElement.getAsJsonArray().size(); } else { size = 0; } if (size == 0) { failures.add("Invalid JSON string"); } return failures.isEmpty(); }
private static String a(JsonReader paramJsonReader) { if (paramJsonReader.peek() == JsonToken.NULL) { paramJsonReader.nextNull(); return null; } return paramJsonReader.nextString(); }
public Map<K, V> a(JsonReader paramJsonReader) throws IOException { Object localObject = paramJsonReader.peek(); if (localObject == JsonToken.NULL) { paramJsonReader.nextNull(); return null; } Map localMap = (Map) d.a(); if (localObject == JsonToken.BEGIN_ARRAY) { paramJsonReader.beginArray(); while (paramJsonReader.hasNext()) { paramJsonReader.beginArray(); localObject = b.b(paramJsonReader); if (localMap.put(localObject, c.b(paramJsonReader)) != null) { throw new JsonSyntaxException("duplicate key: " + localObject); } paramJsonReader.endArray(); } paramJsonReader.endArray(); return localMap; } paramJsonReader.beginObject(); while (paramJsonReader.hasNext()) { d.INSTANCE.promoteNameToValue(paramJsonReader); localObject = b.b(paramJsonReader); if (localMap.put(localObject, c.b(paramJsonReader)) != null) { throw new JsonSyntaxException("duplicate key: " + localObject); } } paramJsonReader.endObject(); return localMap; }
@Override public Locale read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { in.nextNull(); return null; } String locale = in.nextString(); StringTokenizer tokenizer = new StringTokenizer(locale, "_"); String language = null; String country = null; String variant = null; if (tokenizer.hasMoreElements()) { language = tokenizer.nextToken(); } if (tokenizer.hasMoreElements()) { country = tokenizer.nextToken(); } if (tokenizer.hasMoreElements()) { variant = tokenizer.nextToken(); } if (country == null && variant == null) { return new Locale(language); } else if (variant == null) { return new Locale(language, country); } else { return new Locale(language, country, variant); } }
public Date read(JsonReader paramJsonReader) throws IOException { if (paramJsonReader.peek() == JsonToken.NULL) { paramJsonReader.nextNull(); return null; } return deserializeToDate(paramJsonReader.nextString()); }
public Number read(JsonReader jsonReader) throws IOException { if (jsonReader.peek() != JsonToken.NULL) { return Long.valueOf(jsonReader.nextLong()); } jsonReader.nextNull(); return null; }
public <T> T fromJson(JsonReader jsonReader, Type type) throws JsonIOException, JsonSyntaxException { boolean z = true; boolean isLenient = jsonReader.isLenient(); jsonReader.setLenient(true); try { jsonReader.peek(); z = DEFAULT_JSON_NON_EXECUTABLE; T read = getAdapter(TypeToken.get(type)).read(jsonReader); jsonReader.setLenient(isLenient); return read; } catch (Throwable e) { if (z) { jsonReader.setLenient(isLenient); return null; } throw new JsonSyntaxException(e); } catch (Throwable e2) { throw new JsonSyntaxException(e2); } catch (Throwable e22) { throw new JsonSyntaxException(e22); } catch (Throwable th) { jsonReader.setLenient(isLenient); } }
/** * Reads the next JSON value from {@code reader} and convert it to an object of type {@code * typeOfT}. Since Type is not parameterized by T, this method is type unsafe and should be used * carefully * * @throws JsonIOException if there was a problem writing to the Reader * @throws JsonSyntaxException if json is not a valid representation for an object of type */ @SuppressWarnings("unchecked") public <T> T fromJson(JsonReader reader, Type typeOfT) throws JsonIOException, JsonSyntaxException { boolean isEmpty = true; boolean oldLenient = reader.isLenient(); reader.setLenient(true); try { reader.peek(); isEmpty = false; TypeToken<T> typeToken = (TypeToken<T>) TypeToken.get(typeOfT); TypeAdapter<T> typeAdapter = getAdapter(typeToken); T object = typeAdapter.read(reader); return object; } catch (EOFException e) { /* * For compatibility with JSON 1.5 and earlier, we return null for empty * documents instead of throwing. */ if (isEmpty) { return null; } throw new JsonSyntaxException(e); } catch (IllegalStateException e) { throw new JsonSyntaxException(e); } catch (IOException e) { // TODO(inder): Figure out whether it is indeed right to rethrow this as JsonSyntaxException throw new JsonSyntaxException(e); } finally { reader.setLenient(oldLenient); } }
public T read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { in.nextNull(); return null; } return nameToConstant.get(in.nextString()); }
@Override public StringFilter read(JsonReader reader) throws IOException { StringFilter result = null; // reset instance state this.type = null; this.delegate = null; reader.beginObject(); while (reader.peek() != JsonToken.END_OBJECT) { String name = reader.nextName(); try { if (this.delegate != null) { this.delegate.parseValue(reader, name); } else { parseValue(reader, name); } } catch (Exception ex) { LOG.error(ex.getMessage(), ex); throw new IOException(ex); } } reader.endObject(); if (this.type != null) { try { if (this.delegate != null) { result = this.delegate.createInstance(this.type); } else { result = createInstance(this.type); } } catch (Exception ex) { LOG.error(ex.getMessage(), ex); throw new IOException(ex.toString()); } } return result; }
public Float read(JsonReader jsonReader) throws IOException { if (jsonReader.peek() != JsonToken.NULL) { return Float.valueOf((float) jsonReader.nextDouble()); } jsonReader.nextNull(); return null; }
@Override public T read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { in.nextNull(); return null; } T instance = constructor.construct(); try { in.beginObject(); while (in.hasNext()) { String name = in.nextName(); BoundField field = boundFields.get(name); if (field == null || !field.deserialized) { in.skipValue(); } else { field.read(in, instance); } } } catch (IllegalStateException e) { throw new JsonSyntaxException(e); } catch (IllegalAccessException e) { throw new AssertionError(e); } in.endObject(); return instance; }
public Object read(JsonReader jsonreader) { if (jsonreader.peek() == JsonToken.NULL) { jsonreader.nextNull(); return null; } else { return a.read(jsonreader); } }
@Override public Date read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { in.nextNull(); return null; } return deserializeToDate(in.nextString()); }
@Override public UUID read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { in.nextNull(); return null; } return java.util.UUID.fromString(in.nextString()); }
@Override public Number read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { in.nextNull(); return null; } return in.nextDouble(); }
@Override public Boolean read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { in.nextNull(); return null; } return Boolean.valueOf(in.nextString()); }
@Override public void consume(JsonReader in, JsonSchemaObject object) throws IOException { if (in.peek() == JsonToken.NUMBER) { readNumber(in, object); } else { in.skipValue(); } }
@Override public void consume(JsonReader in, JsonSchemaObject object) throws IOException { if (in.peek() == JsonToken.BOOLEAN) { assign(in.nextBoolean(), object); } else { in.skipValue(); } }
@Override public StringBuffer read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { in.nextNull(); return null; } return new StringBuffer(in.nextString()); }
private void readEndArray() throws IOException { endArray(); tokenType = jsonReader.peek(); if (tokenType == JsonToken.END_OBJECT) { state = JsonState.EndArrayEndObject; } else if (tokenType == JsonToken.NAME) { state = JsonState.EndArrayName; } }
@Override public InetAddress read(JsonReader in) throws IOException { if (in.peek() == JsonToken.NULL) { in.nextNull(); return null; } // regrettably, this should have included both the host name and the host address return InetAddress.getByName(in.nextString()); }