Skip to content

Commit edaa7ed

Browse files
committed
minor objectMapper unification
1 parent e2a2268 commit edaa7ed

File tree

2 files changed

+7
-10
lines changed

2 files changed

+7
-10
lines changed

core/src/main/java/com/github/andreaTP/opa/chicory/Opa.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.fasterxml.jackson.core.type.TypeReference;
66
import com.fasterxml.jackson.databind.JsonNode;
77
import com.fasterxml.jackson.databind.ObjectMapper;
8+
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
89
import java.io.ByteArrayInputStream;
910
import java.io.File;
1011
import java.io.FileInputStream;
@@ -19,9 +20,9 @@
1920
// directly porting:
2021
// https://github.com/open-policy-agent/npm-opa-wasm/blob/main/README.md
2122
public class Opa {
22-
23-
// TODO: pass around a concrete instance instead of using a global one
23+
// TODO: pass around a concrete instance instead of using a global
2424
public static ObjectMapper jsonMapper = new ObjectMapper();
25+
public static ObjectMapper yamlMapper = new ObjectMapper(new YAMLFactory());
2526

2627
public static class OpaPolicy {
2728
private final OpaWasm wasm;

core/src/main/java/com/github/andreaTP/opa/chicory/builtins/Yaml.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,19 @@
22

33
import com.fasterxml.jackson.core.JsonProcessingException;
44
import com.fasterxml.jackson.databind.JsonNode;
5-
import com.fasterxml.jackson.databind.ObjectMapper;
65
import com.fasterxml.jackson.databind.node.BooleanNode;
76
import com.fasterxml.jackson.databind.node.TextNode;
8-
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
7+
import com.github.andreaTP.opa.chicory.Opa;
98
import com.github.andreaTP.opa.chicory.OpaBuiltin;
109

1110
public class Yaml {
12-
public static ObjectMapper yamlMapper = new ObjectMapper(new YAMLFactory());
13-
14-
// maybe: .disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER));
1511

1612
private static JsonNode isValidImpl(JsonNode boxedYaml) {
1713
if (!boxedYaml.isTextual()) {
1814
return BooleanNode.getFalse();
1915
} else {
2016
try {
21-
yamlMapper.readTree(boxedYaml.asText());
17+
Opa.yamlMapper.readTree(boxedYaml.asText());
2218
return BooleanNode.getTrue();
2319
} catch (JsonProcessingException e) {
2420
return BooleanNode.getFalse();
@@ -34,7 +30,7 @@ private static JsonNode unmarshalImpl(JsonNode boxedYaml) {
3430
throw new RuntimeException("yaml is not correctly boxed in a Json string");
3531
} else {
3632
try {
37-
return yamlMapper.readTree(boxedYaml.asText());
33+
return Opa.yamlMapper.readTree(boxedYaml.asText());
3834
} catch (JsonProcessingException e) {
3935
// should ignore errors here ...
4036
return BooleanNode.getFalse();
@@ -47,7 +43,7 @@ private static JsonNode unmarshalImpl(JsonNode boxedYaml) {
4743

4844
public static JsonNode marshalImpl(JsonNode json) {
4945
try {
50-
return TextNode.valueOf(yamlMapper.writeValueAsString(json));
46+
return TextNode.valueOf(Opa.yamlMapper.writeValueAsString(json));
5147
} catch (JsonProcessingException e) {
5248
throw new RuntimeException(e);
5349
}

0 commit comments

Comments
 (0)