0

I'm trying to deserialize a JSON object array in C#. The array represents a row of a table, mainly consisting of plain strings. However, one or more of the items in the array may not be a string but infact a JSON object in itself, e.g.

"rows":[[{"date":"20140521","category":"one"},"data","moredata","evenmoredata"],...]

or on a different response from the server, the order may be different

"rows":[["data","moredata",{"date":"20140521","category":"one"},"evenmoredata"],...]

I'm trying to just treat this as a list of objects, with a known type called RowObject below:

[DataContract]
[KnownType(typeof(RowObject))]
public class Table
{
    // other members to deserialize

    [DataMember(Name = "rows")]
    public List<List<object>> Rows { get; set; }
}

[DataContract]
public class RowObject
{
    [DataMember(Name = "date")]
    public DateTime date { get; set; }

    [DataMember(Name = "category")]
    public string category { get; set; }
}

This approach kind of worked in that the plain strings in the row were deserialized, however the RowObjects do not seem to be recognised even though I have tried to put them down as a KnownType. When I look at my deserialized List<object> Row, the RowObject just seems to be a blank object shown as {object} in the debugger.

I've managed to do this with known types and derived types elsewhere in the project but this problem dealing with plain strings has got me stuck. I've had a look at this question which seems pretty similar, but my problem with this answer is that I don't know which elements in the list are going to be the complex type. Also, I'm just using the .Net DataContractJsonSerializer throughout the project so would like to avoid third party libraries if at all possible.

Is it possible to deserialize a JSON array of different types like this?

Community
  • 1
  • 1
SWilliams
  • 691
  • 7
  • 28
  • Is it possible to clear out the non complex type members of the list, and afterwards process it as need be? Just put the other members in a different array for each type or discard as needed? Then use the list's sorting method. – Kat May 21 '14 at 17:37
  • 1
    How do you expect a serializer to deserialize to RowObject, if there is no type information in your json string of whatsoever? – Edin May 21 '14 at 17:42
  • I guess I was naively hoping that if the serializer saw it had some known types it would try to make a RowObject instead of the "base class" object. Not as easy as that! – SWilliams May 23 '14 at 06:23

1 Answers1

0

Set EmitTypeInformation in DataContractJsonSerializerSettings to EmitTypeInformation.Always on the server side. That way you will get information about the types of your objexts, inside the json string.

Edin
  • 1,476
  • 11
  • 21
  • Thanks for this. I couldn't change the server side to do this exactly as per your answer, but it helped a lot. In the end the server now sends across a separate list containing the type information and I wrote a custom deserializer with JSON.Net to populate my List with the right types. – SWilliams May 23 '14 at 06:27