Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport-2.x] Use jsonB instead of jackson (#586) #645

Merged
merged 2 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,9 @@ 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}")
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
Loading