taoensso / nippy

The fastest serialization library for Clojure
https://www.taoensso.com/nippy
Eclipse Public License 1.0
1.04k stars 60 forks source link

Serialization problem across clojure versions #51

Closed kul closed 9 years ago

kul commented 9 years ago

I have the following java class as an example, in a project lets say org.foo/dummy-project

package org;

import java.util.UUID;
import clojure.lang.Keyword;
import java.io.Serializable;

public class Dummy implements Serializable {

  private Keyword foo;
  private UUID id;
  private static final long serialVersionUID = 327399273872L;

  public Dummy(Keyword foo, UUID id) {
    if (id==null) {
      throw new IllegalArgumentException("fooo");
    }
    this.foo = foo;
    this.id = id;
  }
}

Now if the above dependency is available in two clojure projects with nippy 2.6.2 and clojure 1.5.1 and 1.6.0 respectively. The following errors are encountered

From 1.5.1 project

  (import 'java.io.FileOutputStream)
  (require '[taoensso.nippy :as n])

  (with-open [out (FileOutputStream. "/tmp/foo")]
    (.write out (n/freeze (org.Dummy. :foo (java.util.UUID/randomUUID)))))

From 1.6.0 project

  (import 'com.google.common.io.Files)
  (n/thaw (Files/toByteArray (clojure.java.io/file "/tmp/foo")))
  ; gives {:nippy/unthawable "org.Dummy", :type :serializable}

I noticed that this happened after introduction of the Keyword member in Dummy class.

ptaoussanis commented 9 years ago

Hi kul, is this still an issue or did you find a resolution?

kul commented 9 years ago

Hi, I ended up sticking to 1.5.1 for data freezed with 1.5.1. I think this should still be reproducible.

ptaoussanis commented 9 years ago

:nippy/unthawable errors are thrown when the class (in this case org.Dummy) cannot be created through the Serializable implementation that you've provided:

(cast (Class/forName "org.Dummy") (.readObject <as-serialized>))

I don't use it myself so I'm not familiar with Java's Serializable and its portability characteristics. If the docs are no help, I'd suggest using Nippy's extend-freeze, extend-thaw to specify a stable binary representation for your type that doesn't depend on Serializable.

Hope that helps, cheers! :-)