-
Notifications
You must be signed in to change notification settings - Fork 624
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
Conversation
formats/json/commonMain/src/kotlinx/serialization/json/internal/TreeJsonDecoder.kt
Outdated
Show resolved
Hide resolved
formats/json/commonMain/src/kotlinx/serialization/json/JsonNamingStrategy.kt
Show resolved
Hide resolved
formats/json/commonMain/src/kotlinx/serialization/json/internal/JsonNamesMap.kt
Outdated
Show resolved
Hide resolved
…ies' names. Provide basic implementation of SnakeCase strategy Fixes #33
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 Therefore, enum support would be removed entirely from this PR. I'll reopen #209 for it. |
5431f78
to
dedb52e
Compare
formats/json/commonMain/src/kotlinx/serialization/json/JsonNamingStrategy.kt
Show resolved
Hide resolved
formats/json/commonMain/src/kotlinx/serialization/json/internal/JsonNamesMap.kt
Outdated
Show resolved
Hide resolved
formats/json/commonMain/src/kotlinx/serialization/json/internal/JsonNamesMap.kt
Show resolved
Hide resolved
* 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 |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 🤔
How can i use this feature? |
Like:
Edit: |
Thank you so much! |
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 |
to be used in Json for enums and properties (configured separately)
Provide basic implementations: AllLowercase and SnakeCase
Fixes #33