Passing JSON from JavaScript to C/AL–Part 2

I stand corrected. A month ago I wrote about how to pass JSON from JavaScript to C/AL and then handle it inside, and what I wrote is not wrong, but I just figured a simpler way.

Too bad all this is not documented, but hey – that’s why blogging is fun.

In my last blog post, I wrote that the steps are these:

  1. Declare a delegate in NAV that accepts an System.Object
  2. In JavaScript, declare a JSON object
  3. Invoke the InvokeExtensibilityMethod method (no, I am not dyslexic) to pass the JSON object to NAV
  4. In NAV, use DataContractJsonSerializer to deserialize the JSON text received form JavaScript.

Now, this works. But Steps 1 and 4 can be improved.

First, instead of declaring the delegate as this:

public delegate void ObjectEventHandler(object data);

… declare it as this:

public delegate void ObjectEventHandler(Person data);

Then, step 4 is completely unnecessary. When you pass a JSON to C/AL, NAV runtime will attempt to deserialize it into the object type you expect as a parameter. Here, if you expect a Person parameter, and you pass JSON that can be deserialized into an instance of the Person class, NAV runtime will do the deserialization for you, and you get the data as expected.

How does it work? I don’t know for a fact, because this is undocumented, and I only could figure it out by trial and error, but this is what I believe is going on:

  1. If you accept a parameter of type Object, NAV cannot deserialize the JSON object so it keeps it in its original state (Newtonsoft.Json.Linq.JObject).
  2. If you accept a parameter of an exact type, NAV will attempt to deserialize the JSON into that specific type. If it succeeds, you get the deserialized instance of the object.
  3. If deserialization into an exact type fails,  you don’t get an error or a null (I’d expected either one or another), but you get a working instance of the target object, with those properties assigned that exist in JSON, all others having been ignored.

I am too lazy to now go and check when exactly this was supported, was it since the beginning in NAV 2013 R2, or is it something new in NAV 2015, but this really is a nice feature. It allows you to have strong typing between JavaScript and C/AL and pass object seamlessly to JavaScript and back, as if you were really calling into C#. Awesome.

Vjeko

Vjeko has been writing code for living since 1995, and he has shared his knowledge and experience in presentations, articles, blogs, and elsewhere since 2002. Hopelessly curious, passionate about technology, avid language learner no matter human or computer.

This Post Has 8 Comments

  1. Manish Sinha

    Hi Vjeko,
    Last week I prepared a demo code for calling OData Web Service from NAV environment using dot net interoperability and deserialize response from JSon to XML using Newtonsoft.Json Dll.
    Waiting for response from Microsoft Dynamics Community Blog to publish this article.

  2. strangenikolai

    Quote from this link might provide more clues:
    https://msdn.microsoft.com/en-us/library/bb412179(v=vs.110).aspx

    “Normally, JSON serialization and deserialization is handled automatically by Windows Communication Foundation (WCF) when you use data contract types in service operations that are exposed over AJAX-enabled endpoints”

    This is how I suspect NAV Add-ins work i.e. WCF and AJAX

    1. Vjeko

      Nikolai, I think that this is not happening here. First, WCF uses DataContractJsonSerializer class, which is a .NET Class Library class and a part of .NET. NAV obviously uses the Newtonsoft Json.NET which you can tell by checking the GETDOTNETTYPE of an object passed into a System.Object parameter, and also by the fact that there is Newtonsoft’s Json.NET assembly present in the NAV application folder. Apparently, NST is manually serializing and deserializing objects to/from JSON using Json.NET. I never bothered much to test if there is any AJAX in NAV, but I suspect that there is none.

      1. strangenikolai

        Ah – didn’t think to check those details, I just assumed it worked like other .NET/Javascript projects (and it did) so I never questioned it.

        Oh well, happy that Newtonsoft is being promoted, I used to work with JamesNK (the author of it) – the baby-faced genius that he is. Small world…

        1. Vjeko

          Json.NET is really a work of genuis. I love it, no wonder it got its way into NAV.

  3. uugii0602

    Hello Vjeko,
    Can you share this example source file? I dont have .NET experience well, but I have to pass values between JSON string and C/AL?

    1. Vjeko

      There is a bunch of examples of this in all of my TechDays session from this and last year. Check it and let me know if you still need more examples.

      1. mehmet

        It would be great if you could provide a complete example of this solution above as i’m having hard time to make it work and could not find any other solution online. thanks in advance

Leave a Reply