Skip to content

Conversation

@dkhalanskyjb
Copy link
Contributor

Fixes #2948

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR updates the documentation for various serializer() overloads to mention that type unsoundness may result in incorrect results or [ClassCastException] if improperly used.

  • Added a clear Pitfall section warning against potential type mismatches.
  • Revised examples and wording in multiple serializer() overloads to improve clarity.

*
* This overload works with full type information, including type arguments and nullability,
* and is a recommended way to retrieve a serializer.
* For example, `serializer<typeOf<List<String?>>>()` returns [KSerializer] that is able
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know what this dependent-type-style syntax means and was assuming that this was a copy-paste artifact of some sort.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I mistyped the function call: it should be serializer(typeOf<List<String?>>()). Can you please bring this paragraph back in the correct form?

* and is a recommended way to retrieve a serializer.
* For example, `serializer<typeOf<List<String?>>>()` returns [KSerializer] that is able
* to serialize and deserialize list of nullable strings — i.e. `ListSerializer(String.serializer().nullable)`.
* For example, `serializerOrNull(typeOf<List<String?>>)` returns [KSerializer] that is able
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* For example, `serializerOrNull(typeOf<List<String?>>)` returns [KSerializer] that is able
* For example, `serializer(typeOf<List<String?>>())` returns [KSerializer] that is able

This is documentation for regular serializer() function, and typeOf needs invocation

* Caching on JVM platform is disabled for this function, so it may work slower than an overload with [KType].
*
* **Pitfall**: the returned serializer may return incorrect results or throw a [ClassCastException] if it receives
* a value that's not a valid instance of the [KType], even though the type allows passing such a value.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* a value that's not a valid instance of the [KType], even though the type allows passing such a value.
* a value that's not a valid instance of the [KClass], even though the type allows passing such a value.

*
* This overload works with full type information, including type arguments and nullability,
* and is a recommended way to retrieve a serializer.
* For example, `serializerOrNull<typeOf<List<String?>>>()` returns [KSerializer] that is able
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line also needs change to serializerOrNull(typeOf<List<String?>>()) while we're here

* and is a recommended way to retrieve a serializer.
* For example, `serializer<typeOf<List<String?>>>()` returns [KSerializer] that is able
* to serialize and deserialize list of nullable strings — i.e. `ListSerializer(String.serializer().nullable)`.
* For example, `serializer(typeOf<List<String?>>)` returns [KSerializer] that is able
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* For example, `serializer(typeOf<List<String?>>)` returns [KSerializer] that is able
* For example, `serializer(typeOf<List<String?>>())` returns [KSerializer] that is able

*
* This overload works with full type information, including type arguments and nullability,
* and is a recommended way to retrieve a serializer.
* For example, `serializerOrNull<typeOf<List<String?>>>()` returns [KSerializer] that is able
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And here, if you do not mind

@dkhalanskyjb dkhalanskyjb requested a review from sandwwraith June 25, 2025 13:43
@sandwwraith sandwwraith merged commit 596bac8 into dev Jun 25, 2025
3 of 4 checks passed
@dkhalanskyjb dkhalanskyjb deleted the dkhalanskyjb/fix-2948 branch June 26, 2025 07:02
JesusMcCloud pushed a commit to JesusMcCloud/kotlinx.serialization that referenced this pull request Aug 5, 2025
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.

3 participants