Hi Tatu
Thanks for your quick response. So I decided to try option #1 as you
suggested i.e. continue writing single messages to a file in "append" mode
(without using JsonGenerator) for now.
I am still having trouble because it seems like my stream closes with some
sort of NPE and circular reference. This only seems to occur when I use:
objectMapper.writeValue(BufferedWriter (FileWriter), pojo)
It is the same error when I use writeValueAsString().
Instead, If I used these sequence of steps, it works.
FileWrite fw = new FileWriter("myMessages.json", true);
BufferedWriter bw = new BufferedWriter(fw);
String jsonStr = objectMapper.writeValueAsString(pojo)
bw.write(jsonStr).
Of course opening "myMessages.json" file has a lot of red squiggles because
the format is incorrect. It is just a list of messages.
I do have these settings for ObjectMapper:
*objectMapper.setSerializationInclusion(JsonInclude.Include.Non_Null)*
*objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm a z"));*
It does not look like these settings are taking effect.
I am not able to find the NPE because when I look in the debugger through
IDE (IntelliJ), nothing is null.
The error is along the lines of NPE from circular reference. But the rest
of the code uses the same class for future calculations and there is no NPE
anywhere in the code.
I would like to use objectMapper.writeValue(bufferedWriter, pojo) if
possible but don't know how to resolve this circular NPE.
I am going to try loading the values as you mentioned with "readValues()"
and will keep you posted here.
Thanks a lot for your timely responses. I really appreciate it.
~MV
On Saturday, December 29, 2018 at 6:01:03 PM UTC-5, MV wrote:
>
> Hi there,
>
> I am using Akka Actor framework to receive messages as Java objects. When
> I receive the message, I do the following:
> (1) Use ObjectMapper.withPrettyWriter().writeValueAsString(<java pojo>)
> (2) Open a file and use java.nio.Files API to write to the file. When I
> write to the file, I use Arrays.asList(json_string_from step_1) with file
> in *append* mode.
>
> I am having a few issues:
> (1) I am unable to write the first json string as an array. When I write
> it the first time, I *would like to see*:
> [
> {
> "name" : "test"
> "age" : 29
> }, --> I don't get this "comma" with closing "]"
> ] ---> don't get this.
>
> All I get is :
> {
> "name" : "test"
> "age" : 29
> }
>
> (2) While calling successive Files.write (...), the output in the file is
> {
> "name" : "test"
> "age" : 29
> }
> {
> "name" : "test2"
> "age" : 30
> }
> This results in malformed JSON file and I cannot use
> objectMapper.readValue as a list. Looks like I have to use Files API to
> load the data.
>
> Qn) How do I write a single json as an array with ", ]" and then append to
> this list as data starts flowing in through the messaging architecture?
> I did see some solution on reading the file first, add new data to the
> array and then write the whole list again to the file each time. I think
> with messaging architecture this is not very efficient as we will see a lot
> of messages.
> Is there someway to open the file and write the first data and then keep
> appending to the file?
>
> Thanks
> MV
>
>
>
--
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 [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/d/optout.