Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions docs/json/annotations.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,9 +235,15 @@ checks. A skipped duplicate does not erase an earlier value: `{"seats":["a"],"se
keeps `"a"`. Constructor-bypassed objects have no initializer to preserve.

`SET` preserves codec behavior: null may become `Optional.empty()`, or fail for a primitive or
non-null Kotlin value. `SKIP` and `FAIL` act before that decoding. Only the literal JSON null is
affected; the string `"null"` and a custom codec returning Java null for another token are not
filtered. These options do not affect writing or change the root value's null behavior.
non-null Kotlin value. `SKIP` and `FAIL` handle a literal JSON null before that decoding; the string
`"null"` is an ordinary value.

`SKIP` also applies when a selected codec returns Java null for a non-null JSON value. For example,
a trimming property codec that decodes `" "` to null leaves the current value unchanged, and a
trimming `elementCodec` omits that collection element. This covers property and creator
assignments, Any values, collection, reference-array and `AtomicReferenceArray` elements, and map
values. `SET` assigns such a null normally, and `FAIL` rejects only a literal JSON null. These
options do not affect writing or change the root value's null behavior.

Content overrides are shallow. For `List<List<String>>`, a property override skips null inner
lists; elements inside retained lists follow the global setting. Map keys are unaffected, and a
Expand Down
7 changes: 5 additions & 2 deletions docs/json/custom-codecs.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,11 @@ is an array element, collection element, map value, Optional value, or atomic-re
codec receives and owns null during writing. During reading, the codec owns null when the
applicable read setting is `SET`. Property `onNullRead`
and Fory container `onContentNullRead` settings intercept literal null before invoking a selected
child codec when configured as `SKIP` or `FAIL`. A codec returning Java null after consuming a
non-null token is not filtered. The registered instance is shared across concurrent operations and
child codec when configured as `SKIP` or `FAIL`. Under `SKIP`, a Java null returned after
consuming a non-null token is also omitted: the property keeps its current value, and the
collection element, reference-array or `AtomicReferenceArray` element, map value, or Any value is
not inserted. `SET` and `FAIL` assign
such a value normally. The registered instance is shared across concurrent operations and
must be thread-safe.

A complete custom container codec owns its internal representation, so the global content setting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ public ForyJsonBuilder defaultPropertyInclusion(Include inclusion) {
* Sets the default handling of explicit JSON null property values. Defaults to {@code SET}.
* Property {@link org.apache.fory.json.annotation.JsonProperty#onNullRead()} overrides it. {@code
* SKIP} preserves the current field value or existing missing-creator-argument behavior; it does
* not fabricate defaults. Root null values and writing are unaffected.
* not fabricate defaults. {@code SKIP} also applies when the property's codec returns Java null
* for a non-null JSON token. Root null values and writing are unaffected.
*
* @throws IllegalArgumentException if handling is {@code DEFAULT}
* @throws NullPointerException if handling is null
Expand All @@ -140,7 +141,9 @@ public ForyJsonBuilder onNullRead(NullHandling handling) {
* Sets the default handling of explicit null elements and map values in Fory-owned containers,
* including root containers. Defaults to {@code SET}. Property {@link
* org.apache.fory.json.annotation.JsonProperty#onContentNullRead()} overrides the immediate
* container's setting. Complete custom codecs retain their representation. Writing is unaffected.
* container's setting. {@code SKIP} also omits a Java null returned by a child codec for a
* collection, reference-array, or AtomicReferenceArray element, a map value, or an Any value.
* Complete custom codecs retain their representation. Writing is unaffected.
*
* @throws IllegalArgumentException if handling is {@code DEFAULT}
* @throws NullPointerException if handling is null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@
* Returns how an explicit JSON null is read for this property. {@code DEFAULT} inherits the
* builder setting. {@code SKIP} consumes the null without assigning the field, invoking a setter,
* or marking a creator argument present; existing missing-property defaults and requirements
* still apply. This setting affects reading only.
* still apply. {@code SKIP} treats a Java null returned by the property's codec for a non-null
* JSON token the same way. This setting affects reading only.
*/
NullHandling onNullRead() default NullHandling.DEFAULT;

Expand All @@ -97,8 +98,9 @@
* values are read. {@code SKIP} omits an element or map insertion. Nested containers resolve
* their own settings. Explicit {@code SKIP} and {@code FAIL} require a container representation
* owned by Fory; explicit {@code SET} also accepts a complete custom container codec. Encoded
* binary strings and non-container targets reject explicit settings. This does not filter values
* returned by a custom codec for non-null JSON tokens.
* binary strings and non-container targets reject explicit settings. {@code SKIP} also omits a
* Java null returned by the child codec of a collection, reference array, AtomicReferenceArray,
* or map value for a non-null JSON token; {@code FAIL} rejects only explicit JSON null.
*/
NullHandling onContentNullRead() default NullHandling.DEFAULT;

Expand All @@ -108,7 +110,10 @@ enum NullHandling {
DEFAULT,
/** Use the normal type and codec read/assignment, including existing nullability checks. */
SET,
/** Consume the null without assigning the property or inserting the container item. */
/**
* Consume the null without assigning the property or inserting the container item. A Java null
* decoded from a non-null JSON token by the selected codec is treated the same way.
*/
SKIP,
/** Reject an explicit JSON null. Missing properties are unaffected. */
FAIL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,22 @@ static void reserveReferenceBatch(JsonReader reader, int size) {
}
}

/**
* Removes nulls decoded by a reference element codec after SKIP already dropped explicit JSON
* nulls. Compacting after the loop keeps each batch reservation ahead of the batch's final
* element read; the reservation still covers every decoded element.
*/
static int removeDecodedNulls(Object[] values, int size) {
int kept = 0;
for (int i = 0; i < size; i++) {
Object value = values[i];
if (value != null) {
values[kept++] = value;
}
}
return kept;
}

static void finishReferenceArray(JsonReader reader, int size) {
int tailSize = size & ARRAY_BATCH_MASK;
reader.reserveGraphMemory(ARRAY_HEADER_BYTES + tailSize * REFERENCE_BYTES);
Expand Down Expand Up @@ -2716,6 +2732,9 @@ public T readLatin1(Latin1JsonReader reader) {
} while (reader.consumeNextCommaOrEndArray());
}
reader.reserveGraphMemory(ARRAY_HEADER_BYTES + (size & ARRAY_BATCH_MASK) * REFERENCE_BYTES);
if (onContentNullRead == NullHandling.SKIP) {
size = removeDecodedNulls(values, size);
}
T array = (T) (size == values.length ? values : Arrays.copyOf(values, size));
reader.exitDepth();
return array;
Expand Down Expand Up @@ -2748,6 +2767,9 @@ public T readUtf16(Utf16JsonReader reader) {
} while (reader.consumeNextCommaOrEndArray());
}
reader.reserveGraphMemory(ARRAY_HEADER_BYTES + (size & ARRAY_BATCH_MASK) * REFERENCE_BYTES);
if (onContentNullRead == NullHandling.SKIP) {
size = removeDecodedNulls(values, size);
}
T array = (T) (size == values.length ? values : Arrays.copyOf(values, size));
reader.exitDepth();
return array;
Expand Down Expand Up @@ -2780,6 +2802,9 @@ public T readUtf8(Utf8JsonReader reader) {
} while (reader.consumeNextCommaOrEndArray());
}
reader.reserveGraphMemory(ARRAY_HEADER_BYTES + (size & ARRAY_BATCH_MASK) * REFERENCE_BYTES);
if (onContentNullRead == NullHandling.SKIP) {
size = removeDecodedNulls(values, size);
}
T array = (T) (size == values.length ? values : Arrays.copyOf(values, size));
reader.exitDepth();
return array;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1162,7 +1162,10 @@ public Collection<?> readLatin1(Latin1JsonReader reader) {
if ((size & REFERENCE_BATCH_MASK) == REFERENCE_BATCH_MASK) {
reader.reserveGraphMemory(REFERENCE_BATCH_BYTES);
}
collection.add(requireElement(elementTypeInfo, codec.readLatin1(reader)));
Object element = codec.readLatin1(reader);
if (element != null || onContentNullRead != NullHandling.SKIP) {
collection.add(requireElement(elementTypeInfo, element));
}
size++;
} while (reader.consumeNextCommaOrEndArray());
}
Expand Down Expand Up @@ -1195,7 +1198,10 @@ public Collection<?> readUtf16(Utf16JsonReader reader) {
if ((size & REFERENCE_BATCH_MASK) == REFERENCE_BATCH_MASK) {
reader.reserveGraphMemory(REFERENCE_BATCH_BYTES);
}
collection.add(requireElement(elementTypeInfo, codec.readUtf16(reader)));
Object element = codec.readUtf16(reader);
if (element != null || onContentNullRead != NullHandling.SKIP) {
collection.add(requireElement(elementTypeInfo, element));
}
size++;
} while (reader.consumeNextCommaOrEndArray());
}
Expand Down Expand Up @@ -1228,7 +1234,10 @@ public Collection<?> readUtf8(Utf8JsonReader reader) {
if ((size & REFERENCE_BATCH_MASK) == REFERENCE_BATCH_MASK) {
reader.reserveGraphMemory(REFERENCE_BATCH_BYTES);
}
collection.add(requireElement(elementTypeInfo, codec.readUtf8(reader)));
Object element = codec.readUtf8(reader);
if (element != null || onContentNullRead != NullHandling.SKIP) {
collection.add(requireElement(elementTypeInfo, element));
}
size++;
} while (reader.consumeNextCommaOrEndArray());
}
Expand Down Expand Up @@ -1326,7 +1335,10 @@ public Collection<?> readLatin1(Latin1JsonReader reader) {
if ((size & REFERENCE_BATCH_MASK) == REFERENCE_BATCH_MASK) {
reader.reserveGraphMemory(REFERENCE_BATCH_BYTES);
}
collection.add(requireElement(elementTypeInfo, codec.readLatin1(reader)));
Object element = codec.readLatin1(reader);
if (element != null || onContentNullRead != NullHandling.SKIP) {
collection.add(requireElement(elementTypeInfo, element));
}
size++;
} while (reader.consumeNextCommaOrEndArray());
}
Expand Down Expand Up @@ -1362,7 +1374,10 @@ public Collection<?> readUtf16(Utf16JsonReader reader) {
if ((size & REFERENCE_BATCH_MASK) == REFERENCE_BATCH_MASK) {
reader.reserveGraphMemory(REFERENCE_BATCH_BYTES);
}
collection.add(requireElement(elementTypeInfo, codec.readUtf16(reader)));
Object element = codec.readUtf16(reader);
if (element != null || onContentNullRead != NullHandling.SKIP) {
collection.add(requireElement(elementTypeInfo, element));
}
size++;
} while (reader.consumeNextCommaOrEndArray());
}
Expand Down Expand Up @@ -1398,7 +1413,10 @@ public Collection<?> readUtf8(Utf8JsonReader reader) {
if ((size & REFERENCE_BATCH_MASK) == REFERENCE_BATCH_MASK) {
reader.reserveGraphMemory(REFERENCE_BATCH_BYTES);
}
collection.add(requireElement(elementTypeInfo, codec.readUtf8(reader)));
Object element = codec.readUtf8(reader);
if (element != null || onContentNullRead != NullHandling.SKIP) {
collection.add(requireElement(elementTypeInfo, element));
}
size++;
} while (reader.consumeNextCommaOrEndArray());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,10 @@ public void writeUtf8(Utf8JsonWriter writer, Map<?, ?> value) {
if ((size & ENTRY_BATCH_MASK) == ENTRY_BATCH_MASK) {
reader.reserveGraphMemory(ENTRY_BATCH_BYTES);
}
map.put(key, readValue(reader, codec));
Object value = readValue(reader, codec);
if (value != null || onContentNullRead != NullHandling.SKIP) {
map.put(key, value);
}
size++;
} while (reader.consumeNextToken(','));
reader.expectNextToken('}');
Expand Down Expand Up @@ -709,7 +712,10 @@ public void writeUtf8(Utf8JsonWriter writer, Map<?, ?> value) {
if ((size & ENTRY_BATCH_MASK) == ENTRY_BATCH_MASK) {
reader.reserveGraphMemory(ENTRY_BATCH_BYTES);
}
map.put(key, readValue(reader, codec));
Object value = readValue(reader, codec);
if (value != null || onContentNullRead != NullHandling.SKIP) {
map.put(key, value);
}
size++;
} while (reader.consumeNextToken(','));
reader.expectNextToken('}');
Expand Down Expand Up @@ -745,7 +751,10 @@ public void writeUtf8(Utf8JsonWriter writer, Map<?, ?> value) {
if ((size & ENTRY_BATCH_MASK) == ENTRY_BATCH_MASK) {
reader.reserveGraphMemory(ENTRY_BATCH_BYTES);
}
map.put(key, readValue(reader, codec));
Object value = readValue(reader, codec);
if (value != null || onContentNullRead != NullHandling.SKIP) {
map.put(key, value);
}
size++;
} while (reader.consumeNextToken(','));
reader.expectNextToken('}');
Expand Down
Loading
Loading