edn/read does not maintain the order of the sequence

240 Views Asked by At

I am using clojure library to read the edn file Now my data.edn file looks like this.

{:mysites {:locations ["priorityLoc1" "priorityLoc2" "priorityLoc3"]
           :back-up ["backup1uri" "backup2uri"]}
}

Now when i use edn/read the map that get returns looks like

{:mysites {:locations ["priorityLoc2" "priorityLoc1" "priorityLoc3"]
           :back-up ["backup1uri" "backup2uri"]}
}

As you see above in the sequence locations, the positions of the values ie., priorityLoc2 and priorityLoc1 changes. This causes problem with the applications which looks for site locations in order of the sequence as priority. I am not sure why the order gets changed during edn/read , Is there a way I can make sure the order of the sequence is not changed ??

I tried the edn/read as is shown in the documentation page for edn/read https://clojuredocs.org/clojure.edn/read

(defn load-edn
  "Load edn from an io/reader source (filename or io/resource)."
  [source]
  (try
    (with-open [r (io/reader source)]
      (edn/read (java.io.PushbackReader. r)))

    (catch java.io.IOException e
      (printf "Couldn't open '%s': %s\n" source (.getMessage e)))))
    (catch RuntimeException e
      (printf "Error parsing edn file '%s': %s\n" source (.getMessage e)))))))

UPDATE:

I am able to see that edn/read is changing the order when i pass my data edn file , I am trying to replicate the issue with a smaller data file as I cant share my data.edn file here. With my smaller data file the issue is not reproducible.

So, I am not really sure to assume edn/read IS NOT changing the order , is there any chance that edn tries to attempt a sort (like sort by names ?) which causes order to change. Is there a way to make sure order does not change. Below is the code for the edn reader.

(require '[clojure.java.io :as io])
(require '[clojure.tools.reader.edn :as edn])
(require '[clojure.tools.reader.reader-types :as readers])

(defn meine-edn-reader
  "Load edn from an io/reader source (filename or io/resource)."
  [source]
  (try
    (with-open [reader (io/reader source)]
      (-> reader
        readers/push-back-reader
        readers/indexing-push-back-reader
        edn/read))

    (catch java.io.IOException e
      (printf "Couldn't open '%s': %s\n" source (.getMessage e)))))
0

There are 0 best solutions below