Skip to content

Commit 56935de

Browse files
Implemented writing of empty arrays. (#71)
1 parent b16ed3b commit 56935de

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/Dahomey.Cbor.Tests/CborWriterValueTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,5 +142,15 @@ public void WriteArray()
142142
};
143143
Helper.TestWrite(array, hexBuffer);
144144
}
145+
146+
[Fact]
147+
public void WriteEmptyArray()
148+
{
149+
const string hexBuffer = "80";
150+
151+
CborArray array = new CborArray();
152+
153+
Helper.TestWrite(array, hexBuffer);
154+
}
145155
}
146156
}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,13 @@ int ICborArrayWriter<ArrayWriterContext>.GetArraySize(ref ArrayWriterContext con
191191

192192
bool ICborArrayWriter<ArrayWriterContext>.WriteArrayItem(ref CborWriter writer, ref ArrayWriterContext context)
193193
{
194-
Write(ref writer, context.array[context.index++]);
195-
return context.index < context.array.Count;
194+
if (context.array.Count > 0)
195+
{
196+
Write(ref writer, context.array[context.index++]);
197+
return context.index < context.array.Count;
198+
}
199+
200+
return false;
196201
}
197202

198203
CborObject? ICborConverter<CborObject?>.Read(ref CborReader reader)

0 commit comments

Comments
 (0)