data.json

*value-fn* is not applied to contents of a seq

Details

  • Type: Defect Defect
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Declined
  • Affects Version/s: None
  • Fix Version/s: None
  • Component/s: None
  • 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.

Activity

Hide
Stuart Sierra added a comment -

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

Show
Stuart Sierra added a comment - I cannot reproduce this issue. Do you have an example or test case?
Hide
Richard Hull added a comment - - edited

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).

Show
Richard Hull added a comment - - edited 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).
Hide
Stuart Sierra added a comment -

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.

Show
Stuart Sierra added a comment - 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.
Hide
Stuart Sierra added a comment -

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

Show
Stuart Sierra added a comment - I have attempted to clarify the docstrings around :value-fn in commit 9fac8f19.
Stuart Sierra made changes -
Field Original Value New Value
Assignee Stuart Sierra [ stuart.sierra ]
Hide
Stuart Sierra added a comment -

Closing this issue for lack of a reproducible test case.

Show
Stuart Sierra added a comment - Closing this issue for lack of a reproducible test case.
Stuart Sierra made changes -
Resolution Declined [ 2 ]
Status Open [ 1 ] Resolved [ 5 ]
Hide
Stuart Sierra added a comment -

Marking old issues as 'closed'

Show
Stuart Sierra added a comment - Marking old issues as 'closed'
Stuart Sierra made changes -
Status Resolved [ 5 ] Closed [ 6 ]

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: