Win a copy of Event Streams in Action this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

No clue what I'm missing in deserialization of my Json file

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I'm at the end of a long and trying semester in Java!! My professors has had some life altering personal situations arise and isn't super present as of the last few weeks. I have a project to submit but I'm struggling with the I/O of it all.  I've been trying to figure it out on my own with no luck, and I was hoping you guys might be able to help.  The project is one of those where you submit then add another pieces, rinse and repeat.  This was the big thing I couldn't figure out on my last submission and I really want to get it to work before my next one.  Its my first post ever here, so I hope I do this right...

We've been required to build a GUI for a zoo program where a user can add new animals or food.  I've got the current GUI working like a charm when its live.  The problem is that we have to save the user input to a Json file upon program close, and be able to open it back up and read the data back into their respective arraylists.  I've put print statements in to make sure the program is reading and writing to a Json file, it will write a Json file if one doesn't exist, and on physical check of the Json file the data is all there, but when I restart the GUI, and do a simple "search all" (which is just a "print the arraylist" block of code) the data is no where to be found.

I have an IO class for the code below, and the arraylists animal and food are currently being passed from main through button actions.

Current Working Write Code:


and my print statement shows this (for the outgoing and incoming data):

[
 {
   "Type": "Fruit",
   "Food": "Banana",
   "Feed": "Monkey",
   "Units": 10,
   "ItemsPerUnit": 5,
   "TotalItems": 50,
   "searchFood": "",
   "searchFoodDescription": ""
 }
]


Reading the file using this code (mostly provided BY the professor, but not really explained):


I am writing my data to two separate .json files.  I just feel like I'm missing the part where something is done WITH the data. I get that I'm reading the file, and I get that I'm parsing the Json data, but I feel like I'm not setting it to the actual java object using this code...maybe I'm missing a step or don't understand what I think I do (LOL).  And because this is "Homework", I did go to my professor first.  Sadly, she couldn't tell why MY program wasn't reading in correctly and just kept referring back to her own code and saying "It should work, I'm not sure, maybe separate the methods for the two lists"...which I did and it didn't change anything.  Its frustrating because I truly want to understand what I'm missing, not just copy her code!!

Basically I'm looking to allow the user to populate an arraylist with objects, transfer those objects to a Json file, then transfer them from a Json file back into the established arraylist using Gson.  Any suggestions would be immensely appreciated!!!
 
Marshal
Posts: 65050
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Emily Harris wrote:. . . My professors has had some life altering personal situations arise and isn't super present as of the last few weeks. . . .

I presume it is some tragedy or other, so I am sorry for your teacher, but have you requested somebody else stand in for her?

I know precious little about JSON, but you appear to have isolated the problem to somewhere between writing the file and reading it. (By the way: you have put a lot of effort into describing the problem ) When you said this is the file with the bananas in, how did you verify the contents of that file?
I am afraid you're using a very old‑fashioned and not very good way to write to files. But you said you are getting the file written, so it would appear to work. Don't simply use a file writer. I looked up its write() method and found it is overloaded, you are obviously using Writer#write(String). I would use a Formatter or a buffered reader. I presume you are trying to write the whole List of food; you can append a new line to the end of an existing text file by passing a second argument (true) to the FileWriter() constructor.
Don't close your writers and readers. Use try with resources instead.Why are you using a Writer to read and a Scanner to write? There are all sorts of things I would change aboiut your reading code too, but I won't change anything if you are actually reading a Json entry correctly. So the bit about bananas is what you get at line 12, and is it a well‑formed Json?
Are you misleading yourself in line 13? You are printing the size of the List but you haven't actually done anything with the List in that method. Is that your problem?

I can see all sorts of other things I would like to change, but let's sort out one problem at a time.

And (most important), welcome to the Ranch
 
Emily Harris
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I presume it is some tragedy or other, so I am sorry for your teacher, but have you requested somebody else stand in for her?



Yes, we were actually told that was what was going to happen, but it seems she feels that no one else there can do that for her and she needs it to keep her mind off of her loss (spouse passed very suddenly).  I had to try multiple times to get a face to face meeting with her so I could sit down with my laptop and show her my code, and my intention for the code, and get some kind of feedback.  She tends to offer video conference (which I've tried before and hasn't been super helpful) or email conversation, which isn't really time efficient when you have a deadline for a project that isn't flexible, so I've figured it out mostly with out her help.

Campbell Ritchie wrote:
When you said this is the file with the bananas in, how did you verify the contents of that file?



Outside of print statements like the one included above that run on console when the program closes/saves for me to be aware of whats happening, and me looking at the actual .json file the program is creating ....I can't, and that's part of my problem. However, these two things do seem to be formed properly to the extent of my knowledge.

Campbell Ritchie wrote:
I am afraid you're using a very old‑fashioned and not very good way to write to files. But you said you are getting the file written, so it would appear to work.



I have a friend who is a programmer who's given me feedback before and he's mentioned the same thing, or code that has not been done properly.  It definitely makes it harder when I'm trying to figure out on my own whats going wrong.  I've managed to pass all of my classes, but I definitely am earning my degree through SkillShare and YouTube vs. my actual college. LOL

The writer/reader was actually her code. When I took it back to her and said "I can't get it to work and got 10 points off the last assignment for it, how do I fix it?", and she couldn't really tell me, you can believe I was NOT happy.  I think I, HAVE to use Json/Gson but I know I don't HAVE to user her code (I just have to understand the code I am using, ie. not just copying someone else's), and I am absolutely open to learning how to do it properly!!!  Last night I started researching with a "start from scratch" mindset about reading in (ignoring writing for now since its technically working) but a lot of the code and video examples I found utilized defined data.  Being totally honest, this was confusing to me regarding how to translate whats coming back into Java again since I won't know what the values are until the user enters them.  And yes, I'm pretty sure nothing is being done with the data.  I do have standard setters/getters and a constructor that are populating and passing info between classes/stages appropriately in a live run, they just generate nothing on a re-open.  The GUI doesn't do anything super fancy, mostly just click a button to search/display singular objects or the arraylist, so population is the main concern ...so that's why I'm thinking my issue is isolated to that point, but like I said I could be totally wrong.  

I'll give these links and code a read and see if I can get things going.  Thank you so so much for your help, I really appreciate you and your time!!!
 
Campbell Ritchie
Marshal
Posts: 65050
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Emily Harris wrote:. . . her loss (spouse passed very suddenly). . . .

How dreadful for her
I would have thought your college would be under some sort of obligation to provide teaching, but I don't think I ought to get involved in your local management.

. . . Outside of print statements . . . and me looking at the actual .json file the . . . . I can't . . .

If you can verify the contents of the .json file, then it would appear you are successfully writing the text. If you get the text from the.json file printed out when you read it in line 12 (the toString() call is redundant), then you are successfully reading the file. But how do you know that the rest of that method for reading will reliably turn that text into a Food object or add it to the List? What happens in line 8 if anything goes wrong? What do lines 10‑13 do? Where are you adding a Food object to your List (I have already asked this)?

If I used a buffered reader to match the buffered writer, I would use a method of the Files class to get a buffered reader (I should have used the analogous method for a writer earlier) and there are all sorts of ways of testing whether you have a complete record. Let's try counting {}s....or soemthing rather like that.

. . . I definitely am earning my degree through SkillShare and YouTube vs. my actual college. LOL

Careful. There is some good stuff on the Net, and some bad stuff. You probably haven't got enough experience to tell the difference.

. . . I . . . HAVE to use Json/Gson . . .

Check the requirements for your assignment careully. I you want full marks, you will have to fulfil those requirements. And remember it is not possible to change an assignment once it has been published, not even if it is found to contain errors.
 
Saloon Keeper
Posts: 2658
333
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Emily Harris wrote:I just feel like I'm missing the part where something is done WITH the data. I get that I'm reading the file, and I get that I'm parsing the Json data, but I feel like I'm not setting it to the actual java object using this code...maybe I'm missing a step or don't understand what I think I do (LOL).


The reader code does seem a bit funny.  Rather than returning you a List<Food>, it makes you create a ArrayList<Food> in the calling method, and then pass-in a reference to that ArrayList as a parameter for the output to be written to:
I would expect something more like this, were you are returned a List<Food> based on the JSON representation in the file:
 
Campbell Ritchie
Marshal
Posts: 65050
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:. . .

If the gson objectmethod call returns null from an empty file, then your List reference will point to null, and as you know, you want to get rid of nulls as much as possible. So what Ron is doing is replacing the null by an immutable empty List returned by the Collections#emptyList() method. You can use that as an ordinary List to read Food objects from (all none of them ‍), but you cannot add anything to that List.
 
Marshal
Posts: 6970
471
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:The reader code does seem a bit funny.


Indeed.
 
Liutauras Vilda
Marshal
Posts: 6970
471
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:

Ron McLeod wrote:The reader code does seem a bit funny.


Indeed.


Moreover. Such method as it is implemented now can't have an effect on passed argument due to pass-by-value.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!