clojure - get agent content and convert it to JSON -
i'm still newbie in clojure , i'm trying build application read 2 files , write diffrence on json file
(defn read-csv "reads data." [] (with-open [rdr ( io/reader "resources/staples_data.csv")] (doseq [line (rest(line-seq rdr))] (println(vec(re-seq #"[^,]+" line)))))) (defn read-psv "reads data." [] (with-open [rdr ( io/reader "resources/external_data.psv")] (doseq [line (rest(line-seq rdr))] ; (print(vec(re-seq #"[^|]+" line)))))) (doall(vec(re-seq #"[^|]+" line)))))) (defn process-content [] (let [csv-records (agent read-csv) psv-records (agent read-psv)] (json/write-str {"my-data" @csv-records "other-data" @psv-records})) )
im getting exception: exception don't know how write json of class $read_csv clojure.data.json/write-generic (json.clj:385)
please some explanation, in advance!
you giving agent function initial value. perhaps meant asynchronous call function instead? in case, future
better match scenario shown. agent
synchronous, it's send
, send-off
async, , assume propagating state across calls doesn't match usage here.
(defn process-content [] (let [csv-records (future-call read-csv) psv-records (future-call read-psv)] (json/write-str {"my-data" @csv-records "other-data" @psv-records})))
the problem after doseq
side effects, , returns nil. if want results read csv files (evaluating eagerly still in scope of with-open
call), use (doall (for ...))
replacement (doseq ...)
. also, println
in read-csv
need removed, or replaced (doto (vec (re-seq #"[^,]+" line)) println)
because println returns nil, , assume want actual data file, not list of nils.
Comments
Post a Comment