rayokota commented on code in PR #1833: URL: https://github.com/apache/avro/pull/1833#discussion_r953338043
########## lang/csharp/src/apache/main/IO/JsonEncoder.cs: ########## @@ -0,0 +1,356 @@ +/* + * 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 + * + * https://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. + */ + +using Avro.IO.Parsing; +using System.Collections; +using System.IO; +using System.Text; +using Newtonsoft.Json; + +namespace Avro.IO +{ + /// <summary> + /// An <see cref="Encoder"/> for Avro's JSON data encoding. + /// + /// JsonEncoder buffers output, and data may not appear on the output until + /// <see cref="Encoder.Flush()"/> is called. + /// + /// JsonEncoder is not thread-safe. + /// </summary> + public class JsonEncoder : ParsingEncoder, Parser.IActionHandler + { + private readonly Parser parser; + private JsonWriter writer; + private bool includeNamespace = true; + + // Has anything been written into the collections? + private readonly BitArray isEmpty = new BitArray(64); + + /// <summary> + /// Initializes a new instance of the <see cref="JsonEncoder"/> class. + /// </summary> + public JsonEncoder(Schema sc, Stream stream) : this(sc, getJsonWriter(stream, false)) + { + } + + /// <summary> + /// Initializes a new instance of the <see cref="JsonEncoder"/> class. + /// </summary> + public JsonEncoder(Schema sc, Stream stream, bool pretty) : this(sc, getJsonWriter(stream, pretty)) + { + } + + /// <summary> + /// Initializes a new instance of the <see cref="JsonEncoder"/> class. + /// </summary> + public JsonEncoder(Schema sc, JsonWriter writer) + { + Configure(writer); + this.parser = new Parser((new JsonGrammarGenerator()).Generate(sc), this); + } + + /// <inheritdoc /> + public override void Flush() + { + parser.ProcessImplicitActions(); + if (writer != null) + { + writer.Flush(); + } + } + + // by default, one object per line. + // with pretty option use default pretty printer with root line separator. + private static JsonWriter getJsonWriter(Stream stream, bool pretty) + { + JsonWriter writer = new JsonTextWriter(new StreamWriter(stream)); + if (pretty) + { + writer.Formatting = Formatting.Indented; + } + + return writer; + } + + /// <summary> + /// Whether to include the namespace. + /// </summary> + public virtual bool IncludeNamespace Review Comment: Done ########## lang/csharp/src/apache/main/IO/Parsing/JsonGrammarGenerator.cs: ########## @@ -0,0 +1,106 @@ +/* + * 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 + * + * https://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. + */ + +using System; +using System.Collections.Generic; + +namespace Avro.IO.Parsing +{ + /// <summary> + /// The class that generates a grammar suitable to parse Avro data in JSON + /// format. + /// </summary> + public class JsonGrammarGenerator : ValidatingGrammarGenerator + { + /// <summary> + /// Returns the non-terminal that is the start symbol for the grammar for the + /// grammar for the given schema <tt>sc</tt>. Review Comment: Done -- 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]
