org.cruxframework.crux.core.client.bean
Interface JsonEncoder<T>

Type Parameters:
T - The type of the beans to be serialized by the encoder interface.

public interface JsonEncoder<T>

An utility interface to allow object serialization to/from json notation.

You can define encoders by extending this interface.

Imagine you have the following java bean class defined as:

 public class Person {
    private String name;
    private int age;
    
    public String getName(){return name;}
    public void setName(String name){this.name = name;}
    public int getAge(){return age;}
    public void setAge(int age){this.age = age;}
 }
 

You can define the following interface to make serialization to / from json notation:

 public interface PersonEncoder extends JsonEncoder {}
 

To use the encoder, just call GWT.create on the given interface, or inject it on your class.

 public class MyController {
    @Inject
    private PersonEncoder encoder;
    
    @Expose
    public void myMethod() {
       // read the object...
       String json = encoder.encode(person);
       Person decoded = encoder.decode(json);
    }
 }
 

Author:
Thiago da Rosa de Bustamante

Method Summary
 T decode(String jsonText)
          Encode the given JSON notation string in a object
 String encode(T object)
          Encode the given object in a JSON notation string
 T fromJavaScriptObject(com.google.gwt.core.client.JavaScriptObject object)
          Transform the given javascript native object in a java object
 com.google.gwt.core.client.JavaScriptObject toJavaScriptObject(T object)
          Transform the given object in a javascript native object
 

Method Detail

encode

String encode(T object)
Encode the given object in a JSON notation string

Parameters:
object - object to be encoded
Returns:
a string in JSON notation representing the object

toJavaScriptObject

com.google.gwt.core.client.JavaScriptObject toJavaScriptObject(T object)
Transform the given object in a javascript native object

Parameters:
object - object to be encoded
Returns:
a native javascript object

decode

T decode(String jsonText)
Encode the given JSON notation string in a object

Parameters:
jsonText - JSON notation string
Returns:
a java object

fromJavaScriptObject

T fromJavaScriptObject(com.google.gwt.core.client.JavaScriptObject object)
Transform the given javascript native object in a java object

Parameters:
object - javascript native object to be decoded
Returns:
a java object


Copyright © 2014. All rights reserved.