From 1fce1453294f9647178e67849629642be88b57f2 Mon Sep 17 00:00:00 2001 From: Dai MIKURUBE Date: Fri, 18 Aug 2023 16:40:27 +0900 Subject: [PATCH] [WIP] Implement flattening JSON Arrays --- .../org/embulk/util/json/JsonValueParser.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/main/java/org/embulk/util/json/JsonValueParser.java b/src/main/java/org/embulk/util/json/JsonValueParser.java index 8cfa727..5a4bf8b 100644 --- a/src/main/java/org/embulk/util/json/JsonValueParser.java +++ b/src/main/java/org/embulk/util/json/JsonValueParser.java @@ -34,6 +34,7 @@ public final class JsonValueParser implements Closeable { private JsonValueParser( final com.fasterxml.jackson.core.JsonParser jacksonParser, + final int depthToFlattenJsonArrays, final boolean hasLiteralsWithNumbers, final boolean hasFallbacksForUnparsableNumbers, final double defaultDouble, @@ -41,6 +42,7 @@ private JsonValueParser( this.jacksonParser = Objects.requireNonNull(jacksonParser); this.valueReader = new InternalJsonValueReader( hasLiteralsWithNumbers, hasFallbacksForUnparsableNumbers, defaultDouble, defaultLong); + this.depthToFlattenJsonArrays = depthToFlattenJsonArrays; this.hasLiteralsWithNumbers = hasLiteralsWithNumbers; this.hasFallbacksForUnparsableNumbers = hasFallbacksForUnparsableNumbers; this.defaultDouble = defaultDouble; @@ -54,6 +56,7 @@ public static final class Builder { Builder(final JsonFactory factory) { this.factory = Objects.requireNonNull(factory); this.root = null; + this.depthToFlattenJsonArrays = 0; this.hasLiteralsWithNumbers = false; this.hasFallbacksForUnparsableNumbers = false; this.defaultDouble = 0.0; @@ -84,6 +87,17 @@ public Builder root(final String root) { return this; } + /** + * Sets the depth to flatten JSON Arrays to parse. + * + * @param depthToFlattenJsonArrays the depth to flatten JSON Arrays + * @return this builder + */ + public Builder setDepthToFlattenJsonArrays(final int depthToFlattenJsonArrays) { + this.depthToFlattenJsonArrays = depthToFlattenJsonArrays; + return this; + } + /** * Enables creating {@link JsonDouble} and {@link JsonLong} instances with supplemental literal strings. * @@ -126,6 +140,7 @@ public Builder fallbackForUnparsableNumbers(final double defaultDouble, final lo public JsonValueParser build(final String json) throws IOException { return new JsonValueParser( buildJacksonParser(json), + this.depthToFlattenJsonArrays, this.hasLiteralsWithNumbers, this.hasFallbacksForUnparsableNumbers, this.defaultDouble, @@ -141,6 +156,7 @@ public JsonValueParser build(final String json) throws IOException { public JsonValueParser build(final InputStream jsonStream) throws IOException { return new JsonValueParser( buildJacksonParser(jsonStream), + this.depthToFlattenJsonArrays, this.hasLiteralsWithNumbers, this.hasFallbacksForUnparsableNumbers, this.defaultDouble, @@ -170,6 +186,7 @@ private com.fasterxml.jackson.core.JsonParser extendJacksonParser(final com.fast private final JsonFactory factory; private JsonPointer root; + private int depthToFlattenJsonArrays; private boolean hasLiteralsWithNumbers; private boolean hasFallbacksForUnparsableNumbers; private double defaultDouble; @@ -243,6 +260,7 @@ public final void close() throws IOException { private final com.fasterxml.jackson.core.JsonParser jacksonParser; private final InternalJsonValueReader valueReader; + private final int depthToFlattenJsonArrays; private final boolean hasLiteralsWithNumbers; private final boolean hasFallbacksForUnparsableNumbers; private final double defaultDouble;