org.davidmoten.kool.json.LazyArrayNode Maven / Gradle / Ivy
package org.davidmoten.kool.json;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.function.Supplier;
import org.davidmoten.kool.Stream;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.core.TreeNode;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
public final class LazyArrayNode implements Supplier {
private final JsonParser parser;
private final ObjectMapper mapper;
LazyArrayNode(JsonParser parser, ObjectMapper mapper) {
this.parser = parser;
this.mapper = mapper;
}
@Override
public ArrayNode get() {
try {
return (ArrayNode) mapper.readTree(parser);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public Stream values() {
return Stream.defer(() -> //
Stream.generate(emitter -> {
JsonToken token = parser.nextToken();
if (token.equals(JsonToken.END_ARRAY)) {
emitter.onComplete();
} else {
TreeNode v = mapper.readTree(parser);
emitter.onNext((JsonNode) v);
}
})).doOnDispose(() -> parser.close());
}
public Stream values(Class cls) {
return Stream.defer(() -> {
// skip array start
parser.nextToken();
return Stream.generate(emitter -> {
if (parser.isClosed()) {
emitter.onComplete();
} else {
T v = mapper.readValue(parser, cls);
if (v == null) {
emitter.onComplete();
} else {
emitter.onNext(v);
}
}
});
}).doOnDispose(() -> parser.close());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy