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 support for JsonNamingStrategy #2111

Merged
merged 5 commits into from
Jan 24, 2023
Merged

Conversation

sandwwraith
Copy link
Member

to be used in Json for enums and properties (configured separately)

Provide basic implementations: AllLowercase and SnakeCase

Fixes #33

…ies' names.

Provide basic implementation of SnakeCase strategy

Fixes #33
@sandwwraith
Copy link
Member Author

sandwwraith commented Jan 13, 2023

After some consideration and discussions, we've decided that enum parsing is an entirely different feature from naming strategy: while strategy changes names, enum parsing should affect values. Besides, there are some cases when the strategy for enums is not enough: e.g. you want to parse both Foo and foo into FOO, like Jackson's CASE_INSENSETIVE_ENUMS.

Therefore, enum support would be removed entirely from this PR. I'll reopen #209 for it.

@sandwwraith
Copy link
Member Author

@qwwdfsad @shanshin It's ready for review

* Therefore, it is essential for this function to be pure: it should not have any side effects, and it should
* return the same String for a given [descriptor], [elementIndex], and [serialName], regardless of the number of invocations.
*/
public fun serialNameForJson(descriptor: SerialDescriptor, elementIndex: Int, serialName: String): String
Copy link
Contributor

Choose a reason for hiding this comment

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

The serialName parameter could possibly be renamed to elementSerialName as there is also a type serial name. (The documentation here, especially the guarantee in code, make the semantics clear though. The parameter still does present a redundant parameter (as it can be retrieved trivially).

Copy link
Member Author

Choose a reason for hiding this comment

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

In the initial prototype, there was only descriptor and serialName; you can easily also say that elementIndex is redundant, as you can get it via descriptor.getElementName(). However, I think it is easier to provide both of them to avoid hassle

Copy link
Member Author

Choose a reason for hiding this comment

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

Or, it is possible, to name it elementName instead 🤔

@sandwwraith sandwwraith merged commit 60c632c into dev Jan 24, 2023
@sandwwraith sandwwraith deleted the json-naming-strategy branch January 24, 2023 19:28
@Angel-Ponce
Copy link

How can i use this feature?
You probably need to drop an example when pushed new features, in my opinion the main documentation page it's a little bit poor

@sschuberth
Copy link
Contributor

sschuberth commented Apr 24, 2024

How can i use this feature?

Like:

import kotlinx.serialization.json.JsonNamingStrategy

val json = Json {
    ignoreUnknownKeys = true
    namingStrategy = JsonNamingStrategy.SnakeCase
    serializersModule = module
}

Edit: ignoreUnknownKeys = true and serializersModule = module are not relevant, they were just part of my code.

@Angel-Ponce
Copy link

How can i use this feature?

Like:

import kotlinx.serialization.json.JsonNamingStrategy

val json = Json {
    ignoreUnknownKeys = true
    namingStrategy = JsonNamingStrategy.SnakeCase
    serializersModule = module
}

Thank you so much!

@sandwwraith
Copy link
Member Author

All the reference (including this example) is located in our guide: https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/json.md#global-naming-strategy

This pull request was closed.
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.

6 participants