Home / Class/ JsonStream Class — spring-boot Architecture

JsonStream Class — spring-boot Architecture

Architecture documentation for the JsonStream class in JsonStream.java from the spring-boot codebase.

Entity Profile

Relationship Graph

Source Code

buildpack/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/json/JsonStream.java lines 35–92

public class JsonStream {

	private final JsonMapper jsonMapper;

	/**
	 * Create a new {@link JsonStream} backed by the given JSON mapper.
	 * @param jsonMapper the object mapper to use
	 * @since 4.0.0
	 */
	public JsonStream(JsonMapper jsonMapper) {
		this.jsonMapper = jsonMapper;
	}

	/**
	 * Stream {@link ObjectNode object nodes} from the content as they become available.
	 * @param content the source content
	 * @param consumer the {@link ObjectNode} consumer
	 * @throws IOException on IO error
	 */
	public void get(InputStream content, Consumer<ObjectNode> consumer) throws IOException {
		get(content, ObjectNode.class, consumer);
	}

	/**
	 * Stream objects from the content as they become available.
	 * @param <T> the object type
	 * @param content the source content
	 * @param type the object type
	 * @param consumer the {@link ObjectNode} consumer
	 * @throws IOException on IO error
	 */
	public <T> void get(InputStream content, Class<T> type, Consumer<T> consumer) throws IOException {
		try (JsonParser parser = this.jsonMapper.createParser(content)) {
			while (!parser.isClosed()) {
				JsonToken token = parser.nextToken();
				if (token != null && token != JsonToken.END_OBJECT) {
					T node = read(parser, type);
					if (node != null) {
						consumer.accept(node);
					}
				}
			}
		}
	}

	@SuppressWarnings("unchecked")
	private <T> @Nullable T read(JsonParser parser, Class<T> type) {
		if (ObjectNode.class.isAssignableFrom(type)) {
			ObjectNode node = (ObjectNode) this.jsonMapper.readTree(parser);
			if (node == null || node.isMissingNode() || node.isEmpty()) {
				return null;
			}
			return (T) node;
		}
		return this.jsonMapper.readValue(parser, type);
	}

}

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free