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
Change the default serializer behaviour to emit default values #427
Comments
My thinking is if you want to make it a pure breaking change, take the opportunity to design forward and change API's as needed so that old code wont run, or at least can be easily seen as "the old version", etc. Many of the features you speak of are really multiple smaller separate features in my mind and they are split by serializer verses deserializer:
Honestly the flexibility that #6 would give the library would enable all that and more, imho. This also allows callbacks to be used to control the serialized output/input for very advanced scenarios but still keep you safe from people complaining about defaults they cant change. Just a few ideas before I go to bed. :) |
Thanks for your input, @duaneking.
I don't think that the serializer should throw exceptions when a null value is encountered. First because the YAML specification defines null values and thus they should be allowed, and second because it is not the responsibility of a serialization library to perform this kind of validation. That responsibility belongs to a properly designed domain model. Other well-known libraries such as Json.NET seem to agree with this.
Agreed, that's equivalent to my suggestion (2).
Agreed, that's equivalent to my suggestion (3).
We can provide this option for completeness, although I am not convinced that is has that much value.
As for you first point, I don't think that the deserializer should disallow nulls. The domain model should be responsible to validation and ensuring that it doesn't accept invalid values, which is more general than simply disallowing null values. For many use cases, it is simple enough to implement validation based on arbitrary rules.
I agree with extending YamlMemberAttribute. There's no need for a callback system. It is already possible to achieve what you describe by replacing
Sounds good.
Sounds good. I think the only point where we disagree is whether to disallow nulls by default. I even think that this is outside of the scope of this issue, but I'm willing to consider counter-arguments of course. |
What does the rest of the community say? |
I will be away until September. Let's see if anyone has anything to comment, and I will pick this up when I return. |
So I have submitted a PR that addresses this issue. It changes the default behaviour to alway emit all properties, regardless of their value. It is then possible to request to omit properties whose value is There's currently no out-of-the-box way to configure the behaviour for a specific type because it is trivial to extend the serializer to add more complex rules. As I discussed this does nothing to prevent null values while parsing or emitting documents. I don't think that a serialization library should make this kind of decisions. Comments are welcome. |
YamlDotNet 8.0.0, which includes this change, has just been released. |
I don't want null properties to be included. How do I get rid of them? |
That is part of your code, not this library. This bug as created because the old way was a bad design and created a leaky abstraction. You need to have a better handle on your interfaces. |
My use case is to accomplish polymorphism by having a composite type hold all of the possible derived classes, with only one property being set, which corresponds to the appropriate class. I have since found the setting though to change it to prune null values: My use case matches how Ansible YAML files represent polymorphism, though I don't know what their parsing procedure is. I bring in the raw DTO from the deserialized YAML and then pass it through Automapper to make the concrete model. Example of such a YAML in case that wasn't clear:
With the YamlDotNet default, it was serializing into this:
Anyways, I got it working, so I am happy now. Just wanted to point out the reason I wanted this. PS: I should probably do this with a IYamlTypeConverter instead, but I need to better learn how use that first. |
As evidenced by #318, #310, #298, #268, #251, #258, #153, the current behaviour of omitting default values during serialization is misleading and a source of confusion.
I'm opening this issue so that we can discuss the best way to handle this. I suggest the following changes, but feel free to propose alternatives:
Additionally, we could offer some or all of the following options:
YamlMemberAttribute
to override the emission of defaults on a specific member.I think that performing changes (1) and (2) is necessary to properly address the issue. I am inclined on also including (3) and (4) because they offer more flexibility. I don't see any use case for (5), but if someone can come up with a relevant one, let's discuss it.
For more complex behaviour, it is already possible to replace
DefaultExclusiveObjectGraphVisitor
by a custom implementation that can decide whether to emit any value based on arbitrary rules.The text was updated successfully, but these errors were encountered: