Package org.eclipse.ditto.json
Interface JsonObjectBuilder
- All Superinterfaces:
Iterable<JsonField>
,JsonValueContainer<JsonField>
A mutable builder for a
JsonObject
. Implementations of this interface are normally not thread safe and not
reusable.
The order in which the key-value-pairs are set is preserved in the resulting JSON object.-
Method Summary
Modifier and TypeMethodDescriptionbuild()
Creates a newJsonObject
containing all values which were added beforehand.remove
(CharSequence key) Removes the field from the JSON object to be built which is associated with the given key.remove
(JsonFieldDefinition<?> fieldDefinition) Removes the field from the JSON object to be built which is associated with the pointer of the specified JSON field definition.Removes all fields from the JSON object to be built.default JsonObjectBuilder
set
(CharSequence key, boolean value) Sets a new boolean value field to the JSON object to be built.set
(CharSequence key, boolean value, Predicate<JsonField> predicate) Sets a new boolean value field to the JSON object to be built if the specified predicate evaluates totrue
.default JsonObjectBuilder
set
(CharSequence key, double value) Sets a new double value field to the JSON object to be built.set
(CharSequence key, double value, Predicate<JsonField> predicate) Sets a new double value field to the JSON object to be built if the specified predicate evaluates totrue
.default JsonObjectBuilder
set
(CharSequence key, int value) Sets a new int value field to the JSON object to be built.set
(CharSequence key, int value, Predicate<JsonField> predicate) Sets a new int value field to the JSON object to be built if the specified predicate evaluates totrue
.default JsonObjectBuilder
set
(CharSequence key, long value) Sets a new long value field to the JSON object to be built.set
(CharSequence key, long value, Predicate<JsonField> predicate) Sets a new long value field to the JSON object to be built if the specified predicate evaluates totrue
.default JsonObjectBuilder
set
(CharSequence key, String value) Sets a new string value field to the JSON object to be built.set
(CharSequence key, String value, Predicate<JsonField> predicate) Sets a new string value field to the JSON object to be built if the specified predicate evaluates totrue
.default JsonObjectBuilder
set
(CharSequence key, JsonValue value) Sets a newJsonValue
field to the JSON object to be built.set
(CharSequence key, JsonValue value, Predicate<JsonField> predicate) Sets a newJsonValue
field to the JSON object to be built if the specified predicate evaluates totrue
.default JsonObjectBuilder
Sets the specified field to the JSON object to be built.default <T> JsonObjectBuilder
set
(JsonFieldDefinition<T> fieldDefinition, T value) Sets a new field to the JSON object to be built.set
(JsonFieldDefinition<T> fieldDefinition, T value, Predicate<JsonField> predicate) Sets a new field to the JSON object to be built if the specified predicate evaluates totrue
.Sets the specified field to the JSON object to be built if the specified predicate evaluates totrue
.default JsonObjectBuilder
Sets the givenJsonField
s to the JSON object to be built.Sets the givenJsonField
s to the JSON object to be built.Methods inherited from interface java.lang.Iterable
forEach, iterator, spliterator
Methods inherited from interface org.eclipse.ditto.json.JsonValueContainer
getSize, isEmpty, stream
-
Method Details
-
set
Sets a new int value field to the JSON object to be built if the specified predicate evaluates totrue
.- Parameters:
key
- the JSON key or JSON pointer of the field to be set.value
- the value to be set.predicate
- the predicate which finally determines if the value is to be set.- Returns:
- this builder to allow method chaining.
- Throws:
NullPointerException
- ifkeyOrPointer
orpredicate
isnull
.IllegalArgumentException
- ifkey
is empty or ifkey
is an empty JsonPointer. Setting a value with slash as JsonKey object explicitly works.
-
set
Sets a new int value field to the JSON object to be built.- Parameters:
key
- the JSON key or JSON pointer of the field to be set.value
- the value to be set.- Returns:
- this builder to allow method chaining.
- Throws:
NullPointerException
- ifkey
isnull
.IllegalArgumentException
- ifkey
is empty or ifkey
is an empty JsonPointer. Setting a value with slash as JsonKey object explicitly works.
-
set
Sets a new long value field to the JSON object to be built if the specified predicate evaluates totrue
.- Parameters:
key
- the JSON key or JSON pointer of the field to be set.value
- the value to be set.predicate
- the predicate which finally determines if the value is to be set.- Returns:
- this builder to allow method chaining.
- Throws:
NullPointerException
- ifkey
orpredicate
isnull
.IllegalArgumentException
- ifkey
is empty or ifkey
is an empty JsonPointer. Setting a value with slash as JsonKey object explicitly works.
-
set
Sets a new long value field to the JSON object to be built.- Parameters:
key
- the JSON key or JSON pointer of the field to be set.value
- the value to be set.- Returns:
- this builder to allow method chaining.
- Throws:
NullPointerException
- ifkey
isnull
.IllegalArgumentException
- ifkey
is empty or ifkey
is an empty JsonPointer. Setting a value with slash as JsonKey object explicitly works.
-
set
Sets a new double value field to the JSON object to be built if the specified predicate evaluates totrue
.- Parameters:
key
- the JSON key or JSON pointer of the field to be set.value
- the value to be set.predicate
- the predicate which finally determines if the field is to be set.- Returns:
- this builder to allow method chaining.
- Throws:
NullPointerException
- ifkey
orpredicate
isnull
.IllegalArgumentException
- ifkey
is empty or ifkey
is an empty JsonPointer. Setting a value with slash as JsonKey object explicitly works.
-
set
Sets a new double value field to the JSON object to be built.- Parameters:
key
- the JSON key or JSON pointer of the field to be set.value
- the value to be set.- Returns:
- this builder to allow method chaining.
- Throws:
NullPointerException
- ifkey
isnull
.IllegalArgumentException
- ifkey
is empty or ifkey
is an empty JsonPointer. Setting a value with slash as JsonKey object explicitly works.
-
set
Sets a new boolean value field to the JSON object to be built if the specified predicate evaluates totrue
.- Parameters:
key
- the JSON key or JSON pointer of the field to be set.value
- the value to be set.predicate
- the predicate which finally determines if the value is to be set.- Returns:
- this builder to allow method chaining.
- Throws:
NullPointerException
- ifkey
orpredicate
isnull
.IllegalArgumentException
- ifkey
is empty or ifkey
is an empty JsonPointer. Setting a value with slash as JsonKey object explicitly works.
-
set
Sets a new boolean value field to the JSON object to be built.- Parameters:
key
- the JSON key or JSON pointer of the field to be set.value
- the value to be set.- Returns:
- this builder to allow method chaining.
- Throws:
NullPointerException
- ifkey
isnull
.IllegalArgumentException
- ifkey
is empty or ifkey
is an empty JsonPointer. Setting a value with slash as JsonKey object explicitly works.
-
set
Sets a new string value field to the JSON object to be built if the specified predicate evaluates totrue
.- Parameters:
key
- the JSON key or JSON pointer of the field to be set.value
- the value to be set.predicate
- the predicate which finally determines if the field is to be set.- Returns:
- this builder to allow method chaining.
- Throws:
NullPointerException
- ifkey
orpredicate
isnull
.IllegalArgumentException
- ifkey
is empty or ifkey
is an empty JsonPointer. Setting a value with slash as JsonKey object explicitly works.
-
set
Sets a new string value field to the JSON object to be built.- Parameters:
key
- the JSON key or JSON pointer of the field to be set.value
- the value to be set.- Returns:
- this builder to allow method chaining.
- Throws:
NullPointerException
- ifkey
isnull
.IllegalArgumentException
- ifkey
is empty or ifkey
is an empty JsonPointer. Setting a value with slash as JsonKey object explicitly works.
-
set
Sets a newJsonValue
field to the JSON object to be built if the specified predicate evaluates totrue
.- Parameters:
key
- the JSON key or JSON pointer of the field to be set.value
- the value to be set.predicate
- the predicate which finally determines if the field is to be set.- Returns:
- this builder to allow method chaining.
- Throws:
NullPointerException
- if any argument isnull
; the predicate is evaluated before the null check of the value.IllegalArgumentException
- ifkey
is empty or ifkey
is an empty JsonPointer. Setting a value with slash as JsonKey object explicitly works.
-
set
Sets a newJsonValue
field to the JSON object to be built.- Parameters:
key
- the JSON key or JSON pointer of the field to be set.value
- the value of the field to be set.- Returns:
- this builder to allow method chaining.
- Throws:
NullPointerException
- if any argument isnull
.IllegalArgumentException
- ifkey
is empty or ifkey
is an empty JsonPointer. Setting a value with slash as JsonKey object explicitly works.
-
set
<T> JsonObjectBuilder set(JsonFieldDefinition<T> fieldDefinition, @Nullable T value, Predicate<JsonField> predicate) Sets a new field to the JSON object to be built if the specified predicate evaluates totrue
.- Type Parameters:
T
- the type ofvalue
.- Parameters:
fieldDefinition
- defines the location via JsonPointer, the Java value type and optional JsonFieldMarkers of the field to be set.value
- the value of the field to be set.predicate
- the predicate which finally determines if the field is to be set. The field was derived fromfieldDefinition
andvalue
.- Returns:
- this builder to allow method chaining.
- Throws:
NullPointerException
- iffieldDefinition
orpredicate
is null.IllegalArgumentException
- if the value type offieldDefinition
is not aint
.- See Also:
-
set
Sets a new field to the JSON object to be built.- Type Parameters:
T
- the type ofvalue
.- Parameters:
fieldDefinition
- defines the location via JsonPointer, the Java value type and optional JsonFieldMarkers of the field to be set.value
- the value of the field to be set.- Returns:
- this builder to allow method chaining.
- Throws:
NullPointerException
- iffieldDefinition
orpredicate
is null.IllegalArgumentException
- if the value type offieldDefinition
is not aint
.- See Also:
-
set
Sets the specified field to the JSON object to be built if the specified predicate evaluates totrue
. If this builder already contains a field with the same key, the existing one will be replaced.- Parameters:
field
- the field to be set.predicate
- the predicate which finally determines if the field is to be set.- Returns:
- this builder to allow method chaining.
- Throws:
NullPointerException
- if any argument isnull
.
-
set
Sets the specified field to the JSON object to be built. If this builder already contains a field with the same key, the existing one will be replaced.- Parameters:
field
- the field to be set.- Returns:
- this builder to allow method chaining.
- Throws:
NullPointerException
- iffield
isnull
.
-
remove
Removes the field from the JSON object to be built which is associated with the given key. This method has no effect if no such field exists.- Parameters:
key
- the key of the field to be removed.- Returns:
- this builder to allow method chaining.
- Throws:
NullPointerException
- ifkey
isnull
.
-
remove
Removes the field from the JSON object to be built which is associated with the pointer of the specified JSON field definition. This method has no effect if no such field exists.- Parameters:
fieldDefinition
- provides the JSON pointer of the field to be deleted.- Returns:
- this builder to allow method chaining.
- Throws:
NullPointerException
- iffieldDefinition
isnull
.IllegalArgumentException
- if the JSON pointer offieldDefinition
is empty.
-
setAll
Sets the givenJsonField
s to the JSON object to be built. This method prevents duplicates, i. e. if two fields have the same key, the previous field is replaced by the new field; the position of the new field in the resulting JSON object is the same as the position of the previous field. For each field applies that the field is only set if the specified predicate evaluates totrue
.- Parameters:
fields
- the fields to set.predicate
- the predicate which finally determines for each field if it is to be set.- Returns:
- this builder to allow method chaining.
- Throws:
NullPointerException
- if any argument isnull
.
-
setAll
Sets the givenJsonField
s to the JSON object to be built. This method prevents duplicates, i. e. if two fields have the same key, the previous field is replaced by the new field; the position of the new field in the resulting JSON object is the same as the position of the previous field.- Parameters:
fields
- the fields to set.- Returns:
- this builder to allow method chaining.
- Throws:
NullPointerException
- iffields
isnull
.
-
removeAll
JsonObjectBuilder removeAll()Removes all fields from the JSON object to be built.- Returns:
- this builder to allow method chaining.
-
build
JsonObject build()Creates a newJsonObject
containing all values which were added beforehand.- Returns:
- a new JSON object.
-