Class JSON
The Gradle plugin generates and registers a codec for every type annotated
JSONType, so the common case needs nothing but the annotation:
@JSONType
public record Settings(String host, int port) {}
Settings settings = request.json(Settings.class);
Nothing here reflects. A reflective decoder would have to keep field metadata for every type
that could arrive through an Object-typed field, which is the whole-program closure dead
code elimination exists to prune — so the size of the binary would depend on how many types were
serialisable rather than on how many were used.
Request.json(java.util.function.Function) is the escape hatch for a
type with no codec, and costs nothing at build time.
- Since:
- 1.0.0
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> TReads a JavaScript value as a Java type.static <T> TSObjectWrites a Java value as a JavaScript value.static booleanWhether a type has a codec.static <T> TParses JSON text as a Java type.static <T> voidRegisters a codec.static <T> StringSerialises a Java value as JSON text.
-
Method Details
-
register
Registers a codec.Called from the generated registration class during module evaluation. Registering the same type twice replaces the earlier codec, so a hand-written one can override a generated one.
- Type Parameters:
T- the type- Parameters:
type- the typecodec- the codec
-
handles
Whether a type has a codec.- Parameters:
type- the type- Returns:
- whether it can be converted
-
decode
Reads a JavaScript value as a Java type.- Type Parameters:
T- the type- Parameters:
value- the JavaScript valuetype- the type- Returns:
- the Java value, or
nullwhen the JavaScript value was null
-
encode
Writes a Java value as a JavaScript value.- Type Parameters:
T- the type- Parameters:
value- the Java valuetype- the type- Returns:
- the JavaScript value
-
parse
Parses JSON text as a Java type.- Type Parameters:
T- the type- Parameters:
json- the JSONtype- the type- Returns:
- the Java value
-
stringify
Serialises a Java value as JSON text.- Type Parameters:
T- the type- Parameters:
value- the Java valuetype- the type- Returns:
- the JSON
-