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.