dakrone / cheshire

Clojure JSON and JSON SMILE (binary json format) encoding/decoding
https://github.com/dakrone/cheshire
MIT License
1.49k stars 151 forks source link

Deterministic parse and generate #206

Open AndreaCrotti opened 4 months ago

AndreaCrotti commented 4 months ago

Is there any possible way to make something like that work? I have massive JSON files (on single lines so no spacing issues) and for annoying reasons, I have to regenerate exactly the same thing after I parse it. It works more or less randomly, but I sometimes get the keys in the maps in a different order, is there any way to make that deterministic?

(def obj (cheshire.core/parse-string (slurp "myfile.json")))
(def full-string (slurp "myfile.json"))
(= full-string (cheshire.core/generate-string obj)) ;; false

It's probably just because parsing will generate non sorted maps, is there any way to make Cheshire use sorted maps?

AndreaCrotti commented 4 months ago

I just tried one little change https://github.com/dakrone/cheshire/pull/207 but unfortunately that's not enough, it still does not work

rap1ds commented 3 weeks ago

I have a bit similar use-case. I'm using cheshire for pretty-printing JSON.

So I'm reading the JSON from file and then using parse-stream and generate-string with pretty printing options, and the end result is a pretty printed JSON file but the order of the keys is changed. Would be nice if there was an option in Cheshire to keep the order of the keys.

rap1ds commented 3 weeks ago

Related: https://github.com/dakrone/cheshire/issues/73