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

The following examples show how to use com.google.gson.stream.JsonReader#endObject() . 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: JsonLinkConsumer.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
private List<String> readLinksArray(final JsonReader reader) throws IOException, EntityProviderException {
  List<String> links = new ArrayList<String>();

  reader.beginArray();
  while (reader.hasNext()) {
    reader.beginObject();
    String nextName = reader.nextName();
    if (FormatJson.URI.equals(nextName) && reader.peek() == JsonToken.STRING) {
      links.add(reader.nextString());
    } else {
      throw new EntityProviderException(EntityProviderException.INVALID_CONTENT.addContent(FormatJson.URI)
          .addContent(nextName));
    }
    reader.endObject();
  }
  reader.endArray();

  return links;
}
 
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: SourceFilePositionJsonSerializer.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
@Override
public SourceFilePosition read(JsonReader in) throws IOException {
    in.beginObject();
    SourceFile file = SourceFile.UNKNOWN;
    SourcePosition position = SourcePosition.UNKNOWN;
    while(in.hasNext()) {
        String name = in.nextName();
        if (name.equals(FILE)) {
            file = mSourceFileJsonTypeAdapter.read(in);
        } else if (name.equals(POSITION)) {
            position = mSourcePositionJsonTypeAdapter.read(in);
        } else {
            in.skipValue();
        }
    }
    in.endObject();
    return new SourceFilePosition(file, position);
}
 
Example 4
Source File: JsonPropertyDeserializer.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
protected void readAndCheckTypeInfo(final JsonReader reader, String expectedTypeName)
        throws IOException, EntityProviderException {
  reader.beginObject();
  if (!FormatJson.TYPE.equals(reader.nextName())) {
    throw new EntityProviderException(EntityProviderException.MISSING_ATTRIBUTE.addContent(FormatJson.TYPE)
        .addContent(FormatJson.METADATA));
  }
  final String actualTypeName = reader.nextString();
  if (!expectedTypeName.equals(actualTypeName)) {
    throw new EntityProviderException(EntityProviderException.INVALID_ENTITYTYPE.addContent(expectedTypeName)
        .addContent(actualTypeName));
  }
  reader.endObject();
}
 
Example 5
Source File: ImportInputFormat.java    From emr-dynamodb-connector with Apache License 2.0 5 votes vote down vote up
/**
 * An example manifest file looks like
 *
 * {"name":"DynamoDB-export","version":3, "entries":[
 * {"url":"s3://path/to/object/92dd1414-a049-4c68-88fb-a23acd44907e","mandatory":true},
 * {"url":"s3://path/to/object/ba3f3535-7aa1-4f97-a530-e72938bf4b76","mandatory":true} ]}
 */
// @formatter:on
private List<InputSplit> parseManifest(FileSystem fs, Path manifestPath, JobConf job) throws
    IOException {
  List<InputSplit> splits = null;

  FSDataInputStream fp = fs.open(manifestPath);
  JsonReader reader = new JsonReader(new InputStreamReader(fp, Charsets.UTF_8));

  reader.beginObject();
  while (reader.hasNext()) {
    String name = reader.nextName();
    switch (name) {
      case VERSION_JSON_KEY:
        job.set(DynamoDBConstants.EXPORT_FORMAT_VERSION, String.valueOf(reader.nextInt()));
        break;
      case ENTRIES_JSON_KEY:
        splits = readEntries(reader, job);
        break;
      default:
        log.info("Skipping a JSON key in the manifest file: " + name);
        reader.skipValue();
        break;
    }
  }
  reader.endObject();

  if (splits == null) {
    return Collections.emptyList();
  }
  return splits;
}
 
Example 6
Source File: LocalDateTypeAdapter.java    From protools with Apache License 2.0 5 votes vote down vote up
@Override
public LocalDate reading(JsonReader jsonReader) throws IOException {
    jsonReader.beginObject();
    jsonReader.nextName();
    String value = jsonReader.nextString();
    LocalDate localDate = LocalDate.parse(value, DATE_FORMATTER);
    //region 忽略时间戳属性
    if (jsonReader.hasNext()) {
        jsonReader.nextName();
        jsonReader.nextString();
    }
    //endregion
    jsonReader.endObject();
    return localDate;
}
 
Example 7
Source File: PropertyBasedTypeAdapter.java    From graphical-lsp with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public T read(JsonReader in) throws IOException {
	try {
		in.beginObject();
		T result = null;
		Map<String, JsonElement> unassignedProperties = null;
		while (in.hasNext()) {
			String propertyName = in.nextName();
			if (propertyName.equals(discriminator)) {
				if (result != null)
					throw new IllegalStateException("Property '" + discriminator + "' is defined twice.");
				result = createInstance(in.nextString());
				if (unassignedProperties != null) {
					for (Map.Entry<String, JsonElement> entry : unassignedProperties.entrySet()) {
						assignProperty(result, entry.getKey(), entry.getValue());
					}
				}
			} else if (result != null) {
				assignProperty(result, propertyName, in);
			} else {
				if (unassignedProperties == null)
					unassignedProperties = new HashMap<>();
				unassignedProperties.put(propertyName, toTree(in));
			}
		}
		in.endObject();
		return result;
	} catch (IllegalAccessException e) {
		throw new RuntimeException(e);
	}
}
 
Example 8
Source File: BagOfPrimitivesDeserializationBenchmark.java    From gson with Apache License 2.0 5 votes vote down vote up
/**
 * This benchmark measures the ideal Gson performance: the cost of parsing a JSON stream and
 * setting object values by reflection. We should strive to reduce the discrepancy between this
 * and {@link #timeBagOfPrimitivesDefault(int)} .
 */
public void timeBagOfPrimitivesReflectionStreaming(int reps) throws Exception {
  for (int i=0; i<reps; ++i) {
    StringReader reader = new StringReader(json);
    JsonReader jr = new JsonReader(reader);
    jr.beginObject();
    BagOfPrimitives bag = new BagOfPrimitives();
    while(jr.hasNext()) {
      String name = jr.nextName();
      for (Field field : BagOfPrimitives.class.getDeclaredFields()) {
        if (field.getName().equals(name)) {
          Class<?> fieldType = field.getType();
          if (fieldType.equals(long.class)) {
            field.setLong(bag, jr.nextLong());
          } else if (fieldType.equals(int.class)) {
            field.setInt(bag, jr.nextInt());
          } else if (fieldType.equals(boolean.class)) {
            field.setBoolean(bag, jr.nextBoolean());
          } else if (fieldType.equals(String.class)) {
            field.set(bag, jr.nextString());
          } else {
            throw new RuntimeException("Unexpected: type: " + fieldType + ", name: " + name);
          }
        }
      }
    }
    jr.endObject();
  }
}
 
Example 9
Source File: JsonUtils.java    From cloud-odata-java with Apache License 2.0 5 votes vote down vote up
public static boolean endJson(final JsonReader reader, final int openJsonObjects) throws IOException, EntityProviderException {

    for (int closedJsonObjects = 0; closedJsonObjects < openJsonObjects; closedJsonObjects++) {
      reader.endObject();
    }
    return reader.peek() == JsonToken.END_DOCUMENT;
  }
 
Example 10
Source File: TypeAdapters.java    From framework with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override public JsonElement read(JsonReader in) throws IOException {
  switch (in.peek()) {
  case STRING:
    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()) {
      object.add(in.nextName(), read(in));
    }
    in.endObject();
    return object;
  case END_DOCUMENT:
  case NAME:
  case END_OBJECT:
  case END_ARRAY:
  default:
    throw new IllegalArgumentException();
  }
}
 
Example 11
Source File: LocalDateTimeTypeAdapter.java    From protools with Apache License 2.0 5 votes vote down vote up
@Override
public LocalDateTime reading(JsonReader jsonReader) throws IOException {
    jsonReader.beginObject();
    jsonReader.nextName();
    String value = jsonReader.nextString();
    LocalDateTime dateTime = LocalDateTime.parse(value, DATE_TIME_FORMATTER);
    //region 忽略时间戳属性
    if (jsonReader.hasNext()) {
        jsonReader.nextName();
        jsonReader.nextString();
    }
    //endregion
    jsonReader.endObject();
    return dateTime;
}
 
Example 12
Source File: ObjectTypeAdapter.java    From framework with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override public Object read(JsonReader in) throws IOException {
  JsonToken token = in.peek();
  switch (token) {
  case BEGIN_ARRAY:
    List<Object> list = new ArrayList<Object>();
    in.beginArray();
    while (in.hasNext()) {
      list.add(read(in));
    }
    in.endArray();
    return list;

  case BEGIN_OBJECT:
    Map<String, Object> map = new LinkedTreeMap<String, Object>();
    in.beginObject();
    while (in.hasNext()) {
      map.put(in.nextName(), read(in));
    }
    in.endObject();
    return map;

  case STRING:
    return in.nextString();

  case NUMBER:
    return in.nextDouble();

  case BOOLEAN:
    return in.nextBoolean();

  case NULL:
    in.nextNull();
    return null;

  default:
    throw new IllegalStateException();
  }
}
 
Example 13
Source File: MCRAltoLinkTypeAdapter.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
@Override
public MCRMetsAltoLink read(JsonReader jsonReader) throws IOException {
    String fileID = null;
    String begin = null;
    String end = null;

    jsonReader.beginObject();
    while (jsonReader.hasNext()) {
        switch (jsonReader.nextName()) {
            case "altoFile":
                fileID = jsonReader.nextString();
                break;
            case "begin":
                begin = jsonReader.nextString();
                break;
            case "end":
                end = jsonReader.nextString();
                break;
        }
    }
    jsonReader.endObject();

    if (fileID == null || begin == null || end == null) {
        throw new MCRException("Cannot read MCRMetsAltoLink! FileID && begin && end expected!");
    }
    return new MCRAltoLinkPlaceHolder(fileID, begin, end);
}
 
Example 14
Source File: BsonReaderStreamingTest.java    From immutables with Apache License 2.0 5 votes vote down vote up
@Test
public void value3() throws IOException {
  JsonReader reader = createReader("{ \"a\" : 123, \"list\" : [ 12.25, null, true, { }, [ ] ] }");

  reader.beginObject();
  check(reader.nextName()).is("a");
  check(reader.nextLong()).is(123L);
  check(reader.nextName()).is("list");
  check(reader.peek()).is(JsonToken.BEGIN_ARRAY);
  reader.beginArray();
  check(reader.peek()).is(JsonToken.NUMBER);
  check(reader.nextDouble()).is(12.25);
  check(reader.peek()).is(JsonToken.NULL);
  reader.nextNull();
  check(reader.peek()).is(JsonToken.BOOLEAN);
  check(reader.nextBoolean());
  check(reader.peek()).is(JsonToken.BEGIN_OBJECT);
  reader.beginObject();
  check(reader.peek()).is(JsonToken.END_OBJECT);
  reader.endObject();
  check(reader.peek()).is(JsonToken.BEGIN_ARRAY);
  reader.beginArray();
  check(reader.peek()).is(JsonToken.END_ARRAY);
  reader.endArray();
  check(reader.peek()).is(JsonToken.END_ARRAY);
  reader.endArray();
  check(reader.peek()).is(JsonToken.END_OBJECT);
  reader.endObject();
}
 
Example 15
Source File: TestGetSolr.java    From nifi with Apache License 2.0 4 votes vote down vote up
@Test
public void testRecordWriter() throws IOException, InitializationException {
    final org.apache.nifi.processors.solr.TestGetSolr.TestableProcessor proc = new org.apache.nifi.processors.solr.TestGetSolr.TestableProcessor(solrClient);

    TestRunner runner = createDefaultTestRunner(proc);
    runner.setProperty(GetSolr.RETURN_TYPE, GetSolr.MODE_REC.getValue());
    runner.setProperty(GetSolr.RETURN_FIELDS, "id,created,integer_single");
    runner.setProperty(GetSolr.BATCH_SIZE, "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.run(1,true, true);
    runner.assertQueueEmpty();
    runner.assertAllFlowFilesTransferred(GetSolr.REL_SUCCESS, 1);
    runner.assertAllFlowFilesContainAttribute(CoreAttributes.MIME_TYPE.key());

    // Check for valid json
    JsonReader reader = new JsonReader(new InputStreamReader(new ByteArrayInputStream(
            runner.getContentAsByteArray(runner.getFlowFilesForRelationship(GetSolr.REL_SUCCESS).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();
    }
    assertEquals(controlScore, 45);
}
 
Example 16
Source File: SourceFileJsonTypeAdapter.java    From buck with Apache License 2.0 4 votes vote down vote up
@Override
public SourceFile read(JsonReader in) throws IOException {
    switch (in.peek()) {
        case BEGIN_OBJECT:
            in.beginObject();
            String filePath = null;
            String description = null;
            while (in.hasNext()) {
                String name = in.nextName();
                if (name.equals(PATH)) {
                    filePath = in.nextString();
                } else if (DESCRIPTION.equals(name)) {
                    description = in.nextString();
                } else {
                    in.skipValue();
                }
            }
            in.endObject();
            if (!Strings.isNullOrEmpty(filePath)) {
                File file = new File(filePath);
                if (!Strings.isNullOrEmpty(description)) {
                    return new SourceFile(file, description);
                } else {
                    return new SourceFile(file);
                }
            } else {
                if (!Strings.isNullOrEmpty(description)) {
                    return new SourceFile(description);
                } else {
                    return SourceFile.UNKNOWN;
                }
            }
        case STRING:
            String fileName = in.nextString();
            if (Strings.isNullOrEmpty(fileName)) {
                return SourceFile.UNKNOWN;
            }
            return new SourceFile(new File(fileName));
        default:
            return SourceFile.UNKNOWN;
    }

}
 
Example 17
Source File: ChannelReader.java    From packagedrone with Eclipse Public License 1.0 4 votes vote down vote up
private Map<MetaKey, CacheEntryInformation> readCacheEntries ( final JsonReader jr ) throws IOException
{
    final Map<MetaKey, CacheEntryInformation> result = new HashMap<> ();

    jr.beginObject ();
    while ( jr.hasNext () )
    {
        final String entryName = jr.nextName ();
        jr.beginObject ();

        String name = null;
        Long size = null;
        String mimeType = null;
        Instant timestamp = null;

        while ( jr.hasNext () )
        {
            final String ele = jr.nextName ();
            switch ( ele )
            {
                case "name":
                    name = jr.nextString ();
                    break;
                case "size":
                    size = jr.nextLong ();
                    break;
                case "mimeType":
                    mimeType = jr.nextString ();
                    break;
                case "timestamp":
                    timestamp = readTime ( jr );
                    break;
                default:
                    jr.skipValue ();
                    break;
            }
        }

        if ( name == null || size == null || mimeType == null || timestamp == null )
        {
            throw new IOException ( "Invalid format" );
        }

        jr.endObject ();

        final MetaKey key = MetaKey.fromString ( entryName );

        result.put ( key, new CacheEntryInformation ( key, name, size, mimeType, timestamp ) );
    }
    jr.endObject ();

    return result;
}
 
Example 18
Source File: ResponseAdapter.java    From feign with Apache License 2.0 4 votes vote down vote up
/**
 * the wikipedia api doesn't use json arrays, rather a series of nested objects.
 */
@Override
public WikipediaExample.Response<X> read(JsonReader reader) throws IOException {
  WikipediaExample.Response<X> pages = new WikipediaExample.Response<X>();
  reader.beginObject();
  while (reader.hasNext()) {
    String nextName = reader.nextName();
    if ("query".equals(nextName)) {
      reader.beginObject();
      while (reader.hasNext()) {
        if (query().equals(reader.nextName())) {
          reader.beginObject();
          while (reader.hasNext()) {
            // each element is in form: "id" : { object }
            // this advances the pointer to the value and skips the key
            reader.nextName();
            reader.beginObject();
            pages.add(build(reader));
            reader.endObject();
          }
          reader.endObject();
        } else {
          reader.skipValue();
        }
      }
      reader.endObject();
    } else if ("continue".equals(nextName)) {
      reader.beginObject();
      while (reader.hasNext()) {
        if ("gsroffset".equals(reader.nextName())) {
          pages.nextOffset = reader.nextLong();
        } else {
          reader.skipValue();
        }
      }
      reader.endObject();
    } else {
      reader.skipValue();
    }
  }
  reader.endObject();
  return pages;
}
 
Example 19
Source File: StructuredJsonExamplesProvider.java    From vw-webservice with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private Namespace readNamespace(long exampleNumber, JsonReader jsonReader) throws IOException {
	jsonReader.beginObject();

	StructuredExample.Namespace.NamespaceBuilder nsBuilder = new StructuredExample.Namespace.NamespaceBuilder();

	boolean nameRead = false, scalingFactorRead = false, featuresRead = false;

	while (jsonReader.hasNext()) {

		String propertyNameOriginal = jsonReader.nextName();
		String propertyName = propertyNameOriginal.trim().toLowerCase();

		if (propertyName.equals(StructuredJsonPropertyNames.NAMESPACE_NAME_PROPERTY)) {

			if (nameRead) {

				throw new ExampleFormatException(exampleNumber, "The 'name' property must only appear once in a namespace!");
			}

			if (jsonReader.peek() == JsonToken.NULL)
				jsonReader.nextNull();
			else {
				String namespace = jsonReader.nextString();
				nsBuilder.setName(namespace);
			}
			nameRead = true;
		}
		else if (propertyName.equals(StructuredJsonPropertyNames.NAMESPACE_SCALING_FACTOR_PROPERTY)) {

			if (scalingFactorRead) {

				throw new ExampleFormatException(exampleNumber, "The 'value' property must only appear once in a namespace!");
			}

			if (jsonReader.peek() == JsonToken.NULL)
				jsonReader.nextNull();
			else {
				double scalingFactor = jsonReader.nextDouble();
				nsBuilder.setScalingFactor(Float.valueOf((float) scalingFactor));
			}
			scalingFactorRead = true;

		}
		else if (propertyName.equals(StructuredJsonPropertyNames.NAMESPACE_FEATURES_PROPERTY)) {

			if (featuresRead) {

				throw new ExampleFormatException(exampleNumber, "The 'features' property must only appear once in a namespace!");
			}

			if (jsonReader.peek() == JsonToken.NULL) {
				jsonReader.nextNull();
			}
			else {

				jsonReader.beginArray();

				int numFeaturesAdded = 0;

				while (jsonReader.hasNext()) {
					readFeatureIntoNamespace(exampleNumber, nsBuilder, jsonReader);

					numFeaturesAdded++;

					if (maxNumberOfFeaturesPerNamespace > 0 && maxNumberOfFeaturesPerNamespace < Integer.MAX_VALUE && numFeaturesAdded > maxNumberOfFeaturesPerNamespace) {
						throw new ExampleFormatException(exampleNumber, "The maximum number of features per namespace, " + maxNumberOfFeaturesPerNamespace + " was exceeded!");
					}
				}

				jsonReader.endArray();

			}
			featuresRead = true;

		}
		else {
			throw new ExampleFormatException(exampleNumber, "Unknown property: " + propertyNameOriginal + " found while reading namespace!");
		}
	}

	jsonReader.endObject();

	return nsBuilder.build();
}
 
Example 20
Source File: MessageTypeAdapter.java    From lsp4j with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public Message read(JsonReader in) throws IOException, JsonIOException, JsonSyntaxException {
	if (in.peek() == JsonToken.NULL) {
		in.nextNull();
		return null;
	}
	
	in.beginObject();
	String jsonrpc = null, method = null;
	Either<String, Number> id = null;
	Object rawParams = null;
	Object rawResult = null;
	ResponseError responseError = null;
	try {
		
		while (in.hasNext()) {
			String name = in.nextName();
			switch (name) {
			case "jsonrpc": {
				jsonrpc = in.nextString();
				break;
			}
			case "id": {
				if (in.peek() == JsonToken.NUMBER)
					id = Either.forRight(in.nextInt());
				else
					id = Either.forLeft(in.nextString());
				break;
			}
			case "method": {
				method = in.nextString();
				break;
			}
			case "params": {
				rawParams = parseParams(in, method);
				break;
			}
			case "result": {
				rawResult = parseResult(in, id != null ? id.get().toString() : null);
				break;
			}
			case "error": {
				responseError = gson.fromJson(in, ResponseError.class);
				break;
			}
			default:
				in.skipValue();
			}
		}
		Object params = parseParams(rawParams, method);
		Object result = parseResult(rawResult, id != null ? id.get().toString() : null);
		
		in.endObject();
		return createMessage(jsonrpc, id, method, params, result, responseError);
		
	} catch (JsonSyntaxException | MalformedJsonException | EOFException exception) {
		if (id != null || method != null) {
			// Create a message and bundle it to an exception with an issue that wraps the original exception
			Message message = createMessage(jsonrpc, id, method, rawParams, rawResult, responseError);
			MessageIssue issue = new MessageIssue("Message could not be parsed.", ResponseErrorCode.ParseError.getValue(), exception);
			throw new MessageIssueException(message, issue);
		} else {
			throw exception;
		}
	}
}