codope commented on code in PR #9579: URL: https://github.com/apache/hudi/pull/9579#discussion_r1326138425
########## hudi-common/src/main/java/org/apache/hudi/avro/JsonEncoder.java: ########## @@ -0,0 +1,352 @@ +/* + * 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.hudi.avro; + +import org.apache.avro.AvroTypeException; +import org.apache.avro.Schema; +import org.apache.avro.io.Encoder; +import org.apache.avro.io.ParsingEncoder; +import org.apache.avro.io.parsing.JsonGrammarGenerator; +import org.apache.avro.io.parsing.Parser; +import org.apache.avro.io.parsing.Symbol; +import org.apache.avro.util.Utf8; +import org.codehaus.jackson.JsonEncoding; +import org.codehaus.jackson.JsonFactory; +import org.codehaus.jackson.JsonGenerator; +import org.codehaus.jackson.util.MinimalPrettyPrinter; + +import java.io.IOException; +import java.io.OutputStream; +import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; +import java.util.BitSet; +import java.util.EnumSet; +import java.util.Objects; +import java.util.Set; + +/** + * An {@link Encoder} for Avro's JSON data encoding. + * + * <p>NOTE: This class is a copy of Avro's JsonEncoder class, with the only difference being that + * this class does not wrap union types in JSON by overriding {@link #writeIndex(int)}. + * + * <p>By default, Avro's JSON encoding for union types is to wrap the union value + * in a JSON object with the type name as the key (e.g., {"string": "value"}). + * This encoder overrides that behavior to write just the value, resulting in cleaner JSON output. + * + * <p>For instance, a union with schema ["null", "string"] would be encoded as just + * "value" instead of {"string": "value"}. + * + * <p>This encoder is particularly useful when the standard Avro JSON format's verbosity + * for union types is not desired. + */ +public class JsonEncoder extends ParsingEncoder implements Parser.ActionHandler { Review Comment: As mentioned in the javadoc, we need this to avoid wrapping field type when encoding avro to json. Initially, I had tried subclassing this under `org.apache.avro.io` package in hudi-common. Bundle validation failed with that appraoch because of illegal access. The constructor in the original code is package-private. So, I had to port over the code with some minor modifications as mentioned in the javadoc fo this class. I have already attributed this code in `LICENSE` file and copied from Avro's notice in `NOTICE` file. -- 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]
