Ether Framework
Unified API docs for Ether modules
Loading...
Searching...
No Matches
JacksonJsonCodec.java
Go to the documentation of this file.
1/*-
2 * #%L
3 * ether-json
4 * %%
5 * Copyright (C) 2025 Raúl Eduardo González Argote
6 * %%
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
24 * #L%
25 */
26
27package dev.rafex.ether.json;
28
29import java.io.IOException;
30import java.io.InputStream;
31import java.io.OutputStream;
32import java.util.Objects;
33
34import com.fasterxml.jackson.core.JsonProcessingException;
35import com.fasterxml.jackson.core.type.TypeReference;
36import com.fasterxml.jackson.databind.JavaType;
37import com.fasterxml.jackson.databind.JsonNode;
38import com.fasterxml.jackson.databind.ObjectMapper;
39import com.fasterxml.jackson.databind.SerializationFeature;
40import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
41
42public final class JacksonJsonCodec implements JsonCodec {
43
44 private final ObjectMapper mapper;
45
46 public JacksonJsonCodec(final ObjectMapper mapper) {
47 this.mapper = Objects.requireNonNull(mapper, "mapper");
48 }
49
51 return JsonCodecBuilder.create().build();
52 }
53
54 public ObjectMapper mapper() {
55 return mapper;
56 }
57
58 public static ObjectMapper defaultMapper() {
59 final var mapper = new ObjectMapper();
60 mapper.registerModule(new JavaTimeModule());
61 mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
62 return mapper;
63 }
64
65 @Override
66 public String toJson(final Object value) {
67 try {
68 return mapper.writeValueAsString(value);
69 } catch (final JsonProcessingException e) {
70 throw new JsonCodecException("Error serializando a JSON", e);
71 }
72 }
73
74 @Override
75 public String toPrettyJson(final Object value) {
76 try {
77 return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(value);
78 } catch (final JsonProcessingException e) {
79 throw new JsonCodecException("Error serializando a JSON formateado", e);
80 }
81 }
82
83 @Override
84 public byte[] toJsonBytes(final Object value) {
85 try {
86 return mapper.writeValueAsBytes(value);
87 } catch (final JsonProcessingException e) {
88 throw new JsonCodecException("Error serializando a bytes JSON", e);
89 }
90 }
91
92 @Override
93 public void writeValue(final OutputStream output, final Object value) {
94 try {
95 mapper.writeValue(output, value);
96 } catch (final IOException e) {
97 throw new JsonCodecException("Error escribiendo JSON al stream", e);
98 }
99 }
100
101 @Override
102 public <T> T readValue(final InputStream input, final Class<T> type) {
103 try {
104 return mapper.readValue(input, type);
105 } catch (final IOException e) {
106 throw new JsonCodecException("Error deserializando stream JSON", e);
107 }
108 }
109
110 @Override
111 public <T> T readValue(final InputStream input, final TypeReference<T> typeRef) {
112 try {
113 return mapper.readValue(input, typeRef);
114 } catch (final IOException e) {
115 throw new JsonCodecException("Error deserializando stream JSON", e);
116 }
117 }
118
119 @SuppressWarnings("unchecked")
120 @Override
121 public <T> T readValue(final InputStream input, final JavaType type) {
122 try {
123 return (T) mapper.readValue(input, type);
124 } catch (final IOException e) {
125 throw new JsonCodecException("Error deserializando stream JSON", e);
126 }
127 }
128
129 @Override
130 public <T> T readValue(final String content, final Class<T> type) {
131 try {
132 return mapper.readValue(content, type);
133 } catch (final IOException e) {
134 throw new JsonCodecException("Error deserializando JSON", e);
135 }
136 }
137
138 @Override
139 public <T> T readValue(final String content, final TypeReference<T> typeRef) {
140 try {
141 return mapper.readValue(content, typeRef);
142 } catch (final IOException e) {
143 throw new JsonCodecException("Error deserializando JSON", e);
144 }
145 }
146
147 @SuppressWarnings("unchecked")
148 @Override
149 public <T> T readValue(final String content, final JavaType type) {
150 try {
151 return (T) mapper.readValue(content, type);
152 } catch (final IOException e) {
153 throw new JsonCodecException("Error deserializando JSON", e);
154 }
155 }
156
157 @Override
158 public <T> T readValue(final byte[] content, final Class<T> type) {
159 try {
160 return mapper.readValue(content, type);
161 } catch (final IOException e) {
162 throw new JsonCodecException("Error deserializando bytes JSON", e);
163 }
164 }
165
166 @Override
167 public <T> T readValue(final byte[] content, final TypeReference<T> typeRef) {
168 try {
169 return mapper.readValue(content, typeRef);
170 } catch (final IOException e) {
171 throw new JsonCodecException("Error deserializando bytes JSON", e);
172 }
173 }
174
175 @SuppressWarnings("unchecked")
176 @Override
177 public <T> T readValue(final byte[] content, final JavaType type) {
178 try {
179 return (T) mapper.readValue(content, type);
180 } catch (final IOException e) {
181 throw new JsonCodecException("Error deserializando bytes JSON", e);
182 }
183 }
184
185 @Override
186 public JsonNode readTree(final String content) {
187 try {
188 return mapper.readTree(content);
189 } catch (final IOException e) {
190 throw new JsonCodecException("Error parseando JSON a JsonNode", e);
191 }
192 }
193
194 @Override
195 public JsonNode readTree(final InputStream input) {
196 try {
197 return mapper.readTree(input);
198 } catch (final IOException e) {
199 throw new JsonCodecException("Error parseando stream JSON a JsonNode", e);
200 }
201 }
202
203 @Override
204 public JsonNode readTree(final byte[] input) {
205 try {
206 return mapper.readTree(input);
207 } catch (final IOException e) {
208 throw new JsonCodecException("Error parseando bytes JSON a JsonNode", e);
209 }
210 }
211
212 @Override
213 public JsonNode valueToTree(final Object value) {
214 return mapper.valueToTree(value);
215 }
216
217 @Override
218 public <T> T treeToValue(final JsonNode node, final Class<T> type) {
219 try {
220 return mapper.treeToValue(node, type);
221 } catch (final JsonProcessingException e) {
222 throw new JsonCodecException("Error convirtiendo JsonNode a POJO", e);
223 }
224 }
225
226 @Override
227 public <T> T treeToValue(final JsonNode node, final TypeReference<T> typeRef) {
228 return mapper.convertValue(node, typeRef);
229 }
230
231 @Override
232 public JsonNode at(final JsonNode node, final String pointer) {
233 Objects.requireNonNull(node, "node");
234 Objects.requireNonNull(pointer, "pointer");
235 return node.at(pointer);
236 }
237}
byte[] toJsonBytes(final Object value)
String toPrettyJson(final Object value)
JsonNode readTree(final String content)
void writeValue(final OutputStream output, final Object value)
JsonNode readTree(final byte[] input)
JsonNode at(final JsonNode node, final String pointer)
JsonNode readTree(final InputStream input)
JacksonJsonCodec(final ObjectMapper mapper)
JsonNode valueToTree(final Object value)