CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-jboss-errai--errai-common

Common utilities and foundational infrastructure for the Errai GWT-based framework

Pending
Overview
Eval results
Files

json-processing.mddocs/

JSON Processing

Client-side JSON encoding and decoding with support for custom marshalling, type-aware conversion, and integration with the Errai type system.

Capabilities

JSON Encoder

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 string
  • Number, Boolean → Direct JSON representation
  • Collection → JSON array
  • Map → JSON object
  • Object[] → JSON array
  • Serializable 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();

JSON Decoder

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:

  • JSON strings → Java String
  • JSON numbers → Java Double
  • JSON booleans → Java Boolean
  • JSON null → Java null
  • JSON objects → Java Map<String, Object>
  • JSON arrays → Java List<Object>
  • Objects with __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}

Custom Marshalling Integration

The JSON encoder integrates with the marshalling system to handle Serializable objects.

Custom Marshalling Process:

  1. When encoding a Serializable object, the encoder checks TypeMarshallers.hasMarshaller()
  2. If a marshaller exists, it uses TypeMarshallers.getMarshaller().marshall()
  3. The marshalled result is included in JSON output
  4. Type metadata is tracked in getMarshalledTypes()

Custom Demarshalling Process:

  1. When decoding objects with __EncodedType property, the decoder extracts the class name
  2. It checks TypeDemarshallers.hasDemarshaller() for the class
  3. If a demarshaller exists, it uses TypeDemarshallers.getDemarshaller().demarshall()
  4. Otherwise, it throws a RuntimeException

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

Error Handling

Encoding Errors:

  • RuntimeException thrown for objects without available marshallers
  • Non-serializable objects cause encoding to defer (return null)

Decoding Errors:

  • RuntimeException thrown for unknown JSON encodings
  • RuntimeException thrown for missing demarshallers with detailed error message
  • GWT.log() used for debugging missing demarshallers

Example 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());
}

Integration with Type System

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

docs

framework-integration.md

index.md

json-processing.md

marshalling.md

type-conversion.md

tile.json