Common utilities and foundational infrastructure for the Errai GWT-based framework
—
Client-side JSON encoding and decoding with support for custom marshalling, type-aware conversion, and integration with the Errai type system.
Client-side JSON encoder with support for custom marshalling and type metadata tracking.
/**
* Client-side JSON encoder with support for custom marshalling
*/
public class JSONEncoderCli {
/**
* Encode object to JSON string
* @param v - Object to encode
* @return JSON string representation
*/
public String encode(Object v);
/**
* Encode map to JSON with marshalling metadata
* @param map - Map to encode
* @return JSON string with type metadata
*/
public String encodeMap(Map<Object, Object> map);
/**
* Get types that were marshalled during encoding
* @return Map of property names to marshalled type names
*/
public Map<String, String> getMarshalledTypes();
}Supported Types:
null → "null"String → Quoted and escaped JSON stringNumber, Boolean → Direct JSON representationCollection → JSON arrayMap → JSON objectObject[] → JSON arraySerializable objects → Custom marshalling (if marshaller available)Usage Examples:
import org.jboss.errai.common.client.json.JSONEncoderCli;
import java.util.*;
JSONEncoderCli encoder = new JSONEncoderCli();
// Encode primitive values
String nullJson = encoder.encode(null); // "null"
String stringJson = encoder.encode("hello"); // "\"hello\""
String numberJson = encoder.encode(42); // "42"
String boolJson = encoder.encode(true); // "true"
// Encode collections
List<String> list = Arrays.asList("a", "b", "c");
String arrayJson = encoder.encode(list); // "[\"a\",\"b\",\"c\"]"
// Encode maps
Map<String, Object> map = new HashMap<>();
map.put("name", "John");
map.put("age", 30);
String objectJson = encoder.encode(map); // "{\"name\":\"John\",\"age\":30}"
// Check marshalled types
Map<String, String> marshalledTypes = encoder.getMarshalledTypes();Client-side JSON decoder with support for custom demarshalling and type restoration.
/**
* Client-side JSON decoder with support for custom demarshalling
*/
public class JSONDecoderCli {
/**
* Decode JSON string to object
* @param value - JSON string to decode
* @return Decoded object (Map for objects, List for arrays, primitives for values)
*/
public Object decode(Object value);
}Decoding Behavior:
StringDoubleBooleannullMap<String, Object>List<Object>__EncodedType → Custom demarshalling (if demarshaller available)Usage Examples:
import org.jboss.errai.common.client.json.JSONDecoderCli;
JSONDecoderCli decoder = new JSONDecoderCli();
// Decode primitive values
Object nullObj = decoder.decode("null"); // null
Object stringObj = decoder.decode("\"hello\""); // "hello"
Object numberObj = decoder.decode("42"); // 42.0 (Double)
Object boolObj = decoder.decode("true"); // true
// Decode arrays
Object arrayObj = decoder.decode("[1, 2, 3]"); // List<Object> containing [1.0, 2.0, 3.0]
// Decode objects
Object objectObj = decoder.decode("{\"name\":\"John\",\"age\":30}");
// Map<String, Object> containing {"name": "John", "age": 30.0}The JSON encoder integrates with the marshalling system to handle Serializable objects.
Custom Marshalling Process:
Serializable object, the encoder checks TypeMarshallers.hasMarshaller()TypeMarshallers.getMarshaller().marshall()getMarshalledTypes()Custom Demarshalling Process:
__EncodedType property, the decoder extracts the class nameTypeDemarshallers.hasDemarshaller() for the classTypeDemarshallers.getDemarshaller().demarshall()RuntimeExceptionUsage Example:
import org.jboss.errai.common.client.types.*;
import org.jboss.errai.common.client.json.*;
import java.io.Serializable;
// Custom class
public class Person implements Serializable {
public String name;
public int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
}
// Custom marshaller
public class PersonMarshaller implements Marshaller<Person> {
public String marshall(Person person) {
return "{\"name\":\"" + person.name + "\",\"age\":" + person.age +
",\"__EncodedType\":\"Person\"}";
}
}
// Custom demarshaller
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);
}
}
// Registration and usage
TypeMarshallers.addMarshaller(Person.class, new PersonMarshaller());
TypeDemarshallers.addDemarshaller(Person.class, new PersonDemarshaller());
JSONEncoderCli encoder = new JSONEncoderCli();
JSONDecoderCli decoder = new JSONDecoderCli();
Person person = new Person("Alice", 25);
String json = encoder.encode(person);
Person decoded = (Person) decoder.decode(json);Encoding Errors:
RuntimeException thrown for objects without available marshallersDecoding Errors:
RuntimeException thrown for unknown JSON encodingsRuntimeException thrown for missing demarshallers with detailed error messageExample Error Handling:
import org.jboss.errai.common.client.json.*;
JSONEncoderCli encoder = new JSONEncoderCli();
JSONDecoderCli decoder = new JSONDecoderCli();
try {
// This will throw RuntimeException if no marshaller is registered
String json = encoder.encode(new UnknownSerializableClass());
} catch (RuntimeException e) {
// Handle marshalling error
System.err.println("Marshalling failed: " + e.getMessage());
}
try {
// This will throw RuntimeException if encoded type has no demarshaller
Object obj = decoder.decode("{\"__EncodedType\":\"UnknownClass\"}");
} catch (RuntimeException e) {
// Handle demarshalling error
System.err.println("Demarshalling failed: " + e.getMessage());
}The JSON processing classes integrate seamlessly with the type conversion system:
import org.jboss.errai.common.client.types.JSONTypeHelper;
import com.google.gwt.json.client.JSONParser;
// Convert JSON to typed objects using type handlers
JSONValue jsonValue = JSONParser.parseStrict("[1, 2, 3]");
List<Integer> typedList = JSONTypeHelper.convert(jsonValue, List.class);
// Use JSON helper for encoding
String encoded = JSONTypeHelper.encodeHelper("text"); // "\"text\""Install with Tessl CLI
npx tessl i tessl/maven-org-jboss-errai--errai-common