Friday, March 23rd, 2007
JsonMarshaller: Java 1.5 Marshalling
<p>JsonMarshaller is a Java 1.5 library that allows marshalling and unmarshalling of JSON objects to and from Java objects. This project's goal is above all ease of use, transparency and static type safety.Example
If you have the following Java class:
and you created a new Book() and populated it with info it could marshal too:
-
{title: "Vocation Createurs",
isbn: "2829302680",
authors: [{firstName: "Barbara", lastName: "Polla"},
{firstName: "Pascal", lastName: "Perez"}]}
Related Content:











Have you guys thought about circular references? Have you considered using JSPON (JSON extension to facilitate circular references as welll as other things). What if you do this:
@Entity
class Book {
@Value
String title;
@Value
Book relatedBook;
}
...
Book myBook = new Book();
myBook.title = "my book";
myBook.relatedBook = myBook;
This can’t be represented with plain JSON, but with JSPON, the JSON marshaller could produce:
{"id" : "1",
"title" : "my book",
"relatedBook" : {"id" : "1"}}
We just use Jettison for all our stuff– it’s easy to swap between XML and JSON only be replacing the writer and all of our clients can consume XML or JSON without requiring us to support two different sets of annotations/models.
nice idea.
A support for java.util.Map is really mandatory.
Furthermore if you try to marshall something like the code bellow you will get a StackOverFlow:
@Entity
public class Person {
@Value
public int age;
@Value
public String name;
@Value
List childs;
public static void main(String[] args) throws JSONException {
Person me = new Person();
me.age = 27;
me.name = “chuck norris”;
me.childs = new LinkedList() {
{
Person child = new Person();
child.age = 12;
child.name = “timmy”;
add( child );
}
};
Marshaller marshaller = Marshaller.create( Person.class );
JSONObject jsonObject = marshaller.marshall( me );
String string = jsonObject.toString();
System.out.println( string );
JSONObject newObject = new JSONObject( string );
Person clone = marshaller.unmarshall( newObject );
}
}
@Entity
public class Person {
@Value
public int age;
@Value
public String name;
@Value
List childs;
public static void main(String[] args) throws JSONException {
Person me = new Person();
me.age = 27;
me.name = "chuck norris";
me.childs = new LinkedList() {
{
Person child = new Person();
child.age = 12;
child.name = "timmy";
add( child );
}
};
Marshaller marshaller = Marshaller.create( Person.class );
JSONObject jsonObject = marshaller.marshall( me );
String string = jsonObject.toString();
System.out.println( string );
JSONObject newObject = new JSONObject( string );
Person clone = marshaller.unmarshall( newObject );
}
}
Quick Responses:
java.util.Map – yes definitely! we’ll push it into 0.2 ;-)
circular references – we tend to have explicit ‘ids’ like
Person {@Value(inline = true)
Id id;
...
}
which we directly inline to avoid these circular references. But it might be a nice addition for non explicitly ‘id’ed code.
I believe XStream has a JSON marshaller that doesn’t require annotations…
I know that JSONRPC has really good set of features that do not require annotations as well.
I have nothing against one more JSON marshalling library.. unless it requires annotations and provides no benefits comparing to existing libraries.
Very interesting code for a very interesting library. I downloaded the JsonMarshaller and played around with it, but I agree that JSONRPC basically has many of these features and doesn’t require annotations. While this library looks really good, if it’s going to take extra time (like Robert says), then it’s not as efficient as other packages. JSONRPC works really well for me, so I think I’ll be sticking with it. I’m interested to see what XStream has – I haven’t heard of it before. How does it compare to JSONRPC? By the sounds of it, the two are pretty similar? Even though I don’t think it will replace any of my current libraries, thank you for the code for JsonMarshaller. It’s always interesting to get a look at other objects and how they work.