Skip to content

Working with arrays in recursive models #70

@mellis481

Description

@mellis481

Functionality exists to specify the path to arrays in your model in the X2JS options. This prevents an array with a single item from being serialized as an object instead of an array. Code-wise, this is accomplished with the following where Items is an array in MyXml:

 var x2js = new X2JS({
        arrayAccessFormPaths : [
           "MyXml.Items"
        ]
    });

How this is accomplished with a recursive model, though? Using Typescript to give an example:

interface MyEntity {
    id: string;
    name: string;
    children: MyEntity[];
}

In this example, you would have potentially unlimited arrayAccessFormPaths values to account for all the levels of children in MyEntity. Eg.

 var x2js = new X2JS({
        arrayAccessFormPaths : [
           "MyEntity.children",
           "MyEntity.children.children",
           "MyEntity.children.children.children",
           "MyEntity.children.children.children.children",
           "MyEntity.children.children.children.children.children",
           ...
        ]
    });

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions