Deserialize JSON into List with X++

Viewed 44

I have a problem with generic types in X++. I need to deserialize a JSON list yet everything I tried failed. Like using IEnumerables and JsonSerializer(does it find only AX classes and can't see references library classes?).

My helper class is in a C# library and I only need to get access to values inside the response JSON that are in list. How can I archive this in X++?

//X++
defaultException defaultException= new defaultException();
defaultException= JsonConvert::DeserializeObject(response, defaultException.GetType()); <- this gives is correct yet I cant use the values in the list

//values = FormJsonSerializer::deserializeCollection(classnum(List), response, Types::Class, 'defaultException');  

// C#
public class defaultException
{
    public MyException exception { get; set; }
}

public class MyException
{
    public string serviceCtx { get; set; }
    public string serviceCode { get; set; }
    public string serviceName { get; set; }
    public string timestamp { get; set;}
    public string referenceNumber { get; set; }
    public List<exceptionDetailList> exceptionDetailList { get; set; }
}
public class exceptionDetailList
{
    public int exceptionCode { get; set; }
    public string exceptionDescription { get; set; }
}
1 Answers

Found a solution. If we have another list in this list we need to recreate the enumerator in loop again and again as needed.

defaultException defaultException = new defaultException();
defaultException = JsonConvert::DeserializeObject(batch, defaultException.GetType());

System.Collections.IEnumerable exceptionList = defaultException.exception.exceptionDetailList;
System.Collections.IEnumerator enumerator = exceptionList.GetEnumerator();

while (enumerator.MoveNext())
{
    exceptionDetailList exceptionDetailList = new exceptionDetailList();
    exceptionDetailList =  enumerator.Current;
}
Related