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

Provide a way to detect which field exists. #2685

Closed
iseki0 opened this issue May 24, 2024 · 3 comments
Closed

Provide a way to detect which field exists. #2685

iseki0 opened this issue May 24, 2024 · 3 comments
Labels

Comments

@iseki0
Copy link
Contributor

iseki0 commented May 24, 2024

What is your use-case and why do you need this feature?
We often use null as a placeholder for the field which doesn't exists. Example:

{
  "foo": 1
}
@Serializable
data class A(val foo: Int, val bar: Int? = null)
println(Json.decodeFromString<A>("...").bar) // null

But some times, the null is a meaningful value, such as deletion.

Passing null value to clear the field in the database.
If you don't passing the field, the field in database will be leaved unchanged.

Describe the solution you'd like
Provide a new annotation, mark an additional field as the "bitmap". Such as:

@Serializable
data class A(
    val foo: Int,
    val bar: Int? = null,
    @FieldsRecord val declaredFields: Set<String> = emptySet(),
)

assert("bar" !in Json.decodeFromString<A>("...").declaredFieds)

To achieve this goal we need to update the plugin.

If that's a huge feature and you don't like it. Could you provide a way to generate the KSerializer even we use a customized KSerializer? (In that case I try to generate my own serializer that will co-work with the plugin generated one.)

@iseki0 iseki0 added the feature label May 24, 2024
@iseki0
Copy link
Contributor Author

iseki0 commented May 24, 2024

The special Set<String> might be internal implemented using a bitmap, co-works with the SerialDescriptor(which is singleton).

@sandwwraith
Copy link
Member

This request seems to be highly-specific, and introducing a whole new annotation to support this narrow use-case is indeed feels like overkill, so I'll close this for now. I remember similar ideas that had been reported before, and they all eventually converged to a custom serializer that treats null specifically.

Could you provide a way to generate the KSerializer even we use a customized KSerializer? (In that case I try to generate my own serializer that will co-work with the plugin generated one.)

It is #1169 and it will be available in 2.0.20

@iseki0
Copy link
Contributor Author

iseki0 commented May 27, 2024

It is #1169 and it will be available in 2.0.20

So that I can use my own generated serializer to delegate the default generated serializer with a delegated decoder to archieve that. 😎

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants