10

I've been searching for a while for a nice and clean way to convert a JSON object to a dynamic object.

(I could cast to an object but the Twitter Streaming API actually sends two different JSON objects with the possibility of future object types!)

The code I use currently is from:

Deserialize JSON into C# dynamic object?

But its not the cleanest code and I was playing around with Web Matrix and noticed that they have a nice JSON.Decode(string) and JSON.Encode(object) methods and wanted to make use of them.

http://msdn.microsoft.com/en-us/library/system.web.helpers.json(v=vs.99).aspx

Adding a reference to System.Web.Helpers to my C# console application I managed to compile a solution calling JSON.Decode but... it throws a nasty exception.

This is probably down to me using it in a way not intended (outside Web Matrix) but any ideas? Probably expecting a simple, no thats silly answer ;-)

Attempt by method 'System.Web.Helpers.Json.Decode(System.String)' to access field 'System.Web.Helpers.Json._serializer' failed.


I'm using VS2010.

More detail: System.FieldAccessException was caught Message=Attempt by method 'System.Web.Helpers.Json.Decode(System.String)' to access field 'System.Web.Helpers.Json._serializer' failed. Source=System.Web.Helpers StackTrace: at System.Web.Helpers.Json.Decode(String value) at Components.DataCollection.ConvertTwitterStream.ConvertTweets() in C:\Users\Administrator\documents\visual studio 2010\Projects\ISMM\Components\DataCollection\ConvertTwitterStream.cs:line 35 InnerException:

Community
  • 1
  • 1
Phil
  • 4,012
  • 5
  • 39
  • 57
  • Could you post the entire exception? You are trying to compile to full .NET running under what security context? Full Trust? Medium Trust? etc.. – Jeffrey Feb 23 '11 at 19:44
  • @Jeffrey Added more details, using VS2010 so unsure which security context. – Phil Feb 23 '11 at 20:00
  • The guy in the comments of this site http://rhizohm.net/irhetoric/post/2011/02/03/JSON-Deserialization-Made-Simple-With-SystemWebHelpers-and-Listlt;dynamicgt;.aspx is have the same issue as you; claiming that it doesn't work with Console app, but with WinForm. Is it possible that you are not including all the required assemblies? I don't have WebMatrix installed here at work, so the exception is a mystery to me. I would have to look at the source of the Json.Decode method using Reflector. The MSDN docs say that it has the "Medium trust for the immediate caller" required permission. – Jeffrey Feb 23 '11 at 20:25
  • Yeh I found that comment too. I created a blank winforms project and I get the same error as the console app. – Phil Feb 23 '11 at 20:37
  • Copying the code found through reflector into a new C# class I see the error being shown is: 'System.Web.Helpers.DynamicJavaScriptConverter' is inaccessible due to its protection level – Phil Feb 23 '11 at 20:44
  • It is probably just an internal class that cannot be accessed from outside the library's assembly. – Jeffrey Feb 23 '11 at 20:49

6 Answers6

13

Debugging calls to 'Json.Decode' fail when the Visual Studio hosting process is enabled (which is the default). I found it worked with the hosting process disabled or when running without the debugger.

The hosting process can be disabled for your project by following these instructions: http://msdn.microsoft.com/en-us/library/ms185330.aspx

Chris O
  • 131
  • 1
  • 3
  • I used this for a while, but found other corner cases where this solution does not work. – Gleno Sep 08 '12 at 06:33
5

To support jbtule's answer, JsonFx v2 (http://github.com/jsonfx/jsonfx) makes this really easy. The example below shows a full round-trip with dynamic object being built from a JSON string and then serialized back into JSON.

string input = "{ \"foo\": true, \"array\": [ 42, false, \"Hello!\", null ] }";
dynamic value = new JsonReader().Read(input);
// verify that it works
Console.WriteLine(value.foo); // true
Console.WriteLine(value.array[0]); // 42
Console.WriteLine(value.array.Length); // 4

string output = new JsonWriter().Write(value);
// verify that it works
Console.WriteLine(output); // {"foo":true,"array":[42,false,"Hello!",null]}
mckamey
  • 17,359
  • 16
  • 83
  • 116
3

JsonFx Supports several strategies of binding json to .net objects including dynamic objects.

https://github.com/jsonfx/jsonfx

jbtule
  • 31,383
  • 12
  • 95
  • 128
2

I don't recall if Json.NET has support for dynamic objects yet, but it seems that you are able to do so with a little extra custom coding.

http://json.codeplex.com/ http://weblogs.asp.net/britchie/archive/2010/08/05/json-net-dynamic-extensions.aspx

Jeffrey
  • 509
  • 4
  • 11
1

Microsoft has added a Json helper class to Web Matrix Beta 2. Sample code here http://www.mikesdotnetting.com/Article/160/WebMatrix-Working-With-The-JSON-Helper

Kin
  • 1,522
  • 1
  • 13
  • 11
0

There seems to be a privileges issue when using Json.Decode in a Console App. Right-mouse click on your EXE and "Run as administrator..." and it should work.

I am not sure if there is a way to force Visual Studio to run the executable as administrator for debugging purposes or not.

Jeremy H
  • 1,784
  • 4
  • 17
  • 27