Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PODIO JSON Feed to C# Objects polymorphism

Tags:

json

c#

.net

podio

I am writing a program to read a JSON string from Podio, and then convert the contents to c# objects.

But while reading the feed, I came across a strange format; At the same hierarchy-level of object, sometimes the value of the field [value] is a string but other times it is a complex object.

Example is below.

At some places it is like

"values":[
    {
        "value":"Bug on User Interface, Ajax sometimes does not load properly"
    }
],
"type":"text"

and then at the next item, at the same level in the hierarchy, it is like

"values":[
    {
        "value":{
            "perma_link":"https:\/\/ds-test.podio.com\/myworkspace\/files\/23529948",
            "mimetype":"image\/jpeg",
            "hosted_by":"podio",
            "name":"217820_274164679355286_689330144_n.jpg",
            "hosted_by_humanized_name":"Podio",
            "description":null,
            "thumbnail_link":"https:\/\/files.podio.com\/23529948",
            "link":"https:\/\/files.podio.com\/23529948",
            "file_id":23529948,
            "size":39698
        }
    }
],
"type":"image"

Note the value of "type": For the first instance it is "text" and then for the next it is "image". As they are on the same level of the hierarchy, I have no idea how to design the objects for them so that the DataContractJsonSerializer.Read method works smoothly.

Regards,

like image 219
ali zaib Avatar asked Nov 04 '22 14:11

ali zaib


1 Answers

Up front, I'm no JSON expert (I've dabbled), and while I'm a competent C#.net programmer, I've never worked with JSON from .NET. I would've thought you'd have at least one good, definitive answer by now, but since you haven't had any, I'll offer my $0.02 worth, in case it's helpful to you.

This is a valid format, according to http://www.json.org. (I'm not sure if by saying it's a "strange format", you're saying that you think it's not valid, or just that you think it is unusual).

In both example cases you've described, there is a consistent pattern between them:

<string1> : <value1>,
<string2> : <value2>

where in all cases:

  • <string1> == "Values",
  • <string2> == "type",
  • <value1> == [ <object> ] (ie <value1> is always an array containing a single <object>), and
  • <value2> == "a string describing the structure of the <object>"

Also, <object> is either a string when <value2> == "text", or it is a more complex object that contains 1 or more <string> : <value> pairs in a comma separated list when <value2> != "text".

So, how to parse, assuming .Net can't handle this standard format out-of-the-box (I'm surprised it can't)...

  • Build a class definition for each of only the objects you are interested in. You should be able to glean the required structure from the JSON.

  • Have you thought about running a string operation to "find/extract" the types you're interested in, and set up an explicit DataContractJsonSerializer(Type) for each of those types?

  • Otherwise, more generally you should be able to treat everything within <object> when <value2> != "text", as a Dictionary<string,string>, and then process this on a case-by-case basis as required, depending on your type specified at <value2> (or even transform these into your object "manually").


Further, I note that .NET 4.5 introduces this overload:

DataContractJsonSerializer Constructor (Type, DataContractJsonSerializerSettings)

with settings such as:

  • DataContractJsonSerializerSettings.UseSimpleDictionaryFormat,

  • DataContractJsonSerializerSettings.DataContractSurrogate.

And particularly interesting:

  • DataContractJsonSerializerSettings.KnownTypes

...a collection of types that may be present in the object graph serialized using this instance the DataContractJsonSerializerSettings.

These may be relevant, but I can't find any more information about any of these settings - perhaps something for you to experiment with.

like image 177
Sepster Avatar answered Nov 15 '22 05:11

Sepster