|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.parquet.variant; |
| 18 | + |
| 19 | +import com.fasterxml.jackson.core.JsonFactory; |
| 20 | +import com.fasterxml.jackson.core.JsonParseException; |
| 21 | +import com.fasterxml.jackson.core.JsonParser; |
| 22 | +import com.fasterxml.jackson.core.JsonToken; |
| 23 | +import com.fasterxml.jackson.core.StreamReadConstraints; |
| 24 | +import com.fasterxml.jackson.core.exc.InputCoercionException; |
| 25 | +import java.io.IOException; |
| 26 | +import java.math.BigDecimal; |
| 27 | + |
| 28 | +/** |
| 29 | + * Parses JSON into {@link Variant} values using Jackson streaming. |
| 30 | + * |
| 31 | + * <p>This class isolates the Jackson dependency from {@link VariantBuilder}, |
| 32 | + * so that core variant construction does not require Jackson on the classpath. |
| 33 | + * |
| 34 | + * <p>Ported from Apache Spark's {@code VariantBuilder.parseJson}. |
| 35 | + */ |
| 36 | +public final class VariantJsonParser { |
| 37 | + |
| 38 | + private static final JsonFactory JSON_FACTORY = JsonFactory.builder() |
| 39 | + .streamReadConstraints(StreamReadConstraints.builder() |
| 40 | + .maxNestingDepth(500) |
| 41 | + .maxStringLength(10_000_000) |
| 42 | + .maxDocumentLength(50_000_000L) |
| 43 | + .build()) |
| 44 | + .build(); |
| 45 | + |
| 46 | + private VariantJsonParser() {} |
| 47 | + |
| 48 | + /** |
| 49 | + * Parses a JSON string and returns the corresponding {@link Variant}. |
| 50 | + * |
| 51 | + * <p>Uses Jackson streaming parser for single-pass conversion |
| 52 | + * with no intermediate tree. Number handling preserves precision: |
| 53 | + * integers use the smallest fitting type, floating-point numbers |
| 54 | + * prefer decimal encoding (no scientific notation) and fall back |
| 55 | + * to double. |
| 56 | + * |
| 57 | + * @param json the JSON string to parse |
| 58 | + * @return the parsed Variant |
| 59 | + * @throws IOException if the JSON is malformed or an I/O error occurs |
| 60 | + */ |
| 61 | + public static Variant parseJson(String json) throws IOException { |
| 62 | + try (JsonParser parser = JSON_FACTORY.createParser(json)) { |
| 63 | + parser.nextToken(); |
| 64 | + return parseJson(parser); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Parses a JSON value from an already-positioned {@link JsonParser} |
| 70 | + * and returns the corresponding {@link Variant}. The parser must |
| 71 | + * have its current token set (i.e., {@code parser.nextToken()} |
| 72 | + * or equivalent must have been called). |
| 73 | + * |
| 74 | + * @param parser a positioned Jackson JsonParser |
| 75 | + * @return the parsed Variant |
| 76 | + * @throws IOException if the JSON is malformed or an I/O error occurs |
| 77 | + */ |
| 78 | + public static Variant parseJson(JsonParser parser) throws IOException { |
| 79 | + VariantBuilder builder = new VariantBuilder(); |
| 80 | + buildJson(builder, parser); |
| 81 | + return builder.build(); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Recursively builds a Variant value from the current position of a |
| 86 | + * Jackson streaming parser. Handles objects, arrays, strings, numbers |
| 87 | + * (int/long/decimal/double), booleans, and null. |
| 88 | + */ |
| 89 | + private static void buildJson(VariantBuilder builder, JsonParser parser) throws IOException { |
| 90 | + JsonToken token = parser.currentToken(); |
| 91 | + if (token == null) { |
| 92 | + throw new JsonParseException(parser, "Unexpected null token"); |
| 93 | + } |
| 94 | + switch (token) { |
| 95 | + case START_OBJECT: |
| 96 | + buildJsonObject(builder, parser); |
| 97 | + break; |
| 98 | + case START_ARRAY: |
| 99 | + buildJsonArray(builder, parser); |
| 100 | + break; |
| 101 | + case VALUE_STRING: |
| 102 | + builder.appendString(parser.getText()); |
| 103 | + break; |
| 104 | + case VALUE_NUMBER_INT: |
| 105 | + buildJsonInteger(builder, parser); |
| 106 | + break; |
| 107 | + case VALUE_NUMBER_FLOAT: |
| 108 | + buildJsonFloat(builder, parser); |
| 109 | + break; |
| 110 | + case VALUE_TRUE: |
| 111 | + builder.appendBoolean(true); |
| 112 | + break; |
| 113 | + case VALUE_FALSE: |
| 114 | + builder.appendBoolean(false); |
| 115 | + break; |
| 116 | + case VALUE_NULL: |
| 117 | + builder.appendNull(); |
| 118 | + break; |
| 119 | + default: |
| 120 | + throw new JsonParseException(parser, "Unexpected token " + token); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + private static void buildJsonObject(VariantBuilder builder, JsonParser parser) throws IOException { |
| 125 | + VariantObjectBuilder obj = builder.startObject(); |
| 126 | + while (parser.nextToken() != JsonToken.END_OBJECT) { |
| 127 | + obj.appendKey(parser.currentName()); |
| 128 | + parser.nextToken(); |
| 129 | + buildJson(obj, parser); |
| 130 | + } |
| 131 | + builder.endObject(); |
| 132 | + } |
| 133 | + |
| 134 | + private static void buildJsonArray(VariantBuilder builder, JsonParser parser) throws IOException { |
| 135 | + VariantArrayBuilder arr = builder.startArray(); |
| 136 | + while (parser.nextToken() != JsonToken.END_ARRAY) { |
| 137 | + buildJson(arr, parser); |
| 138 | + } |
| 139 | + builder.endArray(); |
| 140 | + } |
| 141 | + |
| 142 | + private static void buildJsonInteger(VariantBuilder builder, JsonParser parser) throws IOException { |
| 143 | + try { |
| 144 | + appendSmallestLong(builder, parser.getLongValue()); |
| 145 | + } catch (InputCoercionException ignored) { |
| 146 | + buildJsonFloat(builder, parser); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + private static void buildJsonFloat(VariantBuilder builder, JsonParser parser) throws IOException { |
| 151 | + if (!tryAppendDecimal(builder, parser.getText())) { |
| 152 | + builder.appendDouble(parser.getDoubleValue()); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Appends a long value using the smallest integer type that fits. |
| 158 | + */ |
| 159 | + private static void appendSmallestLong(VariantBuilder builder, long l) { |
| 160 | + if (l == (byte) l) { |
| 161 | + builder.appendByte((byte) l); |
| 162 | + } else if (l == (short) l) { |
| 163 | + builder.appendShort((short) l); |
| 164 | + } else if (l == (int) l) { |
| 165 | + builder.appendInt((int) l); |
| 166 | + } else { |
| 167 | + builder.appendLong(l); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * Tries to parse a number string as a decimal. Only accepts plain |
| 173 | + * decimal format (digits, minus, dot -- no scientific notation). |
| 174 | + * Returns true if the number was successfully appended as a decimal. |
| 175 | + */ |
| 176 | + private static boolean tryAppendDecimal(VariantBuilder builder, String input) { |
| 177 | + for (int i = 0; i < input.length(); i++) { |
| 178 | + char ch = input.charAt(i); |
| 179 | + if (ch != '-' && ch != '.' && !(ch >= '0' && ch <= '9')) { |
| 180 | + return false; |
| 181 | + } |
| 182 | + } |
| 183 | + BigDecimal d = new BigDecimal(input); |
| 184 | + if (d.scale() <= VariantUtil.MAX_DECIMAL16_PRECISION |
| 185 | + && d.precision() <= VariantUtil.MAX_DECIMAL16_PRECISION) { |
| 186 | + builder.appendDecimal(d); |
| 187 | + return true; |
| 188 | + } |
| 189 | + return false; |
| 190 | + } |
| 191 | +} |
0 commit comments