Monday, July 11, 2022

Serialize/Deserialize JSON using contract class In D365FO

 We need to create (Serialize)/ Deserialize JSON using contract class in D365FO.

We can serialzing/ Deserializing in multiple places.

  • 1. Using JSON class
  • 2. Using Contract classes.
Way 1:

Way 2:

JSON response:

{
  "Contract": {
    "CustomerId": "Customer 1",
    "Name"      : "Name 1"
  }
}

Below is the Response contract :

[DataContractAttribute] 
public class responceContract
{
    private Contract CustomerContract;     [DataMemberAttribute('Contract'),     DataCollection(Types::Class, classStr(Contract)),     AifCollectionTypeAttribute('_contractClass', Types::Class, classStr(Contract)),     AifCollectionTypeAttribute('return', Types::Class, classStr(Contract))]     public Contract parmContract(Contract _CustomerContract = CustomerContract)     {           CustomerContract = _CustomerContract;       return CustomerContract;     } }


JSON response:

{
  "Contract": [
    {
      "CustomerId": "Customer 1",
      "Name": "Name 1"
    },
    {
      "CustomerId": "Customer 2",
      "Name": "Name 2"
    }
  ]
}

Below is the Response contract :

[DataContractAttribute] 
public class responceContract
{
    private List    Customer;     [DataMemberAttribute('Contract'),     DataCollection(Types::Class, classStr(Customer Contract)),     AifCollectionTypeAttribute('_contractClass', Types::Class, classStr(Customer Contract)),     AifCollectionTypeAttribute('return', Types::Class, classStr(Customer Contract))]     public List parmCustomer(List _contractClass = Customer)     {         Customer = _Customer;         return Customer;     } }


Contract class:

private str    CustomerId; 
private str    Name; 
[DataMemberAttribute('CustomerId')] 
public Str parmCustomerId(Str _CustomerId = CustomerId)
{
    CustomerId = _CustomerId;

    return CustomerId;
} 

[DataMemberAttribute('Name')] 
public Str parmName(Str _Name = Name)
{
    Name = _Name;

    return Name;
}


Keep Daxing!!


No comments:

Post a Comment