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

Encoding and Decoding case insensitive enums #587

Merged
merged 8 commits into from
Aug 11, 2024

Conversation

AbhigyaKrishna
Copy link
Contributor

Implemented case-insensitive enumeration encoding and decoding.

Changelogs

  • Configuration value for decodeEnumCaseInsensitive defaults to false.
  • Implementation of lenient deserialization in enum decoder.
  • Emits lowercase enum names when configured to true for consistency with Json format.
  • Implemented tests for case insensitive enums deserialization.
enum class TestEnum {
    VALUE
}

val yaml = Yaml(configuration = YamlConfiguration(decodeEnumCaseInsensitive = true))

val inResult = yaml.encodeToString(TestEnum.VALUE) // Outputs: value
val outResult = yaml.decodeFromString<TestEnum>("value") // Outputs: TestEnum.VALUE

@rasros
Copy link

rasros commented Jul 30, 2024

I need this as well. We switched from json files to yaml but are missing case-insensitive enum deserialization that the kotlinx serialization library has. Would love to see this merged soon if possible!

Comment on lines 96 to 100
override fun encodeEnum(enumDescriptor: SerialDescriptor, index: Int) {
val element = enumDescriptor.getElementName(index)

emitQuotedScalar(element, configuration.singleLineStringStyle.scalarStyle)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

note: I think if we don't change the encoding we should revert the code to the original state

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Its the same code, rather descriptive.

Copy link
Owner

@charleskorn charleskorn left a comment

Choose a reason for hiding this comment

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

Thanks for the PR!

src/commonMain/kotlin/com/charleskorn/kaml/YamlOutput.kt Outdated Show resolved Hide resolved
@@ -52,6 +54,8 @@ public data class YamlConfiguration(
internal val allowAnchorsAndAliases: Boolean = false,
internal val yamlNamingStrategy: YamlNamingStrategy? = null,
internal val codePointLimit: Int? = null,
@ExperimentalSerializationApi
Copy link
Owner

Choose a reason for hiding this comment

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

I don't think there's a need to mark this as experimental.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

All the new api features are marked as experimental in the kotlinx.serialization library when they are introduced. As a matter of fact, its their convention.

Copy link
Owner

@charleskorn charleskorn left a comment

Choose a reason for hiding this comment

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

Thanks again for the PR!

@charleskorn charleskorn merged commit 098d578 into charleskorn:main Aug 11, 2024
5 checks passed
Copy link

🎉 This PR is included in version 0.62.0 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

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.

4 participants