0

Currently, I have some JSON data that I am attempting to deserialize using the DataContractJsonSerializer class. However, one of the arrays in the data contains multiple types of objects. Is there a way to deserialize this data properly? I am aware that a very similar question exists, but I would prefer not to use Json.NET or any other third-party libraries.

EDIT: A small example:

In this instance, let's say the JSON is of form [{"foo":string},{"bar":string},{"foo":string},{"foo":string},...] where each element is either of form {"foo":string} or {"bar":string}. Then, the contracts could be set up as such:

[DataContract]
class Foo { [DataMember] public string foo; }

[DataContract]
class Bar { [DataMember] public string bar; }

In this context, my question is, how do I deserialize this array of Foos and Bars?

dbc
  • 104,963
  • 20
  • 228
  • 340
LegionMammal978
  • 680
  • 8
  • 15
  • `However, one of the arrays in the data contains multiple types of objects` -- could you provide an example? How are these object types different? If for some reason they are completely different you may consider `dynamic` type. Posting a sample of the JSON and the model you are attempting to deserialize it to would be helpful. – Shane Ray Feb 22 '17 at 16:20
  • @ShaneRay Added a small example, in the real case, my `Foo` and `Bar` have completely different properties. – LegionMammal978 Feb 22 '17 at 16:26

1 Answers1

0

This does not sound right. There should not be two completely different types in a single array. Given the JSON provided I would try something like this....

[DataContract]
class SomeClass
{
    [DataMember]
    public string foo { get; set;}
    [DataMember]
    public string bar { get; set;}
}

Then check for IsNullOrWhiteSpace() on each property.

Updated with more code...

   static void Main(string[] args)
    {
        SomeClass[] output;
        var json = "[{\"foo\":\"value\"},{\"bar\":\"value\"},{\"foo\":\"value1\"},{\"foo\":\"value1\"}]";

        using (var ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))
        {
            var deserializer = new DataContractJsonSerializer(typeof(SomeClass[]));
             output = (SomeClass[])deserializer.ReadObject(ms);
        }

        // do something with output
        Console.WriteLine(output.Length);
    }
Shane Ray
  • 1,449
  • 1
  • 13
  • 18
  • Specifically, I am querying a certain JSON web API which gives 2 responses (with different formats) in an array. – LegionMammal978 Feb 22 '17 at 16:33
  • In that case you need to have two separate models. For the first API call that returns an array you deserialize to `ModelA`. The second API call you deserialize to `ModelB`. – Shane Ray Feb 22 '17 at 16:35
  • I think you're misunderstanding what I'm trying to say. I pass one request to the server, and receive a single response with an array with a `Foo` and a `Bar`. I'm asking here how to deserialize this array to retrieve the `Foo` and `Bar` in it. – LegionMammal978 Feb 22 '17 at 16:37
  • The JSON you provided is an array of objects... It sounds like you may be referring to a dictionary but `foo` is repeated so it can not be an indexer. I have updated my answer with more example code. – Shane Ray Feb 22 '17 at 16:49
  • So pretty much, the only solution is to create a union of the two and set the array type to that? – LegionMammal978 Feb 22 '17 at 16:51
  • That is the best solution I can come up with given the provided data. If I had an example endpoint or a better example of the JSON returned there may be other options. – Shane Ray Feb 22 '17 at 16:53
  • If this answer helped solve your problem, please mark as "answered". – Shane Ray Feb 22 '17 at 17:26
  • 1
    I will, just waiting a day or two to make sure no better answers crop up. – LegionMammal978 Feb 22 '17 at 19:00