Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve serialization #8448

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft

Improve serialization #8448

wants to merge 3 commits into from

Conversation

flobernd
Copy link
Member

@flobernd flobernd commented Jan 21, 2025

Changes:

  • Improve custom converter performance.
    • Replace nested JsonSerializer.{Serialize|Deserialize} calls with direct converter access (JsonSerializerOptions.GetConverter -> converter.{Read|Write}) to prevent frequent creation of top-level JSON reader stacks.
    • Prefer direct ValueSpan access instead of allocating intermediate string values (e.g. when comparing property names or parsing/formatting values).
    • Use pre-encoded JsonEncodedText constants for compile-time constant literals (e.g. property names).
    • Use Utf8Parser/Utf8Formatter for primitive value conversions.
    • Envelope-stype marker types like e.g. SourceMarker<T> should only (de-)serialize the wrapped type to prevent unnecessary allocations.
  • Implement round-trip support for all types (limited to all JSON serializable types).
    • Requests
      • Regular requests
      • Requests with required path-parameters (deserialization works, but we need a generic way to set the path-parameters)
      • NDJSON requests (IStreamSerializable)
    • Responses
      • Regular responses
      • Dictionary responses
      • NDJSON responses (IStreamSerializable)
    • Regular classes
    • Unions
    • Enums
    • Internal variants
    • External variants
    • Untagged variants
    • Container variants
    • SortOptions special case
  • Change descriptors to wrap the corresponding object representations in order to re-use serialization code.

Breaking user facing changes:

  1. 💥 Removed Field.Format property and corresponding constructor and inferrer overloads.
    This property was not used anymore for quite some time (got replaced by the FieldAndFormat type).
  2. 💥 Field/Fields static factory methods and conversion operators no longer return nullable references but throw exceptions instead (Field) if the input string/Expression/PropertyInfo argument is null.
    This especially makes implicit conversions to Field more easily usable without having to apply the null-forgiveness operator (!) all the time.
  3. 💥 Removed FieldValue.IsLazyDocument, FieldValue.IsComposite and the corresponding members in the FieldValue.ValueKind enum. These values were not used anymore for quite some time.

Non-breaking user facing changes:

  1. All requests do now expose read/write access to path properties (like index, id, ...) which previously could only be initialized from the constructor.

Later/bonus:

  • Do not generate custom converters when property attributes are sufficient.
  • Serialization AOT support.
  • Get rid of ISelfTwoWaySerializable (used for source-body/array/dictionary requests/responses).
  • Get rid of ISelfSerializable (used for descriptors).
  • Get rid of ElasticsearchClient.SettingsTable (used for descriptors and in some other places). Replace with ContextProvider access.
  • Validate required properties and nullability.
  • Replace globally registered IsADictionaryConverterFactory with case-by-case converters.

Related to:

@flobernd flobernd changed the title Serialization refactor Improve serialization Jan 21, 2025
@flobernd flobernd force-pushed the serialization-refactor branch 8 times, most recently from 5cf34c2 to b5c10aa Compare January 31, 2025 10:18
@flobernd flobernd force-pushed the serialization-refactor branch from b5c10aa to 0a6739e Compare February 4, 2025 15:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant