docling-java icon indicating copy to clipboard operation
docling-java copied to clipboard

`@JsonInclude(NON_EMPTY)` makes DoclingDocument serialization lossy

Open rkudryashov opened this issue 4 months ago • 11 comments

Serve keeps "text": "" on formula items (do_formula_enrichment=false). The Java DoclingDocument still has text as "", but serialization drops it:

DoclingDocument doc = response.getDocument().getJsonContent();
// doc still has text == ""
byte[] bytes = jsonMapper.writeValueAsBytes(doc); // "text" omitted in JSON

Include.ALWAYS on the mapper does not help — class-level @JsonInclude(NON_EMPTY) wins. No raw json_content accessor either.

Wanted: lossless serialize, or raw json_content bytes/JsonNode.

Related: docling#3780, docling-java#386

rkudryashov avatar Jul 20 '26 09:07 rkudryashov

@all-contributors add @rkudryashov for issue

edeandrea avatar Jul 20 '26 12:07 edeandrea

@edeandrea

I couldn't determine any contributions to add, did you specify any contributions? Please make sure to use valid contribution names.

allcontributors[bot] avatar Jul 20 '26 12:07 allcontributors[bot]

@all-contributors add @rkudryashov for bug

edeandrea avatar Jul 20 '26 12:07 edeandrea

@edeandrea

I've put up a pull request to add @rkudryashov! :tada:

allcontributors[bot] avatar Jul 20 '26 12:07 allcontributors[bot]

Hi @rkudryashov I'm not sure I fully understand the issue? Could you explain a bit more?

Are you saying that "on the wire" the field is present in the json but with an empty value, but after deserialization the field is null?

edeandrea avatar Jul 20 '26 12:07 edeandrea

@edeandrea Not quite — after deserialization the Java field is still "" (not null). The loss happens on the way back out:

  1. Serve wire JSON: "text": ""
  2. DoclingDocument in memory: text == ""
  3. jsonMapper.writeValueAsBytes(doc) → text key absent (class-level @JsonInclude(NON_EMPTY))

Setting Include.ALWAYS on the JsonMapper/ObjectMapper does not help, because @JsonInclude(NON_EMPTY) on the model class takes precedence. So with the public API there is no straightforward way to persist a lossless DoclingDocument JSON that keeps "text": "" — writeValueAsBytes(doc) will drop those fields.

rkudryashov avatar Jul 20 '26 13:07 rkudryashov

Let me ask a question - do you consider an empty string different from null?

edeandrea avatar Jul 20 '26 14:07 edeandrea

Yes. For us "" and null/absent are different.

text: "" is valid DoclingDocument state (e.g. formulas with enrichment off). After Jackson serialize + S3, empty strings are dropped → text is missing.

When we rehydrate that JSON we hit two failures:

  1. Schema validation — we validate against the official DoclingDocument JSON Schema (DoclingDocument.json). Missing text fails validation; "text": "" passes.
  2. Chunking — hybrid / json_docling chunking then fails on the same invalid document.

rkudryashov avatar Jul 20 '26 14:07 rkudryashov

Let me check with the docling team on the intention (i.e. do they differentiate between empty/null/absent). I've always been in the camp that "" == null/absent, but if the docling team specifically treats it differently then we should respect that here. If they don't have any guidance or they don't care, then I'm not sure you can make that assumption on your side.

edeandrea avatar Jul 20 '26 14:07 edeandrea

@rkudryashov thanks for the detailed report, and for tracking the root cause back here from docling-project/docling#3780 and docling-project/docling#4107.

The fix is in #723. The cause was what you described: the inner classes of DoclingDocument carry a class-level @JsonInclude(NON_EMPTY), which drops "" on write, and a mapper-level inclusion setting doesn't override it.

On whether "" is different from null or absent: docling hasn't answered that directly, but the DoclingDocument JSON schema settles the part that matters here. text and orig are required strings, so "" is valid and a missing field is not. #723 keeps "" as "" by adding a field-level @JsonInclude(NON_NULL) to the 23 required text/orig fields. Empty lists are still omitted, and a field that is really unset (null) is still left out.

This issue will close automatically when #723 merges.

edeandrea avatar Oct 01 '26 17:10 edeandrea

🎉 This issue has been resolved in v0.7.0 (Release Notes)

docling-java-ops[bot] avatar Oct 05 '26 17:10 docling-java-ops[bot]