Skip to content

How to serialize enum to int #863

Closed Answered by EdwardCooke
GardenHamster asked this question in Q&A
Discussion options

You must be logged in to vote

I don't think it does this out of the box, nor do I think there is an option for it. You could create a custom type converter for it though.

Here's a very basic example.

https://github.com/aaubry/YamlDotNet/blob/master/YamlDotNet/Serialization/Converters/GuidConverter.cs

In the accepts type you could do a type.IsEnum or something like that and write out an scalar with the int version of the enum in the WriteYaml and read in a Scalar as the int and parse it through Enum.TryParse (or something like that) and return that value.

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by GardenHamster
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants