I'm saving a nested map of data to disk via spit
. I want some of the maps inside my map to be sorted, and to stay sorted when I slurp
the map back into my program. Sorted maps don't have a unique literal representation, so when I spit
the map-of-maps onto disk, the sorted maps and the unsorted maps are represented the same, and #(read-string (slurp %))
ing the data makes every map the usual unsorted type. Here's a toy example illustrating the problem:
(def sorted-thing (sorted-map :c 3 :e 5 :a 1))
;= #'user/sorted-thing
(spit "disk" sorted-thing)
;= nil
(def read-thing (read-string (slurp "disk")))
;= #'user/read-thing
(assoc sorted-thing :b 2)
;= {:a 1, :b 2, :c 3, :e 5}
(assoc read-thing :b 2)
;= {:b 2, :a 1, :c 3, :e 5}
Is there some way to read the maps in as sorted in the first place, rather than converting them to sorted maps after reading? Or is this a sign that I should be using some kind of real database?
sorted-map
, does that satisfy your requirements?clojure.data.json
what to read an object in as? Or did you mean a different library?