7

Assuming all fields of a java class are java primitives, if such an object has been serialized, can it be successfully deserialized by C# into an instance of an "equivalent" C# class?

Is the reverse possible - C# to java?


I realise there are many language agnostic formats, such as XML that could be used to get the job done. I am more interested in whether using the native serialized data is feasible.

Bohemian
  • 412,405
  • 93
  • 575
  • 722

5 Answers5

5

The formats of serialized streams are available. I think you can write a class easily to parse the byte stream and create the required class in C#.

An article that specifies the serialized format: http://www.javaworld.com/community/node/2915

Raji
  • 527
  • 3
  • 6
  • This is more what I was wondering. My C# knowledge is very limited, but if it can be described it can be programmed... – Bohemian Jul 23 '13 at 05:34
  • It is just byte[] traversal. You can look at a sample java code to traverse the serialization bytes here [link](https://github.com/smartplatf/a-utilities/blob/master/src/main/java/org/anon/utilities/serialize/srdr/SerialStreamReader.java). We have modified the default serialization, so our traversal is a little different. We have used to detect dirty fields. – Raji Jul 23 '13 at 05:52
  • 1
    A more formal definition of the serialization format can be [found in the official Java documentation](http://docs.oracle.com/javase/7/docs/platform/serialization/spec/protocol.html). – zneak Nov 05 '14 at 18:10
2

WOX will be helpful to achieve interoperable serialization.
it can serialize/deserialize Java/C# objects into/from standard XML(platform independent)

Ashok Damani
  • 3,896
  • 4
  • 30
  • 48
1

This is not possible, at least not using the native serialization libraries that both frameworks provide, as stated in this previous SO post.

If you want to achieve cross language serialization/deserialization, you could resort to XML (XSTream for Java, XStream-dot-net for C#) or WOX:

WOX is an XML serializer for Java and C# objects. In other words, WOX is a library (woxSerializer.jar for Java, and woxSerializer.dll for C#) to serialize Java and C# objects to XML and back again.

Community
  • 1
  • 1
npinti
  • 51,780
  • 5
  • 72
  • 96
  • 1
    This is not correct. The Java serialization format is openly documented and stable since Java version 1.1.7. Assuming that all fields of a Java class are Java primitives, there is no reason that you can't deserialize a Java object into C#, except that you would have to implement the parser yourself. – zneak Nov 05 '14 at 18:07
0

If you're OK with including another dependency, you might consider using an object database such as db4o for the job. I haven't tried this myself, but according the Wikipedia article,

db4o uses a custom feature called "generic reflector" to represent class information, when class definitions are not available, which allows to use it in a mixed Java-.NET environment, for example Java client - .NET server and vice versa.

You can find more information about the above-mentioned reflection API here and here.

In a nutshell, this would lead to a system where you store your Java/C# objects to an (embedded) database (i.e., without client/server architecture, but by loading a single file that contains the whole database) and retrieve C#/Java objects from the database afterwards.

bigge
  • 1,488
  • 15
  • 27
0

I have used this document with a high amount of success to parse data stored in serialized format on a database:

http://www.jtech.ua.es/j2ee/2005-2006/modulos/rmi/recursos/serial-1.5.0.pdf

The most meaningful info for me was from page 63 to 68.

In my case I had the source code used to serialize the data which was useful to both identify fields and read the data when was written in a non standard way using the ISerializable.WriteObject/ReadObject calls.

I don't know the reason but my serialized data had not "handler" field on any object, it would take 0 bytes. Other than that, everything followed the docs but it gets kind of tricky if you have never done such kind of tasks before

As noted on some comment, this is a good base even if it's written in java:

https://github.com/smartplatf/a-utilities/blob/master/src/main/java/org/anon/utilities/serialize/srdr/SerialStreamReader.java

Noman_1
  • 473
  • 1
  • 6
  • 17