Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

write-value closes the object #61

Open
helins opened this issue Mar 5, 2022 · 2 comments
Open

write-value closes the object #61

helins opened this issue Mar 5, 2022 · 2 comments

Comments

@helins
Copy link

helins commented Mar 5, 2022

Writing to an object in a streaming fashion is a typical use case and since Jsonista aims for performance, supporting that is rather expected. As opposed to using write-value-as-string or write-value-as-bytes and then copying the result to an object like a writer.

Currently, this is not possible since write-value closes the given object, hence an exception is thrown. Or maybe is it Jackson itself?

I realized this while trying to write a line-delimited JSON file.

(with-open [out (clojure.java.io/writer "/tmp/foo.json")]
    (json/write-value out [:a])
    (.write out (int \newline))
    (json/write-value out [:b]))
@FiV0
Copy link

FiV0 commented Nov 30, 2023

Workaround for now (credit @jarohen)

(do
  (let [obj-mapper (doto (jsonista.core/object-mapper)
                     (.disable (into-array com.fasterxml.jackson.core.JsonGenerator$Feature [com.fasterxml.jackson.core.JsonGenerator$Feature/AUTO_CLOSE_TARGET])))]
    (with-open [out (io/output-stream "/tmp/foo.json")
                wrt (io/writer out)]
      (jsonista.core/write-value wrt {"foo" 1} obj-mapper)
      (.write wrt "\n")
      (jsonista.core/write-value wrt {"bar" 1} obj-mapper)))
  (slurp "/tmp/foo.json"))

@jarohen
Copy link

jarohen commented Nov 30, 2023

Could add it into the cond-> when you build up the mapper in j/object-mapper?

(:close-targets? options) (doto (-> .getFactory (.disable JsonGenerator$Feature/AUTO_CLOSE_TARGET)))

(Happy to raise a PR or have you guys add it in, whatever's easier for you 🙂)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants