<< Back to previous view

[DJSON-7] Write-str outputs invalid JSON when key/value pairs are removed Created: 08/Jan/13  Updated: 10/Jan/14  Resolved: 02/Apr/13

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

Type: Defect Priority: Minor
Reporter: Fred Caton Assignee: Stuart Sierra
Resolution: Completed Votes: 1
Labels: None

clojure.data.json 0.2.0

Attachments: Text File djson-7-dont-write-unnecessary-commas-v1.txt    
Patch: Code and Test


When key/value pairs are removed by the function defined for :value-fn, commas are still output and this results in invalid JSON. To remove the errant commas, I've had to wrap write-str in (write-str (read-str ())).

Here is a simple example from the REPL:

main=> (defn remove-nils [k v]
#_=> (if (nil? v)
#_=> remove-nils
#_=> v))

main=> (def test
#_=> {:a nil,
#_=> :b nil,
#_=> :c 1,
#_=> :d nil,
#_=> :e 2
#_=> :f nil})

main=> (json/write-str test :value-fn remove-nils)

main=> (json/write-str (json/read-str (json/write-str test :value-fn remove-nils)))

Comment by Andy Fingerhut [ 22/Jan/13 12:29 AM ]

djson-7-dont-write-unnecessary-commas-v1.txt dated Jan 21 2013 modifies write-object so that it only writes out a comma if :value-fn does not cause the key/value pair to be omitted.

Comment by Stuart Sierra [ 02/Apr/13 7:05 PM ]

Patch applied.

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

Marking old issues as 'closed'

Generated at Sun May 19 07:30:02 CDT 2019 using JIRA 4.4#649-r158309.