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 Serialization Configuration for Char and Upgrade Kotlin #75

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

HatoYuze
Copy link

@HatoYuze HatoYuze commented Jan 27, 2025

fix #53 , #68

✨ Add Serialization Configuration for Char

When creating a Yaml object, you can specify the handling logic for char:

 public enum class CharSerialization {
        SINGLE_QUOTATION,
        DOUBLE_QUOTATION,
        UNICODE_CODE,
        PLAIN,
}

By default, this setting (charSerialization in YamlConfigurationInternal) is set to PLAIN.

Among these, UNICODE_CODE supports the serialization and deserialization of Unicode. You can now handle Char with logic similar to Byte.

Additionally, escaping support for char serialization has been added to prevent certain characters (like '\n', '"', etc.) from being incorrectly input into the content. They will now be escaped with quotes.

The handling logic here is similar to String.

⬆️ Upgrade kotlin

Upgraded the project's kotlin version to 2.0.0. For compatibility, also updated kotlinx.serialization to 1.7.0.

I encountered the same compilation issue as in #72 while attempting the update and fixed it by referring to the corresponding comment.

Therefore, theoretically, this PR should also support the content in #72 and it seems it should fix #67 as well.

feat: Add char serialization using Unicode code points
fix: Correct serialization for chars with special characters
Also:
Updated kotlinx.serialization to 1.7.0 for compatibility
@OMGCA
Copy link

OMGCA commented Jan 27, 2025

😊

@Him188 Him188 self-requested a review January 27, 2025 08:52
Comment on lines 71 to 80
@Test
fun testCharEscape() {
assertEquals("\" \"", doubleChar.encodeToString<Char>(' '))
assertEquals("\' \'", singleChar.encodeToString<Char>(' '))
assertEquals("\' \'", PLAIN.encodeToString<Char>(' '))
assertEquals("32", unicode.encodeToString<Char>(' '))

assertEquals(' ', unicode.decodeFromString<Char>("32"))
}

Copy link
Owner

Choose a reason for hiding this comment

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

IIRC class properties are handled differently than top-level values. We should also test serializable classes:

@Serializable
class Container(
    val c: Char,
)

Copy link
Author

Choose a reason for hiding this comment

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

Where should I place this test? Should it be in an existing test class or should I create a new class?

Copy link
Owner

Choose a reason for hiding this comment

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

The same file is a good one.

@HatoYuze
Copy link
Author

@Him188 Do you think we should support some special codes? For example, should we support deserialization of 'a' from U+0061 (in Unicode number), &#97; (in HTML code), and \0061 (in CSS code)? Or should we add these formats to enum CharSerialization?

@Him188
Copy link
Owner

Him188 commented Jan 31, 2025

If that's in the YAML spec then we are good to support them, if not, then we have to consider whether they are really needed.

This PR is already good enough. Let's focus on fixing the bug first.

Copy link
Owner

@Him188 Him188 left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks!

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.

Add watchosDeviceArm64 无法正确序列化空字节
3 participants