Skip to content

Simplify JSON and XML de/serialization in Java - #679

Merged
mristin merged 1 commit into
mainfrom
mristin/Simplify-java-jsonization-and-xmlization
Sep 5, 2026
Merged

Simplify JSON and XML de/serialization in Java#679
mristin merged 1 commit into
mainfrom
mristin/Simplify-java-jsonization-and-xmlization

Conversation

@mristin

@mristin mristin commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

We add generic parseArray/serializeArray helpers for JSON list (de)serialization, replacing per-property inlined boilerplate with shared calls.

We factor out a generic serializeElement helper for XML serialization, mirroring C# (#676). It unifies the previously duplicated writeStartElement/topLevel/writeEndElement/ try-catch wrapping across all five property kinds (primitive, enumeration, interface, concrete class, list), which also tightens two branches' overly broad catch (Exception) down to catch (XMLStreamException), matching what the other three branches already did. A further serializeItems helper does the list-item iteration generically as well, so every list-typed property collapses to a single call instead of its own inlined for loop.

Rather than let the per-item and per-property content-writing logic live on as duplicated lambda text at each call site, we follow the shape of the merged C++ simplification (SerializeBool/ SerializeInt64/etc. plus a serialize_{enum} per enumeration): we generate named methods once -- writeStringifiedContent (shared across boolean/long/double/String, since Object.toString() is universal in Java, unlike C++), writeByteArrayContent, and one write{Enum}Content per enumeration and reference them via this::... from both the standalone property and the list-item path.

A per-enum method is still needed there, even though the string conversion itself is shared (see below), because Java resolves that overload statically per concrete enum type, so a single generic wrapper cannot call it.

We add Stringification.mustToString, a per-enum helper that returns the string representation of a literal or throws, and reuse it for JSON's {enum}ToJsonValue and for XML's per-enum content writer, removing three separate copies of the same Optional-or-throw check.

We promote the _Result<T> class -- previously defined independently, and near-identically, in both Jsonization.java and Xmlization.java -- to a single shared Reporting.Result<T>, since Reporting is already imported by both.

Finally, on the JSON side, we factor the byte-array-to-JsonNode conversion (Base64-encode, then wrap in a text node) into a bytesToJsonNode method on _Transformer, mirroring the existing toJsonNode for Long.

We add generic ``parseArray``/``serializeArray`` helpers for JSON
list (de)serialization, replacing per-property inlined boilerplate
with shared calls.

We factor out a generic ``serializeElement`` helper for XML
serialization, mirroring C# (#676). It unifies the previously
duplicated ``writeStartElement``/``topLevel``/``writeEndElement``/
try-catch wrapping across all five property kinds (primitive,
enumeration, interface, concrete class, list), which also tightens
two branches' overly broad ``catch (Exception)`` down to ``catch
(XMLStreamException)``, matching what the other three branches
already did. A further ``serializeItems`` helper does the list-item
iteration generically as well, so every list-typed property collapses
to a single call instead of its own inlined ``for`` loop.

Rather than let the per-item and per-property content-writing logic
live on as duplicated lambda text at each call site, we follow the
shape of the merged C++ simplification (``SerializeBool``/
``SerializeInt64``/etc. plus a ``serialize_{enum}`` per enumeration):
we generate named methods once -- ``writeStringifiedContent``
(shared across ``boolean``/``long``/``double``/``String``, since
``Object.toString()`` is universal in Java, unlike C++),
``writeByteArrayContent``, and one ``write{Enum}Content`` per
enumeration and reference them via ``this::...`` from both the
standalone property and the list-item path.

A per-enum method is still needed there, even though the string
conversion itself is shared (see below), because Java resolves that
overload statically per concrete enum type, so a single generic wrapper
cannot call it.

We add ``Stringification.mustToString``, a per-enum helper that
returns the string representation of a literal or throws, and reuse
it for JSON's ``{enum}ToJsonValue`` and for XML's per-enum content
writer, removing three separate copies of the same
``Optional``-or-throw check.

We promote the ``_Result<T>`` class -- previously defined
independently, and near-identically, in both ``Jsonization.java`` and
``Xmlization.java`` -- to a single shared ``Reporting.Result<T>``,
since ``Reporting`` is already imported by both.

Finally, on the JSON side, we factor the byte-array-to-``JsonNode``
conversion (``Base64``-encode, then wrap in a text node) into a
``bytesToJsonNode`` method on ``_Transformer``, mirroring the
existing ``toJsonNode`` for ``Long``.
@mristin
mristin merged commit e4549ab into main Sep 5, 2026
3 checks passed
@mristin
mristin deleted the mristin/Simplify-java-jsonization-and-xmlization branch September 5, 2026 16:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant