Serializing mixed type Collections
Opened this issue · 2 comments
Deleted user commented
Hi there,
I was expecting mixed type Collections to work, yet I am getting the following exception when I try
var list = new object[] { "hello", 4.854, "dio" };
CborArray test = CborArray.FromCollection(list);
System.InvalidCastException
HResult=0x80004002
Message=Unable to cast object of type 'Dahomey.Cbor.Serialization.Converters.StringConverter' to type 'Dahomey.Cbor.Serialization.Converters.IObjectConverter'.
Source=Dahomey.Cbor
StackTrace:
at Dahomey.Cbor.Serialization.Converters.ObjectConverter`1.Write(CborWriter& writer, T value, LengthMode lengthMode) in D:\Dahomey.Cbor\Dahomey.Cbor\src\Dahomey.Cbor\Serialization\Converters\ObjectConverter.cs:line 252
at Dahomey.Cbor.Serialization.Converters.CborConverterBase`1.Write(CborWriter& writer, T value) in D:\Dahomey.Cbor\Dahomey.Cbor\src\Dahomey.Cbor\Serialization\Converters\CborConverter.cs:line 32
at Dahomey.Cbor.Serialization.Converters.ArrayConverter`1.WriteArrayItem(CborWriter& writer, WriterContext& context) in D:\Dahomey.Cbor\Dahomey.Cbor\src\Dahomey.Cbor\Serialization\Converters\ArrayConverter.cs:line 104
at Dahomey.Cbor.Serialization.CborWriter.WriteArray[TC](ICborArrayWriter`1 arrayWriter, TC& context) in D:\Dahomey.Cbor\Dahomey.Cbor\src\Dahomey.Cbor\Serialization\CborWriter.cs:line 260
at Dahomey.Cbor.Serialization.Converters.ArrayConverter`1.Write(CborWriter& writer, TI[] value, LengthMode lengthMode) in D:\Dahomey.Cbor\Dahomey.Cbor\src\Dahomey.Cbor\Serialization\Converters\ArrayConverter.cs:line 68
at Dahomey.Cbor.Serialization.Converters.CborConverterBase`1.Write(CborWriter& writer, T value) in D:\Dahomey.Cbor\Dahomey.Cbor\src\Dahomey.Cbor\Serialization\Converters\CborConverter.cs:line 32
at Dahomey.Cbor.ObjectModel.CborArray.FromCollection[T](T collection, CborOptions options) in D:\Dahomey.Cbor\Dahomey.Cbor\src\Dahomey.Cbor\ObjectModel\CborArray.cs:line 188
at CosmosPal.Program.DeserializeFromFileStream() in D:\repos\powershell\vs2019\CosmosPal\CosmosPal\Program.cs:line 67
at CosmosPal.Program.Main(String[] args) in D:\repos\powershell\vs2019\CosmosPal\CosmosPal\Program.cs:line 125
Am I missing something here? I thought mixed type arrays are part of the CBOR standard, no?
Thanks so much,
N.
mcatanzariti commented
Hi,
Mixed type arrays are supported but CborArray.FromCollection is maint to be used for uniform collections of the same type.
In your specific case, you could use implicit conversion to CborValue mixed with the CborArray constructor from a CborValue array:
CborArray test = new CborArray("hello", 4.854, "dio");
Hope it helps!
mcatanzariti commented
@ardeego
Finally I found a way to support your example:
var list = new object[] { "hello", 4.854, "dio" };
CborArray test = CborArray.FromCollection(list);
in the following commit: f05f1e6