From e4cffd1e7be1d6770767e4a1fffc16521a51612b Mon Sep 17 00:00:00 2001 From: Zoltan Csizmadia Date: Thu, 4 Apr 2024 12:49:04 -0500 Subject: [PATCH] Use WriteMapStart in WriteMap --- .../apache/main/Specific/SpecificWriter.cs | 2 +- .../src/apache/test/IO/JsonCodecTests.cs | 45 +++++++++++++++++-- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/lang/csharp/src/apache/main/Specific/SpecificWriter.cs b/lang/csharp/src/apache/main/Specific/SpecificWriter.cs index b595241f39a..53d6407e947 100644 --- a/lang/csharp/src/apache/main/Specific/SpecificWriter.cs +++ b/lang/csharp/src/apache/main/Specific/SpecificWriter.cs @@ -149,7 +149,7 @@ protected override void WriteMap(MapSchema schema, object value, Encoder encoder if (map == null) throw new AvroTypeException("Map does not implement non-generic IDictionary"); - encoder.WriteArrayStart(); + encoder.WriteMapStart(); encoder.SetItemCount(map.Count); foreach (System.Collections.DictionaryEntry de in map) { diff --git a/lang/csharp/src/apache/test/IO/JsonCodecTests.cs b/lang/csharp/src/apache/test/IO/JsonCodecTests.cs index 28aab10e70c..1c909275594 100644 --- a/lang/csharp/src/apache/test/IO/JsonCodecTests.cs +++ b/lang/csharp/src/apache/test/IO/JsonCodecTests.cs @@ -287,11 +287,12 @@ public void TestJsonDecoderReorderFields() } [Test] - public void TestJsonDecoderSpecificWithArray() + public void TestJsonDecoderSpecificDatumWriterWithArrayAndMap() { Root data = new Root(); Item item = new Item { id = 123456 }; data.myarray = new List { item }; + data.mymap = new Dictionary { { "1", 1 }, { "2", 2 }, { "3", 3 }, { "4", 4 } }; DatumWriter writer = new SpecificDatumWriter(data.Schema); @@ -306,7 +307,32 @@ public void TestJsonDecoderSpecificWithArray() using (StreamReader reader = new StreamReader(listStreams[0])) { String output = reader.ReadToEnd(); - Assert.AreEqual("{\"myarray\":[{\"id\":123456}]}", output); + Assert.AreEqual("{\"myarray\":[{\"id\":123456}],\"mymap\":{\"map\":{\"1\":1,\"2\":2,\"3\":3,\"4\":4}}}", output); + } + } + + [Test] + public void TestJsonDecoderSpecificDefaultWriterWithArrayAndMap() + { + Root data = new Root(); + Item item = new Item { id = 123456 }; + data.myarray = new List { item }; + data.mymap = new Dictionary { { "1", 1 }, { "2", 2 }, { "3", 3 }, { "4", 4 } }; + + SpecificDefaultWriter writer = new SpecificDefaultWriter(data.Schema); + + ByteBufferOutputStream bbos = new ByteBufferOutputStream(); + + Encoder encoder = new JsonEncoder(data.Schema, bbos); + writer.Write(data, encoder); + encoder.Flush(); + + List listStreams = bbos.GetBufferList(); + + using (StreamReader reader = new StreamReader(listStreams[0])) + { + String output = reader.ReadToEnd(); + Assert.AreEqual("{\"myarray\":[{\"id\":123456}],\"mymap\":{\"map\":{\"1\":1,\"2\":2,\"3\":3,\"4\":4}}}", output); } } @@ -357,9 +383,10 @@ public partial class Root : global::Avro.Specific.ISpecificRecord public static global::Avro.Schema _SCHEMA = global::Avro.Schema.Parse( "{\"type\":\"record\",\"name\":\"Root\",\"namespace\":\"Avro.Test\",\"fields\":[{\"name\":\"myarray" + "\",\"type\":{\"type\":\"array\",\"items\":{\"type\":\"record\",\"name\":\"Item\",\"namespace\":\"Avr" + - "o.Test\",\"fields\":[{\"name\":\"id\",\"type\":\"long\"}]}}}]}"); - + "o.Test\",\"fields\":[{\"name\":\"id\",\"type\":\"long\"}]}}},{\"name\":\"mymap\",\"default\":null," + + "\"type\":[\"null\",{\"type\":\"map\",\"values\":\"int\"}]}]}"); private IList _myarray; + private IDictionary _mymap; public virtual global::Avro.Schema Schema { @@ -372,11 +399,18 @@ public IList myarray set { this._myarray = value; } } + public IDictionary mymap + { + get { return this._mymap; } + set { this._mymap = value; } + } + public virtual object Get(int fieldPos) { switch (fieldPos) { case 0: return this.myarray; + case 1: return this.mymap; default: throw new global::Avro.AvroRuntimeException("Bad index " + fieldPos + " in Get()"); } } @@ -388,6 +422,9 @@ public virtual void Put(int fieldPos, object fieldValue) case 0: this.myarray = (IList)fieldValue; break; + case 1: + this.mymap = (IDictionary)fieldValue; + break; default: throw new global::Avro.AvroRuntimeException("Bad index " + fieldPos + " in Put()"); } }