views:

332

answers:

6

I'm working on a project where I'll need to serialize some data in a java 6 app and deserialize it a c# 2.0 app. Is there a strategy or something already in existence I can look at that would allow me to do this with these two languages? I'm guessing they both support XML serialization but I really need it to be binary serialized.

A: 

I don't believe binary serialization will work as C# and Java have no idea of each others native types.

TheHurt
"Binary serialization" is not necessarily the same thing as "type/metadata serialization". Many binary formats don't depend on the types used at any implementation.
Marc Gravell
+6  A: 

Protocol Buffers (Google Site)

Java Tutorial

Jon Skeet's C# Port

Marc Gravell's C# Port

Upsides: Fast and you can bug quite some people that are involved with this thing on SO.. ;-)

Let me exploit Marc's project site: Performance is quite acceptable..

Benjamin Podszun
Yeah, like Marc and I ever show up here. 'Tis pure fluke that we happen to have both looked at this question.
Jon Skeet
Glad that I could summon you then. Quite a rare sight indeed..
Benjamin Podszun
+3  A: 

The default binary serialization of each language is incompatible so you will not be able to use that.

There are many cross-language serialization technologies that support Java, C#, and other languages:

Of these, JSON is not binary but very efficient for a string-based language. Thrift and Protocol Buffers are binary and have a very compact representation.

Michael Greene
+8  A: 

Protocol buffers would be a good option here. On the C# side, I would recommend Jon Skeet's dotnet-protobufs for this use-case, since it has the same API on both sides (his C# version is a port of the Google Java version, part of the core distribution). If you want the C# to be more "typical .NET", then protobuf-net may help.

(the wire format is obviously identical between versions; the API may vary)

Small, fast, efficient, portable.

For info, I know that protobuf-net has .NET 2.0 support; I honestly haven't tried this on Jon's version, but I expect it would - there isn't much that you need 3.0/3.5 for in protobuf.

Marc Gravell
Yes, I target .NET 2.0, as well as Silverlight 2+ and Compact Framework 2+ (IIRC - the CF version may be 3.5).
Jon Skeet
+2  A: 

Google's Protocol Buffers is something that you could look into. You will need to check into the state of the usability of the C# implementation, but in all other respects, I think that it meets your needs.

Adam Crossland
+1  A: 

You could try Hessian:

http://hessian.caucho.com/index.xtp

It's binary, and supports Java, C++, and several other languages. I've never used it myself, but came across it, thought it was interesting, and bookmarked it...

Sarah K