vibe-d / vibe.d

Official vibe.d development
MIT License
1.15k stars 284 forks source link

Serialization of custom made JSON object doesn't preserve order #1997

Open nazriel opened 6 years ago

nazriel commented 6 years ago

I am currently working with project that has .json files under version control. The previous version of application written in NodeJS was using stringify to make sure that order of keys in json objects was sorted in a given way.

I am able to reproduce this behaviour when using serialization on data structures. OK Example:

    struct Foo
    {
        bool c;
        int x;
        double a;
    }
    Foo f;
    f.x = 1; f.a = 2.0, f.c = true;
    immutable output = serializeToJsonString(f);
    assert(output == `{"c":true,"x":1,"a":2}`, output);

NOT OK example:

    Json bar = Json.emptyObject();
    bar["c"] = true;
    bar["x"] = 1;
    bar["a"] = 2.0;

    assert(bar.toString() == `{"c":true,"x":1,"a":2}`, bar.toString());

Is there any way to make NOT OK example, the OK one? ;)

bausshf commented 6 years ago

Tbh. You should never rely on the order of entries in json; that's bad practice and will only lead to problems.

nazriel commented 6 years ago

Maybe you are right. Maybe you are not.

The fact is: 1) I have such need despite whenever it is good practice or not. JavaScript's stringify preserves order for some reason. And that reason was used to "normalize" behaviour in the application I am working with in order to make this data output independent of the language that is used. 2) vibe.d currently is inconsistent. One way preserves the order. Another doesn't. So either inconsistency should be removed or optional parametrization should be available.

IMHO current "random" behaviour is only due to fact that D's AA are used as "output type" for JSON objects and this doesn't preserve the "insertion" order.

        m_object[key].m_type = Type.undefined; // DMDBUG: AAs are teh $H1T!!!11

;)

I am not sure which way is the way to go - but I would like to know the opinion of original authors like @s-ludwig .

bausshf commented 6 years ago

I can only imagine it will drag performance down using a sorted collection, instead of AA's.

An alternative implementation would probably be better than modifying the existing Json implementation.