| @@ -0,0 +1,126 | |||||
|
|
1 | package org.implab.gradle.common.utils; | |||
|
|
2 | ||||
|
|
3 | import java.io.File; | |||
|
|
4 | import java.io.IOException; | |||
|
|
5 | import java.util.function.Consumer; | |||
|
|
6 | ||||
|
|
7 | import org.implab.gradle.common.utils.LazyValue; | |||
|
|
8 | ||||
|
|
9 | import com.fasterxml.jackson.annotation.JsonInclude.Include; | |||
|
|
10 | import com.fasterxml.jackson.core.JsonProcessingException; | |||
|
|
11 | import com.fasterxml.jackson.core.type.TypeReference; | |||
|
|
12 | import com.fasterxml.jackson.core.util.DefaultIndenter; | |||
|
|
13 | import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; | |||
|
|
14 | import com.fasterxml.jackson.databind.JsonMappingException; | |||
|
|
15 | import com.fasterxml.jackson.databind.ObjectMapper; | |||
|
|
16 | import com.fasterxml.jackson.databind.SerializationFeature; | |||
|
|
17 | import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; | |||
|
|
18 | import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; | |||
|
|
19 | ||||
|
|
20 | public class Json { | |||
|
|
21 | ||||
|
|
22 | private final static LazyValue<ObjectMapper> mapper = new LazyValue<>(Json::createMapper); | |||
|
|
23 | ||||
|
|
24 | private static ObjectMapper createMapper() { | |||
|
|
25 | DefaultPrettyPrinter prettyPrinter = new DefaultPrettyPrinter(); | |||
|
|
26 | prettyPrinter.indentArraysWith(DefaultIndenter.SYSTEM_LINEFEED_INSTANCE); | |||
|
|
27 | ||||
|
|
28 | return new ObjectMapper() | |||
|
|
29 | .registerModule(new Jdk8Module()) | |||
|
|
30 | .registerModule(new JavaTimeModule()) | |||
|
|
31 | .setSerializationInclusion(Include.NON_EMPTY) | |||
|
|
32 | .enable(SerializationFeature.INDENT_OUTPUT) | |||
|
|
33 | .setDefaultPrettyPrinter(prettyPrinter); | |||
|
|
34 | } | |||
|
|
35 | ||||
|
|
36 | public static ObjectMapper jsonMapper() { | |||
|
|
37 | return mapper.get(); | |||
|
|
38 | } | |||
|
|
39 | ||||
|
|
40 | public static Consumer<File> fileWriter(Object value) { | |||
|
|
41 | return file -> { | |||
|
|
42 | try { | |||
|
|
43 | jsonMapper().writeValue(file, value); | |||
|
|
44 | } catch (IOException e) { | |||
|
|
45 | throw new SerializationException(e); | |||
|
|
46 | } | |||
|
|
47 | }; | |||
|
|
48 | } | |||
|
|
49 | ||||
|
|
50 | public static void write(File file, Object value) { | |||
|
|
51 | try { | |||
|
|
52 | jsonMapper().writeValue(file, value); | |||
|
|
53 | } catch (IOException e) { | |||
|
|
54 | throw new SerializationException(e); | |||
|
|
55 | } | |||
|
|
56 | } | |||
|
|
57 | ||||
|
|
58 | public static String stringify(Object value) { | |||
|
|
59 | try { | |||
|
|
60 | return jsonMapper().writeValueAsString(value); | |||
|
|
61 | } catch (JsonProcessingException e) { | |||
|
|
62 | throw new SerializationException(e); | |||
|
|
63 | } | |||
|
|
64 | } | |||
|
|
65 | ||||
|
|
66 | public static <T> T parse(String json, Class<T> clazz) { | |||
|
|
67 | try { | |||
|
|
68 | return jsonMapper().readValue(json, clazz); | |||
|
|
69 | } catch (JsonProcessingException e) { | |||
|
|
70 | throw new SerializationException(e); | |||
|
|
71 | } | |||
|
|
72 | } | |||
|
|
73 | ||||
|
|
74 | public static <T> T parse(String json, TypeReference<T> type) { | |||
|
|
75 | try { | |||
|
|
76 | return jsonMapper().readValue(json, type); | |||
|
|
77 | } catch (JsonProcessingException e) { | |||
|
|
78 | throw new SerializationException(e); | |||
|
|
79 | } | |||
|
|
80 | } | |||
|
|
81 | ||||
|
|
82 | public static <T> T read(File file, TypeReference<T> type) { | |||
|
|
83 | try { | |||
|
|
84 | return jsonMapper().readValue(file, type); | |||
|
|
85 | } catch (IOException e) { | |||
|
|
86 | throw new SerializationException(e); | |||
|
|
87 | } | |||
|
|
88 | } | |||
|
|
89 | ||||
|
|
90 | public static <T> T updateValue(T target, Object source) { | |||
|
|
91 | try { | |||
|
|
92 | return jsonMapper().updateValue(target, source); | |||
|
|
93 | } catch (JsonMappingException e) { | |||
|
|
94 | throw new SerializationException(e); | |||
|
|
95 | } | |||
|
|
96 | } | |||
|
|
97 | ||||
|
|
98 | public static <T> T convertValue(Object source, Class<T> clazz) { | |||
|
|
99 | var mapper = jsonMapper(); | |||
|
|
100 | var buf = mapper.getSerializerProvider().bufferForValueConversion(jsonMapper()); | |||
|
|
101 | ||||
|
|
102 | try { | |||
|
|
103 | mapper.writeValue(buf, source); | |||
|
|
104 | return mapper.readValue(buf.asParser(), clazz); | |||
|
|
105 | } catch (IOException e) { | |||
|
|
106 | throw new SerializationException(e); | |||
|
|
107 | } | |||
|
|
108 | } | |||
|
|
109 | ||||
|
|
110 | public static class SerializationException extends RuntimeException { | |||
|
|
111 | public SerializationException() { | |||
|
|
112 | } | |||
|
|
113 | ||||
|
|
114 | public SerializationException(String message) { | |||
|
|
115 | super(message); | |||
|
|
116 | } | |||
|
|
117 | ||||
|
|
118 | public SerializationException(Throwable cause) { | |||
|
|
119 | super(cause); | |||
|
|
120 | } | |||
|
|
121 | ||||
|
|
122 | public SerializationException(String message, Throwable cause) { | |||
|
|
123 | super(message, cause); | |||
|
|
124 | } | |||
|
|
125 | } | |||
|
|
126 | } | |||
| @@ -7,13 +7,15 java { | |||||
| 7 | withJavadocJar() |
|
7 | withJavadocJar() | |
| 8 | withSourcesJar() |
|
8 | withSourcesJar() | |
| 9 | toolchain { |
|
9 | toolchain { | |
| 10 |
languageVersion = JavaLanguageVersion.of(1 |
|
10 | languageVersion = JavaLanguageVersion.of(21) | |
| 11 | } |
|
11 | } | |
| 12 | } |
|
12 | } | |
| 13 |
|
13 | |||
| 14 | dependencies { |
|
14 | dependencies { | |
| 15 | compileOnly libs.jdt.annotations |
|
15 | compileOnly libs.jdt.annotations | |
| 16 |
|
16 | |||
|
|
17 | implementation libs.bundles.jackson | |||
|
|
18 | ||||
| 17 | api gradleApi() |
|
19 | api gradleApi() | |
| 18 | } |
|
20 | } | |
| 19 |
|
21 | |||
| @@ -14,15 +14,10 public final class Properties { | |||||
| 14 | } |
|
14 | } | |
| 15 |
|
15 | |||
| 16 | public static <K> void mergeMap(MapProperty<K, Object> property, Map<K, Object> map) { |
|
16 | public static <K> void mergeMap(MapProperty<K, Object> property, Map<K, Object> map) { | |
| 17 | map.forEach((k, v) -> { |
|
17 | mergeMap(property, map, Function.identity()); | |
| 18 | if (v instanceof Provider<?>) |
|
|||
| 19 | property.put(k, (Provider<?>) v); |
|
|||
| 20 | else |
|
|||
| 21 | property.put(k, v); |
|
|||
| 22 | }); |
|
|||
| 23 | } |
|
18 | } | |
| 24 |
|
19 | |||
| 25 | public static <K, V> void mergeMap(MapProperty<K, V> property, Map<K, ?> map, Function<Object, V> mapper) { |
|
20 | public static <K, V> void mergeMap(MapProperty<K, V> property, Map<K, ?> map, Function<Object,? extends V> mapper) { | |
| 26 | map.forEach((k, v) -> { |
|
21 | map.forEach((k, v) -> { | |
| 27 | if (v instanceof Provider<?>) |
|
22 | if (v instanceof Provider<?>) | |
| 28 | property.put(k, ((Provider<?>) v).map(mapper::apply)); |
|
23 | property.put(k, ((Provider<?>) v).map(mapper::apply)); | |
| @@ -32,15 +27,10 public final class Properties { | |||||
| 32 | } |
|
27 | } | |
| 33 |
|
28 | |||
| 34 | public static void mergeList(ListProperty<Object> property, Iterable<Object> values) { |
|
29 | public static void mergeList(ListProperty<Object> property, Iterable<Object> values) { | |
| 35 | values.forEach(v -> { |
|
30 | mergeList(property, values, Function.identity()); | |
| 36 | if (v instanceof Provider<?>) |
|
|||
| 37 | property.add((Provider<?>) v); |
|
|||
| 38 | else |
|
|||
| 39 | property.add(v); |
|
|||
| 40 | }); |
|
|||
| 41 | } |
|
31 | } | |
| 42 |
|
32 | |||
| 43 | public static <V> void mergeList(ListProperty<V> property, Iterable<Object> values, Function<Object, V> mapper) { |
|
33 | public static <V> void mergeList(ListProperty<V> property, Iterable<Object> values, Function<Object, ? extends V> mapper) { | |
| 44 | values.forEach(v -> { |
|
34 | values.forEach(v -> { | |
| 45 | if (v instanceof Provider<?>) |
|
35 | if (v instanceof Provider<?>) | |
| 46 | property.add(((Provider<?>) v).map(mapper::apply)); |
|
36 | property.add(((Provider<?>) v).map(mapper::apply)); | |
| @@ -50,9 +40,7 public final class Properties { | |||||
| 50 | } |
|
40 | } | |
| 51 |
|
41 | |||
| 52 | public static <K> void configureMap(MapProperty<K, Object> prop, Action<Map<K, Object>> configure) { |
|
42 | public static <K> void configureMap(MapProperty<K, Object> prop, Action<Map<K, Object>> configure) { | |
| 53 | var map = new HashMap<K, Object>(); |
|
43 | configureMap(prop, configure, Function.identity()); | |
| 54 | configure.execute(map); |
|
|||
| 55 | mergeMap(prop, map); |
|
|||
| 56 | } |
|
44 | } | |
| 57 |
|
45 | |||
| 58 | public static <K, V> void configureMap(MapProperty<K, V> prop, Action<Map<K, Object>> configure, Function<Object, V> mapper) { |
|
46 | public static <K, V> void configureMap(MapProperty<K, V> prop, Action<Map<K, Object>> configure, Function<Object, V> mapper) { | |
| @@ -60,10 +60,6 public final class Values { | |||||
| 60 | return provider.get(); |
|
60 | return provider.get(); | |
| 61 | } |
|
61 | } | |
| 62 |
|
62 | |||
| 63 | public static <U,V> Set<V> mapSet(Set<U> values, Function<U,V> mapper) { |
|
|||
| 64 | return values.stream().map(mapper).collect(Collectors.toUnmodifiableSet()); |
|
|||
| 65 | } |
|
|||
| 66 |
|
||||
| 67 | private static class ArrayIterator<T> implements Iterator<T> { |
|
63 | private static class ArrayIterator<T> implements Iterator<T> { | |
| 68 | private final T[] data; |
|
64 | private final T[] data; | |
| 69 |
|
65 | |||
| @@ -1,7 +1,17 | |||||
| 1 | [versions] |
|
1 | [versions] | |
| 2 | immutables = "2.10.1" |
|
2 | immutables = "2.10.1" | |
| 3 | jdt = "2.3.0" |
|
3 | jdt = "2.3.0" | |
|
|
4 | jackson = "2.15.4" | |||
| 4 |
|
5 | |||
| 5 | [libraries] |
|
6 | [libraries] | |
| 6 | immutables = { module = "org.immutables:value", version.ref = "immutables" } |
|
7 | immutables = { module = "org.immutables:value", version.ref = "immutables" } | |
| 7 | jdt-annotations = { module = "org.eclipse.jdt:org.eclipse.jdt.annotation", version.ref = "jdt" } |
|
8 | jdt-annotations = { module = "org.eclipse.jdt:org.eclipse.jdt.annotation", version.ref = "jdt" } | |
|
|
9 | ||||
|
|
10 | #jackson | |||
|
|
11 | jackson-core = { module = "com.fasterxml.jackson.core:jackson-core", version.ref = "jackson" } | |||
|
|
12 | jackson-datatype-jdk8 = { module = "com.fasterxml.jackson.datatype:jackson-datatype-jdk8", version.ref = "jackson" } | |||
|
|
13 | jackson-datatype-jsr310 = { module = "com.fasterxml.jackson.datatype:jackson-datatype-jsr310", version.ref = "jackson" } | |||
|
|
14 | ||||
|
|
15 | [bundles] | |||
|
|
16 | ||||
|
|
17 | jackson = ["jackson-core", "jackson-datatype-jdk8", "jackson-datatype-jsr310"] No newline at end of file | |||
General Comments 0
You need to be logged in to leave comments.
Login now
