2

I am writing an Android app for a co-worker that keeps track of Kids signed up for a soccer league. I am currently having trouble saving/serializing my roster then deserializing it later. The object I am serializing is an array of Player objects. The custom player class implements serializable so an array of them should be fine to serialize (as far as I know).

My serialization/saving method:

    String ser = SerializeObject.objectToString(currentRoster.getRosterArray());
    if (ser != null && !ser.equalsIgnoreCase("")) {
        SerializeObject.WriteSettings(this, ser, "playerRoster");  //.dat extension
    } else {
        System.out.println("Object not saved");
        SerializeObject.WriteSettings(this, "", "playerRoster");
    }   

My deserialization method:

    String ser = SerializeObject.ReadSettings(this, "playerRoster");
    if (ser != null && !ser.equalsIgnoreCase("")) {
        Object obj = SerializeObject.stringToObject(ser);
        // Then cast it to your object and 
        if (obj instanceof Player[]) {
            // Do something
            loadedRoster = (Player[]) obj;
            System.out.println(loadedRoster[0]);
        }
    }

The result I am getting in my app is jargon for every player in the array when deserialized. My question is on if I am correctly saving and loading the data, or am I forgetting something. (I left out some of the filler code and exception handling to keep it cleaner) Thanks for any help!

ChristopherW
  • 993
  • 1
  • 12
  • 25
  • are you sure that the serialized data is read while you are providing the file name as "playerRoster" ? ... try adding the file extension ... like "playerRoster.dat" ... i never tried the SerializeObject, i always serialize things manually so i dont know this technique myself – Ahmed Adel Ismail Sep 15 '13 at 00:15
  • Just tried that and I am getting the same results. This is what shows up for each player: [Lcom.example.soccersignup.Player;@420dc7a8 – ChristopherW Sep 15 '13 at 00:26
  • Now i'm confused , if this result is the same for all players (including the hashCode in the end), then your array holds duplicate values of the same player object with the same hashCode, and this means that your problem is not with serializing or deserializing your objects !!! is there more code, i'm afraid i got to go for now, but maybe more code will be helpful for any body who tries to help (or maybe i'm wrong) – Ahmed Adel Ismail Sep 15 '13 at 00:48
  • I have tested all of my other code for generating the array of players and manipulating them. It doesn't throw an exception when serializing and when deserialized it comes out weird. I will looking into it again tomorrow when I wake up. Thanks for trying regardless. – ChristopherW Sep 15 '13 at 07:24

1 Answers1

0

When facing the problem of (de)serialization or (un)marshalling, I turned to JAXB to (de)serialize json or xml. I tried Jackson, but didn't get the results I was looking for, particularly with my xml. Jackson likes to set namespaces for XML (and defaults to "") and I needed mine without. Other than that, it was great, no dependencies and it'll handle well formed XML and JSON unlike gson and JSON-java.

When reading about using any of the above approaches you can't go wrong reading anything by Blaise Doughan or StaxMan. You can find a tutorial about JAXB in general right here. For using MOXy as your JAXB provider, this shows all the necessary code and links to anything else you need to know to (de)serialize/(un)marshal your objects.

Community
  • 1
  • 1
Prmths
  • 2,022
  • 4
  • 21
  • 38