|
22 | 22 | import static com.google.gson.Gson.DEFAULT_FORMATTING_STYLE;
|
23 | 23 | import static com.google.gson.Gson.DEFAULT_JSON_NON_EXECUTABLE;
|
24 | 24 | import static com.google.gson.Gson.DEFAULT_LENIENT;
|
| 25 | +import static com.google.gson.Gson.DEFAULT_MISSING_FIELD_VALUE_STRATEGY; |
25 | 26 | import static com.google.gson.Gson.DEFAULT_NUMBER_TO_NUMBER_STRATEGY;
|
26 | 27 | import static com.google.gson.Gson.DEFAULT_OBJECT_TO_NUMBER_STRATEGY;
|
27 | 28 | import static com.google.gson.Gson.DEFAULT_SERIALIZE_NULLS;
|
28 | 29 | import static com.google.gson.Gson.DEFAULT_SPECIALIZE_FLOAT_VALUES;
|
| 30 | +import static com.google.gson.Gson.DEFAULT_UNKNOWN_FIELD_STRATEGY; |
29 | 31 | import static com.google.gson.Gson.DEFAULT_USE_JDK_UNSAFE;
|
30 | 32 |
|
31 | 33 | import com.google.gson.annotations.Since;
|
|
56 | 58 | * use {@code new Gson()}. {@code GsonBuilder} is best used by creating it, and then invoking its
|
57 | 59 | * various configuration methods, and finally calling create.</p>
|
58 | 60 | *
|
59 |
| - * <p>The following is an example shows how to use the {@code GsonBuilder} to construct a Gson |
| 61 | + * <p>The following example shows how to use the {@code GsonBuilder} to construct a Gson |
60 | 62 | * instance:
|
61 | 63 | *
|
62 | 64 | * <pre>
|
|
73 | 75 | *
|
74 | 76 | * <p>NOTES:
|
75 | 77 | * <ul>
|
76 |
| - * <li> the order of invocation of configuration methods does not matter.</li> |
77 |
| - * <li> The default serialization of {@link Date} and its subclasses in Gson does |
| 78 | + * <li>the order of invocation of configuration methods does not matter.</li> |
| 79 | + * <li>the default serialization of {@link Date} and its subclasses in Gson does |
78 | 80 | * not contain time-zone information. So, if you are using date/time instances,
|
79 | 81 | * use {@code GsonBuilder} and its {@code setDateFormat} methods.</li>
|
80 | 82 | * </ul>
|
@@ -104,6 +106,8 @@ public final class GsonBuilder {
|
104 | 106 | private boolean useJdkUnsafe = DEFAULT_USE_JDK_UNSAFE;
|
105 | 107 | private ToNumberStrategy objectToNumberStrategy = DEFAULT_OBJECT_TO_NUMBER_STRATEGY;
|
106 | 108 | private ToNumberStrategy numberToNumberStrategy = DEFAULT_NUMBER_TO_NUMBER_STRATEGY;
|
| 109 | + private MissingFieldValueStrategy missingFieldValueStrategy = DEFAULT_MISSING_FIELD_VALUE_STRATEGY; |
| 110 | + private UnknownFieldStrategy unknownFieldStrategy = DEFAULT_UNKNOWN_FIELD_STRATEGY; |
107 | 111 | private final ArrayDeque<ReflectionAccessFilter> reflectionFilters = new ArrayDeque<>();
|
108 | 112 |
|
109 | 113 | /**
|
@@ -141,6 +145,8 @@ public GsonBuilder() {
|
141 | 145 | this.useJdkUnsafe = gson.useJdkUnsafe;
|
142 | 146 | this.objectToNumberStrategy = gson.objectToNumberStrategy;
|
143 | 147 | this.numberToNumberStrategy = gson.numberToNumberStrategy;
|
| 148 | + this.missingFieldValueStrategy = gson.missingFieldValueStrategy; |
| 149 | + this.unknownFieldStrategy = gson.unknownFieldStrategy; |
144 | 150 | this.reflectionFilters.addAll(gson.reflectionFilters);
|
145 | 151 | }
|
146 | 152 |
|
@@ -388,6 +394,36 @@ public GsonBuilder setObjectToNumberStrategy(ToNumberStrategy objectToNumberStra
|
388 | 394 | return this;
|
389 | 395 | }
|
390 | 396 |
|
| 397 | + /** |
| 398 | + * Configures Gson to apply a specific missing field value strategy during deserialization. |
| 399 | + * The strategy is used during reflection-based deserialization when the JSON data does |
| 400 | + * not contain a value for a field. A field with explicit JSON null is not considered missing. |
| 401 | + * |
| 402 | + * @param missingFieldValueStrategy strategy handling missing field values |
| 403 | + * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern |
| 404 | + * @see MissingFieldValueStrategy#DO_NOTHING The default missing field value strategy |
| 405 | + * @since $next-version$ |
| 406 | + */ |
| 407 | + public GsonBuilder setMissingFieldValueStrategy(MissingFieldValueStrategy missingFieldValueStrategy) { |
| 408 | + this.missingFieldValueStrategy = Objects.requireNonNull(missingFieldValueStrategy); |
| 409 | + return this; |
| 410 | + } |
| 411 | + |
| 412 | + /** |
| 413 | + * Configures Gson to apply a specific unknown field strategy during deserialization. |
| 414 | + * The strategy is used during reflection-based deserialization when an unknown field |
| 415 | + * is encountered in the JSON data. |
| 416 | + * |
| 417 | + * @param unknownFieldStrategy strategy handling unknown fields |
| 418 | + * @return a reference to this {@code GsonBuilder} object to fulfill the "Builder" pattern |
| 419 | + * @see UnknownFieldStrategy#IGNORE The default unknown field strategy |
| 420 | + * @since $next-version$ |
| 421 | + */ |
| 422 | + public GsonBuilder setUnknownFieldStrategy(UnknownFieldStrategy unknownFieldStrategy) { |
| 423 | + this.unknownFieldStrategy = Objects.requireNonNull(unknownFieldStrategy); |
| 424 | + return this; |
| 425 | + } |
| 426 | + |
391 | 427 | /**
|
392 | 428 | * Configures Gson to apply a specific number strategy during deserialization of {@link Number}.
|
393 | 429 | *
|
@@ -782,7 +818,8 @@ public Gson create() {
|
782 | 818 | serializeSpecialFloatingPointValues, useJdkUnsafe, longSerializationPolicy,
|
783 | 819 | datePattern, dateStyle, timeStyle, new ArrayList<>(this.factories),
|
784 | 820 | new ArrayList<>(this.hierarchyFactories), factories,
|
785 |
| - objectToNumberStrategy, numberToNumberStrategy, new ArrayList<>(reflectionFilters)); |
| 821 | + objectToNumberStrategy, numberToNumberStrategy, |
| 822 | + missingFieldValueStrategy, unknownFieldStrategy, new ArrayList<>(reflectionFilters)); |
786 | 823 | }
|
787 | 824 |
|
788 | 825 | private void addTypeAdaptersForDate(String datePattern, int dateStyle, int timeStyle,
|
|
0 commit comments