Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-700

Cannot Serialize/Deserialize ObjectId using Adobe BlazeDS(flex-messaging)

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.9.1, 2.9.2, 2.9.3
    • Component/s: API, Codecs
    • Environment:
      Windows 2008 Server
      Java JDK 7u07
      MongoDB Driver : mongo-2.9.3.jar
      Adobe BlazeDS : flex-messaging-core.jar

      Hello,

      I use Native Adobe BlazeDS Library used to serialize and deserialize AMF (Action Message Format) which can convert native ActionScript3/Flex objects to Java Objects. This serialization library looks for "public" non-transient fields in the object, recursively and transform them from AS/Java | Java/As. However, in the ObjectId class, the "_inc", "_time" and "_machine" fields are using default access modifiers and are "final int".

      In the ObjectId default constructor a new ID is generated :
      /** Create a new object id.
      */
      public ObjectId(){
      _time = (int) (System.currentTimeMillis() / 1000);
      _machine = _genmachine;
      _inc = _nextInc.getAndIncrement();
      _new = true;
      }

      Doing so, BlazeDS creates a new ObjectId on deserialization and cannot modifier furthermore the "final int". The ObjectId is a new one.

      **********************************************************
      Serializing ObjectId...
      Before serialization : time '10000', machine '20', inc '30', _id '0000000a000000140000001e'
      Deserialized ObjectId...
      After serialization : time '1353591710000', machine '1079454374', inc '-693442528', _id '50ae2b9e40572aa6d6aae820'
      **********************************************************

      I've modified your ObjectId class and rebuilt it with "public int _inc" instead of "final int _inc" and it now works properly. However, I understand that it breaks your class encapsulation.

      **********************************************************
      Serializing ObjectId...
      Before serialization : time '10000', machine '20', inc '30', _id '0000000a000000140000001e'
      Deserialized ObjectId...
      After serialization : time '10000', machine '20', inc '30', _id '0000000a000000140000001e'
      **********************************************************

      I would like to know if you plan on something regarding this class. Would it be possible to :

      1 - make fields public instead of package-protected final?
      2 - would it be possible to leave the constructor empty and add a new ".newId()" method?
      3 - Is it a good idea to create a ObjectId2 class that encapsulates an ObjectId and a public String so that the library BlazeDS can properly serialize/deserialize the String and create the ObjectId using the String? What would be the impact in the database PrimaryKey?
      4 - Do you have any better idea?

      I've attached a demo with the native library and the modified one.

      Thanks for your time.

      Daniel Marcotte

            Assignee:
            Unassigned Unassigned
            Reporter:
            dmarcotte Daniel Marcotte
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: