<< Back to previous view

[DJSON-12] *value-fn* is not applied to contents of a seq Created: 11/Jul/13  Updated: 10/Jan/14  Resolved: 30/Aug/13

Status: Closed
Project: data.json
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Defect Priority: Major
Reporter: Michael Nygard Assignee: Stuart Sierra
Resolution: Declined Votes: 0
Labels: None


 Description   

When the value of a key is any sequential type, value-fn is not being used to translate the component values in the sequence.



 Comments   
Comment by Stuart Sierra [ 02/Aug/13 3:35 PM ]

I cannot reproduce this issue. Do you have an example or test case?

Comment by Richard Hull [ 05/Aug/13 5:13 AM ]

I just came across the same/similar issue. For example,

(json/write-str [ (java.util.Date.) ] :value-fn (fn [k v] (str v)))

The write-array function does not make use of the value-fn at all, so it defaults to use write-generic instead, which falls through to throw the exception.

Re-using value-fn seems wrong somehow - because it is expecting a key and value perhaps, and in this case there is only ever a value (and there is no concept of a key for sequences).

Comment by Stuart Sierra [ 07/Aug/13 1:39 PM ]

The value-fn was not intended to be a general-purpose transformation function, as in Richard Hull's example. It is only for the limited case of transforming selected values with known keys in structured maps, such as:

(json/write-str [{:date (java.util.Date.)}] :value-fn (fn [k v] (if (= k :date) (str v) v)))
;;=> "[{\"date\":\"Wed Aug 07 14:13:00 EDT 2013\"}]"

If you want to extend the write functions to new types, you can extend the JSONWriter protocol.

If you need a general-purpose transformation that walks the entire data structure, you can use clojure.walk.

Comment by Stuart Sierra [ 07/Aug/13 1:42 PM ]

I have attempted to clarify the docstrings around :value-fn in commit 9fac8f19.

Comment by Stuart Sierra [ 30/Aug/13 8:47 AM ]

Closing this issue for lack of a reproducible test case.

Comment by Stuart Sierra [ 10/Jan/14 11:10 AM ]

Marking old issues as 'closed'

Generated at Sat Oct 25 14:57:45 CDT 2014 using JIRA 4.4#649-r158309.