Common utilities and foundational infrastructure for the Errai GWT-based framework
—
Extensible marshalling system for converting objects to/from string representations and JSON objects, with registry classes for managing custom marshaller and demarshaller implementations.
Interface for converting objects to string representations.
/**
* Interface for marshalling objects to string representation
* @param <T> - Type of object to marshall
*/
public interface Marshaller<T> {
/**
* Marshall object to string representation
* @param object - Object to marshall
* @return String representation of the object
*/
public String marshall(T object);
}Usage Example:
import org.jboss.errai.common.client.types.Marshaller;
// Custom marshaller for Person objects
public class PersonMarshaller implements Marshaller<Person> {
public String marshall(Person person) {
return "{\"name\":\"" + person.getName() + "\",\"age\":" + person.getAge() + "}";
}
}
// Use the marshaller
PersonMarshaller marshaller = new PersonMarshaller();
Person person = new Person("John", 30);
String json = marshaller.marshall(person);Interface for converting JSON objects back to typed Java objects.
/**
* Interface for demarshalling JSONObject to typed objects
* @param <T> - Type of object to demarshall to
*/
public interface Demarshaller<T> {
/**
* Demarshall JSONObject to typed object
* @param o - JSONObject to demarshall
* @return Typed object instance
*/
public T demarshall(JSONObject o);
}Usage Example:
import org.jboss.errai.common.client.types.Demarshaller;
import com.google.gwt.json.client.JSONObject;
// Custom demarshaller for Person objects
public class PersonDemarshaller implements Demarshaller<Person> {
public Person demarshall(JSONObject o) {
String name = o.get("name").isString().stringValue();
int age = (int) o.get("age").isNumber().doubleValue();
return new Person(name, age);
}
}
// Use the demarshaller
PersonDemarshaller demarshaller = new PersonDemarshaller();
JSONObject jsonObject = /* parsed JSON object */;
Person person = demarshaller.demarshall(jsonObject);Static registry for managing marshaller instances by type.
/**
* Registry for managing marshaller instances
*/
public class TypeMarshallers {
/**
* Register marshaller for specific type
* @param type - Class to register marshaller for
* @param d - Marshaller implementation
*/
public static void addMarshaller(Class type, Marshaller d);
/**
* Get marshaller by class type
* @param type - Class to get marshaller for
* @return Marshaller instance for the type
*/
public static <T> Marshaller<T> getMarshaller(Class<? extends T> type);
/**
* Get marshaller by class name
* @param type - Class name to get marshaller for
* @return Marshaller instance for the type
*/
public static Marshaller getMarshaller(String type);
/**
* Check if marshaller exists for class
* @param type - Class to check
* @return true if marshaller is registered
*/
public static boolean hasMarshaller(Class type);
/**
* Check if marshaller exists for class name
* @param type - Class name to check
* @return true if marshaller is registered
*/
public static boolean hasMarshaller(String type);
}Usage Examples:
import org.jboss.errai.common.client.types.TypeMarshallers;
// Register custom marshaller
TypeMarshallers.addMarshaller(Person.class, new PersonMarshaller());
// Check if marshaller exists
if (TypeMarshallers.hasMarshaller(Person.class)) {
// Get and use marshaller
Marshaller<Person> marshaller = TypeMarshallers.getMarshaller(Person.class);
String json = marshaller.marshall(person);
}
// Get marshaller by class name
Marshaller marshaller = TypeMarshallers.getMarshaller("com.example.Person");Static registry for managing demarshaller instances by type.
/**
* Registry for managing demarshaller instances
*/
public class TypeDemarshallers {
/**
* Register demarshaller for specific type
* @param type - Class to register demarshaller for
* @param d - Demarshaller implementation
*/
public static void addDemarshaller(Class type, Demarshaller d);
/**
* Get demarshaller by class type
* @param type - Class to get demarshaller for
* @return Demarshaller instance for the type
*/
public static <T> Demarshaller<T> getDemarshaller(Class<? extends T> type);
/**
* Get demarshaller by class name
* @param type - Class name to get demarshaller for
* @return Demarshaller instance for the type
*/
public static Demarshaller getDemarshaller(String type);
/**
* Check if demarshaller exists for class
* @param type - Class to check
* @return true if demarshaller is registered
*/
public static boolean hasDemarshaller(Class type);
/**
* Check if demarshaller exists for class name
* @param type - Class name to check
* @return true if demarshaller is registered
*/
public static boolean hasDemarshaller(String type);
}Usage Examples:
import org.jboss.errai.common.client.types.TypeDemarshallers;
import com.google.gwt.json.client.JSONObject;
// Register custom demarshaller
TypeDemarshallers.addDemarshaller(Person.class, new PersonDemarshaller());
// Check if demarshaller exists
if (TypeDemarshallers.hasDemarshaller(Person.class)) {
// Get and use demarshaller
Demarshaller<Person> demarshaller = TypeDemarshallers.getDemarshaller(Person.class);
Person person = demarshaller.demarshall(jsonObject);
}
// Get demarshaller by class name
Demarshaller demarshaller = TypeDemarshallers.getDemarshaller("com.example.Person");The library includes built-in marshallers for common Java types:
Pre-configured marshallers for date types that convert dates to timestamp strings.
Built-in Date Marshallers:
java.sql.Date → timestamp stringjava.util.Date → timestamp stringUsage Example:
import java.util.Date;
import java.sql.Date as SQLDate;
import org.jboss.errai.common.client.types.TypeMarshallers;
// Built-in marshallers are automatically registered
Date utilDate = new Date();
Marshaller<Date> utilDateMarshaller = TypeMarshallers.getMarshaller(Date.class);
String utilDateString = utilDateMarshaller.marshall(utilDate); // "1234567890000"
SQLDate sqlDate = new SQLDate(System.currentTimeMillis());
Marshaller<SQLDate> sqlDateMarshaller = TypeMarshallers.getMarshaller(SQLDate.class);
String sqlDateString = sqlDateMarshaller.marshall(sqlDate); // "1234567890000"Here's a complete example showing custom marshalling and demarshalling:
import org.jboss.errai.common.client.types.*;
import org.jboss.errai.common.client.json.*;
import com.google.gwt.json.client.JSONObject;
import java.io.Serializable;
// 1. Define custom class
public class User implements Serializable {
private String username;
private String email;
private boolean active;
public User(String username, String email, boolean active) {
this.username = username;
this.email = email;
this.active = active;
}
// getters and setters...
}
// 2. Implement marshaller
public class UserMarshaller implements Marshaller<User> {
public String marshall(User user) {
return "{\"username\":\"" + user.getUsername() +
"\",\"email\":\"" + user.getEmail() +
"\",\"active\":" + user.isActive() +
",\"__EncodedType\":\"User\"}";
}
}
// 3. Implement demarshaller
public class UserDemarshaller implements Demarshaller<User> {
public User demarshall(JSONObject o) {
String username = o.get("username").isString().stringValue();
String email = o.get("email").isString().stringValue();
boolean active = o.get("active").isBoolean().booleanValue();
return new User(username, email, active);
}
}
// 4. Register marshallers
TypeMarshallers.addMarshaller(User.class, new UserMarshaller());
TypeDemarshallers.addDemarshaller(User.class, new UserDemarshaller());
// 5. Use with JSON processing
JSONEncoderCli encoder = new JSONEncoderCli();
JSONDecoderCli decoder = new JSONDecoderCli();
User user = new User("john", "john@example.com", true);
// Marshall to JSON
String json = encoder.encode(user);
// Result: {"username":"john","email":"john@example.com","active":true,"__EncodedType":"User"}
// Demarshall from JSON
User decodedUser = (User) decoder.decode(json);Missing Marshaller:
RuntimeException when no marshaller is available for Serializable objectsMissing Demarshaller:
RuntimeException when encountering __EncodedType without available demarshallerExample Error Handling:
// Handle missing marshaller
try {
if (!TypeMarshallers.hasMarshaller(MyClass.class)) {
TypeMarshallers.addMarshaller(MyClass.class, new MyClassMarshaller());
}
String json = encoder.encode(myObject);
} catch (RuntimeException e) {
System.err.println("Marshalling error: " + e.getMessage());
}
// Handle missing demarshaller
try {
if (!TypeDemarshallers.hasDemarshaller("MyClass")) {
TypeDemarshallers.addDemarshaller(MyClass.class, new MyClassDemarshaller());
}
Object obj = decoder.decode(jsonString);
} catch (RuntimeException e) {
System.err.println("Demarshalling error: " + e.getMessage());
}Install with Tessl CLI
npx tessl i tessl/maven-org-jboss-errai--errai-common