proycon/codemetapy

Possible bug: Serialization to JSON is not deterministic

apirogov opened this issue · 3 comments

I'm using codemetapy as a linter with pre-commit and noticed that for some reason it fails sometimes because the codemeta.json file changed.

In my case, this is caused by my classifiers:

classifiers = [
    "Operating System :: POSIX :: Linux",
    "Development Status :: 3 - Alpha",
    "Intended Audience :: Science/Research",
    "Intended Audience :: Developers",
]

Then codemetapy will randomly orders them, sometimes resulting in:

    "audience": [
        {
            "@type": "Audience",
            "audienceType": "Science/Research"
        },
        {
            "@type": "Audience",
            "audienceType": "Developers"
        }
    ],

and sometimes in:

    "audience": [
        {
            "@type": "Audience",
            "audienceType": "Developers"
        },
        {
            "@type": "Audience",
            "audienceType": "Science/Research"
        }
    ],

Would it be possible to make sure that a consistent order is ensured?

As a workaround I currently add a global exclude: '^codemeta.json$' to my hook, which is not nice because it prevents my JSON linter to format the JSON.

Expected behavior

On the same input files exactly the same output file should be generated.

Thanks for the report, I agree that the output should be deterministic. It already is in the ordering of fields in a map but not yet in the ordering of objects in a sequence, I'll implement a solution for it.