Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AVRO-3969: Use WriteMapStart in WriteMap #2836

Merged
merged 1 commit into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading