How to make SequenceWriter append rootValueSeparator also at the end

128 views
Skip to first unread message

Jakub Liska

unread,
Jul 6, 2016, 12:36:32 PM7/6/16
to jackson-dev
Hey,

I'm writing json objects to file as lines this way : 

       
ObjMapper
         
.writer()
         
.withRootValueSeparator("\n")
         
.writeValues(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true), StandardCharsets.UTF_8), 524288))
         
.writeAll(recs)


Which would be OK if I didn't write multiple times to the same file because the separator is not used at the end, if I write to a file 3 lines this way multiple times I get : 

{}
{}
{}{}
{}
{}{}
{}
{}{}
{}
{}{}
 
Is there a setting for this? Or I have to do it manually by writing "\n" to the outputstream?

Tatu Saloranta

unread,
Jul 6, 2016, 1:10:08 PM7/6/16
to jacks...@googlegroups.com
As the name indicates, separator is only used between values, and there is no option to force it in the end. I don't know how easy it'd be to add an option to also add separator before `close()`; may be worth filing an issue.

However, you should be able to write this into output stream through Jackson API. Methods `writeRaw()` and `writeRawValue()` might both work for this particular purpose: if using `writeRaw()`, you would write separator String itself; if using `writeRawValue()`, I think you would try to write empty String as it would add separator before (since call implies that you are writing a pre-encoded JSON value and thereby a separator is needed).

-+ Tatu +-


--
You received this message because you are subscribed to the Google Groups "jackson-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jackson-dev...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages