Serialize the input card to a string

I am trying to write a generic serialization function in clojure. Something like that

(def input-map {:Name "Ashwani" :Title "Dev"}) (defn serialize [input-map delimiter] ...rest of the code ) 

What when calling

 (serialize input-map ",") Produces Ashwani,Dev 

I have something at the moment that needs certain card keys, but does it

 (defn serialize [input-map] (map #(str (% :Name) "," (% :Title) "\n") input-map ) ) 

What I want to avoid is the name and the name of hardcoding. There must be some way to use reflection or do something for this, but unfortunately I don't know enough clojure to do this.

+4
source share
4 answers

Take a picture:

 (require 'clojure.string) (defn serialize [m sep] (str (clojure.string/join sep (map (fn [[_ v]] v) m)) "\n")) (def input-map {:Name "Ashwani" :Title "Dev"}) (serialize input-map ",") 

gives

 "Ashwani,Dev\n" 

Not sure how idiomatic this is, but it should work for you.

Update: Julien's answer is better than mine! vals ... how could i skip this :)

+3
source
 (defn serialize [m sep] (apply str (concat (interpose sep (vals m)) ["\n"]))) 
+5
source

It's simple.

 (str input-map) 
+2
source

The "normal" clojure types can be serialized using pr-str and re-injected using the read string. If you have no reason to format serialized data in a specific way, I would suggest using pr-str instead, just because its output is more readable.

0
source

All Articles