|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch B.V. under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch B.V. licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package co.elastic.clients.json.jackson; |
| 21 | + |
| 22 | +import co.elastic.clients.json.JsonBuffer; |
| 23 | +import co.elastic.clients.json.JsonData; |
| 24 | +import co.elastic.clients.json.JsonpDeserializer; |
| 25 | +import co.elastic.clients.json.JsonpMapper; |
| 26 | +import co.elastic.clients.json.JsonpUtils; |
| 27 | + |
| 28 | +import jakarta.json.JsonValue; |
| 29 | +import jakarta.json.stream.JsonGenerator; |
| 30 | +import jakarta.json.stream.JsonParser; |
| 31 | +import tools.jackson.core.JacksonException; |
| 32 | +import tools.jackson.databind.util.TokenBuffer; |
| 33 | + |
| 34 | +import java.io.StringWriter; |
| 35 | +import java.lang.reflect.Type; |
| 36 | + |
| 37 | +class Jackson3JsonBuffer implements JsonBuffer, JsonData { |
| 38 | + private final TokenBuffer buffer; |
| 39 | + private final Jackson3JsonpMapper mapper; |
| 40 | + |
| 41 | + Jackson3JsonBuffer(TokenBuffer buffer, Jackson3JsonpMapper mapper) { |
| 42 | + this.buffer = buffer; |
| 43 | + this.mapper = mapper; |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public JsonParser asParser() { |
| 48 | + return new Jackson3JsonpParser(buffer.asParser(), mapper); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public JsonValue toJson() { |
| 53 | + try (JsonParser parser = asParser()) { |
| 54 | + parser.next(); // move to first event |
| 55 | + return parser.getValue(); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public JsonValue toJson(JsonpMapper mapper) { |
| 61 | + // We don't need the mapper |
| 62 | + return toJson(); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public <T> T to(Type type) { |
| 67 | + return to(type, this.mapper); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public <T> T to(Type type, JsonpMapper mapper) { |
| 72 | + try (JsonParser parser = asParser()) { |
| 73 | + return mapper.deserialize(parser, type); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public <T> T deserialize(JsonpDeserializer<T> deserializer) { |
| 79 | + return deserialize(deserializer, this.mapper); |
| 80 | + } |
| 81 | + |
| 82 | + @Override |
| 83 | + public <T> T deserialize(JsonpDeserializer<T> deserializer, JsonpMapper mapper) { |
| 84 | + try (JsonParser parser = asParser()) { |
| 85 | + return deserializer.deserialize(parser, mapper); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public void serialize(JsonGenerator generator, JsonpMapper mapper) { |
| 91 | + if (generator instanceof Jackson3JsonpGenerator) { |
| 92 | + Jackson3JsonpGenerator jkGenerator = (Jackson3JsonpGenerator) generator; |
| 93 | + try { |
| 94 | + buffer.serialize(jkGenerator.jacksonGenerator()); |
| 95 | + } catch (JacksonException e) { |
| 96 | + throw Jackson3Utils.convertException(e); |
| 97 | + } |
| 98 | + } else { |
| 99 | + try (JsonParser parser = asParser()) { |
| 100 | + JsonpUtils.copy(parser, generator); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Renders this buffer as a JSON string for debugger and logging convenience. |
| 107 | + */ |
| 108 | + @Override |
| 109 | + public String toString() { |
| 110 | + StringWriter writer = new StringWriter(); |
| 111 | + try (Jackson3JsonpGenerator generator = |
| 112 | + new Jackson3JsonpGenerator(mapper.objectMapper().createGenerator(writer))) { |
| 113 | + serialize(generator, mapper); |
| 114 | + generator.close(); |
| 115 | + return writer.toString(); |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments