Serializing and deserializing Java objects from a .NET program
Every so often, a customer asks us whether JNBridgePro can help them serialize or deserialize Java objects from their .NET code. The answer is yes! Your .NET application can do this by making use of Java’s serialization APIs. Here’s a simple example that shows how it’s done.
Let’s start with simple Java classes representing a person and an address:
public class Person implements java.io.Serializable { public String name; public Address address; public int age; public Person(String n, String streetAddress, String town, String state, String zip, int a) { name = n; address = new Address(streetAddress, town, state, zip); age = a; } public String toString() { return name + ", " + address.toString() + ", " + age; } } public class Address implements java.io.Serializable { public String streetAddress; public String town; public String state; public String zip; public Address(String sa, String t, String st, String z) { streetAddress = sa; town = t; state = st; zip = z; } public String toString() { return streetAddress + ", " + town + ", " + state + " " + zip; } }
Note that both classes are properly marked as serializable, and that all their fields are serializable.
We’re going to write a C# program that first creates a Person
object and its associated Address
object, then serializes the objects and writes them out to a file. After that, the program reads the objects back in, and prints them out to verify that the correct object was properly deserialized.
We start by launching the proxy generation tool and creating a .NET-to-Java project. We’ll proxy Person
and Address
. Since we want to serialize and deserialize the objects, we’ll also proxy java.io.ObjectInputStream
and java.io.ObjectOutputStream
. Since we’ll be writing the serialized data to a file and then reading from the file, we’ll also proxy java.io.FileInputStream
and java.io.FileOutputStream
. We’ll also proxy all supporting classes.
Once we’ve got the proxies, we’ll write our simple C# application:
using System; using java.io; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { Person p = new Person("John Doe", "123 Main Street", "Anytown", "OH", "12345", 30); Person p2 = null; // serialize p and save it away FileOutputStream fileOutputStream = new FileOutputStream("serializedData"); ObjectOutputStream objOutputStream = new ObjectOutputStream(fileOutputStream); objOutputStream.writeObject(p); objOutputStream.close(); fileOutputStream.close(); // deserialize into p2 FileInputStream fileInputStream = new FileInputStream("serializedData"); ObjectInputStream objInputStream = new ObjectInputStream(fileInputStream); p2 = (Person)objInputStream.readObject(); objInputStream.close(); fileInputStream.close(); Console.WriteLine(p2); } } }
The various java.io
calls can throw exceptions, so to create a truly robust application we should catch and handle the exceptions, but we’ll leave that as a detail for the reader.
Include the proxy dll in our project, as well as jnbshare.dll
and the appropriate jnbsharedmem.dll
and rlm932.dll
, and configure JNBridgePro to use shared memory and specifying the folder containing Person.class
and Address.class
as the classpath, then build and run the application. We’ll see the output
John Doe, 123 Main Street, Anytown, OH 12345, 30
which means that the serialized Person and Address Java objects were properly deserialized from our C# code.
Are you doing cross-platform serialization and deserialization? Are there any other Java APIs you’d like to see us demonstrate from .NET code? Let us know.