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

Add a mechanism for writing sequences of values via ObjectWriter.writeValues() #655

Closed
cowtowncoder opened this issue Dec 17, 2014 · 3 comments
Milestone

Comments

@cowtowncoder
Copy link
Member

It has been possible to read value sequences with ObjectReader.readValues() for a while; this is especially useful for formats like CSV that are basically sequences of more or less uniform values.
But there is no similar mechanism for writing, so developers have to construct JsonGenerator directly first, then use ObjectMapper (or ObjectWriter) on that. This is cumbersome.

It would make sense to add a new sequence writer abstraction, accessible via ObjectWriter; method could be named ObjectWriter.writeValues().

@mjball
Copy link

mjball commented Dec 17, 2014

Thanks for adding this – I'll be using it as soon as it's released :)

@l15k4
Copy link

l15k4 commented Feb 26, 2016

Hey, is it possible to choose separator? It uses space separator by default, I would need EOL separator.

I tried writer.withRootValueSeparator("\n") but that's not it. It seems to be separating the Collection by \n but the individual elements of the Collection are space separated, so that if you write multiple times to a single file it leads to weird combined delimiting by \n and space

@cowtowncoder It is weird though because based on the documentation :

Caller may want to override so-called "root value separator",
String added (verbatim, with no quoting or escaping) between
values in root context. Default value is a single space character,
but this is often changed to linefeed.

It should be it, right? I'm using :

"com.fasterxml.jackson.module" %% "jackson-module-scala" % "2.6.3"

Code :

object ObjMapper extends ObjectMapper with ScalaObjectMapper {
  setSerializationInclusion(JsonInclude.Include.NON_NULL)
  registerModule(DefaultScalaModule)
  val prettyWriter = writer(new DefaultPrettyPrinter)
  val miniWriter = writer(new MinimalPrettyPrinter)
}
def writeJson(file: File, recs: Seq[Rec]) = {
    import scala.collection.JavaConverters._
    var writer: SequenceWriter = null
    try {
      writer =
        ObjMapper
          .miniWriter
          .withRootValueSeparator("\n")
          .writeValues(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true), StandardCharsets.UTF_8), 32768))
          .writeAll(recs.asJava)
    } finally Option(writer).foreach(_.close())
}

And this is the result if you write sequence twice into a file:

{"1":1} {"2":2} {"3":3}{"1":1} {"2":2} {"3":3}

@l15k4
Copy link

l15k4 commented Feb 26, 2016

Got it, it was the MinimalPrettyPrinter. It somehow doesn't allow for the withRootValueSeparator("\n") to take effect...

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