Skip to content

Commit

Permalink
Add parsson implementation of API
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Widdis <[email protected]>
  • Loading branch information
dbwiddis committed Apr 3, 2024
1 parent f48cd92 commit f6a6501
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 21 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ dependencies {
implementation "jakarta.json:jakarta.json-api:2.0.1"
implementation "jakarta.json.bind:jakarta.json.bind-api:2.0.0"
implementation ("org.eclipse:yasson:2.0.2")
implementation ("org.eclipse.parsson:parsson:1.1.5")


// ZipArchive dependencies used for integration tests
Expand Down
34 changes: 13 additions & 21 deletions src/main/java/org/opensearch/flowframework/util/ParseUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,6 @@
*/
package org.opensearch.flowframework.util;

import com.fasterxml.jackson.core.JsonProcessingException;

import jakarta.json.bind.Jsonb;
import jakarta.json.bind.JsonbBuilder;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.client.Client;
Expand Down Expand Up @@ -48,6 +43,9 @@
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import jakarta.json.bind.Jsonb;
import jakarta.json.bind.JsonbBuilder;

import static org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken;
import static org.opensearch.flowframework.common.CommonValue.PARAMETERS_FIELD;
import static org.opensearch.flowframework.common.WorkflowResources.MODEL_ID;
Expand All @@ -59,14 +57,11 @@ public class ParseUtils {
private static final Logger logger = LogManager.getLogger(ParseUtils.class);

// Matches ${{ foo.bar }} (whitespace optional) with capturing groups 1=foo, 2=bar
// private static final Pattern SUBSTITUTION_PATTERN = Pattern.compile("\\$\\{\\{\\s*(.+)\\.(.+?)\\s*\\}\\}");
private static final Pattern SUBSTITUTION_PATTERN = Pattern.compile("\\$\\{\\{\\s*([\\w_]+)\\.([\\w_]+)\\s*\\}\\}");
private static final Pattern JSON_ARRAY_DOUBLE_QUOTES_PATTERN = Pattern.compile("\"\\[(.*?)]\"");

private ParseUtils() {}

private static final ObjectMapper mapper = new ObjectMapper();

/**
* Converts a JSON string into an XContentParser
*
Expand Down Expand Up @@ -416,17 +411,11 @@ public static Object conditionallySubstitute(Object value, Map<String, WorkflowD
*
* @param map content map
* @return instance of the string
* @throws JsonProcessingException JsonProcessingException from Jackson for issues processing map
* @throws Exception for issues processing map
*/
public static String parseArbitraryStringToObjectMapToString(Map<String, Object> map) throws Exception {
Jsonb jsonb = JsonbBuilder.create();
try {
// Convert the map to a JSON string
String jsonString = jsonb.toJson(map);
return jsonString;
} finally {
// Close the Jsonb instance
jsonb.close();
try (Jsonb jsonb = JsonbBuilder.create()) {
return jsonb.toJson(map);
}
}

Expand All @@ -435,12 +424,15 @@ public static String parseArbitraryStringToObjectMapToString(Map<String, Object>
*
* @param path file path
* @return instance of the string
* @throws JsonProcessingException JsonProcessingException from Jackson for issues processing map
* @throws Exception for issues processing map
*/
public static Map<String, String> parseJsonFileToStringToStringMap(String path) throws IOException {
public static Map<String, String> parseJsonFileToStringToStringMap(String path) throws Exception {
String jsonContent = resourceToString(path);
Map<String, String> mappedJsonFile = mapper.readValue(jsonContent, Map.class);
return mappedJsonFile;
try (Jsonb jsonb = JsonbBuilder.create()) {
@SuppressWarnings("unchecked")
Map<String, String> resultMap = jsonb.fromJson(jsonContent, Map.class);
return resultMap;
}
}

/**
Expand Down

0 comments on commit f6a6501

Please sign in to comment.