Fluent builder for a nested map of string to object, used to assemble JSON-shaped documents such as Elasticsearch index bodies. Supports nested objects and arrays via startObject/endMapObject and startArray/endArrayObject, and typed field overloads for the common value types. It implements the standard map interface so a built instance can be used, iterated and serialised like any other map, and it is exported to GraalJS so app scripts can build documents the same way.
Group: Builders
Implements: Map
Properties
| Property | Returns | Description |
|---|---|---|
| empty | boolean | Whether this map currently has no fields. |
Methods
startObject(String name) · endMapObject() · startArray(String name) · endArrayObject() · field(String name, List value) · fieldObject(String name, Object value) · field(String name, String value) · field(String name, Boolean value) · fieldIfTrue(String name, Boolean value) · fieldIfTrue(String name, Boolean check, String value) · fieldIfNotNull(String name, Object value) · field(String name, Long value) · field(String name, Integer value) · field(String name, Date value) · field(String name, BigDecimal value) · field(String name, Double value) · field(String name, Set value) · field(String name, Object value) · isEmpty() · addData(String name, String json)
startObject(String name)
Returns: MapBuilder
Creates a new nested map, inserts it under the given key in this map, and returns the nested map so fields can be added to it. Call endMapObject() on the result to return to this map.
| Parameter | Description |
|---|---|
name | the key the nested map is inserted under |
endMapObject()
Returns: MapBuilder
Returns the parent map this builder was created from via startObject(), so field-building can continue on the enclosing map. Null if this builder was not created as a nested object.
startArray(String name)
Returns: ArrayBuilder
Creates a new array builder, inserts it under the given key in this map, and returns it so elements can be added. Call endArray() on the result to return to this map.
| Parameter | Description |
|---|---|
name | the key the array is inserted under |
endArrayObject()
Returns: ArrayBuilder
Returns the parent array this builder was created from via ArrayBuilder.startObject(), so element-adding can continue on the enclosing array. Null if this builder was not created as an array element.
field(String name, List value)
Returns: MapBuilder
Inserts a list value under the given key.
| Parameter | Description |
|---|---|
name | the key the value is inserted under |
value | the list to insert |
fieldObject(String name, Object value)
Returns: MapBuilder
Inserts an arbitrary value under the given key, with no type-specific handling.
| Parameter | Description |
|---|---|
name | the key the value is inserted under |
value | the value to insert |
field(String name, String value)
Returns: MapBuilder
Inserts a string value under the given key.
| Parameter | Description |
|---|---|
name | the key the value is inserted under |
value | the string to insert |
field(String name, Boolean value)
Returns: MapBuilder
Inserts a boolean value under the given key.
| Parameter | Description |
|---|---|
name | the key the value is inserted under |
value | the boolean to insert |
fieldIfTrue(String name, Boolean value)
Returns: MapBuilder
Inserts a boolean value under the given key, but only when the value is true. Nothing is inserted when the value is null or false.
| Parameter | Description |
|---|---|
name | the key the value would be inserted under |
value | the boolean to test and insert |
fieldIfTrue(String name, Boolean check, String value)
Returns: MapBuilder
Inserts a string value under the given key, but only when the given check flag is true. Nothing is inserted when the check is null or false.
| Parameter | Description |
|---|---|
name | the key the value would be inserted under |
check | the flag that determines whether the value is inserted |
value | the string to insert when the check passes |
fieldIfNotNull(String name, Object value)
Returns: MapBuilder
Inserts an arbitrary value under the given key, but only when the value is not null.
| Parameter | Description |
|---|---|
name | the key the value would be inserted under |
value | the value to insert if not null |
field(String name, Long value)
Returns: MapBuilder
Inserts a long value under the given key.
| Parameter | Description |
|---|---|
name | the key the value is inserted under |
value | the value to insert |
field(String name, Integer value)
Returns: MapBuilder
Inserts an integer value under the given key.
| Parameter | Description |
|---|---|
name | the key the value is inserted under |
value | the value to insert |
field(String name, Date value)
Returns: MapBuilder
Inserts a date value under the given key, stored as its epoch millisecond value to avoid timezone handling issues on read. Inserts a null value when the date is null.
| Parameter | Description |
|---|---|
name | the key the value is inserted under |
value | the date to insert |
field(String name, BigDecimal value)
Returns: MapBuilder
Inserts a decimal value under the given key.
| Parameter | Description |
|---|---|
name | the key the value is inserted under |
value | the value to insert |
field(String name, Double value)
Returns: MapBuilder
Inserts a double value under the given key.
| Parameter | Description |
|---|---|
name | the key the value is inserted under |
value | the value to insert |
field(String name, Set value)
Returns: MapBuilder
Inserts a set value under the given key.
| Parameter | Description |
|---|---|
name | the key the value is inserted under |
value | the set to insert |
field(String name, Object value)
Returns: MapBuilder
Inserts an arbitrary value under the given key, with no type-specific handling. Used as the fallback overload for value types that do not have a dedicated field() method.
| Parameter | Description |
|---|---|
name | the key the value is inserted under |
value | the value to insert |
isEmpty()
Returns: boolean
Whether this map currently has no fields.
addData(String name, String json)
Returns: boolean
Parses a JSON object string and inserts its fields as a single-element array under the given key. Intended for use from JS, where a caller already has data as a JSON string rather than a built map. Returns false and logs the parse failure to standard error instead of throwing when the JSON is invalid.
| Parameter | Description |
|---|---|
name | the key the array is inserted under |
json | a JSON object string whose fields become the fields of the single array element |