Question

I have two different programs- one which serializes data and saves it to a local file, and the other program loads the data and use it. The problem is that the deserializing is not working - the same structure it was serealized in cannot be reconstructed, no matter how i Serialize / Deserialize the data.

Edit: The problem specifically with loading from file the result of the serialize action.

Edit2: The exception I get is Exception in thread "main" java.lang.ClassCastException: java.util.ArrayList cannot be cast to myImage

I use deep serialize, as you can see here:

ArrayList<ArrayList<Images>> data;
...filling the data object with my data...
String serialized = new JSONSerializer().exclude("*.class")
                .deepSerialize(data);

The output of serealize action looks like this:

[
 [
   {"datetaken":"date-time","filePath":"a-file-path","folderPath":"a-folder-path","meta1":"val1","meta2":"val2"}
 ]
 [
   {more..}, 
   {more..}
 ]
]

and for deserializing I use:

ArrayList<ArrayList<TrimmedImageData>> data;
...some initializing code...
data = (ArrayList<List<Images>>) new JSONDeserializer<ArrayList<List<Images>>>()
             .use("value.values", myImage.class)
                    .deserialize(new BufferedReader(new FileReader(
                            resultsFile)));
Was it helpful?

Solution

The problem in my case was two things:

  1. I excluded the "*.class". No good reason to do it in my case, therefore I removed it.
  2. I changed in the deserialize part to get a String of my data, I read the data using nio and then concatenating it with StringBuilder, as this example shows:
try {
  List<String> res = Files.readAllLines(Paths.get(myObject),
          StandardCharsets.UTF_8);
  StringBuilder anotherTest = new StringBuilder();
  for(String str : res) {
      anotherTest.append(str);
  }
  serializedString = anotherTest.toString();
} catch (IOException e) {
  e.printStackTrace();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top