raminqaf commented on code in PR #29310:
URL: https://github.com/apache/flink/pull/29310#discussion_r4122532453


##########
flink-core/src/main/java/org/apache/flink/types/variant/JsonVariantFormatter.java:
##########
@@ -0,0 +1,259 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.flink.types.variant;
+
+import org.apache.flink.annotation.Internal;
+
+import org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonFactory;
+import 
org.apache.flink.shaded.jackson2.com.fasterxml.jackson.core.JsonGenerator;
+
+import java.io.CharArrayWriter;
+import java.io.IOException;
+import java.time.LocalDate;
+import java.time.LocalTime;
+import java.time.ZoneOffset;
+import java.util.Base64;
+
+import static 
org.apache.flink.types.variant.BinaryVariantUtil.TIMESTAMP_FORMATTER;
+import static 
org.apache.flink.types.variant.BinaryVariantUtil.TIMESTAMP_LTZ_FORMATTER;
+import static org.apache.flink.types.variant.BinaryVariantUtil.TIME_FORMATTER;
+import static org.apache.flink.types.variant.BinaryVariantUtil.getMetadataKey;
+import static org.apache.flink.types.variant.BinaryVariantUtil.handleArray;
+import static org.apache.flink.types.variant.BinaryVariantUtil.handleObject;
+import static org.apache.flink.types.variant.BinaryVariantUtil.microsToInstant;
+import static org.apache.flink.types.variant.BinaryVariantUtil.nanosToInstant;
+import static org.apache.flink.types.variant.BinaryVariantUtil.readUnsigned;
+import static org.apache.flink.types.variant.BinaryVariantUtil.unexpectedType;
+
+/**
+ * Renders a {@link BinaryVariant} as JSON.
+ *
+ * <p>{@link #STRICT} backs {@link Variant#toJson()} and fails on NaN, 
infinity, and undecodable
+ * nodes. {@link #LENIENT} backs {@link BinaryVariant#toString()} and never 
fails: it writes NaN as
+ * {@code "NaN"}, a type this version does not know as {@code "<UNKNOWN>"}, 
and other undecodable
+ * data as {@code "<INVALID>"}.
+ */
+@Internal
+public final class JsonVariantFormatter implements VariantFormatter {
+
+    public static final JsonVariantFormatter STRICT = new 
JsonVariantFormatter(false);
+
+    public static final JsonVariantFormatter LENIENT = new 
JsonVariantFormatter(true);
+
+    private static final String UNKNOWN_NODE = "<UNKNOWN>";
+
+    private static final String INVALID_NODE = "<INVALID>";
+
+    private final boolean lenient;
+
+    private JsonVariantFormatter(final boolean lenient) {
+        this.lenient = lenient;
+    }
+
+    @Override
+    public String format(final Variant variant) {
+        final BinaryVariant binary = (BinaryVariant) variant;
+        final StringBuilder sb = new StringBuilder();
+        append(binary.rawValue(), binary.getMetadata(), binary.getPos(), sb);
+        return sb.toString();
+    }
+
+    private void append(
+            final byte[] value, final byte[] metadata, final int pos, final 
StringBuilder sb) {
+        final int start = sb.length();
+        try {
+            appendNode(value, metadata, pos, sb);
+        } catch (VariantTypeException e) {
+            if (!lenient) {
+                throw e;
+            }
+            // Drop the node's partial output, such as a dangling key.
+            sb.setLength(start);
+            appendQuoted(
+                    sb, BinaryVariantUtil.isUnknownType(value, pos) ? 
UNKNOWN_NODE : INVALID_NODE);
+        }
+    }
+
+    private void appendNode(byte[] value, byte[] metadata, int pos, 
StringBuilder sb) {
+        switch (BinaryVariantUtil.getType(value, pos)) {
+            case OBJECT:
+                handleObject(
+                        value,
+                        pos,
+                        (size, idSize, offsetSize, idStart, offsetStart, 
dataStart) -> {
+                            sb.append('{');
+                            for (int i = 0; i < size; ++i) {
+                                int id = readUnsigned(value, idStart + idSize 
* i, idSize);
+                                int offset =
+                                        readUnsigned(
+                                                value, offsetStart + 
offsetSize * i, offsetSize);
+                                int elementPos = dataStart + offset;
+                                if (i != 0) {
+                                    sb.append(',');
+                                }
+                                sb.append(escapeJson(getMetadataKey(metadata, 
id)));
+                                sb.append(':');
+                                append(value, metadata, elementPos, sb);
+                            }
+                            sb.append('}');
+                            return null;
+                        });
+                break;
+            case ARRAY:
+                handleArray(
+                        value,
+                        pos,
+                        (size, offsetSize, offsetStart, dataStart) -> {
+                            sb.append('[');
+                            for (int i = 0; i < size; ++i) {
+                                int offset =
+                                        readUnsigned(
+                                                value, offsetStart + 
offsetSize * i, offsetSize);
+                                int elementPos = dataStart + offset;
+                                if (i != 0) {
+                                    sb.append(',');
+                                }
+                                append(value, metadata, elementPos, sb);
+                            }
+                            sb.append(']');
+                            return null;
+                        });
+                break;
+            case NULL:
+                sb.append("null");
+                break;
+            case BOOLEAN:
+                sb.append(BinaryVariantUtil.getBoolean(value, pos));
+                break;
+            case TINYINT:
+            case SMALLINT:
+            case INT:
+            case BIGINT:
+                sb.append(BinaryVariantUtil.getLong(value, pos));

Review Comment:
   `BinaryVariantUtil.getLong` is the byte-level reader. It reads 1, 2, 4 or 8 
bytes by the header (`INT1` to
   `INT8`) and widens to `long`.
   I will add a comment to make it a bit more clear.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to