So I made a change which allows passing a map of deserializers instead of having them initialized globally, it's more or less the same as the previous version otherwise, let me know if you feel it makes the reader too complex
http://gist.github.com/565764 here's sample usage with that approach (defn write-date [date #^PrintWriter out] (.print out (str "\"" (.format (new java.text.SimpleDateFormat "MMM dd, yyyy hh:mm:ss a") date) "\""))) (extend Date Write-JSON {:write-json write-date}) (def deserializers {:age #(.parse (new java.text.SimpleDateFormat "MMM dd, yyyy hh:mm:ss a") %)}) (defn read-json-from-str [input] (read-json input true true nil deserializers)) (let [data {:name "John" :age (new java.util.Date) :address [:street "1 Bay" :city "Toronto"]} encoded (json-str data)] (println "encoded" encoded) (println "decoded" (read-json-from-str encoded))) output: encoded {"name":"John","age":"Sep 05, 2010 01:04:02 AM","address": ["street","1 Bay","city","Toronto"]} decoded {:name John, :age #<Date Sun Sep 05 01:04:02 EDT 2010>, :address [street 1 Bay city Toronto]} On Sep 3, 3:17 pm, Stuart Sierra <the.stuart.sie...@gmail.com> wrote: > No. I'm talking about collisions when multiple deserialization > functions are added from different sources. It cannot be a global > setting. > > -S > > On Sep 3, 1:28 pm, Dmitri <dmitri.sotni...@gmail.com> wrote: > > > > > > > > > The problem I was trying to avoid is having to do a second pass over > > the data after it comes out of the parser, it's more expensive and > > it's also ugly for nested data structures. Would using defonce- and > > defmacro- from clojure-contrib address the problem with namespace > > collisions? > > > On Sep 3, 12:01 pm, Stuart Sierra <the.stuart.sie...@gmail.com> wrote: > > > > You can already extend the Write-JSON protocol to any type. But it > > > doesn't work in reverse. JSON has no standardized way to express types > > > beyond Object/Array/String/Number, so any deserialization will always > > > be application-specific. > > > > -S > > > > On Sep 3, 8:58 am, Baishampayan Ghose <b.gh...@gmail.com> wrote: > > > > > > Sorry, I can't accept any patch that modifies behavior globally. What > > > > > happens when two different libraries try to parse JSON with different > > > > > deserializers? > > > > > > The only thing I would consider is a function that is passed into > > > > > read- > > > > > json and invoked in read-json-object. But even that seems like adding > > > > > unnecessary complication to the library. > > > > > Just curious, but does using protocols in clojure.contrib.json help? > > > > May be people can extend the protocol to their types and make them > > > > serializable to JSON and vice versa? > > > > > Regards, > > > > BG > > > > > -- > > > > Baishampayan Ghose > > > > b.ghose at gmail.com -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en