Skip to content

Commit 65816fb

Browse files
author
Michaël Catanzariti
committed
fix #75 Empty arrays fail to serialize
1 parent 56935de commit 65816fb

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

src/Dahomey.Cbor.Tests/CollectionTests.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,5 +105,56 @@ public void WriteObjectList()
105105

106106
Helper.TestWrite(lst, hexBuffer);
107107
}
108+
109+
[Fact]
110+
public void ReadEmptyArray()
111+
{
112+
string hexBuffer = "80";
113+
int[] array = Helper.Read<int[]>(hexBuffer);
114+
115+
Assert.Empty(array);
116+
}
117+
118+
[Fact]
119+
public void WriteEmptyArray()
120+
{
121+
string hexBuffer = "80";
122+
int[] array = { };
123+
Helper.TestWrite(array, hexBuffer);
124+
}
125+
126+
[Fact]
127+
public void ReadEmptyList()
128+
{
129+
string hexBuffer = "80";
130+
List<int> list = Helper.Read<List<int>>(hexBuffer);
131+
132+
Assert.Empty(list);
133+
}
134+
135+
[Fact]
136+
public void WriteEmptyList()
137+
{
138+
string hexBuffer = "80";
139+
List<int> list = new List<int>();
140+
Helper.TestWrite(list, hexBuffer);
141+
}
142+
143+
[Fact]
144+
public void ReadEmptyDictionary()
145+
{
146+
string hexBuffer = "A0";
147+
Dictionary<int, int> dict = Helper.Read<Dictionary<int, int>>(hexBuffer);
148+
149+
Assert.Empty(dict);
150+
}
151+
152+
[Fact]
153+
public void WriteEmptyDictionary()
154+
{
155+
string hexBuffer = "A0";
156+
Dictionary<int, int> dict = new Dictionary<int, int>();
157+
Helper.TestWrite(dict, hexBuffer);
158+
}
108159
}
109160
}

src/Dahomey.Cbor/Serialization/Converters/ArrayConverter.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ public int GetArraySize(ref WriterContext context)
101101

102102
public bool WriteArrayItem(ref CborWriter writer, ref WriterContext context)
103103
{
104+
if (context.array.Length == 0)
105+
{
106+
return false;
107+
}
108+
104109
_itemConverter.Write(ref writer, context.array[context.index++]);
105110
return context.index < context.array.Length;
106111
}

0 commit comments

Comments
 (0)