how not to append "\n" when calling csv ObjectWriter.writeValueAsString?

1,033 views
Skip to first unread message

fc2...@gmail.com

unread,
Jun 3, 2015, 1:17:15 AM6/3/15
to jackso...@googlegroups.com
new to jackson lib and when I tried to use the csv writer like the following:
        CsvMapper mapper = new CsvMapper()
        CsvSchema wschema = mapper.schemaFor(String.class);
        System.out.printf("jackson: %s", mapper.writer(schema).writeValueAsString(splits));

I found the output has "\n" appended.  Is there any easy way to get rid of the return char? 

Thanks,
Fang

Nate Bauernfeind

unread,
Jun 3, 2015, 10:36:33 AM6/3/15
to jackso...@googlegroups.com
This must be because of the CsvMapper. I know the ObjectMapper does not append a '\n' automatically.

Would you consider using String.trim()?

i.e. System.out.printf("jackson: %s", mapper.writer(schema).writeValueAsString(splits).trim());

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

fc2...@gmail.com

unread,
Jun 3, 2015, 1:59:56 PM6/3/15
to jackso...@googlegroups.com
Thanks for the suggestion. I wanted to eliminate unnecessary operations since it's in a high-throughput env, but I will try and see how much the perf. is affected.

Fang

Tatu Saloranta

unread,
Jun 3, 2015, 3:03:01 PM6/3/15
to jackso...@googlegroups.com
CSV is assumed to be line-oriented output, so each line is followed by a linefeed.
But I am bit surprised that `mapper.schemaFor(String.class)` works -- I am not quite sure what the intent there is. Schema should describe mapping from logical property names to column positions.

-+ Tatu +-


--

fc2...@gmail.com

unread,
Jun 3, 2015, 8:38:10 PM6/3/15
to jackso...@googlegroups.com
a little background so you can see if did something wrong:

I need to concatenate/encode tens of attributes (possibly with commas each) and save to Cassandra tables, and later on split/decode by callers. I need the most succinct way to encode to save io latency and improve e2e throughtput (cassandra have compressions so I don't need it in my space). Csv seems like the right option for this purpose.  Actually the linefeed is ok in general but just seems a bit odd as the attributes themselves don't have it

Tatu Saloranta

unread,
Jun 3, 2015, 9:15:39 PM6/3/15
to jackso...@googlegroups.com
Use case sounds appropriate, I am just saying that `schemaFor(String.class)` is probably not what you want.

-+ Tatu +-
Reply all
Reply to author
Forward
0 new messages