Java Code Examples for com.google.gson.stream.JsonReader#nextInt()

The following examples show how to use com.google.gson.stream.JsonReader#nextInt() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JsonFeedDeserializer.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
/**
 * 
 * @param reader
 * @param feedMetadata
 * @throws IOException
 * @throws EntityProviderException
 */
protected static void readInlineCount(final JsonReader reader, final FeedMetadataImpl feedMetadata)
    throws IOException, EntityProviderException {
  if (reader.peek() == JsonToken.STRING && feedMetadata.getInlineCount() == null) {
    int inlineCount;
    try {
      inlineCount = reader.nextInt();
    } catch (final NumberFormatException e) {
      throw new EntityProviderException(EntityProviderException.INLINECOUNT_INVALID.addContent(""), e);
    }
    if (inlineCount >= 0) {
      feedMetadata.setInlineCount(inlineCount);
    } else {
      throw new EntityProviderException(EntityProviderException.INLINECOUNT_INVALID.addContent(inlineCount));
    }
  } else {
    throw new EntityProviderException(EntityProviderException.INLINECOUNT_INVALID.addContent(reader.peek()));
  }
}
 
Example 2
Source File: TestQuerySolr.java    From nifi with Apache License 2.0 6 votes vote down vote up
private int returnCheckSumForArrayOfJsonObjects(JsonReader reader) throws IOException {
    int checkSum = 0;
    reader.beginArray();
    while (reader.hasNext()) {
        reader.beginObject();
        while (reader.hasNext()) {
            if (reader.nextName().equals("count")) {
                checkSum += reader.nextInt();
            } else {
                reader.skipValue();
            }
        }
        reader.endObject();
    }
    reader.endArray();
    return checkSum;
}
 
Example 3
Source File: DesignateAdapters.java    From denominator with Apache License 2.0 6 votes vote down vote up
protected Zone build(JsonReader reader) throws IOException {
  String name = null, id = null, email = null;
  int ttl = -1;
  while (reader.hasNext()) {
    String nextName = reader.nextName();
    if (nextName.equals("id")) {
      id = reader.nextString();
    } else if (nextName.equals("name")) {
      name = reader.nextString();
    } else if (nextName.equals("ttl")) {
      ttl = reader.nextInt();
    } else if (nextName.equals("email")) {
      email = reader.nextString();
    } else {
      reader.skipValue();
    }
  }
  return Zone.create(id, name, ttl, email);
}
 
Example 4
Source File: SentryEnvelopeItemHeaderAdapter.java    From sentry-android with MIT License 5 votes vote down vote up
@Override
public SentryEnvelopeItemHeader read(JsonReader reader) throws IOException {
  if (reader.peek() == JsonToken.NULL) {
    reader.nextNull();
    return null;
  }

  String contentType = null;
  String fileName = null;
  SentryItemType type = SentryItemType.Unknown;
  int length = 0;

  reader.beginObject();
  while (reader.hasNext()) {
    switch (reader.nextName()) {
      case "content_type":
        contentType = reader.nextString();
        break;
      case "filename":
        fileName = reader.nextString();
        break;
      case "type":
        try {
          type = SentryItemType.valueOf(StringUtils.capitalize(reader.nextString()));
        } catch (IllegalArgumentException ignored) {
          // invalid type
        }
        break;
      case "length":
        length = reader.nextInt();
        break;
      default:
        reader.skipValue();
        break;
    }
  }
  reader.endObject();

  return new SentryEnvelopeItemHeader(type, length, contentType, fileName);
}
 
Example 5
Source File: TypeAdapters.java    From gson with Apache License 2.0 5 votes vote down vote up
@Override
public Number read(JsonReader in) throws IOException {
  if (in.peek() == JsonToken.NULL) {
    in.nextNull();
    return null;
  }
  try {
    return in.nextInt();
  } catch (NumberFormatException e) {
    throw new JsonSyntaxException(e);
  }
}
 
Example 6
Source File: UserRoleAdapter.java    From devicehive-java-server with Apache License 2.0 5 votes vote down vote up
@Override
public UserRole read(JsonReader in) throws IOException {
    JsonToken jsonToken = in.peek();
    if (jsonToken == JsonToken.NULL) {
        in.nextNull();
        return null;
    } else {
        try {
            return UserRole.values()[in.nextInt()];
        } catch (RuntimeException e) {
            throw new IOException(Messages.INVALID_USER_ROLE, e);
        }
    }
}
 
Example 7
Source File: SourcePositionJsonTypeAdapter.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
public SourcePosition read(JsonReader in) throws IOException {
    int startLine = -1, startColumn = -1, startOffset = -1;
    int endLine = -1, endColumn = -1, endOffset = -1;
    in.beginObject();
    while (in.hasNext()) {
        String name = in.nextName();
        if (name.equals(START_LINE)) {
            startLine = in.nextInt();
        } else if (name.equals(START_COLUMN)) {
            startColumn = in.nextInt();
        } else if (name.equals(START_OFFSET)) {
            startOffset = in.nextInt();
        } else if (name.equals(END_LINE)) {
            endLine = in.nextInt();
        } else if (name.equals(END_COLUMN)) {
            endColumn = in.nextInt();
        } else if (name.equals(END_OFFSET)) {
            endOffset = in.nextInt();
        } else {
            in.skipValue();
        }
    }
    in.endObject();

    endLine = (endLine != -1) ? endLine : startLine;
    endColumn = (endColumn != -1) ? endColumn : startColumn;
    endOffset = (endOffset != -1) ? endOffset : startOffset;
    return new SourcePosition(startLine, startColumn, startOffset, endLine, endColumn,
            endOffset);
}
 
Example 8
Source File: DayOfWeekAdapter.java    From google-maps-services-java with Apache License 2.0 5 votes vote down vote up
@Override
public DayOfWeek read(JsonReader reader) throws IOException {
  if (reader.peek() == JsonToken.NULL) {
    reader.nextNull();
    return null;
  }

  if (reader.peek() == JsonToken.NUMBER) {
    int day = reader.nextInt();

    switch (day) {
      case 0:
        return DayOfWeek.SUNDAY;
      case 1:
        return DayOfWeek.MONDAY;
      case 2:
        return DayOfWeek.TUESDAY;
      case 3:
        return DayOfWeek.WEDNESDAY;
      case 4:
        return DayOfWeek.THURSDAY;
      case 5:
        return DayOfWeek.FRIDAY;
      case 6:
        return DayOfWeek.SATURDAY;
    }
  }

  return DayOfWeek.UNKNOWN;
}
 
Example 9
Source File: StatusTypeAdapter.java    From problem with MIT License 5 votes vote down vote up
@Override
public StatusType read(final JsonReader in) throws IOException {
    final JsonToken peek = in.peek();

    if (peek == JsonToken.NULL) {
        in.nextNull();
        return null;
    }

    final int statusCode = in.nextInt();
    @Nullable final StatusType status = index.get(statusCode);
    return status == null ? new UnknownStatus(statusCode) : status;
}
 
Example 10
Source File: CollectionsDeserializationBenchmark.java    From gson with Apache License 2.0 5 votes vote down vote up
/**
 * Benchmark to measure deserializing objects by hand
 */
public void timeCollectionsStreaming(int reps) throws IOException {
  for (int i=0; i<reps; ++i) {
    StringReader reader = new StringReader(json);
    JsonReader jr = new JsonReader(reader);
    jr.beginArray();
    List<BagOfPrimitives> bags = new ArrayList<BagOfPrimitives>();
    while(jr.hasNext()) {
      jr.beginObject();
      long longValue = 0;
      int intValue = 0;
      boolean booleanValue = false;
      String stringValue = null;
      while(jr.hasNext()) {
        String name = jr.nextName();
        if (name.equals("longValue")) {
          longValue = jr.nextLong();
        } else if (name.equals("intValue")) {
          intValue = jr.nextInt();
        } else if (name.equals("booleanValue")) {
          booleanValue = jr.nextBoolean();
        } else if (name.equals("stringValue")) {
          stringValue = jr.nextString();
        } else {
          throw new IOException("Unexpected name: " + name);
        }
      }
      jr.endObject();
      bags.add(new BagOfPrimitives(longValue, intValue, booleanValue, stringValue));
    }
    jr.endArray();
  }
}
 
Example 11
Source File: CommandStub.java    From ForgeHax with MIT License 5 votes vote down vote up
@Override
public void deserialize(JsonReader reader) throws IOException {
  reader.beginObject();
  
  reader.nextName();
  int kc = reader.nextInt();
  if (kc > -1) {
    bind(kc);
  }
  
  reader.endObject();
}
 
Example 12
Source File: TypeAdapters.java    From framework with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public Number read(JsonReader in) throws IOException {
  if (in.peek() == JsonToken.NULL) {
    in.nextNull();
    return null;
  }
  try {
    return in.nextInt();
  } catch (NumberFormatException e) {
    throw new JsonSyntaxException(e);
  }
}
 
Example 13
Source File: JsonPropertyConsumer.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
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;
  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);
}
 
Example 14
Source File: IndexTMDB.java    From quaerite with Apache License 2.0 4 votes vote down vote up
private static int getInt(JsonReader jsonReader, int nullValue) throws IOException {
    if (jsonReader.peek().equals(JsonToken.NULL)) {
        return nullValue;
    }
    return jsonReader.nextInt();
}
 
Example 15
Source File: AbstractJobConfigurationGsonTypeAdapter.java    From shardingsphere-elasticjob-cloud with Apache License 2.0 4 votes vote down vote up
@Override
public T read(final JsonReader in) throws IOException {
    String jobName = "";
    String cron = "";
    int shardingTotalCount = 0;
    String shardingItemParameters = "";
    String jobParameter = "";
    boolean failover = false;
    boolean misfire = failover;
    String description = "";
    JobProperties jobProperties = new JobProperties();
    JobType jobType = null;
    String jobClass = "";
    boolean streamingProcess = false;
    String scriptCommandLine = "";
    Map<String, Object> customizedValueMap = new HashMap<>(32, 1);
    in.beginObject();
    while (in.hasNext()) {
        String jsonName = in.nextName();
        switch (jsonName) {
            case "jobName":
                jobName = in.nextString();
                break;
            case "cron":
                cron = in.nextString();
                break;
            case "shardingTotalCount":
                shardingTotalCount = in.nextInt();
                break;
            case "shardingItemParameters":
                shardingItemParameters = in.nextString();
                break;
            case "jobParameter":
                jobParameter = in.nextString();
                break;
            case "failover":
                failover = in.nextBoolean();
                break;
            case "misfire":
                misfire = in.nextBoolean();
                break;
            case "description":
                description = in.nextString();
                break;
            case "jobProperties":
                jobProperties = getJobProperties(in);
                break;
            case "jobType":
                jobType = JobType.valueOf(in.nextString());
                break;
            case "jobClass":
                jobClass = in.nextString();
                break;
            case "streamingProcess":
                streamingProcess = in.nextBoolean();
                break;
            case "scriptCommandLine":
                scriptCommandLine = in.nextString();
                break;
            default:
                addToCustomizedValueMap(jsonName, in, customizedValueMap);
                break;
        }
    }
    in.endObject();
    JobCoreConfiguration coreConfig = getJobCoreConfiguration(jobName, cron, shardingTotalCount, shardingItemParameters,
            jobParameter, failover, misfire, description, jobProperties);
    JobTypeConfiguration typeConfig = getJobTypeConfiguration(coreConfig, jobType, jobClass, streamingProcess, scriptCommandLine);
    return getJobRootConfiguration(typeConfig, customizedValueMap);
}
 
Example 16
Source File: CloudAppConfigurationGsonFactory.java    From shardingsphere-elasticjob-cloud with Apache License 2.0 4 votes vote down vote up
@Override
public CloudAppConfiguration read(final JsonReader in) throws IOException {
    String appURL = "";
    String appName = "";
    String bootstrapScript = "";
    double cpuCount = 1.0d;
    double memoryMB = 128.0d;
    boolean appCacheEnable = true;
    int eventTraceSamplingCount = 0;
    in.beginObject();
    while (in.hasNext()) {
        String jsonName = in.nextName();
        switch (jsonName) {
            case CloudConfigurationConstants.APP_NAME:
                appName = in.nextString();
                break;
            case CloudConfigurationConstants.APP_URL:
                appURL = in.nextString();
                break;
            case CloudConfigurationConstants.BOOTSTRAP_SCRIPT:
                bootstrapScript = in.nextString();
                break;
            case CloudConfigurationConstants.CPU_COUNT:
                cpuCount = in.nextDouble();
                break;
            case CloudConfigurationConstants.MEMORY_MB:
                memoryMB = in.nextDouble();
                break;
            case CloudConfigurationConstants.APP_CACHE_ENABLE:
                appCacheEnable = in.nextBoolean();
                break;
            case CloudConfigurationConstants.EVENT_TRACE_SAMPLING_COUNT:
                eventTraceSamplingCount = in.nextInt();
                break;
            default:
                break;
        }
    }
    in.endObject();
    return new CloudAppConfiguration(appName, appURL, bootstrapScript, cpuCount, memoryMB, appCacheEnable, eventTraceSamplingCount);
}
 
Example 17
Source File: EnumTest.java    From openapi-generator with Apache License 2.0 4 votes vote down vote up
@Override
public EnumIntegerEnum read(final JsonReader jsonReader) throws IOException {
  Integer value =  jsonReader.nextInt();
  return EnumIntegerEnum.fromValue(value);
}
 
Example 18
Source File: QuerySolrIT.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Test
public void testRecordResponse() throws IOException, InitializationException {
    SolrClient solrClient = createSolrClient();
    TestRunner runner = createRunnerWithSolrClient(solrClient);

    runner.setProperty(QuerySolr.RETURN_TYPE, QuerySolr.MODE_REC.getValue());
    runner.setProperty(QuerySolr.SOLR_PARAM_FIELD_LIST, "id,created,integer_single");
    runner.setProperty(QuerySolr.SOLR_PARAM_ROWS, "10");

    final String outputSchemaText = new String(Files.readAllBytes(Paths.get("src/test/resources/test-schema.avsc")));

    final JsonRecordSetWriter jsonWriter = new JsonRecordSetWriter();
    runner.addControllerService("writer", jsonWriter);
    runner.setProperty(jsonWriter, SchemaAccessUtils.SCHEMA_ACCESS_STRATEGY, SchemaAccessUtils.SCHEMA_TEXT_PROPERTY);
    runner.setProperty(jsonWriter, SchemaAccessUtils.SCHEMA_TEXT, outputSchemaText);
    runner.setProperty(jsonWriter, "Pretty Print JSON", "true");
    runner.setProperty(jsonWriter, "Schema Write Strategy", "full-schema-attribute");
    runner.enableControllerService(jsonWriter);
    runner.setProperty(SolrUtils.RECORD_WRITER, "writer");

    runner.setNonLoopConnection(false);

    runner.run(1);
    runner.assertQueueEmpty();
    runner.assertTransferCount(QuerySolr.RESULTS, 1);

    JsonReader reader = new JsonReader(new InputStreamReader(new ByteArrayInputStream(
            runner.getContentAsByteArray(runner.getFlowFilesForRelationship(QuerySolr.RESULTS).get(0)))));
    reader.beginArray();
    int controlScore = 0;
    while (reader.hasNext()) {
        reader.beginObject();
        while (reader.hasNext()) {
            if (reader.nextName().equals("integer_single")) {
                controlScore += reader.nextInt();
            } else {
                reader.skipValue();
            }
        }
        reader.endObject();
    }
    reader.close();
    solrClient.close();

    assertEquals(controlScore, 45);
}
 
Example 19
Source File: EnumTest.java    From openapi-generator with Apache License 2.0 4 votes vote down vote up
@Override
public EnumIntegerEnum read(final JsonReader jsonReader) throws IOException {
  Integer value =  jsonReader.nextInt();
  return EnumIntegerEnum.fromValue(value);
}
 
Example 20
Source File: D.java    From MiBandDecompiled with Apache License 2.0 4 votes vote down vote up
public Calendar a(JsonReader jsonreader)
{
    int i = 0;
    if (jsonreader.peek() == JsonToken.NULL)
    {
        jsonreader.nextNull();
        return null;
    }
    jsonreader.beginObject();
    int j = 0;
    int k = 0;
    int l = 0;
    int i1 = 0;
    int j1 = 0;
    do
    {
        if (jsonreader.peek() == JsonToken.END_OBJECT)
        {
            break;
        }
        String s = jsonreader.nextName();
        int k1 = jsonreader.nextInt();
        if ("year".equals(s))
        {
            j1 = k1;
        } else
        if ("month".equals(s))
        {
            i1 = k1;
        } else
        if ("dayOfMonth".equals(s))
        {
            l = k1;
        } else
        if ("hourOfDay".equals(s))
        {
            k = k1;
        } else
        if ("minute".equals(s))
        {
            j = k1;
        } else
        if ("second".equals(s))
        {
            i = k1;
        }
    } while (true);
    jsonreader.endObject();
    return new GregorianCalendar(j1, i1, l, k, j, i);
}