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!

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.

Copy link
Owner

Choose a reason for hiding this comment

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

What if we just always encode the value as a string?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Then we would encode a double 1.2 as "1.2", right? So the output might be unexpected to the user.

I could create toBooleanOrNull, toByteOrNull, … functions in the YamlScalar class and use it in toBoolean, toByte, … functions and in the YamlScalarSerializer. This way the code doesn't get duplicated and we don't have to try catch everything.

- fix small issues
@EdwarDDay EdwarDDay requested a review from charleskorn December 1, 2024 07:37
Comment on lines +82 to +83
value.content.singleOrNull()?.also { return encoder.encodeChar(it) }
encoder.encodeString(value.content)
Copy link
Owner

Choose a reason for hiding this comment

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

Is there a test for this special handling of characters?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, I just wanted to use the short version of encodeChar, to not have the overhead from encodeString. There are also no extra tests for encodeBoolean, encodeLong, encodeDouble. I thought, these are properly tested in the tests for YamlOutput.
But I can of course write extra tests, if you think, that's needed.

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.

What if we just always encode the value as a string?

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