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

Enum properties do not serialize if set to the first value in the enum (position 0) #251

Closed
solvingj opened this issue Apr 25, 2017 · 6 comments

Comments

@solvingj
Copy link

solvingj commented Apr 25, 2017

Removing original description of issue as it's more clearly demonstrated in the first comment below.

@solvingj
Copy link
Author

solvingj commented Apr 25, 2017

Here's a more pure demonstration of the issue which is rather fundamental. You can run it here:
https://dotnetfiddle.net/WT8jon

In summary, the first value of an enum seems to fail to serialize. This can be worked around by adding a "dummy" enum in the first position. Objectively speaking this seems like a major fundamental issue with a very basic case of serialization, which might mean that it's a relatively new bug. Otherwise, I expect this would have been experienced and reported by many.

using System;
using YamlDotNet.Serialization;

public class SampleType
{
	public WorkingEnum BrokenEnumOne { get; set; }
	public WorkingEnum WorkingEnumOne { get; set; }
	public WorkingEnum WorkingEnumTwo { get; set; }
}

public enum WorkingEnum 
{
	First,
	Second,
	Third
}


public class Program
{
	public static void Main()
	{
		var serializer = new SerializerBuilder()
			.Build();
		
		SampleType sample = new SampleType
		{
			BrokenEnumOne = WorkingEnum.First,
			WorkingEnumOne = WorkingEnum.Second,
			WorkingEnumTwo = WorkingEnum.Third
		};
		
		var serialized = serializer.Serialize(sample);
		Console.Write(serialized);
	}
}

@solvingj solvingj reopened this Apr 25, 2017
@solvingj solvingj changed the title Enum do not serialize for me Enum does not serialize an enum if it's set to the first value in an enum (0) Apr 26, 2017
@solvingj solvingj changed the title Enum does not serialize an enum if it's set to the first value in an enum (0) Enum properties do not serialize if set to the first value in the enum (position 0) Apr 26, 2017
@aaubry
Copy link
Owner

aaubry commented Apr 26, 2017

This is a known (to me) issue. This is because default values are not emitted unless you call .EmitDefaults() on the SerializerBuilder. The intent was to not serialize properties as null, but as a side effect other default values ended up being dropped. There should probably be different settings to emit nulls and default values. Note that if the first value of the enum is not zero, it will be emitted correctly.

@solvingj
Copy link
Author

Ok, sounds good. I'm glad there's a ticket now. This way further people won't have to spend 3 hours debugging it and feeling stupid when trying to learn YamlDotNet.

@solvingj
Copy link
Author

So to clarify, the effective workaround is numbering the Enum and not using 0.

For example:

public enum WorkingEnum 
{
	First = 1,
	Second = 2,
	Third = 3
}

@RedlineTriad
Copy link

Could an attribute be implemented that determines whether or not the default value of a field/property is serialized?

@aaubry
Copy link
Owner

aaubry commented Aug 15, 2019

I have opened issue #427 that is related to your question. One of the points is the inclusion of such an attribute. Feel free to join the discussion and provide any feedback that might be relevant. I am closing the issue to avoid duplicate discussion.

@aaubry aaubry closed this as completed Aug 15, 2019
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

No branches or pull requests

3 participants