Skip to content

Commit

Permalink
Use WriteMapStart in WriteMap (apache#2836)
Browse files Browse the repository at this point in the history
Co-authored-by: Zoltan Csizmadia <[email protected]>
  • Loading branch information
2 people authored and Ranbir Kumar committed May 13, 2024
1 parent 3ba8930 commit a808e4c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
2 changes: 1 addition & 1 deletion lang/csharp/src/apache/main/Specific/SpecificWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
45 changes: 41 additions & 4 deletions lang/csharp/src/apache/test/IO/JsonCodecTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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> { item };
data.mymap = new Dictionary<string, int> { { "1", 1 }, { "2", 2 }, { "3", 3 }, { "4", 4 } };

DatumWriter<Root> writer = new SpecificDatumWriter<Root>(data.Schema);

Expand All @@ -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> { item };
data.mymap = new Dictionary<string, int> { { "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<MemoryStream> 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);
}
}

Expand Down Expand Up @@ -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<Avro.Test.Item> _myarray;
private IDictionary<string,System.Int32> _mymap;

public virtual global::Avro.Schema Schema
{
Expand All @@ -372,11 +399,18 @@ public IList<Avro.Test.Item> myarray
set { this._myarray = value; }
}

public IDictionary<string,System.Int32> 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()");
}
}
Expand All @@ -388,6 +422,9 @@ public virtual void Put(int fieldPos, object fieldValue)
case 0:
this.myarray = (IList<Avro.Test.Item>)fieldValue;
break;
case 1:
this.mymap = (IDictionary<string,System.Int32>)fieldValue;
break;
default: throw new global::Avro.AvroRuntimeException("Bad index " + fieldPos + " in Put()");
}
}
Expand Down

0 comments on commit a808e4c

Please sign in to comment.