4

I have a system where a serialized file is created with a C# program and then deserialized in another C# program. I'm wondering if it's possible to do binary deserialization of a C# file in Java?

Thanks

2
  • 2
    I don't think, java can use the output of BinaryFormatter, but you can use Xml,Json,Protobuf. Commented Aug 16, 2013 at 16:21
  • 1
    @I4V: Of course Java can use the output of a .Net binary formatter. It's just that there is no known implementation... :) Commented Aug 16, 2013 at 18:10

4 Answers 4

4

You can try using some serializator that has implementations for both platforms and outputs data in a platform-independet format, like Protobuf.

Or if you need a full RPC over network between Java and C# application, you can go for Apache Thrift.

Sign up to request clarification or add additional context in comments.

Comments

3

I assume you are speaking of an object serialized with BinaryFormatter. The answer then is a qualified "yes," since Java implements a Turing machine. However, this is will not be straightforward.

In this case the data will be in a format most suitable for consumption by a .NET runtime, and will contain information about .NET types and assemblies. You would have to implement your own reader for this format, and then have some way to map between .NET and Java types. (The Mono project implements a BinaryFormatter compatible with .NET's, so you could use their reader implementation as a reference.)

As an alternative, consider using another format for data serialization, such as JSON. This will give you instant portability to a wide array of languages, as well as the possibility for easy human inspection of the data.

7 Comments

JSON is technically not binary serialization. Maybe its a viable alternative to the OP. Just pointing that out.
@WilliamMorrison I never claimed that it was binary serialization. The OP does not indicate that binary serialization is a requirement, they are just asking if it's possible to read a specifically-formatted binary-serialized C# object graph from Java.
Its not what the OP asked for, that's all I'm saying.
OP did not ask for Protobuf either, they are discussing binary object serialization inbuilt into the .NET Framework. By your logic, suggesting any alternative format is "not what the OP asked for."
Protobuf is a binary serialization alternative which is why I suggest it. JSON is not. If you'd like to discuss my answer, do so on my answer please.
|
1

Deserializing an object in Java which was serialized with C#'s built-in binary serialization would you'd to implement C#'s deserialization logic in java. That's a pretty involved process, so let's compare some options:

  1. Use a third party library for serialization which works for C# and Java.

  2. Write a routine to serialize each object. One in C#, one in Java. This will be tedious, and hard to maintain.

  3. Implement C#'s serialization logic in Java, or vice versa. This will be difficult, time consuming, and you likely won't get it right the first time.

I recommend option 1, use a third-party library. Here's two third-party libraries I've used and highly suggest.

Google ProtoBufs

Apache Thrift

Comments

0

You can use any cross-platform binary format. Your options include, among others:

  • Protobuf
  • BSON (Binary JSON)
  • GZIP

JSON and XML (herrrrp) are also options, albeit text-based ones.

One other option would be to base64-encode the data, and decode it on the other side; albeit you may get a huge payload because it's binary (probably not a good idea).

2 Comments

gzip is compression format. I do not see how it fits in that list. Did you mean something else?
@leppie if it's the same physical system, GZip it in one program and unzip it in the other. Isn't that what you want? Even if it's a network, you can send it across the wire and unzip it on the other side.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.