Class InsertKeyTransformer
java.lang.Object
com.invirgance.convirgance.transform.InsertKeyTransformer
- All Implemented Interfaces:
IdentityTransformer,Transformer
A transformer that inserts or updates a key-value pair in a JSON object.
If the key already exists, its value will be replaced. If the key doesn't exist,
a new key-value pair will be added.
- Author:
- jbanes
-
Constructor Summary
ConstructorsConstructorDescriptionPrepares a new InsertKeyTransformer.InsertKeyTransformer(String key, Object value) Creates a new InsertKeyTransformer with the specified key-value pair. -
Method Summary
Modifier and TypeMethodDescriptiongetKey()Gets the key that will be inserted or updated.getValue()Gets the value that will be associated with the key.voidSets the key that will be inserted or updated.voidSets the value that will be associated with the key.transform(JSONObject record) Transforms a JSON object by inserting or updating the specified key-value pair.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface com.invirgance.convirgance.transform.IdentityTransformer
transformMethods inherited from interface com.invirgance.convirgance.transform.Transformer
transform
-
Constructor Details
-
InsertKeyTransformer
public InsertKeyTransformer()Prepares a new InsertKeyTransformer. ThesetKey(String)andsetValue(Object)methods must be called before attempting a transform. -
InsertKeyTransformer
Creates a new InsertKeyTransformer with the specified key-value pair.Example usage:
// Create transformer to add/update a "status" field InsertKeyTransformer transformer = new InsertKeyTransformer("status", "active"); // Input: {"id": 123, "name": "John"} // Output: {"id": 123, "name": "John", "status": "active"} // Input: {"id": 456, "status": "inactive", "name": "Jane"} // Output: {"id": 456, "status": "active", "name": "Jane"}- Parameters:
key- The key to insert or update.value- The value to associate with the key. Supports the use of aValueGenerator
-
-
Method Details
-
getKey
Gets the key that will be inserted or updated.- Returns:
- The key name.
- Throws:
NullPointerException- if the key has not been set.
-
setKey
Sets the key that will be inserted or updated.- Parameters:
key- The key name.
-
getValue
Gets the value that will be associated with the key.- Returns:
- The value to be inserted or used for update.
- Throws:
NullPointerException- if the value has not been set.
-
setValue
Sets the value that will be associated with the key. If the value is aValueGenerator, the result of the generator will be inserted rather than the generator itself.- Parameters:
value- The value to be inserted or used for update.
-
transform
Transforms a JSON object by inserting or updating the specified key-value pair. This operation modifies the input object directly.If the key already exists in the input object, its value will be replaced. If the key doesn't exist, a new key-value pair will be added to the object.
If the value is a
ValueGenerator, the result of generation will be set rather than the generator itself.- Specified by:
transformin interfaceIdentityTransformer- Parameters:
record- The JSON object to modify.- Returns:
- The modified JSON object (same instance as input).
- Throws:
ConvirganceException- if the key is null, or if there's an error during the modification operation.
-