Ether Framework
Unified API docs for Ether modules
Loading...
Searching...
No Matches
JsonUtils.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.nio.file.Files;
33import java.nio.file.Path;
34import java.util.List;
35
36import com.fasterxml.jackson.core.type.TypeReference;
37import com.fasterxml.jackson.databind.JavaType;
38import com.fasterxml.jackson.databind.JsonNode;
39
40public final class JsonUtils {
41
42 private static final JacksonJsonCodec DEFAULT_CODEC = JacksonJsonCodec.defaultCodec();
43
44 private JsonUtils() {
45 }
46
47 public static String toJson(final Object value) {
48 return DEFAULT_CODEC.toJson(value);
49 }
50
51 public static String toPrettyJson(final Object value) {
52 return DEFAULT_CODEC.toPrettyJson(value);
53 }
54
55 public static byte[] toJsonBytes(final Object value) {
56 return DEFAULT_CODEC.toJsonBytes(value);
57 }
58
59 public static void writeJson(final OutputStream output, final Object value) {
60 DEFAULT_CODEC.writeValue(output, value);
61 }
62
63 public static <T> T fromJson(final String json, final Class<T> clazz) {
64 return DEFAULT_CODEC.readValue(json, clazz);
65 }
66
67 public static <T> T fromJson(final String json, final TypeReference<T> typeRef) {
68 return DEFAULT_CODEC.readValue(json, typeRef);
69 }
70
71 public static <T> T fromJson(final String json, final JavaType type) {
72 return DEFAULT_CODEC.readValue(json, type);
73 }
74
75 public static <T> T fromJson(final InputStream input, final Class<T> clazz) {
76 return DEFAULT_CODEC.readValue(input, clazz);
77 }
78
79 public static <T> T fromJson(final InputStream input, final TypeReference<T> typeRef) {
80 return DEFAULT_CODEC.readValue(input, typeRef);
81 }
82
83 public static <T> T fromJson(final InputStream input, final JavaType type) {
84 return DEFAULT_CODEC.readValue(input, type);
85 }
86
87 public static <T> T fromJson(final byte[] input, final Class<T> clazz) {
88 return DEFAULT_CODEC.readValue(input, clazz);
89 }
90
91 public static <T> T fromJson(final byte[] input, final TypeReference<T> typeRef) {
92 return DEFAULT_CODEC.readValue(input, typeRef);
93 }
94
95 public static <T> T fromJson(final byte[] input, final JavaType type) {
96 return DEFAULT_CODEC.readValue(input, type);
97 }
98
99 public static <T> List<T> fromJsonToList(final String json, final Class<T> elementClass) {
100 final var listType = DEFAULT_CODEC.mapper().getTypeFactory().constructCollectionType(List.class, elementClass);
101 return DEFAULT_CODEC.readValue(json, listType);
102 }
103
104 public static JsonNode parseTree(final String json) {
105 return DEFAULT_CODEC.readTree(json);
106 }
107
108 public static JsonNode parseTree(final InputStream input) {
109 return DEFAULT_CODEC.readTree(input);
110 }
111
112 public static JsonNode parseTree(final byte[] input) {
113 return DEFAULT_CODEC.readTree(input);
114 }
115
116 public static JsonNode readTreeFromFile(final Path path) {
117 try (InputStream input = Files.newInputStream(path)) {
118 return DEFAULT_CODEC.readTree(input);
119 } catch (final IOException e) {
120 throw new JsonCodecException("Error leyendo JSON desde fichero", e);
121 }
122 }
123
124 public static JsonNode valueToTree(final Object value) {
125 return DEFAULT_CODEC.valueToTree(value);
126 }
127
128 public static JsonNode at(final JsonNode node, final String pointer) {
129 return DEFAULT_CODEC.at(node, pointer);
130 }
131
132 public static <T> T treeToValue(final JsonNode node, final Class<T> clazz) {
133 return DEFAULT_CODEC.treeToValue(node, clazz);
134 }
135
136 public static <T> T treeToValue(final JsonNode node, final TypeReference<T> typeRef) {
137 return DEFAULT_CODEC.treeToValue(node, typeRef);
138 }
139
140 public static JsonCodec codec() {
141 return DEFAULT_CODEC;
142 }
143
144 public static JsonCodec codec(final JsonCodecBuilder builder) {
145 return builder.build();
146 }
147}
static< T > List< T > fromJsonToList(final String json, final Class< T > elementClass)
static< T > T fromJson(final InputStream input, final TypeReference< T > typeRef)
static JsonNode parseTree(final String json)
static< T > T fromJson(final String json, final TypeReference< T > typeRef)
static JsonNode readTreeFromFile(final Path path)
static String toJson(final Object value)
static< T > T treeToValue(final JsonNode node, final Class< T > clazz)
static JsonNode parseTree(final InputStream input)
static JsonCodec codec(final JsonCodecBuilder builder)
static< T > T fromJson(final byte[] input, final JavaType type)
static JsonNode parseTree(final byte[] input)
static< T > T fromJson(final InputStream input, final JavaType type)
static byte[] toJsonBytes(final Object value)
static void writeJson(final OutputStream output, final Object value)
static JsonNode valueToTree(final Object value)
static< T > T fromJson(final InputStream input, final Class< T > clazz)
static< T > T fromJson(final String json, final JavaType type)
static< T > T fromJson(final String json, final Class< T > clazz)
static< T > T fromJson(final byte[] input, final Class< T > clazz)
static String toPrettyJson(final Object value)
static JsonNode at(final JsonNode node, final String pointer)
static< T > T fromJson(final byte[] input, final TypeReference< T > typeRef)
static< T > T treeToValue(final JsonNode node, final TypeReference< T > typeRef)