All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.xlrit.gears.engine.export.JsonExportTarget Maven / Gradle / Ivy

package com.xlrit.gears.engine.export;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Objects;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

public class JsonExportTarget implements ExportTarget {
	private final ObjectNode result;
	private final Deque collection;

	public JsonExportTarget(ObjectMapper objectMapper) {
		this.result = objectMapper.createObjectNode();
		this.collection = new ArrayDeque<>();
	}

	@Override
	public ExportOptions defaultOptions() {
		return ExportOptions.DEFAULT;
	}

	@Override
	public void startCollection(String name) {
		collection.push(result.putArray(name));
	}

	@Override
	public void add(String id, JsonNode element) {
		Objects.requireNonNull(id, "id must not be null");
		Objects.requireNonNull(element, "element must not be null");
		ArrayNode currentCollectionNode = collection.peek();
		currentCollectionNode.add(element);
	}

	@Override
	public void endCollection(String name) {
		collection.pop();
	}

	@Override
	public JsonNode result() {
		return result;
	}

	@Override
	public void close() {
		// nothing to do
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy