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

Add support for enum-based map keys #8

Open
AmibeSkyfy16 opened this issue Mar 7, 2023 · 2 comments
Open

Add support for enum-based map keys #8

AmibeSkyfy16 opened this issue Mar 7, 2023 · 2 comments
Labels
enhancement New feature or request

Comments

@AmibeSkyfy16
Copy link

AmibeSkyfy16 commented Mar 7, 2023

Im getting this error:
Caused by: java.lang.UnsupportedOperationException: map key must be a non-nullable string
full error: https://pastebin.com/AAD74gky

my data class looks like this

@serializable
data class Config(
val map: MutableMap<ExplosionType, Boolean>,
) : Validatable

ExplosionType is an enum

but if I do this: val map: MutableMap<ExplosionType?, Boolean?>, it works again

I saw this bug on github, maybe updating kotlinx.serialization to 1.5.0 can fix the bug (Kotlin/kotlinx.serialization#2176)

@AmibeSkyfy16
Copy link
Author

For example this code will preduce an error
https://pasteboard.co/PYioYh4KXi8b.png

@xn32
Copy link
Owner

xn32 commented Mar 12, 2023

At the time being, this is actually the expected behavior.

From a JSON5 point of view, object keys (officially referred to as JSON5MemberName) are non-nullable strings, either in the form of an unquoted identifier or in the form of a quoted string literal (see here).

I would argue that even for simple types such as Map<String?, Int>, parsing JSON5 text can be ambiguous. For example, how shall the text {null: 100} be parsed? Does the key refer to an absent value or null as a string literal?

Accepting certain primitives (such as floating-point numbers) can be difficult as well. For example, keep in mind that for the Float data type, all decimal numbers from 1.99999983 to 1.99999994 actually refer to the very same value. How would you handle a case in which multiple such values appear as keys? Shall they be treated as duplicates?

Unfortunately, the library is often unable to detect that such map keys are part of the data model until a prohibited value appears. Then, however, an UnsupportedOperationException will be raised.

The only type for which I see an intuitive one-to-one mapping to strings are non-nullable enum values. During my attempt to implement this feature, I ran into this issue, which I am currently not sure how to handle from the perspective of json5k. As soon as this is resolved, I am happy to add enum key support to the library.

@xn32 xn32 changed the title Bugs Add support for enum-based map keys Mar 12, 2023
@xn32 xn32 added the enhancement New feature or request label Mar 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants