Help, Define a TREE structure in a domain class?

Edd

New member
Hi, I'd like to define a tree structure. Here's what I got so far.

DOMAIN CLASS
Code:
class Tree {
    String text
    Tree parent
    
    static hasMany = [ children: Tree ]
    
    static constraints = {
        parent(nullable: true)
    }
    
    String toString(){
        text
    }
}

I think it works fine, but, with this class I want to generate a JSON like this:

JSON (I want):

Code:
{
    "redInstanceList": [

        {

            "text": "Root",

            "children": [

                {

                    "text": "Parent 2",

                    "children": []

                },

                {

                    "text": "Parent 1",

                    "children": [

                        {

                            "text": "Child 1",

                            "children": []

                        }

                    ]

                }

            ]

        }

    ],

    "success": true

}

Instead I'm getting this JSON:

Code:
{

    "redInstanceList": [

        {

            "text": "Root",

            "children": [

                {

                    "text": "Parent 2",

                    "children": []

                },

                {

                    "text": "Parent 1",

                    "children": [

                        {

                            "text": "Child 1",

                            "children": []

                        }

                    ]

                }

            ]

        },

        {

            "text": "Parent 1",

            "children": [

                {

                    "text": "Child 1",

                    "children": []

                }

            ]

        },

        {

            "text": "Parent 2",

            "children": []

        },

        {

            "text": "Child 1",

            "children": []

        }

    ],

    "success": true

}

What am I doing wrong?
 
Top