|
18 | 18 | */
|
19 | 19 | public class JacksonAdapter implements JsonStreamAdapter {
|
20 | 20 |
|
| 21 | + /** |
| 22 | + * Used to build JacksonAdapter with custom settings. |
| 23 | + */ |
| 24 | + public static class Builder { |
| 25 | + |
| 26 | + private JsonFactory jsonFactory; |
| 27 | + private boolean serializeNulls; |
| 28 | + private boolean serializeEmpty; |
| 29 | + private boolean failOnUnknown; |
| 30 | + |
| 31 | + /** |
| 32 | + * Set the Jackson JsonFactory to use. |
| 33 | + */ |
| 34 | + public Builder jsonFactory(JsonFactory jsonFactory) { |
| 35 | + this.jsonFactory = jsonFactory; |
| 36 | + return this; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Set to true to serialize nulls. Defaults to false. |
| 41 | + */ |
| 42 | + public Builder serializeNulls(boolean serializeNulls) { |
| 43 | + this.serializeNulls = serializeNulls; |
| 44 | + return this; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Set to true to serialize empty collections. Defaults to false. |
| 49 | + */ |
| 50 | + public Builder serializeEmpty(boolean serializeEmpty) { |
| 51 | + this.serializeEmpty = serializeEmpty; |
| 52 | + return this; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Set to true to fail on unknown properties. Defaults to false. |
| 57 | + */ |
| 58 | + public Builder failOnUnknown(boolean failOnUnknown) { |
| 59 | + this.failOnUnknown = failOnUnknown; |
| 60 | + return this; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Build and return the JacksonAdapter. |
| 65 | + */ |
| 66 | + public JacksonAdapter build() { |
| 67 | + return new JacksonAdapter(serializeNulls, serializeEmpty, failOnUnknown, jsonFactory); |
| 68 | + } |
| 69 | + } |
| 70 | + |
21 | 71 | private final JsonFactory jsonFactory;
|
22 | 72 | private final boolean serializeNulls;
|
23 | 73 | private final boolean serializeEmpty;
|
@@ -47,6 +97,24 @@ public JacksonAdapter(boolean serializeNulls, boolean serializeEmpty, boolean fa
|
47 | 97 | this.jsonFactory = jsonFactory;
|
48 | 98 | }
|
49 | 99 |
|
| 100 | + /** |
| 101 | + * Return a new builder to create a JacksonAdapter. |
| 102 | + * |
| 103 | + * <pre>{@code |
| 104 | + * |
| 105 | + * JsonFactory customFactory = ...; |
| 106 | + * |
| 107 | + * var jacksonAdapter = JacksonAdapter.newBuilder() |
| 108 | + * .serializeNulls(true) |
| 109 | + * .jsonFactory(customFactory) |
| 110 | + * .build(); |
| 111 | + * |
| 112 | + * }</pre> |
| 113 | + */ |
| 114 | + public static Builder newBuilder() { |
| 115 | + return new Builder(); |
| 116 | + } |
| 117 | + |
50 | 118 | @Override
|
51 | 119 | public PropertyNames properties(String... names) {
|
52 | 120 | return new JacksonNames(names);
|
|
0 commit comments