Uploaded image for project: 'data.json'
  1. DJSON-7

Write-str outputs invalid JSON when key/value pairs are removed

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Completed
    • Labels:
      None
    • Environment:

      clojure.data.json 0.2.0

    • Patch:
      Code and Test

      Description

      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)
      ;=>"

      {,\"c\":1,,,\"e\":2,}

      "

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

      {\"c\":1,\"e\":2}

      "

        Attachments

          Activity

            People

            • Assignee:
              stuart.sierra Stuart Sierra
              Reporter:
              alex+import import
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: