Friday, March 23rd, 2007

JsonMarshaller: Java 1.5 Marshalling

Category: Java, JSON, Library

<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:

  1. @Entity
  2. class Book {
  3.   @Value
  4.   private String title;
  5.   @Value
  6.   private String isbn;
  7.   @Value
  8.   private Set<author> authors;
  9. }
  10.  
  11. @Entity
  12. class Author {
  13.   @Value
  14.   private String firstName;
  15.   @Value
  16.   private String lastName;
  17. }

and you created a new Book() and populated it with info it could marshal too:

[json]
{title: “Vocation Createurs”,
isbn: “2829302680″,
authors: [{firstName: "Barbara", lastName: "Polla"},
{firstName: "Pascal", lastName: "Perez"}]}
[/json]

Related Content:

Posted by Dion Almaer at 8:50 am
7 Comments

+++--
3.5 rating from 42 votes

7 Comments »

Comments feed TrackBack URI

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"}}

Comment by Kris Zyp — March 23, 2007

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.

Comment by Jacob — March 23, 2007

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 );

}

}

Comment by jni — March 23, 2007


@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 );

}

}

Comment by jni — March 23, 2007

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.

Comment by Pascal — March 23, 2007

I believe XStream has a JSON marshaller that doesn’t require annotations…

Comment by Ivan — March 25, 2007

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.

Comment by Robert — March 25, 2007

Leave a comment

You must be logged in to post a comment.