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

feat: Make Yaml nodes serializable #642

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

EdwarDDay
Copy link
Contributor

@EdwarDDay EdwarDDay changed the title Make Yaml nodes serializable feat: Make Yaml nodes serializable Nov 17, 2024
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 @EdwarDDay!

import kotlinx.serialization.encoding.Encoder
import kotlinx.serialization.encoding.encodeStructure

internal object YamlNodeSerializer : YamlContentPolymorphicSerializer<YamlNode>(YamlNode::class) {
Copy link
Owner

Choose a reason for hiding this comment

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

Can't this just be an ordinary KSerializer?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed. I mainly did it, because the Marker annotation is needed for the polymorphic handling.

Comment on lines +72 to +83
try {
return encoder.encodeBoolean(value.toBoolean())
} catch (_: SerializationException) {
}
try {
return encoder.encodeLong(value.toLong())
} catch (_: SerializationException) {
}
try {
return encoder.encodeDouble(value.toDouble())
} catch (_: SerializationException) {
}
Copy link
Owner

Choose a reason for hiding this comment

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

Rather than this chain of try / catch, couldn't we inspect the value and determine the best option to use?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I didn't want to copy the parsing code, to avoid duplicated code. Do you have an idea how to implement this without duplicating most of the YamlScalar code?
I mean Boolean has extra parsing code, integer values extra checks for octal, decimal and hexadecimal numbers, floating point numbers have extra checks for infinity and NaN. This was too much code for me to copy. Else the library gets inconsistent with future changes.

Comment on lines +65 to +71
is PolymorphicKind -> {
if (descriptor.isContentBasedPolymorphic) {
createContextual(node, yaml, context, configuration, descriptor)
} else {
throw MissingTypeTagException(node.path)
}
}
Copy link
Owner

Choose a reason for hiding this comment

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

Why is this change required?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To be able to decode a YamlList still as YamlNode. The YamlNodeSerializer.descriptor is polymorphic (because we don't know the type of it). So a list needs to be readable as polymorphic.

Comment on lines 810 to 812
"'foo'": "'bar'"
"'baz'": 1
"'test'":
Copy link
Owner

Choose a reason for hiding this comment

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

Why are the values quoted twice here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, that was an error. Fixed it.

- fix small issues
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.

Make YamlNode serializable
2 participants