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:
- Declare a delegate in NAV that accepts an System.Object
- Invoke the InvokeExtensibilityMethod method (no, I am not dyslexic) to pass the JSON object to NAV
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:
- 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).
- 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.
- 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.
This Post Has 8 Comments
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.
Quote from this link might provide more clues:
“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
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.
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…
Json.NET is really a work of genuis. I love it, no wonder it got its way into NAV.
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?
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.
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