I think at this point I would need actual unit test. Reading code seemed ok, but without actual exception message it's hard to know what might be wrong.
-+ Tatu +- On Fri, Jan 4, 2019 at 4:01 AM MV <[email protected]> wrote: > Hi Tatu > > I made a few changes to my POJO to avoid the circular reference issue. Now > I can write the objects to file and the output in the file is in the > format: > { pojo } > { pojo } > > I am unable to deserialize using your technique and from the error > message, I think it is because the individual fields inside the POJO are > lists (ArrayList) and some items in the ArrayList also contain a Map (e.g. > travelDistanceMap item is a LinkedHashMap in the file attached). > > The error I am seeing right now, is that when deserializing "homeList", it > says it cannot find the property "homeId" when it is clearly found in the > output and defined in the POJO. I am guessing it is because it is a list > and that is not how I am reading it back. > > By the way, I am using your suggested technique of MappinIterator and > objectMapper.readValues() to read these. I am attaching 2 screenshots: > (1) IS the JSON output > (2) My deserializer code > > After you take a peek at it, here are the questions: > (1) When I get the next token, should I actually check if the token == > "homeList" and then call objectMapper.readValue(token, TypeReference)? > (2) Should I be using annotations in the class on the fields that are a > list? What about the case of travelDistanceMap where it is a Map field > inside a token? > (3) Writing this to a file with *.json and opening in an editor complains > about malformed JSON. I understand this is because I am writing out > individual JSON objects instead of a proper JSON list. Should I just rename > the file to *.txt? > > Thanks in advance for your time, > MV > > On Monday, December 31, 2018 at 4:22:13 PM UTC-5, MV wrote: >> >> 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. > -- 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.
