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

com.groupbyinc.common.jackson.jq.JsonQueryUtils Maven / Gradle / Ivy

There is a newer version: 198
Show newest version
package net.thisptr.jackson.jq;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JsonQueryUtils {
	private JsonQueryUtils() {}

	@Deprecated
	private static Scope defaultScope = new Scope();

	@Deprecated
	public static  List apply(final JsonQuery jq, final Object in, final Class resultType) throws IOException {
		return apply(defaultScope, jq, in, resultType);
	}

	public static  List apply(final Scope scope, final JsonQuery jq, final Object in, final Class resultType) throws IOException {
		return map(scope.getObjectMapper(), jq.apply(scope, (JsonNode) scope.getObjectMapper().valueToTree(in)), resultType);
	}

	@Deprecated
	public static  List apply(final JsonQuery jq, final Object in, final TypeReference resultType) throws IOException {
		return apply(defaultScope, jq, in, resultType);
	}

	public static  List apply(final Scope scope, final JsonQuery jq, final Object in, final TypeReference resultType) throws IOException {
		return map(scope.getObjectMapper(), jq.apply(scope, (JsonNode) scope.getObjectMapper().valueToTree(in)), resultType);
	}

	public static  List map(final ObjectMapper mapper, final List xs, final TypeReference resultType) throws IOException {
		final List result = new ArrayList<>();
		for (final JsonNode x : xs) {
			final T tmp = mapper. readValue(mapper.treeAsTokens(x), resultType);
			result.add(tmp);
		}
		return result;
	}

	public static  List map(final ObjectMapper mapper, final List xs, final Class resultType) throws IOException {
		final List result = new ArrayList<>();
		for (final JsonNode x : xs)
			result.add(mapper.treeToValue(x, resultType));
		return result;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy