AXForum  
Вернуться   AXForum > Microsoft Dynamics CRM > Dynamics CRM: Blogs
All
Забыли пароль?
Зарегистрироваться Правила Справка Пользователи Сообщения за день Поиск

 
 
Опции темы Поиск в этой теме Опции просмотра
Старый 29.01.2015, 15:13   #1  
Blog bot is offline
Blog bot
Участник
 
25,475 / 846 (79) +++++++
Регистрация: 28.10.2006
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.

Read this post at its original location at http://vjeko.com/blog/passing-json-javascript-calpart-2, or visit the original blog at http://vjeko.com. 5e33c5f6cb90c441bd1f23d5b9eeca34The post Passing JSON from JavaScript to C/AL–Part 2 appeared first on Vjeko.com.

</img> </img>

Читать дальше
__________________
Расскажите о новых и интересных блогах по Microsoft Dynamics, напишите личное сообщение администратору.
 


Ваши права в разделе
Вы не можете создавать новые темы
Вы не можете отвечать в темах
Вы не можете прикреплять вложения
Вы не можете редактировать свои сообщения

BB коды Вкл.
Смайлы Вкл.
[IMG] код Вкл.
HTML код Выкл.
Быстрый переход

Рейтинг@Mail.ru
Часовой пояс GMT +3, время: 00:14.
Powered by vBulletin® v3.8.5. Перевод: zCarot
Контактная информация, Реклама.