Hi guys, Today we see how to deSerialize the JSON file using x++.
I have created a new job to upload JSON files and retrieve the data from that file.
Below JSON file I have used.
JSON File:
Example 1:
[
{
"CUSTOMERGROUPID": 10,
"CLEARINGPERIODPAYMENTTERMNAME": "Net30",
"CUSTOMERACCOUNTNUMBERSEQUENCE": "",
"DEFAULTDIMENSIONDISPLAYVALUE": "",
"DESCRIPTION": "Wholesales customers",
"ISSALESTAXINCLUDEDINPRICE": "No",
"PAYMENTTERMID": "Net30",
"TAXGROUPID": "",
"WRITEOFFREASON": ""
},
{
"CUSTOMERGROUPID": 100,
"CLEARINGPERIODPAYMENTTERMNAME": "Net10",
"CUSTOMERACCOUNTNUMBERSEQUENCE": "",
"DEFAULTDIMENSIONDISPLAYVALUE": "",
"DESCRIPTION": "Intercompany retail customers",
"ISSALESTAXINCLUDEDINPRICE": "No",
"PAYMENTTERMID": "Net10",
"TAXGROUPID": "",
"WRITEOFFREASON": ""
},
{
"CUSTOMERGROUPID": 1000,
"CLEARINGPERIODPAYMENTTERMNAME": "Net30",
"CUSTOMERACCOUNTNUMBERSEQUENCE": "",
"DEFAULTDIMENSIONDISPLAYVALUE": "",
"DESCRIPTION": "Wholesales customers",
"ISSALESTAXINCLUDEDINPRICE": "No",
"PAYMENTTERMID": "Net30",
"TAXGROUPID": "",
"WRITEOFFREASON": ""
}
]
Job:
I created a new job to upload the JSON file.
class TestJsonFile
{
public static void main(Args _args)
{
str jsonString;
FileUploadTemporaryStorageResult fileUpload = File::GetFileFromUser()
as FileUploadTemporaryStorageResult;
System.IO.Stream stream = fileUpload.openResult();
using (var reader = new System.IO.StreamReader(stream))
{
jsonString = reader.ReadToEnd();
}
if (jsonString)
{
custGpService custGpService = new custGpService();
custGpService.test(jsonString);
}
}
}
Upload the file:
Service class:
Here I wrote the main logic to deSerialize the JSON file
class custGpService
{
public void test(str json)
{
List values = new List(Types::String);
ListEnumerator value;
//custGpMultiContract contract = FormJsonSerializer::deserializeObject(
//classnum(custGpMultiContract), json);
values = FormJsonSerializer::deserializeCollection(classnum(List),
json, Types::Class, classStr(custGpContractClass));
//value = contract.parmCustomerList().getEnumerator();
value = values.getEnumerator();
while(value.moveNext())
{
custGpContractClass custGpContractClass = value.current();
info(strFmt("CustomerGroupId-%1, Description-%2, PAYMENTTERMID-%3",
custGpContractClass.parmId(),
custGpContractClass.parmDescription(),
custGpContractClass.parmPaymentTermId()));
}
}
}
Contract class:
[DataContract]
class custGpContractClass
{
private str id;
private str description;
private str paymentTermId;
[DataMember("CustomerGroupId")]
public str parmId(str _id = id)
{
id = _id;
return id;
}
[DataMember("Description")]
public str parmDescription(str _description = description)
{
description = _description;
return description;
}
[DataMember("PAYMENTTERMID")]
public str parmPaymentTermId(str _paymentTermId = paymentTermId)
{
paymentTermId = _paymentTermId;
return paymentTermId;
}
}
Output:
Json:
{
"result": {
"CountryId": "Country 1",
"StateList": [
{
"StateCode": "St1"
},
{
"StateCode": "St2"
},
{
"StateCode": "St3"
}
]
}
}
For the above schema, we need to create 2 contract classes. One is for the country and the second is for the state code. We need to create a new parm method in the 1st contract class and the return type is list. Like below.
[DataMemberAttribute("Name from Json file"),
DataCollection(Types::Class, classStr("2nd contract")),
AifCollectionTypeAttribute('_list', Types::Class, classStr("2nd contract")),
AifCollectionTypeAttribute('return', Types::Class, classStr("2nd contract"))
]
public List parmList(List _list = list)
{
list = _list;
return list;
}
Contract class1(Country):
[datacontract]
public class countryContract
{
CountryCode country;
List stateList;
[DataMemberAttribute('CountryId')]
public CountryCode parmCountryCode(CountryCode _country = country)
{
country = _country;
return country;
}
[DataMemberAttribute("StateList"),
DataCollection(Types::Class, classStr(StateContract)),
AifCollectionTypeAttribute('_stateList', Types::Class, classStr(StateContract)),
AifCollectionTypeAttribute('return', Types::Class, classStr(StateContract))
]
public List parmCountryList(List _stateList = stateList)
{
stateList = _stateList;
return stateList;
}
}
Contract class2(State):
[DataContract]
public class stateContract
{
StateId state;
[DataMemberAttribute('StateCode')]
public StateId parmStateCode(StateId _state = state)
{
state = _state;
return state;
}
}
Same service class we can use.