web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :

Deserialize Json build from a dataContract class in D365FO

Chaitanya Golla Profile Picture Chaitanya Golla 17,225

Hi,

In this post we will see the process of deserializing Json build from a datacontract class. For demo purpose, I created a datacontract class by name CG_CustomerDataContract which hosts the data of the fields AccountNum and CustomerGroup from the table CustTable. Serialized Json string with these two values(of the selected customer) and deserialized it.

DataContract class: CG_CustomerDataContract

[DataContractAttribute]
class CG_CustomerDataContract
{
    CustAccount custAccount;
    CustGroupId custGroupId;
 
    [
        DataMemberAttribute('CustAccount'),
        SysOperationLabelAttribute(literalstr("Customer")),
        SysOperationHelpTextAttribute(literalstr("Customer account"))
    ]
    public CustAccount parmCustAccount(CustAccount _custAccount = custAccount)
    {
        custAccount = _custAccount;
        return custAccount;
    }
 
    [
        DataMemberAttribute('custGroupId'),
        SysOperationLabelAttribute(literalstr("Customer group")),
        SysOperationHelpTextAttribute(literalstr("Customer groupId"))
    ]
    public CustGroupId parmCustGroupId(CustGroupId _custGroupId = custGroupId)
    {
        custGroupId = _custGroupId;
        return custGroupId;
    }
 
}

Runnable Class: CG_DeSerializeJsonFromList

class CG_DeSerializeJsonFromDataContract
{       
    /// <summary>
    /// Runs the class with the specified arguments.
    /// </summary>
    /// <param name = "_args">The specified arguments.</param>
    public static void main(Args _args)
    {   
        CustTable               custTable;
        CG_CustomerDataContract dataContract = new CG_CustomerDataContract();
        str                     json;
 
        select firstonly custTable
            where custTable.AccountNum == "1000";
        dataContract.parmCustAccount(custTable.AccountNum);
        dataContract.parmCustGroupId(custTable.CustGroup);
 
        // Serializing class object
        json = FormJsonSerializer::serializeClass(dataContract);
       
        // Deserializing class object
        dataContract = FormJsonSerializer::deserializeObject(classnum(CG_CustomerDataContract), json);
 
        Info(strFmt("AccountNum: %1", dataContract.parmCustAccount()));
        Info(strFmt("CustGroup: %1",  dataContract.parmCustGroupId()));
 
    }
 
}

Output: https://drive.google.com/open?id=1HARQ35AQxtUi7BiPtLkRJaaJStY1OfCu

Regards,

Chaitanya Golla

Comments

*This post is locked for comments