Package lib.aide
Class JsonContentAction
java.lang.Object
lib.aide.JsonContentAction
JsonContentAction provides a framework for defining rejection rules based on JSONPath
expressions and applying transformations to JSON objects using Spring SpEL expressions.
The class supports two main types of transformations: - Appending objects to arrays. - Applying key-value pairs to maps.
This is useful in scenarios where you need to reject or modify JSON content based on specific rules, like handling errors or modifying content dynamically.
The class uses a builder pattern to allow chaining multiple rejection rules, and each rule defines a JSONPath condition and a set of transformations to apply when the condition is met.
Example usage:
// Create JsonContentAction with reject rules
JsonContentAction jsonContentAction = new JsonContentAction.Builder()
.withReject(
".location[?(@[0] =~ /Bundle\\.entry\\[\\d+\\]\\.resource\\/Patient\\/.*\\.meta\\.lastUpdated$/)]",
List.of(new JsonContentAction.ApplyKeyValuePairs(
JsonPath.compile("$.rejectionsMap"), Map.of(
"'injectedKey' + '1'", "'Injected value for ' + rule.description() + ' ' + rule.elaboration.e1"))),
"test 1", Map.of("e1", "e1Value"))
.withReject(
".location[?(@[0] =~ /Bundle\\.entry\\[\\d+\\]\\.resource\\/Patient\\/.*\\.meta\\.lastUpdated$/)]",
List.of(new JsonContentAction.AppendObject(
JsonPath.compile("$.rejectionsList"), Map.of(
"'injectedKey' + '1'", "'Injected value for ' + rule.description() + ' ' + rule.elaboration.e1"))),
"test 2", Map.of("e1", "e1Value"))
.build();
// Execute the action on a sample JSON
Map<String, Object> json = Map.of(
"severity", "error",
"location", List.of("Bundle.entry[0].resource/Patient/12345/.meta.lastUpdated"),
"rejectionsList", List.of(),
"rejectionsMap", Map.of()
);
JsonContentAction.ExecuteResults result = jsonContentAction.execute(json);
// Assert that a rejection occurred and transformations were applied
Map<String, Object> transformedJson = result.rejections().get(0).transformed(new HashMap<>(json), Map.of());
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic final record
AcceptContent represents a case where no rejection occurred and the original JSON is returned.static interface
static final record
AppendObject assumes thatinjectInJsonPath
is an array and appends the evaluated SpEL expressions to that array.static final record
ApplyKeyValuePairs assumes thatinjectInJsonPath
is a map and applies the evaluated SpEL expressions as key-value pairs.static class
Builder pattern for creatingJsonContentAction
instances.static interface
static final record
ExecuteResults stores the results of applying the rejection rules, including any rejections and the content that was accepted without rejection.static interface
static final record
RejectActionRule defines a rejection rule with a JSONPath condition and a list of transformations to apply if the condition is met.static final record
RejectContent represents a case where a rejection occurred and the JSON was transformed. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionExecutes the rejection rules on the provided JSON and returns the results.
-
Constructor Details
-
JsonContentAction
public JsonContentAction()
-
-
Method Details
-
execute
Executes the rejection rules on the provided JSON and returns the results.- Parameters:
json
- the JSON object to be processed- Returns:
- the results of the execution, including any rejections and accepted content
-