proycon / codemetapy

A Python package for generating and working with codemeta
https://codemeta.github.io/
GNU General Public License v3.0
24 stars 5 forks source link

Possible bug: Serialization to JSON is not deterministic #39

Closed apirogov closed 11 months ago

apirogov commented 1 year ago

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.

proycon commented 1 year ago

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.

proycon commented 11 months ago

Should be fixed and released now

apirogov commented 11 months ago

Great! Thanks for fixing this and all the others issues! I will test the new version once I have a bit of time in the coming days/weeks :)