c# - Mass Transit unable to deserialize collections with exactly one element -
i've got mass transit message interface this:
public interface iperson { icollection<personalname> names { get; } } public class personalname { public string familyname { get; set; } public string givenname { get; set; } public string secondname { get; set; } public string use { get; set; } } and works serializing , deserializing message using jsonmessageserializer. can serialize message using xmlmessageserializer, , result looks bit this:
<person> <names> <familyname>simpson</familyname> <givenname>homer</givenname> <secondname>jay</secondname> <use>official</use> </names> <names> <givenname>homie</givenname> <use>nickname</use> </names> </person> and can deserialize if collection empty or if has more 1 element. however, if collection contains exactly one element, when go deserialize it, error:
cannot deserialize current json object (e.g.
{"name":"value"}) type 'system.collections.generic.icollection`1[myns.personalname]' because type requires json array (e.g.[1,2,3]) deserialize correctly.to fix error either change json json array (e.g.
[1,2,3]) or change deserialized type normal .net type (e.g. not primitive type integer, not collection type array orlist<t>) can deserialized json object. jsonobjectattribute can added type force deserialize json object.path 'person.names.familyname'.
i can fix using array or list<t>, i'd avoid doing that. there way mass transit's xmlmessageserializer deserialize icollection<t> types? since mass transit uses json.net serialization under hood (even xml), i'm hoping there's way of annotating type can accept icollection<t>.
instead of trying deserialize:
{"familyname":"smith","givenname":"john"} you have pass in array deserialize:
[{"familyname":"smith","givenname":"john"}]
Comments
Post a Comment