Skip to main content

Notifications

Announcements

No record found.

Finance | Project Operations, Human Resources, ...
Suggested answer

JSON serialization of a class that contains a list/array of objects of another class in X++

(0) ShareShare
ReportReport
Posted on by 30

As the subject suggests, I am trying to do the JSON serialization of an object, an instance of a class that, among other properties, has a list or array (I don't know what's better, that could be another question) of objects, instances of another class. Basically, my main class is of a header-details type. When I try JSON serialization using Newtonsoft.Json.JsonConvert::SerializeObject method, the main object is serialized correctly, but its Details property is empty in the JSON. What am I missing or doing wrong?

  • Ascalab Profile Picture
    Ascalab 30 on at
    RE: JSON serialization of a class that contains a list/array of objects of another class in X++

    I have implemented it the way you suggested, but the output was still empty details list. I eventually found a solution that looks a bit silly, but it worked. I found it on another thread of this forum, and it was posted by the user M.K. It's not marked as a verified answer, but it worked for me (and for another member of the mentioned discussion). The command that worked is:

    str formatedJSON = JsonConvert::SerializeObject(JsonConvert::DeserializeObject(FormJsonSerializer::serializeClass(object)), Formatting::Indented);

    So the object was first serialized using FormJsonSerializer, and then deserialized and serialized again using Newtonsoft's JsonConvert class.

  • Suggested answer
    Anton Venter Profile Picture
    Anton Venter 19,457 Super User 2024 Season 2 on at
    RE: JSON serialization of a class that contains a list/array of objects of another class in X++

    To serialize correctly as children of the parent, the child objects must be contained in a class member variable like a List for example and be exposed properly (method must be decorated as such). Below is an untested example just to demonstrate what I mean. I use the FormJsonSerializer class to serialize/deserialize but the NewtonSoft library should work as well. If you search online for FormJsonSerializer, you will find some alternative examples.

    public class MyHeaderClass
    {
    	protected List childrenList;
    
        [DataMemberAttribute('Lines'), DataCollectionAttribute(Types::Class, classStr(MyLineClass))]
        public List parmChildren(List _childrenList = childrenList)
        {
            childrenList = _childrenList;
            return childrenList;
        }
    }

  • Bharani Preetham Peraka Profile Picture
    Bharani Preetham Pe... 3,587 Super User 2024 Season 1 on at
    RE: JSON serialization of a class that contains a list/array of objects of another class in X++

    Empty means either your details list is null completely. Means you did not implement the details class. Can you send the parm method for this class?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Congratulations 2024 Spotlight Honorees

Kudos to all of our 2024 community stars! 🎉

Meet the Top 10 leaders for December

Congratulations to our December super stars! 🥳

Start Your Super User Journey Pt 2

Join the ranks of our community heros! 🦹

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,791 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,488 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans