In my project, I have a class called Closet which holds a list of clothing called clothes. I have implemented the code to saved the information of Closet objects into a .json file which turns out like this:
{
"clothes" : [ {
"name" : "mypants",
"type" : "pants",
"color" : "red",
"size" : 32.0,
"needsWashing" : false
}, {
"name" : "myshirt",
"type" : "shirt",
"color" : "blue",
"size" : 2.0,
"needsWashing" : false
} ],
"numberOfClothing" : 2
}
However, a JsonProcessingException is thrown when I try to retrieve the Json file and convert it back into a closet.
Closet closet = getDefaultObjectMapper().readValue(Paths.get("./data/Closet.json")
.toFile(), Closet.class);
I am a beginner java programmer and am unsure of how I can approach this issue. I have done some searching with creating custom deserializers, however, I'm unsure of how to implement the deserializer with my nested objects (Clothing nested in Closet) and the fact that there could be an arbitrary number of Clothing in the Closet object.
I have the same problem with another class called StyleBoard which is a list of Outfit (another class), and an Outfit, is a list of Clothing. An example of the jsonfile for StyleBoard written to .json file is
contains 2 outfits "shirt and pants" and "pants and socks" each with 2 Clothing objects
{
"styleBoard" : [ {
"clothes" : [ {
"name" : "mypants",
"type" : "pants",
"color" : "blue",
"size" : 32.0,
"needsWashing" : false
}, {
"name" : "myshirt",
"type" : "shirt",
"color" : "blue",
"size" : 0.0,
"needsWashing" : false
} ],
"favorite" : false,
"name" : "shirt and pants",
"numberOfClothing" : 2
}, {
"clothes" : [ {
"name" : "mypants",
"type" : "pants",
"color" : "blue",
"size" : 32.0,
"needsWashing" : false
}, {
"name" : "mysocks",
"type" : "socks",
"color" : "white",
"size" : 3.0,
"needsWashing" : false
} ],
"favorite" : false,
"name" : "pants and socks",
"numberOfClothing" : 2
} ],
"numberOfOutfits" : 2
}
This is the Error Message I am getting:
Exception in thread "main" java.lang.RuntimeException: Cannot construct instance of `model.Clothing` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
at [Source: (String)"{
"clothes" : [ {
"name" : "myshirt",
"type" : "shirt",
"color" : "black",
"size" : 2.09,
"needsWashing" : false
}, {
"name" : "mypants",
"type" : "pants",
"color" : "red",
"size" : 32.0,
"needsWashing" : false
} ],
"collectionSize" : 2
}"; line: 3, column: 5] (through reference chain: model.Closet["clothes"]->java.util.ArrayList[0])
at persistence.Json.fromJson(Json.java:43)
at persistence.Json.parseUserCloset(Json.java:104)
at ui.ClosetApp.loadUser(ClosetApp.java:195)
at ui.ClosetApp.runClosetApp(ClosetApp.java:173)
at ui.ClosetApp.doLogin(ClosetApp.java:103)
at ui.ClosetApp.processLoginCommand(ClosetApp.java:68)
at ui.ClosetApp.runLogin(ClosetApp.java:49)
at ui.ClosetApp.<init>(ClosetApp.java:26)
at ui.Main.main(Main.java:5)
A problem that I noticed that might be causing this, is that when I write my Closet Object to a .json File, it adds an extra field "numberOfClothing" as you can see above. This is not part of my Closet constructor, however I do have a getter called getNumberOfClothing. When I change the name of this getter to getCollectionSize, this extra field added to my json becomes "collectionSize". Why is this extra field being created? How can I prevent it? I think this extra field is causing the problem with reconstructing the object from json.