Newbie: Deserializing Expando object to JSON string?

837 views
Skip to first unread message

NM

unread,
Sep 18, 2013, 1:16:20 PM9/18/13
to jay...@googlegroups.com
Hi,

   Is it possible to deserialize an Expando object (which might contain arrays and/or dictionary of other Expando object) to a JSON string using the Jayrock? I was trying to find a quick example but was unsuccessful.

Thanks in advance and regards

Atif Aziz

unread,
Sep 18, 2013, 6:42:04 PM9/18/13
to jay...@googlegroups.com
I'm assuming you meant serializing instead of deserializing to a JSON string. If so, the following should just work:

dynamic o = new ExpandoObject();

o.Num = 1.234;

o.Str = "bar";

o.Obj = new ExpandoObject();

o.Obj.Arr = new bool[3];

JsonConvert.ExportToString((object) o).Dump();


and produce the JSON:

{"Num":1.234,"Str":"bar","Obj":{"Arr":[false,false,false]}}

- Atif


--
You received this message because you are subscribed to the Google Groups "Jayrock" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jayrock+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Navin Mishra

unread,
Sep 18, 2013, 7:58:25 PM9/18/13
to jay...@googlegroups.com
Thanks. (I did mean serializing...sorry for the typo.)

Consider this example code snippet when the JSON needs to have attributes that have hypen (-) in the name and so the dynamic object (to generate that JSON) needs to be accessed as a dictionary:

           dynamic request = new ExpandoObject();

            request.username = "jford";
            ((IDictionary<string, object>)request)["attr-a"] = "00sss0";
            ((IDictionary<string, object>)request)["attr-b"] = "test_role";
           
            string strJson = JsonConvert.ExportToString(request);

    The converted JSON does not look right. It is NOT same as {"username": "jford", "attr-a":"00sss0", "attr-b"] = "test_role"}.

  Does ExportToString has some options to generate JSON in correct format?

Thanks again and regards


From: Atif Aziz <aziz...@gmail.com>
To: "jay...@googlegroups.com" <jay...@googlegroups.com>
Sent: Wednesday, September 18, 2013 3:42 PM
Subject: Re: [Jayrock] Newbie: Deserializing Expando object to JSON string?

You received this message because you are subscribed to a topic in the Google Groups "Jayrock" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jayrock/XN0CuPhL1AA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jayrock+u...@googlegroups.com.

Atif Aziz

unread,
Sep 19, 2013, 12:33:28 PM9/19/13
to jay...@googlegroups.com
I just tried the exact code snippet you sent without any modifications and got the following JSON output, which seems correct:

{"username":"jford","attr-a":"00sss0","attr-b":"test_role"}

What version of Jayrock are you using?

- Atif

Navin Mishra

unread,
Sep 20, 2013, 2:27:48 AM9/20/13
to jay...@googlegroups.com
Thanks. That is interesting because I get this:

["[username, jford]","[attr-a, 00sss0]","[attr-b, test_role]"]

I am using the Jayrock version 0.9.12915.0. Is that not the latest then?

Thanks again and regards


Sent: Thursday, September 19, 2013 9:33 AM
Subject: Re: [Jayrock] Newbie: Seserializing Expando object to JSON string?

Navin Mishra

unread,
Sep 20, 2013, 2:41:08 AM9/20/13
to jay...@googlegroups.com
Scratch that. It looks like I was using .NET 2.0 version of the Jayrock. I noticed that there is a .NET 4.0 version as well and after using that the below code snippet produces correct JSON. Thanks!

I'm assuming then Jayrock can correctly serialize an Expando object (which might contain arrays and/or dictionary of other Expando object) to a JSON string. Can it correctly serialize anonymous objects (which might contain arrays and/or dictionary of other anonymous objects) as well?

Thanks again and regards


From: Navin Mishra <navin...@yahoo.com>
To: "jay...@googlegroups.com" <jay...@googlegroups.com>
Sent: Thursday, September 19, 2013 11:27 PM

Navin Mishra

unread,
Sep 20, 2013, 3:01:40 AM9/20/13
to jay...@googlegroups.com
Trying the same code but now with anonymous objects:

JsonConvert.ExportToString(new { username = "jford", attr_a = "00sss0", attr_b = "test_role"  });
It gave the correct JSON as well. Now I want to convert the underscores in object properties (whether Expando or anonymous) to to hypens when serializing to JSON so that it looks like:

{"username":"jford","attr-a":"00sss0","attr-b":"test_role"}
Is that possible via probably setting some option in Jayrock?

Thanks in advance and regards


Sent: Thursday, September 19, 2013 11:41 PM
Subject: Re: [Jayrock] Newbie: Serializing Expando/Anonymous object to JSON string?

Atif Aziz

unread,
Sep 20, 2013, 10:32:54 AM9/20/13
to jay...@googlegroups.com
Glad to hear that got sorted out. If you're using the NuGet package then normally it should reference the right build depending on your project target. If you re-targeted your project to .NET 4.0 later, then it may be that your NuGet version doesn't retarget the references automatically.

As for anonymous objects, those are support as well and with nested arrays, dictionary and anonymous objects.

- Atif

Atif Aziz

unread,
Sep 20, 2013, 10:36:28 AM9/20/13
to jay...@googlegroups.com
Check out the following message for a way to accomplish this:

- Atif

Navin Mishra

unread,
Sep 20, 2013, 1:18:52 PM9/20/13
to jay...@googlegroups.com
Thanks. I will try to follow that. I was hoping  there might be a complete example available (if at all) since it might be pretty common need.

Thanks again and regards



Sent: Friday, September 20, 2013 7:36 AM
Subject: Re: [Jayrock] Converting underscores in object property names to to hyphens when serializing ?

Navin Mishra

unread,
Aug 21, 2014, 2:31:29 PM8/21/14
to jay...@googlegroups.com
Thank you. It was very helpful. That works just fine.

But when using a custom implementation of Dynamic (such as here https://www.nuget.org/packages/GracefulDynamicDictionary/), it does not produce the JSON. Any ideas?

Thanks again and regards



Reply all
Reply to author
Forward
0 new messages