dropbox / json11

A tiny JSON library for C++11.
MIT License
2.54k stars 613 forks source link

Order serealization Json::object #136

Closed vincenzopalazzo closed 5 years ago

vincenzopalazzo commented 5 years ago

Hello guys,

I have a problem with serialization json into a test

I had to keep an isConverted parameter because I have an object of the same type inside the object to be serialized. Leaving aside this I have performed all the correct steps but why do I get a wrong order?

I have this code `Json Person::toJSon() {

if(isConverted || boyOrGirlFrined == nullptr){
    return Json::object{
            {"name", name},
            {"surname", surname},
            {"sex", sex},
    };
}
boyOrGirlFrined->isConverted = true;
return Json::object{
        {"name", name},
        {"surname", surname},
        {"sex", sex},
        {"AnimaGemella", boyOrGirlFriend->toJSon()},
};

}`

with i get this result {"AnimaGemella": {"name": "Sara", "sex": "f", "surname": "Durante"}, "name": "Vincenzo", "sex": "m", "surname": "Palazzo"}

and not it? {"name": "Vincenzo", "sex": "m", "surname": "Palazzo", "AnimaGemella": {"name": "Sara", "sex": "f", "surname": "Durante"}}

Sorry for my English but I'm learning

j4cbo commented 5 years ago

json11 maps the json object type to std::map, which doesn't preserve the original order. Most other languages' implementations behave the same way.

vincenzopalazzo commented 5 years ago

@j4cbo Thanks for your answer