107
107
*
108
108
* <h2 id="default-lenient">JSON Strictness handling</h2>
109
109
* For legacy reasons most of the {@code Gson} methods allow JSON data which does not
110
- * comply with the JSON specification when the strictness is set to {@code null} (the default value).
111
- * To specify the {@linkplain Strictness strictness} of a {@code Gson} instance, you should set it through
112
- * {@link GsonBuilder#setStrictness(Strictness)}. If the strictness of a {@code Gson} instance is set to a not-null
113
- * value, the strictness will always be enforced.
110
+ * comply with the JSON specification when no explicit {@linkplain Strictness strictness} is set (the default).
111
+ * To specify the strictness of a {@code Gson} instance, you should set it through
112
+ * {@link GsonBuilder#setStrictness(Strictness)}.
114
113
*
115
- * <p>For older Gson versions which don't have the {@link Strictness} mode API the following
114
+ * <p>For older Gson versions, which don't have the strictness mode API, the following
116
115
* workarounds can be used:
117
116
*
118
117
* <h3>Serialization</h3>
145
144
*/
146
145
public final class Gson {
147
146
static final boolean DEFAULT_JSON_NON_EXECUTABLE = false ;
147
+ // Strictness of `null` is the legacy mode where some Gson APIs are always lenient
148
148
static final Strictness DEFAULT_STRICTNESS = null ;
149
149
static final FormattingStyle DEFAULT_FORMATTING_STYLE = FormattingStyle .COMPACT ;
150
150
static final boolean DEFAULT_ESCAPE_HTML = true ;
@@ -236,7 +236,8 @@ public final class Gson {
236
236
* <li>By default, Gson excludes <code>transient</code> or <code>static</code> fields from
237
237
* consideration for serialization and deserialization. You can change this behavior through
238
238
* {@link GsonBuilder#excludeFieldsWithModifiers(int...)}.</li>
239
- * <li>The strictness is set to {@code null}.</li>
239
+ * <li>No explicit strictness is set. You can change this by calling
240
+ * {@link GsonBuilder#setStrictness(Strictness)}.</li>
240
241
* </ul>
241
242
*/
242
243
public Gson () {
@@ -808,7 +809,7 @@ public void toJson(Object src, Appendable writer) throws JsonIOException {
808
809
* <pre>
809
810
* Type typeOfSrc = new TypeToken<Collection<Foo>>(){}.getType();
810
811
* </pre>
811
- * @param writer Writer to which the JSON representation of src needs to be written.
812
+ * @param writer Writer to which the JSON representation of src needs to be written
812
813
* @throws JsonIOException if there was a problem writing to the writer
813
814
* @since 1.2
814
815
*
@@ -828,19 +829,20 @@ public void toJson(Object src, Type typeOfSrc, Appendable writer) throws JsonIOE
828
829
* Writes the JSON representation of {@code src} of type {@code typeOfSrc} to
829
830
* {@code writer}.
830
831
*
831
- <p> If the {@code Gson} instance has a not-null strictness setting, this setting will be used for reading the JSON
832
- * regardless of the {@linkplain JsonReader#getStrictness() strictness} of the provided {@link JsonReader}. For legacy
833
- * reasons, if the {@code Gson} instance has {@code null} as its strictness setting and the provided {@link JsonReader}
834
- * has a strictness of {@link Strictness#LEGACY_STRICT}, the JSON will be read in {@linkplain Strictness#LENIENT}
835
- * mode. Note that in both cases the old strictness value of the reader will be restored when this method returns.
832
+ * <p>If the {@code Gson} instance has an {@linkplain GsonBuilder#setStrictness(Strictness) explicit strictness setting},
833
+ * this setting will be used for writing the JSON regardless of the {@linkplain JsonWriter#getStrictness() strictness}
834
+ * of the provided {@link JsonWriter}. For legacy reasons, if the {@code Gson} instance has no explicit strictness setting
835
+ * and the writer does not have the strictness {@link Strictness#STRICT}, the JSON will be written in {@link Strictness#LENIENT}
836
+ * mode.<br>
837
+ * Note that in all cases the old strictness setting of the writer will be restored when this method returns.
836
838
*
837
839
* <p>The 'HTML-safe' and 'serialize {@code null}' settings of this {@code Gson} instance
838
840
* (configured by the {@link GsonBuilder}) are applied, and the original settings of the
839
841
* writer are restored once this method returns.
840
842
*
841
- * @param src the object to be written.
842
- * @param typeOfSrc the type of the object to be written.
843
- * @param writer the {@link JsonWriter} writer to which the provided object will be written.
843
+ * @param src the object for which JSON representation is to be created
844
+ * @param typeOfSrc the type of the object to be written
845
+ * @param writer Writer to which the JSON representation of src needs to be written
844
846
*
845
847
* @throws JsonIOException if there was a problem writing to the writer
846
848
*/
@@ -851,7 +853,7 @@ public void toJson(Object src, Type typeOfSrc, JsonWriter writer) throws JsonIOE
851
853
Strictness oldStrictness = writer .getStrictness ();
852
854
if (this .strictness != null ) {
853
855
writer .setStrictness (this .strictness );
854
- } else if (writer .getStrictness () == Strictness .LEGACY_STRICT ) {
856
+ } else if (writer .getStrictness () != Strictness .STRICT ) {
855
857
writer .setStrictness (Strictness .LENIENT );
856
858
}
857
859
@@ -911,9 +913,10 @@ public void toJson(JsonElement jsonElement, Appendable writer) throws JsonIOExce
911
913
* <li>{@link GsonBuilder#disableHtmlEscaping()}</li>
912
914
* <li>{@link GsonBuilder#generateNonExecutableJson()}</li>
913
915
* <li>{@link GsonBuilder#serializeNulls()}</li>
914
- * <li>{@link GsonBuilder#setStrictness(Strictness)}. If the strictness of this {@code Gson} instance
915
- * is set to {@code null}, the created writer will have a strictness of {@link Strictness#LEGACY_STRICT}.
916
- * If the strictness is set to a non-null value, this strictness will be used for the created writer.</li>
916
+ * <li>{@link GsonBuilder#setStrictness(Strictness)}. If no
917
+ * {@linkplain GsonBuilder#setStrictness(Strictness) explicit strictness has been set} the created
918
+ * writer will have a strictness of {@link Strictness#LEGACY_STRICT}. Otherwise, this strictness of
919
+ * the {@code Gson} instance will be used for the created writer.</li>
917
920
* <li>{@link GsonBuilder#setPrettyPrinting()}</li>
918
921
* <li>{@link GsonBuilder#setFormattingStyle(FormattingStyle)}</li>
919
922
* </ul>
@@ -935,9 +938,10 @@ public JsonWriter newJsonWriter(Writer writer) throws IOException {
935
938
*
936
939
* <p>The following settings are considered:
937
940
* <ul>
938
- * <li>{@link GsonBuilder#setStrictness(Strictness)}. If the strictness of this {@code Gson} instance
939
- * is set to {@code null}, the created reader will have a strictness of {@link Strictness#LEGACY_STRICT}.
940
- * If the strictness is set to a non-null value, this strictness will be used for the created reader.</li>
941
+ * <li>{@link GsonBuilder#setStrictness(Strictness)}. If no
942
+ * {@linkplain GsonBuilder#setStrictness(Strictness) explicit strictness has been set} the created
943
+ * reader will have a strictness of {@link Strictness#LEGACY_STRICT}. Otherwise, this strictness of
944
+ * the {@code Gson} instance will be used for the created reader.</li>
941
945
* </ul>
942
946
*/
943
947
public JsonReader newJsonReader (Reader reader ) {
@@ -949,18 +953,19 @@ public JsonReader newJsonReader(Reader reader) {
949
953
/**
950
954
* Writes the JSON for {@code jsonElement} to {@code writer}.
951
955
*
952
- * <p> If the {@code Gson} instance has a not-null strictness setting, this setting will be used for writing the JSON
953
- * regardless of the {@linkplain JsonWriter#getStrictness() strictness} of the provided {@link JsonWriter}. For legacy
954
- * reasons, if the {@code Gson} instance has {@code null} as its strictness setting and the provided {@link JsonWriter}
955
- * has a strictness of {@link Strictness#LEGACY_STRICT}, the JSON will be written in {@linkplain Strictness#LENIENT}
956
- * mode. Note that in both cases the old strictness value of the writer will be restored when this method returns.
956
+ * <p>If the {@code Gson} instance has an {@linkplain GsonBuilder#setStrictness(Strictness) explicit strictness setting},
957
+ * this setting will be used for writing the JSON regardless of the {@linkplain JsonWriter#getStrictness() strictness}
958
+ * of the provided {@link JsonWriter}. For legacy reasons, if the {@code Gson} instance has no explicit strictness setting
959
+ * and the writer does not have the strictness {@link Strictness#STRICT}, the JSON will be written in {@link Strictness#LENIENT}
960
+ * mode.<br>
961
+ * Note that in all cases the old strictness setting of the writer will be restored when this method returns.
957
962
*
958
963
* <p>The 'HTML-safe' and 'serialize {@code null}' settings of this {@code Gson} instance
959
964
* (configured by the {@link GsonBuilder}) are applied, and the original settings of the
960
965
* writer are restored once this method returns.
961
966
*
962
- * @param jsonElement the JSON element to be written.
963
- * @param writer the JSON writer to which the provided element will be written.
967
+ * @param jsonElement the JSON element to be written
968
+ * @param writer the JSON writer to which the provided element will be written
964
969
* @throws JsonIOException if there was a problem writing to the writer
965
970
*/
966
971
public void toJson (JsonElement jsonElement , JsonWriter writer ) throws JsonIOException {
@@ -973,7 +978,7 @@ public void toJson(JsonElement jsonElement, JsonWriter writer) throws JsonIOExce
973
978
974
979
if (this .strictness != null ) {
975
980
writer .setStrictness (this .strictness );
976
- } else if (writer .getStrictness () == Strictness .LEGACY_STRICT ) {
981
+ } else if (writer .getStrictness () != Strictness .STRICT ) {
977
982
writer .setStrictness (Strictness .LENIENT );
978
983
}
979
984
@@ -1203,9 +1208,12 @@ private static void assertFullConsumption(Object obj, JsonReader reader) {
1203
1208
* <p>Unlike the other {@code fromJson} methods, no exception is thrown if the JSON data has
1204
1209
* multiple top-level JSON elements, or if there is trailing data.
1205
1210
*
1206
- * <p>The JSON data is parsed in {@linkplain JsonReader#setStrictness(Strictness) lenient mode},
1207
- * regardless of the strictness setting of the provided reader. The strictness setting
1208
- * of the reader is restored once this method returns.
1211
+ * <p>If the {@code Gson} instance has an {@linkplain GsonBuilder#setStrictness(Strictness) explicit strictness setting},
1212
+ * this setting will be used for reading the JSON regardless of the {@linkplain JsonReader#getStrictness() strictness}
1213
+ * of the provided {@link JsonReader}. For legacy reasons, if the {@code Gson} instance has no explicit strictness setting
1214
+ * and the reader does not have the strictness {@link Strictness#STRICT}, the JSON will be written in {@link Strictness#LENIENT}
1215
+ * mode.<br>
1216
+ * Note that in all cases the old strictness setting of the reader will be restored when this method returns.
1209
1217
*
1210
1218
* @param <T> the type of the desired object
1211
1219
* @param reader the reader whose next JSON value should be deserialized
@@ -1232,11 +1240,12 @@ public <T> T fromJson(JsonReader reader, Type typeOfT) throws JsonIOException, J
1232
1240
* <p>Unlike the other {@code fromJson} methods, no exception is thrown if the JSON data has
1233
1241
* multiple top-level JSON elements, or if there is trailing data.
1234
1242
*
1235
- * <p> If the {@code Gson} instance has a not-null strictness setting, this setting will be used for reading the JSON
1236
- * regardless of the {@linkplain JsonReader#getStrictness() strictness} of the provided {@link JsonReader}. For legacy
1237
- * reasons, if the {@code Gson} instance has {@code null} as its strictness setting and the provided {@link JsonReader}
1238
- * has a strictness of {@link Strictness#LEGACY_STRICT}, the JSON will be read in {@linkplain Strictness#LENIENT}
1239
- * mode. Note that in both cases the old strictness value of the reader will be restored when this method returns.
1243
+ * <p>If the {@code Gson} instance has an {@linkplain GsonBuilder#setStrictness(Strictness) explicit strictness setting},
1244
+ * this setting will be used for reading the JSON regardless of the {@linkplain JsonReader#getStrictness() strictness}
1245
+ * of the provided {@link JsonReader}. For legacy reasons, if the {@code Gson} instance has no explicit strictness setting
1246
+ * and the reader does not have the strictness {@link Strictness#STRICT}, the JSON will be written in {@link Strictness#LENIENT}
1247
+ * mode.<br>
1248
+ * Note that in all cases the old strictness setting of the reader will be restored when this method returns.
1240
1249
*
1241
1250
* @param <T> the type of the desired object
1242
1251
* @param reader the reader whose next JSON value should be deserialized
@@ -1260,7 +1269,7 @@ public <T> T fromJson(JsonReader reader, TypeToken<T> typeOfT) throws JsonIOExce
1260
1269
1261
1270
if (this .strictness != null ) {
1262
1271
reader .setStrictness (this .strictness );
1263
- } else if (reader .getStrictness () == Strictness .LEGACY_STRICT ) {
1272
+ } else if (reader .getStrictness () != Strictness .STRICT ) {
1264
1273
reader .setStrictness (Strictness .LENIENT );
1265
1274
}
1266
1275
0 commit comments