Skip to content

Commit 0ab046d

Browse files
committed
Do not use ObjectMapper directly
It doesn't have the configuration that we need
1 parent e47f1e6 commit 0ab046d

File tree

3 files changed

+49
-18
lines changed

3 files changed

+49
-18
lines changed

core/trino-main/src/main/java/io/trino/operator/scalar/json/JsonInputFunctions.java

+12-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
*/
1414
package io.trino.operator.scalar.json;
1515

16+
import com.fasterxml.jackson.core.JsonFactory;
17+
import com.fasterxml.jackson.core.JsonParser;
1618
import com.fasterxml.jackson.core.JsonProcessingException;
1719
import com.fasterxml.jackson.databind.JsonNode;
1820
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -29,6 +31,7 @@
2931

3032
import static io.trino.json.JsonInputErrorNode.JSON_ERROR;
3133
import static io.trino.spi.StandardErrorCode.GENERIC_INTERNAL_ERROR;
34+
import static io.trino.util.JsonUtil.createJsonFactory;
3235
import static java.nio.charset.StandardCharsets.UTF_16LE;
3336
import static java.nio.charset.StandardCharsets.UTF_8;
3437

@@ -56,7 +59,13 @@ public final class JsonInputFunctions
5659
public static final String VARBINARY_UTF16_TO_JSON = "$varbinary_utf16_to_json";
5760
public static final String VARBINARY_UTF32_TO_JSON = "$varbinary_utf32_to_json";
5861

59-
private static final ObjectMapper MAPPER = new ObjectMapper();
62+
private static final JsonFactory JSON_FACTORY = createJsonFactory();
63+
64+
static {
65+
// Changes factory. Necessary for JsonParser.readValueAsTree to work.
66+
new ObjectMapper(JSON_FACTORY);
67+
}
68+
6069
private static final Charset UTF_32LE = Charset.forName("UTF-32LE");
6170

6271
private JsonInputFunctions() {}
@@ -102,8 +111,8 @@ public static JsonNode varbinaryUtf32ToJson(@SqlType(StandardTypes.VARBINARY) Sl
102111

103112
private static JsonNode toJson(Reader reader, boolean failOnError)
104113
{
105-
try {
106-
return MAPPER.readTree(reader);
114+
try (JsonParser parser = JSON_FACTORY.createParser(reader)) {
115+
return parser.readValueAsTree();
107116
}
108117
catch (JsonProcessingException e) {
109118
if (failOnError) {

core/trino-main/src/main/java/io/trino/operator/scalar/json/JsonOutputFunctions.java

+11-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
package io.trino.operator.scalar.json;
1515

1616
import com.fasterxml.jackson.core.JsonEncoding;
17+
import com.fasterxml.jackson.core.JsonFactory;
1718
import com.fasterxml.jackson.core.JsonGenerator;
1819
import com.fasterxml.jackson.core.JsonProcessingException;
1920
import com.fasterxml.jackson.core.util.ByteArrayBuilder;
@@ -40,6 +41,7 @@
4041
import static io.trino.sql.tree.JsonQuery.EmptyOrErrorBehavior.EMPTY_OBJECT;
4142
import static io.trino.sql.tree.JsonQuery.EmptyOrErrorBehavior.ERROR;
4243
import static io.trino.sql.tree.JsonQuery.EmptyOrErrorBehavior.NULL;
44+
import static io.trino.util.JsonUtil.createJsonFactory;
4345
import static java.util.Objects.requireNonNull;
4446

4547
/**
@@ -60,7 +62,13 @@ public final class JsonOutputFunctions
6062
public static final String JSON_TO_VARBINARY_UTF16 = "$json_to_varbinary_utf16";
6163
public static final String JSON_TO_VARBINARY_UTF32 = "$json_to_varbinary_utf32";
6264

63-
private static final ObjectMapper MAPPER = new ObjectMapper();
65+
private static final JsonFactory JSON_FACTORY = createJsonFactory();
66+
67+
static {
68+
// Changes factory. Necessary for JsonParser.readValueAsTree to work.
69+
new ObjectMapper(JSON_FACTORY);
70+
}
71+
6472
private static final EncodingSpecificConstants UTF_8 = new EncodingSpecificConstants(
6573
JsonEncoding.UTF8,
6674
StandardCharsets.UTF_8,
@@ -126,8 +134,8 @@ private static Slice serialize(JsonNode json, EncodingSpecificConstants constant
126134
}
127135

128136
ByteArrayBuilder builder = new ByteArrayBuilder();
129-
try (JsonGenerator generator = MAPPER.createGenerator(builder, constants.jsonEncoding)) {
130-
MAPPER.writeTree(generator, json);
137+
try (JsonGenerator generator = JSON_FACTORY.createGenerator(builder, constants.jsonEncoding)) {
138+
generator.writeTree(json);
131139
}
132140
catch (JsonProcessingException e) {
133141
if (errorBehavior == NULL.ordinal()) {

core/trino-main/src/main/java/io/trino/type/Json2016Type.java

+26-12
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
*/
1414
package io.trino.type;
1515

16-
import com.fasterxml.jackson.core.JsonProcessingException;
16+
import com.fasterxml.jackson.core.JsonFactory;
17+
import com.fasterxml.jackson.core.JsonGenerator;
18+
import com.fasterxml.jackson.core.JsonParser;
19+
import com.fasterxml.jackson.core.util.ByteArrayBuilder;
1720
import com.fasterxml.jackson.databind.JsonNode;
1821
import com.fasterxml.jackson.databind.ObjectMapper;
1922
import io.airlift.slice.Slice;
@@ -28,14 +31,23 @@
2831
import io.trino.spi.type.StandardTypes;
2932
import io.trino.spi.type.TypeSignature;
3033

31-
import static io.airlift.slice.Slices.utf8Slice;
34+
import java.io.IOException;
35+
36+
import static io.airlift.slice.Slices.wrappedBuffer;
3237
import static io.trino.json.JsonInputErrorNode.JSON_ERROR;
38+
import static io.trino.util.JsonUtil.createJsonFactory;
39+
import static java.nio.charset.StandardCharsets.UTF_8;
3340

3441
public class Json2016Type
3542
extends AbstractVariableWidthType
3643
{
3744
public static final Json2016Type JSON_2016 = new Json2016Type();
38-
private static final ObjectMapper MAPPER = new ObjectMapper();
45+
private static final JsonFactory JSON_FACTORY = createJsonFactory();
46+
47+
static {
48+
// Changes factory. Necessary for JsonParser.readValueAsTree to work.
49+
new ObjectMapper(JSON_FACTORY);
50+
}
3951

4052
public Json2016Type()
4153
{
@@ -61,30 +73,32 @@ public Object getObject(Block block, int position)
6173
if (json.equals(JSON_ERROR.toString())) {
6274
return JSON_ERROR;
6375
}
64-
try {
65-
return MAPPER.readTree(json);
76+
try (JsonParser parser = JSON_FACTORY.createParser(json)) {
77+
return parser.readValueAsTree();
6678
}
67-
catch (JsonProcessingException e) {
79+
catch (IOException e) {
6880
throw new JsonInputConversionException(e);
6981
}
7082
}
7183

7284
@Override
7385
public void writeObject(BlockBuilder blockBuilder, Object value)
7486
{
75-
String json;
87+
byte[] json;
7688
if (value == JSON_ERROR) {
77-
json = JSON_ERROR.toString();
89+
json = JSON_ERROR.toString().getBytes(UTF_8);
7890
}
7991
else {
80-
try {
81-
json = MAPPER.writeValueAsString(value);
92+
ByteArrayBuilder builder = new ByteArrayBuilder();
93+
try (JsonGenerator generator = JSON_FACTORY.createGenerator(builder)) {
94+
generator.writeObject(value);
8295
}
83-
catch (JsonProcessingException e) {
96+
catch (IOException e) {
8497
throw new JsonOutputConversionException(e);
8598
}
99+
json = builder.toByteArray();
86100
}
87-
Slice bytes = utf8Slice(json);
101+
Slice bytes = wrappedBuffer(json);
88102
((VariableWidthBlockBuilder) blockBuilder).writeEntry(bytes);
89103
}
90104
}

0 commit comments

Comments
 (0)