Hello,
I'm using one object in which I want de deserialize data from multiple formats (for the moment just 2: XML and JSON). Here is how the object will look like:
[DataContract]
[XmlRoot(ElementName = "root")]
public class Person
{
[DataMember(Name="name")]
[XmlElement("name")]
public string Name { get; set; }
[DataMember(Name = "account")]
[XmlElement("account")]
public List<Account> Accounts { get; set; }
}
[DataContract]
public class Account
{
[DataMember(Name = "id")]
[XmlAttribute("id")]
public string Id { get; set; }
}
The input XML could be:
<root>
<name>John</name>
<account id="1"/>
<account id="2"/>
<account id="3"/>
</root>
And the JSON input could be:
{
"name":"John",
"account": [
{ "id": "1" },
{ "id": "2" },
{ "id": "3" },
]
}
This works fine untill I'm getting as input a file with just one 'account' element. For JSON the "account" element isn't an array anymore but just a simple element (so the "List<Account> Accounts" type doesn't apply, it only works with "Account Accounts"):
{
"name":"John",
"account": { "id": "1" }
}
For JSON I'm using DataContractJsonSerializer for deserialization.
Is there any way to have just one single object and deserialize both JSONs in it (there is no way of modifying the input JSONs, this is how they are generated)?
Thanks,
George