Package lib.aide

Class JsonContentAction

java.lang.Object
lib.aide.JsonContentAction

public class JsonContentAction extends Object
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());
 
  • 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