Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to keep the order of the keys in the template? #69

Closed
arkanos opened this issue Jun 12, 2019 · 6 comments · Fixed by #70
Closed

Is it possible to keep the order of the keys in the template? #69

arkanos opened this issue Jun 12, 2019 · 6 comments · Fixed by #70

Comments

@arkanos
Copy link

arkanos commented Jun 12, 2019

Hi,
First of all, thank you so much for this great package.

I have been using it for a while and I have found that all the result JSON structures are returned with the keys ordered alphabetically, ignoring completely the order specified in the template.

Is there a way to control this?

Thanks!

@tuananh
Copy link
Owner

tuananh commented Jun 12, 2019

4.3.3 Object
An object is a member of the type Object. It is an unordered collection of properties each of which contains a primitive value, object, or function. A function stored in a property of an object is called a method.

Because of this, I don't think it's possible. But lemme look into it more.

@tuananh
Copy link
Owner

tuananh commented Jun 12, 2019

also because of the c++ lib camaro uses to parse the template

https://github.com/nlohmann/json#order-of-object-keys

@tuananh
Copy link
Owner

tuananh commented Jun 13, 2019

@arkanos can you try using the next release?

npm i camaro@next (4.0.4-apha.0)

@arkanos
Copy link
Author

arkanos commented Jun 13, 2019

@tuananh wow did not expect an answer that fast!

Thank you sir! Gonna test it out and put some results over here.

@arkanos
Copy link
Author

arkanos commented Jun 13, 2019

Hi @tuananh!

I have been testing during the last 3 hours with 37 different XML files and so far so good! It works like a charm!

Here is an example:

let xml = `
    <notes>
        <note>
            <to>Ruben</to>
            <from>Jane</from>
            <heading>Test</heading>
            <body>Hello there!</body>
        </note>
        <note>
            <to>Tove</to>
            <from>Jani</from>
            <heading>Reminder</heading>
            <body>Don't forget me this weekend!</body>
        </note>
    </notes>
`;

let template = {
    notes: ['//note',
    {
        to: 'to',
        from: 'from',
        heading: 'heading',
        body: 'body'
    }]
};

Result:

{
    "notes":[
        {
            "to":"Ruben",
            "from":"Jane",
            "heading":"Test",
            "body":"Hello there!"
        },
        {
            "to":"Tove",
            "from":"Jani",
            "heading":"Reminder",
            "body":"Don't forget me this weekend!"
        }
    ]
}

@tuananh
Copy link
Owner

tuananh commented Jun 14, 2019

Released in v4.0.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants