lynaghk / json-tagged-literals

More palatable JSON serialization
Other
40 stars 0 forks source link

CLJS serialization #3

Open si14 opened 10 years ago

si14 commented 10 years ago

I can be missing something, but I always get RangeError: Maximum call stack size exceeded when I run test2 from the following code (test1 works fine):

(defn ^:export test []
  (->> test-json
      (.parse js/JSON)
      (jtl-deserialize)))

(defn ^:export test2 []
  (let [obj (test)]
    (jtl/serialize obj {"constructor_table"
                        {codenotes.types.Repository
                         (fn [x] ["repository" (clj->js x)])}})))

Is it OK? When I add debug output to the function inside constructor table I don't get an output from it. What exactly should be provided as a key in that map?

lynaghk commented 10 years ago

From the README: "Note that this is a plain JavaScript library, so if you want to pass options make sure you first coerce them to a JavaScript object".

I think the issue is that you are passing the jtl/serialize function a ClojureScript map when it is expecting a JavaScript object.

lynaghk commented 10 years ago

See the last paragraph of the "Usage" section of the README---the serialize fn actually accepts a goog Closure map instance.

si14 commented 10 years ago

Here is a minimal example with goog.structs/Map:

(defrecord Testrec [foo])

(defn ^:export test3 []
  (let [cons-table {Testrec (fn [x]
                               (.log js/console "serialization reached")
                               ["repository" (clj->js x)])}
        cons-table (goog.structs/Map. (clj->js cons-table))
        arg (clj->js {"constructor_table", cons-table})]
    (jtl/serialize (Testrec. "bar") arg)))

It fails with RangeError: Maximum call stack size exceeded without ever calling serialization function. Does it really an intended behavior even if something is wrong with constructor_table? Can you please provide an example of working serialization with record?