44 private final ObjectMapper mapper;
47 this.mapper = Objects.requireNonNull(mapper,
"mapper");
59 final var mapper =
new ObjectMapper();
60 mapper.registerModule(
new JavaTimeModule());
61 mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
66 public String
toJson(
final Object value) {
68 return mapper.writeValueAsString(value);
69 }
catch (
final JsonProcessingException e) {
77 return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(value);
78 }
catch (
final JsonProcessingException e) {
86 return mapper.writeValueAsBytes(value);
87 }
catch (
final JsonProcessingException e) {
93 public void writeValue(
final OutputStream output,
final Object value) {
95 mapper.writeValue(output, value);
96 }
catch (
final IOException e) {
102 public <T> T readValue(
final InputStream input,
final Class<T> type) {
104 return mapper.readValue(input, type);
105 }
catch (
final IOException e) {
106 throw new JsonCodecException(
"Error deserializando stream JSON", e);
111 public <T> T readValue(
final InputStream input,
final TypeReference<T> typeRef) {
113 return mapper.readValue(input, typeRef);
114 }
catch (
final IOException e) {
115 throw new JsonCodecException(
"Error deserializando stream JSON", e);
119 @SuppressWarnings(
"unchecked")
121 public <T> T readValue(
final InputStream input,
final JavaType type) {
123 return (T) mapper.readValue(input, type);
124 }
catch (
final IOException e) {
125 throw new JsonCodecException(
"Error deserializando stream JSON", e);
130 public <T> T readValue(
final String content,
final Class<T> type) {
132 return mapper.readValue(content, type);
133 }
catch (
final IOException e) {
134 throw new JsonCodecException(
"Error deserializando JSON", e);
139 public <T> T readValue(
final String content,
final TypeReference<T> typeRef) {
141 return mapper.readValue(content, typeRef);
142 }
catch (
final IOException e) {
143 throw new JsonCodecException(
"Error deserializando JSON", e);
147 @SuppressWarnings(
"unchecked")
149 public <T> T readValue(
final String content,
final JavaType type) {
151 return (T) mapper.readValue(content, type);
152 }
catch (
final IOException e) {
153 throw new JsonCodecException(
"Error deserializando JSON", e);
158 public <T> T readValue(
final byte[] content,
final Class<T> type) {
160 return mapper.readValue(content, type);
161 }
catch (
final IOException e) {
162 throw new JsonCodecException(
"Error deserializando bytes JSON", e);
167 public <T> T readValue(
final byte[] content,
final TypeReference<T> typeRef) {
169 return mapper.readValue(content, typeRef);
170 }
catch (
final IOException e) {
171 throw new JsonCodecException(
"Error deserializando bytes JSON", e);
175 @SuppressWarnings(
"unchecked")
177 public <T> T readValue(
final byte[] content,
final JavaType type) {
179 return (T) mapper.readValue(content, type);
180 }
catch (
final IOException e) {
181 throw new JsonCodecException(
"Error deserializando bytes JSON", e);
188 return mapper.readTree(content);
189 }
catch (
final IOException e) {
195 public JsonNode
readTree(
final InputStream input) {
197 return mapper.readTree(input);
198 }
catch (
final IOException e) {
206 return mapper.readTree(input);
207 }
catch (
final IOException e) {
214 return mapper.valueToTree(value);
218 public <T> T treeToValue(
final JsonNode node,
final Class<T> type) {
220 return mapper.treeToValue(node, type);
221 }
catch (
final JsonProcessingException e) {
222 throw new JsonCodecException(
"Error convirtiendo JsonNode a POJO", e);
227 public <T> T treeToValue(
final JsonNode node,
final TypeReference<T> typeRef) {
228 return mapper.convertValue(node, typeRef);
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);