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

json with preserve_order_policy does not preserve order of elements #210

Closed
jkalmar opened this issue Jan 21, 2020 · 2 comments
Closed

json with preserve_order_policy does not preserve order of elements #210

jkalmar opened this issue Jan 21, 2020 · 2 comments

Comments

@jkalmar
Copy link

jkalmar commented Jan 21, 2020

We use ojson with multiple levels of objects that we then fill with data in code and want to pretty-print them, but the data are printed in reverse order:
Example code:

   jsoncons::ojson oj;

   oj[ "1" ][ "1" ][ "1" ] = 1;
   oj[ "1" ][ "1" ][ "2" ] = 1;
   oj[ "1" ][ "2" ][ "1" ] = 2;
   oj[ "1" ][ "2" ][ "2" ] = 2;
   oj[ "1" ][ "3" ][ "1" ] = 3;
   oj[ "1" ][ "3" ][ "2" ] = 3;

   oj[ "2" ][ "1" ][ "1" ] = 1;
   oj[ "2" ][ "1" ][ "2" ] = 1;
   oj[ "2" ][ "2" ][ "1" ] = 2;
   oj[ "2" ][ "2" ][ "2" ] = 2;
   oj[ "2" ][ "3" ][ "1" ] = 3;
   oj[ "2" ][ "3" ][ "2" ] = 3;

   std::cout << jsoncons::pretty_print( oj ) << std::endl;

The result is:

{
    "2": {
        "3": {
            "1": 3, 
            "2": 3
        }, 
        "2": {
            "1": 2, 
            "2": 2
        }, 
        "1": {
            "1": 1, 
            "2": 1
        }
    }, 
    "1": {
        "3": {
            "1": 3, 
            "2": 3
        }, 
        "2": {
            "1": 2, 
            "2": 2
        }, 
        "1": {
            "1": 1, 
            "2": 1
        }
    }
}

As you can see the first and second level keys are reversed, only the third level element order is preserved.
I have also tried to first create the upper levels but the output was the same:

   oj[ "1" ];
   oj[ "2" ];

   oj[ "1" ][ "1" ];
   oj[ "1" ][ "2" ];
   oj[ "1" ][ "3" ];

   oj[ "1" ][ "1" ][ "1" ] = 1;
   oj[ "1" ][ "1" ][ "2" ] = 1;
   oj[ "1" ][ "2" ][ "1" ] = 2;
   oj[ "1" ][ "2" ][ "2" ] = 2;
   oj[ "1" ][ "3" ][ "1" ] = 3;
   oj[ "1" ][ "3" ][ "2" ] = 3;

   oj[ "2" ];
   oj[ "2" ];

   oj[ "2" ][ "1" ];
   oj[ "2" ][ "2" ];
   oj[ "2" ][ "3" ];


   oj[ "2" ][ "1" ][ "1" ] = 1;
   oj[ "2" ][ "1" ][ "2" ] = 1;
   oj[ "2" ][ "2" ][ "1" ] = 2;
   oj[ "2" ][ "2" ][ "2" ] = 2;
   oj[ "2" ][ "3" ][ "1" ] = 3;
   oj[ "2" ][ "3" ][ "2" ] = 3;

Is this how it should work?
What I expected from ojson is to preserve the order of all elements, but it seem that it does not do it.
Can you please check it and help?
I use jsoncons from git master commit c4a77de

danielaparker added a commit that referenced this issue Jan 21, 2020
@danielaparker
Copy link
Owner

danielaparker commented Jan 21, 2020

Thanks for reporting this. The ojson::operator[], when creating an object automatically, was incorrectly placing the new object at the beginning of its parent. This has been fixed on master.

@jkalmar
Copy link
Author

jkalmar commented Jan 22, 2020

Thanks,
It seems to be working correctly now with my tests. The result is now:

{
    "1": {
        "1": {
            "1": 1, 
            "2": 1
        }, 
        "2": {
            "1": 2, 
            "2": 2
        }, 
        "3": {
            "1": 3, 
            "2": 3
        }
    }, 
    "2": {
        "1": {
            "1": 1, 
            "2": 1
        }, 
        "2": {
            "1": 2, 
            "2": 2
        }, 
        "3": {
            "1": 3, 
            "2": 3
        }
    }
}

Closing this issue

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

No branches or pull requests

2 participants