Skip to content

Add SerializationFeature.ORDER_SET_ENTRIES_BY_ELEMENTS for #3166#5776

Closed
dlwldnjs1009 wants to merge 2 commits intoFasterXML:2.xfrom
dlwldnjs1009:fix/3166-order-set-entries
Closed

Add SerializationFeature.ORDER_SET_ENTRIES_BY_ELEMENTS for #3166#5776
dlwldnjs1009 wants to merge 2 commits intoFasterXML:2.xfrom
dlwldnjs1009:fix/3166-order-set-entries

Conversation

@dlwldnjs1009
Copy link
Copy Markdown
Contributor

@dlwldnjs1009 dlwldnjs1009 commented Mar 13, 2026

Summary

This adds a Phase 1 implementation for #3166 by introducing global Set ordering during serialization.

  • add SerializationFeature.ORDER_SET_ENTRIES_BY_ELEMENTS (disabled by default)
  • add SerializationFeature.FAIL_ON_ORDER_SET_BY_INCOMPARABLE_ELEMENT (enabled by default)
  • add ordering hooks to both CollectionSerializer and StringCollectionSerializer
  • add behavioral regression tests for ordered, null, incomparable, and typed paths

Implementation notes

Set<String> takes the specialized StringCollectionSerializer path, while other Set types use CollectionSerializer, so both serializers need the ordering hook.

The hook is placed in serializeContents(), which also covers the serializeWithType() path because AsArraySerializerBase delegates there directly.

SortedSet and EnumSet are left as-is.

For incomparable elements, the implementation attempts sorting and catches ClassCastException. FAIL_ON_ORDER_SET_BY_INCOMPARABLE_ELEMENT controls whether serialization should fail or simply skip sorting.

Tests

Adds CollectionSerializationOrderTest covering:

  • feature disabled / enabled behavior
  • integer and string Set ordering
  • SortedSet and EnumSet
  • null handling
  • incomparable elements (fail and skip)
  • polymorphic serialization
  • custom content serializer fallback

Out of scope

Per-property annotation support (JsonFormat.Feature counterpart) is intentionally deferred to a follow-up change.

Support deterministic Set serialization with a new SerializationFeature
and tests for String and general Collection paths.
Verify that SortedSet with a custom comparator (e.g. reverseOrder)
preserves its own ordering when the feature is enabled.
@cowtowncoder
Copy link
Copy Markdown
Member

We are trying to stop adding new features in 2.x and mostly just apply fixes so I think this should go against 3.x.

@dlwldnjs1009
Copy link
Copy Markdown
Contributor Author

Closing this in favor of a new PR against 3.x, per your suggestion. The implementation has been adapted to the 3.x APIs. Thank you for the guidance.

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.

2 participants