13
13
*/
14
14
package io .trino .type ;
15
15
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 ;
17
20
import com .fasterxml .jackson .databind .JsonNode ;
18
21
import com .fasterxml .jackson .databind .ObjectMapper ;
19
22
import io .airlift .slice .Slice ;
28
31
import io .trino .spi .type .StandardTypes ;
29
32
import io .trino .spi .type .TypeSignature ;
30
33
31
- import static io .airlift .slice .Slices .utf8Slice ;
34
+ import java .io .IOException ;
35
+
36
+ import static io .airlift .slice .Slices .wrappedBuffer ;
32
37
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 ;
33
40
34
41
public class Json2016Type
35
42
extends AbstractVariableWidthType
36
43
{
37
44
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
+ }
39
51
40
52
public Json2016Type ()
41
53
{
@@ -61,30 +73,32 @@ public Object getObject(Block block, int position)
61
73
if (json .equals (JSON_ERROR .toString ())) {
62
74
return JSON_ERROR ;
63
75
}
64
- try {
65
- return MAPPER . readTree ( json );
76
+ try ( JsonParser parser = JSON_FACTORY . createParser ( json )) {
77
+ return parser . readValueAsTree ( );
66
78
}
67
- catch (JsonProcessingException e ) {
79
+ catch (IOException e ) {
68
80
throw new JsonInputConversionException (e );
69
81
}
70
82
}
71
83
72
84
@ Override
73
85
public void writeObject (BlockBuilder blockBuilder , Object value )
74
86
{
75
- String json ;
87
+ byte [] json ;
76
88
if (value == JSON_ERROR ) {
77
- json = JSON_ERROR .toString ();
89
+ json = JSON_ERROR .toString (). getBytes ( UTF_8 ) ;
78
90
}
79
91
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 );
82
95
}
83
- catch (JsonProcessingException e ) {
96
+ catch (IOException e ) {
84
97
throw new JsonOutputConversionException (e );
85
98
}
99
+ json = builder .toByteArray ();
86
100
}
87
- Slice bytes = utf8Slice (json );
101
+ Slice bytes = wrappedBuffer (json );
88
102
((VariableWidthBlockBuilder ) blockBuilder ).writeEntry (bytes );
89
103
}
90
104
}
0 commit comments