Posts: 7
Joined: Wed Jul 15, 2015 10:04 pm


Postby lix2k3 » Tue Jul 28, 2015 3:36 pm

Another 3rd party tool question....will Json.net be compatible with cshtml5?

Or is there access to System.Runtime.Serialization.Json?

JS-Support @Userware
Site Admin
Posts: 1093
Joined: Tue Apr 08, 2014 3:42 pm

Re: Json.net

Postby JS-Support @Userware » Thu Jul 30, 2015 9:28 am

Hi lix2k3,

At the moment there is no access to System.Runtime.Serialization.Json, but built-in JSON support is on the roadmap of CSHTML5.

As fas as JSON.NET is concerned, we have not tried to port its source code to CSHTML5 yet, but maybe someone in the community can try to do so.

It shouldn't be too difficult for someone in the community to implement JSON support for CSHTML5. For example, with the current version of CSHTML5 (beta 4), it is already possible to use the "JSIL.JSON.Parse" JavaScript method (defined in "Output\Libraries\JSIL.JSON.js") to transform a JSON string into a C# object made of nested List<object> and Dictionary<string,object>. Here is an example of C# code that does so. To test it, create a new empty CSHTML5 project and paste the following code into the MainPage constructor:

Code: Select all

if (CSharpXamlForHtml5.Environment.IsRunningInJavaScript)
    string jsonToParse = "{\"result\":true,\"count\":1}";
    Dictionary<string, object> deserializedObject = JSIL.Verbatim.Expression("JSIL.JSON.Parse($0)", jsonToParse);
    string result = deserializedObject["result"].ToString();
    string count = deserializedObject["count"].ToString();

Please note that the code will only be executed when your app is run inside the web browser, not inside the Simulator.

By the way, in case a reader is wondering how the "JSIL.Verbatim.Expression" method works, here is a short explanation that we will soon add to the CSHTML5 documentation. It is a very powerful feature that lets you call JavaScript code from within C#. Here, the method "JSIL.JSON.Parse" is a JavaScript method. To call it from C#, we need to use "JSIL.Verbatim.Expression", which keeps the string "as is" during the conversion from C# to JavaScript. Please note that "$0" is replaced AT COMPILE TIME (not at runtime) by the JS-equivalent code of the first argument (in this case, it is replaced by the JS code that allows to read "jsonToParse" at runtime). You can see the result by looking at the generated JavaScript code. Please note that because the replacement is done at compile time, the first argument of the "JSIL.Verbatim.Expression" method must be a constant expression (it cannot be a variable). The other arguments can be variables.


Posts: 7
Joined: Wed Jul 15, 2015 10:04 pm

Re: Json.net

Postby lix2k3 » Thu Jul 30, 2015 12:18 pm

I wasn't aware of that javascript functionality. Thanks for that.

Return to “General Discussion and Other”

Who is online

Users browsing this forum: No registered users and 1 guest