-
Notifications
You must be signed in to change notification settings - Fork 50
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
base: main
Are you sure you want to change the base?
feat: Make Yaml nodes serializable #642
Conversation
EdwarDDay
commented
Nov 17, 2024
- fixes Make YamlNode serializable #641
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.
Thanks for the PR @EdwarDDay!
import kotlinx.serialization.encoding.Encoder | ||
import kotlinx.serialization.encoding.encodeStructure | ||
|
||
internal object YamlNodeSerializer : YamlContentPolymorphicSerializer<YamlNode>(YamlNode::class) { |
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.
Can't this just be an ordinary KSerializer
?
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.
Changed. I mainly did it, because the Marker
annotation is needed for the polymorphic handling.
try { | ||
return encoder.encodeBoolean(value.toBoolean()) | ||
} catch (_: SerializationException) { | ||
} | ||
try { | ||
return encoder.encodeLong(value.toLong()) | ||
} catch (_: SerializationException) { | ||
} | ||
try { | ||
return encoder.encodeDouble(value.toDouble()) | ||
} catch (_: SerializationException) { | ||
} |
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.
Rather than this chain of try
/ catch
, couldn't we inspect the value and determine the best option to use?
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.
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.
is PolymorphicKind -> { | ||
if (descriptor.isContentBasedPolymorphic) { | ||
createContextual(node, yaml, context, configuration, descriptor) | ||
} else { | ||
throw MissingTypeTagException(node.path) | ||
} | ||
} |
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.
Why is this change required?
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.
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.
"'foo'": "'bar'" | ||
"'baz'": 1 | ||
"'test'": |
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.
Why are the values quoted twice here?
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.
Oh, that was an error. Fixed it.
- fix small issues