27package dev.rafex.ether.json;
29import java.io.IOException;
30import java.io.InputStream;
31import java.io.OutputStream;
32import java.util.Objects;
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;
51 private final ObjectMapper mapper;
60 this.mapper = Objects.requireNonNull(mapper,
"mapper");
89 final var mapper =
new ObjectMapper();
90 mapper.registerModule(
new JavaTimeModule());
91 mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
101 public String
toJson(
final Object value) {
103 return mapper.writeValueAsString(value);
104 }
catch (
final JsonProcessingException e) {
117 return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(value);
118 }
catch (
final JsonProcessingException e) {
131 return mapper.writeValueAsBytes(value);
132 }
catch (
final JsonProcessingException e) {
143 public void writeValue(
final OutputStream output,
final Object value) {
145 mapper.writeValue(output, value);
146 }
catch (
final IOException e) {
152 public <T> T readValue(
final InputStream input,
final Class<T> type) {
154 return mapper.readValue(input, type);
155 }
catch (
final IOException e) {
156 throw new JsonCodecException(
"Error deserializando stream JSON", e);
161 public <T> T readValue(
final InputStream input,
final TypeReference<T> typeRef) {
163 return mapper.readValue(input, typeRef);
164 }
catch (
final IOException e) {
165 throw new JsonCodecException(
"Error deserializando stream JSON", e);
169 @SuppressWarnings(
"unchecked")
171 public <T> T readValue(
final InputStream input,
final JavaType type) {
173 return (T) mapper.readValue(input, type);
174 }
catch (
final IOException e) {
175 throw new JsonCodecException(
"Error deserializando stream JSON", e);
180 public <T> T readValue(
final String content,
final Class<T> type) {
182 return mapper.readValue(content, type);
183 }
catch (
final IOException e) {
184 throw new JsonCodecException(
"Error deserializando JSON", e);
189 public <T> T readValue(
final String content,
final TypeReference<T> typeRef) {
191 return mapper.readValue(content, typeRef);
192 }
catch (
final IOException e) {
193 throw new JsonCodecException(
"Error deserializando JSON", e);
197 @SuppressWarnings(
"unchecked")
199 public <T> T readValue(
final String content,
final JavaType type) {
201 return (T) mapper.readValue(content, type);
202 }
catch (
final IOException e) {
203 throw new JsonCodecException(
"Error deserializando JSON", e);
208 public <T> T readValue(
final byte[] content,
final Class<T> type) {
210 return mapper.readValue(content, type);
211 }
catch (
final IOException e) {
212 throw new JsonCodecException(
"Error deserializando bytes JSON", e);
217 public <T> T readValue(
final byte[] content,
final TypeReference<T> typeRef) {
219 return mapper.readValue(content, typeRef);
220 }
catch (
final IOException e) {
221 throw new JsonCodecException(
"Error deserializando bytes JSON", e);
225 @SuppressWarnings(
"unchecked")
227 public <T> T readValue(
final byte[] content,
final JavaType type) {
229 return (T) mapper.readValue(content, type);
230 }
catch (
final IOException e) {
231 throw new JsonCodecException(
"Error deserializando bytes JSON", e);
238 return mapper.readTree(content);
239 }
catch (
final IOException e) {
245 public JsonNode
readTree(
final InputStream input) {
247 return mapper.readTree(input);
248 }
catch (
final IOException e) {
256 return mapper.readTree(input);
257 }
catch (
final IOException e) {
264 return mapper.valueToTree(value);
268 public <T> T treeToValue(
final JsonNode node,
final Class<T> type) {
270 return mapper.treeToValue(node, type);
271 }
catch (
final JsonProcessingException e) {
272 throw new JsonCodecException(
"Error convirtiendo JsonNode a POJO", e);
277 public <T> T treeToValue(
final JsonNode node,
final TypeReference<T> typeRef) {
278 return mapper.convertValue(node, typeRef);
282 public JsonNode
at(
final JsonNode node,
final String pointer) {
283 Objects.requireNonNull(node,
"node");
284 Objects.requireNonNull(pointer,
"pointer");
285 return node.at(pointer);
byte[] toJsonBytes(final Object value)
static JacksonJsonCodec defaultCodec()
Devuelve una instancia de codec con la configuración por defecto (módulo JavaTime habilitado,...
String toPrettyJson(final Object value)
ObjectMapper mapper()
Devuelve el ObjectMapper interno utilizado por este codec.
static ObjectMapper defaultMapper()
Crea un ObjectMapper con la configuración por defecto del módulo: registra JavaTimeModule y deshabili...
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)
Crea un codec que utiliza el ObjectMapper proporcionado.
String toJson(final Object value)
JsonNode valueToTree(final Object value)
static JsonCodecBuilder create()