Ether Framework
Unified API docs for Ether modules
Loading...
Searching...
No Matches
JsonCodecBuilder.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.util.Objects;
30import java.util.function.Consumer;
31
32import com.fasterxml.jackson.annotation.JsonInclude;
33import com.fasterxml.jackson.databind.DeserializationFeature;
34import com.fasterxml.jackson.databind.Module;
35import com.fasterxml.jackson.databind.ObjectMapper;
36import com.fasterxml.jackson.databind.PropertyNamingStrategy;
37import com.fasterxml.jackson.databind.SerializationFeature;
38
39public final class JsonCodecBuilder {
40
41 private final ObjectMapper mapper;
42
43 private JsonCodecBuilder(final ObjectMapper mapper) {
44 this.mapper = mapper;
45 }
46
47 public static JsonCodecBuilder create() {
48 return new JsonCodecBuilder(JacksonJsonCodec.defaultMapper());
49 }
50
51 public static JsonCodecBuilder strict() {
52 return create().failOnUnknownProperties(true);
53 }
54
55 public static JsonCodecBuilder lenient() {
56 return create().failOnUnknownProperties(false);
57 }
58
59 public JsonCodecBuilder configure(final Consumer<ObjectMapper> customizer) {
60 Objects.requireNonNull(customizer, "customizer").accept(mapper);
61 return this;
62 }
63
64 public JsonCodecBuilder failOnUnknownProperties(final boolean enabled) {
65 mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, enabled);
66 return this;
67 }
68
69 public JsonCodecBuilder writeDatesAsTimestamps(final boolean enabled) {
70 mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, enabled);
71 return this;
72 }
73
74 public JsonCodecBuilder prettyPrint(final boolean enabled) {
75 mapper.configure(SerializationFeature.INDENT_OUTPUT, enabled);
76 return this;
77 }
78
79 public JsonCodecBuilder namingStrategy(final PropertyNamingStrategy strategy) {
80 mapper.setPropertyNamingStrategy(strategy);
81 return this;
82 }
83
84 public JsonCodecBuilder include(final JsonInclude.Include include) {
85 mapper.setSerializationInclusion(include);
86 return this;
87 }
88
89 public JsonCodecBuilder registerModule(final Module module) {
90 mapper.registerModule(Objects.requireNonNull(module, "module"));
91 return this;
92 }
93
95 return new JacksonJsonCodec(mapper.copy());
96 }
97}
JsonCodecBuilder writeDatesAsTimestamps(final boolean enabled)
JsonCodecBuilder namingStrategy(final PropertyNamingStrategy strategy)
JsonCodecBuilder registerModule(final Module module)
JsonCodecBuilder include(final JsonInclude.Include include)
JsonCodecBuilder prettyPrint(final boolean enabled)
JsonCodecBuilder configure(final Consumer< ObjectMapper > customizer)
JsonCodecBuilder failOnUnknownProperties(final boolean enabled)