Skip to content

Commit

Permalink
Use jsonB instead of jackson (opensearch-project#586)
Browse files Browse the repository at this point in the history
* Use jsonB instead of jackson

Signed-off-by: Owais Kazi <[email protected]>

* Resolve Jar Hell with newest versions and jakarta dependencies

Signed-off-by: Daniel Widdis <[email protected]>

---------

Signed-off-by: Owais Kazi <[email protected]>
Signed-off-by: Daniel Widdis <[email protected]>
Co-authored-by: Daniel Widdis <[email protected]>
  • Loading branch information
owaiskazi19 and dbwiddis committed Apr 4, 2024
1 parent c71020a commit 97448ac
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
6 changes: 4 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,10 @@ dependencies {
implementation "org.opensearch:common-utils:${common_utils_version}"
implementation 'com.amazonaws:aws-encryption-sdk-java:2.4.1'
implementation 'org.bouncycastle:bcprov-jdk18on:1.77'
implementation("com.fasterxml.jackson.core:jackson-annotations:${versions.jackson}")
implementation("com.fasterxml.jackson.core:jackson-databind:${versions.jackson_databind}")
api "org.apache.httpcomponents.core5:httpcore5:5.2.2"
implementation "jakarta.json.bind:jakarta.json.bind-api:3.0.0"
implementation "org.glassfish:jakarta.json:2.0.1"
implementation "org.eclipse:yasson:3.0.3"

// ZipArchive dependencies used for integration tests
zipArchive group: 'org.opensearch.plugin', name:'opensearch-ml-plugin', version: "${opensearch_build}"
Expand Down
30 changes: 15 additions & 15 deletions src/main/java/org/opensearch/flowframework/util/ParseUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
*/
package org.opensearch.flowframework.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.opensearch.client.Client;
Expand Down Expand Up @@ -46,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 @@ -57,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 @@ -414,25 +411,28 @@ 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 JsonProcessingException {
// Convert the map to a JSON string
String mappedString = mapper.writeValueAsString(map);
return mappedString;
public static String parseArbitraryStringToObjectMapToString(Map<String, Object> map) throws Exception {
try (Jsonb jsonb = JsonbBuilder.create()) {
return jsonb.toJson(map);
}
}

/**
* Generates a String to String map based on a Json File
*
* @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
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public void testBuildAndParseStringToStringMap() throws IOException {
assertEquals(stringMap.get("one"), parsedMap.get("one"));
}

public void testParseArbitraryStringToObjectMapToString() throws IOException {
public void testParseArbitraryStringToObjectMapToString() throws Exception {
Map<String, Object> map = Map.ofEntries(Map.entry("test-1", Map.of("test-1", "test-1")));
String parsedMap = ParseUtils.parseArbitraryStringToObjectMapToString(map);
assertEquals("{\"test-1\":{\"test-1\":\"test-1\"}}", parsedMap);
Expand Down

0 comments on commit 97448ac

Please sign in to comment.