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

com.groupbyinc.common.jackson.jq.internal.functions.ImplodeFunction Maven / Gradle / Ivy

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

import java.util.Collections;
import java.util.List;

import net.thisptr.jackson.jq.Function;
import net.thisptr.jackson.jq.JsonQuery;
import net.thisptr.jackson.jq.Scope;
import net.thisptr.jackson.jq.exception.JsonQueryException;
import net.thisptr.jackson.jq.internal.BuiltinFunction;
import net.thisptr.jackson.jq.internal.misc.Preconditions;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeType;
import com.fasterxml.jackson.databind.node.TextNode;

@BuiltinFunction("implode/0")
public class ImplodeFunction implements Function {
	@Override
	public List apply(final Scope scope, final List args, final JsonNode in) throws JsonQueryException {
		Preconditions.checkInputArrayType("implode", in, JsonNodeType.NUMBER);

		final StringBuilder builder = new StringBuilder();
		for (final JsonNode ch : in) {
			if (ch.canConvertToInt()) {
				builder.append((char) ch.asInt());
			} else {
				throw new JsonQueryException("input to implode() must be a list of codepoints; " + ch.getNodeType() + " found");
			}
		}
		return Collections. singletonList(new TextNode(builder.toString()));
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy