5 Replies Latest reply: Mar 7, 2008 3:49 PM by 807591 RSS

    Testing Object Equality using Serialization

    807591
      Hey everyone! I was wondering if somebody could help me figure out how to compare two objects using serialization.

      I have two objects that I'm trying to compare. Both of these objects extend a common "Model" class that has a method getSerialized() that returns a serialized form of an instance, shown below:
                // Serialize the object to an array
               ByteArrayOutputStream baos = new ByteArrayOutputStream(1000);
               ObjectOutputStream oos;
                try {
                     oos = new ObjectOutputStream(baos);
                    oos.writeObject(this);
                    oos.close();
                } catch (IOException e) {
                     e.printStackTrace();
                }
               //Deserialize array into a String array
               return baos.toByteArray();
      This Model class also has an equals(Model obj) method that allows for the current model object to be compared to a model object that is passed in:
              //Store both models' serialized forms into byte arrays
              byte [] thisClass = this.getSerialized();
              byte [] otherClass = obj.getSerialized();
      This is where things get a little funny. The byte arrays don't equal - one array is a byte larger than the other. If a byte-by-byte comparison is done, the arrays are equal for the first 15-20% and then not equal for the rest. If I deserialize the byte arrays back into Models and do a toString() on those models, I find that they are equal.

      I have a feeling there's something about the serialization process that I don't fully comprehend. Is there a way to properly implement object comparison using serialization?

      Thanks in advance!
        • 1. Re: Testing Object Equality using Serialization
          807591
          What makes you think comparing the serialized form of an object is the proper way to test for equality?
          • 2. Re: Testing Object Equality using Serialization
            DrClap
            I have a feeling there's something about the serialization process that I don't fully comprehend.
            I would agree with that.
            Is there a way to properly implement object comparison using serialization?
            I wouldn't think so. In fact I wouldn't have thought it possible at all. The serialization process is incompatible with just about every notion of "equal" in Java. I would think you'd be better off just writing a proper equals(Object) method for your class.
            • 3. Re: Testing Object Equality using Serialization
              807591
              the quick answer is: you cant
              the longer answer is: you cant test object equality using serialization
              • 4. Re: Testing Object Equality using Serialization
                807591
                Hi,

                Thanks for the prompt reply! This was an avenue we wanted to explore as our existing framework makes it difficult to compare objects otherwise (we have a bunch of aggregation/composition/backreferences and cyclic references that need to be handled by this framework). We were using a custom toString that used our framework to walk to object model before, but this proved to be a bit inefficient.

                If possible, could you explain why object serialization fails?
                • 5. Re: Testing Object Equality using Serialization
                  DrClap
                  When you serialize an object, you also serialize the entire tree of references based on that object (except for transient variables). That tree is the complicated business you described there. Serialization stores all the objects in the tree, along with data that explains which objects refer to which other objects. Furthermore if the tree is actually a graph, and there are multiple ways to get to an object, it still only stores each object once. I don't see any reason to believe that all that relationship data would be encoded identically for a pair of trees that you deemed to be equal. And your experiment shows that indeed it isn't.