Fork me on GitHub
#transit
<
2024-02-25
>
simonacca21:02:51

Hi all, I've noticed that a sorted-map going for a round trip trough transit will become a regular map (not sorted? anymore). This is the workaround that I have at the moment to keep the ordering property:

(-> (let [out (ByteArrayOutputStream. 4096)
          writer (transit/writer out :json
                                 {:handlers {PersistentTreeMap (transit/write-handler "sorted-map" seq)}})

          _ (transit/write writer (sorted-map 3 4 1 2))

          in (ByteArrayInputStream. (.toByteArray out))
          reader (transit/reader in  ENCODING {:handlers {"sorted-map" (transit/read-handler (partial apply sorted-map))}})]

      (-> reader transit/read sorted?)))
Is there a way to achieve the same result while avoiding to rely on the concretion that is PersistentTreeMap ? Ideally this would entail picking a specific handler for an object that is an instance of clojure.lang.IPersistentMap and clojure.lang.Sorted