Transforming JSON Strings and X++ Contract Classes in D365FO - Microsoft Dynamics 365 Vietnam

Microsoft Dynamics 365 Vietnam

Song Nghia - Microsoft Dynamics 365 Vietnam

Breaking

Tuesday, July 9, 2024

Transforming JSON Strings and X++ Contract Classes in D365FO


Transforming JSON Strings and X++ Contract Classes in D365FO



Introduction to D365FO Contract Classes and JSON Serialization

In Dynamics 365 for Finance and Operations (D365FO), working with contract classes and JSON serialization/deserialization is crucial for efficient data handling. This blog post aims to share some effective techniques for developing data contracts and managing JSON data in D365FO.

Key to Working with Complex JSON Structures: Data Collection Attribute

When dealing with complex JSON structures, the data collection attribute becomes essential. Here is an example of a JSON structure and how it can be managed within D365FO.

Example JSON Structure

json
MyBaseContract: { "MySubContractClassArray": [{ "Id": "1", "Value": "Value1" }, { "Id": "2", "Value": "Value2" } ], "SomeValue1": "1 2 3", "MyAddressClass": { "Address1": "123 White Road", "City": "Los Angeles", "Country": "USA", "Email": "", "Name": "Forest Wholesales", "Phone": "123-555-0159", "Zipdiv": "90004" }, "SomeValue2": "Some description", "SomeStringArray": ["AAA", "BBB", "CCC"] }

Data Contract Parameter Methods

Single Value Element of Basic Type

For a standard single value element of a basic type:

x++
[DataMember('SomeValue1')] public str parmSomeValue1(str _someValue1 = someValue1) { someValue1 = _someValue1; return someValue1; }

List Collection of a Custom Class

For a method that handles a list collection of a specific custom class:

x++
[DataMember('MySubContractClassArray'), DataCollection(Types::Class, classStr(MySubContractClass))] public List parmMySubContractClassArray(List _mySubContractClassArray = mySubContractClassArray) { mySubContractClassArray = _mySubContractClassArray; return mySubContractClassArray; }

Custom Class as a Type

For a method that uses a custom class as a type:

x++
[DataMember('MyAddressClass')] public MyAddressClass parmReceiver(MyAddressClass _addressContract = addressContract) { addressContract = _addressContract; return addressContract; }

List Type with Data Collection Attribute

For a method that handles a list type with a data collection attribute of the base type string:

x++
[DataMember('SomeStringArray'), DataCollection(Types::String)] public List parmSomeStringArray(List _someStringArray = someStringArray) { someStringArray = _someStringArray; return someStringArray; }

Conclusion

Using the correct data contract structure for a known JSON structure makes casting JSON into a contract class straightforward. Even complex structures with sub-contract classes, lists of sub-contract classes, and arrays can be managed easily.

Here's how you can deserialize and serialize JSON strings with your contract class:

Deserialization

x++
responseContract = FormJsonSerializer::deserializeObject(classNum(MyBaseContract), responseBody);
  • responseContract
    : Your data contract object.
  • MyBaseContract
    : Your contract class name.
  • responseBody
    : The JSON string.

Serialization

x++
responseBody = FormJsonSerializer::serializeClass(responseContract);

I hope this guide serves as a handy reference for your future projects involving D365FO contract classes and JSON data handling.

No comments:

Post a Comment