0
votes

I'm looking to share object data between two independent desktop applications. Normally, I would use a Serialization Library to serialize objects in one app, and then Deserialize them in the other app (and casting the data to an object).

The Problem:

  • I'm serializing objects in C#
  • I want to deserialize them in Java

Example

Here is a c# class (Foo) that I want to deserialize in Java:

class Foo
{
    public List<Bar> BarList {get;set;}
    public string Name { get; set; }
}
class Bar
{
    public string X { get; set; }
    public int Y { get; set; }
}

Note: I'm using JSON.NET to serialize

JSON for an example Foo Object

{ "BarList": [ { "X": "hi", "Y": 5 }, { "X": "hello", "Y": 10 } ], "Name": "foo" }

Consuming this object in Java

Let's assume I have a very similar Foo class in Java. Instead of Properties, it has standard private members with public getter/setter methods.

Now, I could simply do something like:

class Foo
{
    private ArrayList<Bar> barList;
    private string name;

    public Foo(string pathToJsonFile)
    {
         // Method/Code to manually grab the needed JSON and call setters
    }

    // Setters/Getters
}

This solution works, but I would rather avoid having this constructor on every single object in my Java project. Plus, if I have deeply coupled objects, this method would be huge (deserialize this object, but it contains objects that also need deserialized, etc, etc).

My Question:

Are there any Deserialization methods that are in the Java Standard Library, that could make this process easier?

Algorithm ideas would also be appreciated (I don't mind creating a small framework).

Third-party solutions are okay, as long as they are completely open-source, with no .jar dependencies.

2

2 Answers

1
votes

Have you looked at JSON-B (JSR 367)? Although this does not appear to be in the standard library yet, the final spec was approved in June of this year. There is an official reference implementation called Yasson that is open source.

If that is not what you are looking for, there are all kinds of third-party open source libraries which handle JSON binding in Java, such as Jackson, Gson, Genson, FlexJson and json-simple to name just a few. Of those, the first two seem to be the most popular. You can find even more libraries listed on json.org.

0
votes

Last time I checked, Java SE had none of that. In Java EE, there is API in package java.ws.rs, at least. You could use the API and be pretty much independent on the implementation.

If you want to create your own framework, have fun. JSON is a lot more complex than you might think, so depending on how big subset of it you want to support, this task could be quite time-consuming.