Simplify JSON and XML de/serialization in Java - #679
Merged
Conversation
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``.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
We add generic
parseArray/serializeArrayhelpers for JSON list (de)serialization, replacing per-property inlined boilerplate with shared calls.We factor out a generic
serializeElementhelper for XML serialization, mirroring C# (#676). It unifies the previously duplicatedwriteStartElement/topLevel/writeEndElement/ try-catch wrapping across all five property kinds (primitive, enumeration, interface, concrete class, list), which also tightens two branches' overly broadcatch (Exception)down tocatch (XMLStreamException), matching what the other three branches already did. A furtherserializeItemshelper does the list-item iteration generically as well, so every list-typed property collapses to a single call instead of its own inlinedforloop.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 aserialize_{enum}per enumeration): we generate named methods once --writeStringifiedContent(shared acrossboolean/long/double/String, sinceObject.toString()is universal in Java, unlike C++),writeByteArrayContent, and onewrite{Enum}Contentper enumeration and reference them viathis::...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}ToJsonValueand for XML's per-enum content writer, removing three separate copies of the sameOptional-or-throw check.We promote the
_Result<T>class -- previously defined independently, and near-identically, in bothJsonization.javaandXmlization.java-- to a single sharedReporting.Result<T>, sinceReportingis already imported by both.Finally, on the JSON side, we factor the byte-array-to-
JsonNodeconversion (Base64-encode, then wrap in a text node) into abytesToJsonNodemethod on_Transformer, mirroring the existingtoJsonNodeforLong.