Skip to content

Output of Json.to_str_try(some_dict) should not depend on order of dict keys #10042

Description

@ageron

It's currently possible for two dicts to be equal yet have a different JSON encoding, depending on the order of the entries:

» Dict.from_list([("a", "b"), ("c", "d")]) == Dict.from_list([("c", "d"), ("a", "b")])
True
» Json.to_str_try(Dict.from_list([("c", "d"), ("a", "b")]))
Ok("{\"c\":\"d\",\"a\":\"b\"}")
» Json.to_str_try(Dict.from_list([("a", "b"), ("c", "d")]))
Ok("{\"a\":\"b\",\"c\":\"d\"}")

IMHO, Dict JSON should have sorted entries, just like records:

» {a: "one", b: "two"}->Json.to_str_try()
Ok("{\"a\":\"one\",\"b\":\"two\"}")
» {b: "two", a: "one"}->Json.to_str_try()
Ok("{\"a\":\"one\",\"b\":\"two\"}")

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Fields

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions