YamlDotNet icon indicating copy to clipboard operation
YamlDotNet copied to clipboard

Renaming a property via an attribute + better aliasing method

Open xucian opened this issue 4 years ago • 6 comments

Hey,

Great library, thanks!

Something that it lacks is the ability to easily rename a node. Unity does this via a FormerlySerializedAs attribute. I'm sure it's not a big deal integrating a similar functionality here. In my particular case, I want to rename some properties from "prop" to "Prop" to follow C# Naming convention, but that's very hard to do manually on a big project. Can this functionality be added ?

Also, another very useful feature would be having something like this:

[YamlDeserializeAlias("name")]
[YamlDeserializeAlias("Name")]
[YamlSerializeAlias("Name")]
public string Name {get; set;}

In case the property is found both as name and Name inside the serialized data, use the first one that has a compatible scalar type.

xucian avatar May 22 '20 10:05 xucian

You can use the YamlMember attribute to specify a name for the member, by setting the Alias property. Providing alternative names is not supported. I'm not sure if that would be easy to add.

aaubry avatar May 22 '20 10:05 aaubry

My problem is I need the EnsureRoundtrip() on the serializer while renaming the prop and also keep its value. The current aliasing won't work.

Side question: Does naming conventions require both the name in the serialized file and the name in the code to be the same or does it only refer to the former?

In any case, is there an easy way of reading a property with name X and serializing it with name Y (which corresponds to a rename)?

xucian avatar May 26 '20 11:05 xucian

Can you provide more details ? I'm not sure what you are trying to achieve. Can you explain in detail what you tried to do, what output you got and what you would expect ?

This is an example that does what I tried to explain: https://dotnetfiddle.net/awXNnp But I'm not sure if that's what you need.

Regarding naming conventions, the purpose of naming conventions is exactly to have different ways of naming identifiers in YAML and C#. The naming conventions assume that C# identifiers follow the standard conventions, and provide a way to map to the corresponding YAML identifier.

aaubry avatar May 26 '20 11:05 aaubry

I see. Thanks. Your example does the first part - deserializing from lowercase to uppercase. I also need the second part - serializing from uppercase to uppercase using the same code. Any plans about integrating this in the near future?

xucian avatar May 29 '20 10:05 xucian

You can do that if you use a different configuration for the serializer and the deserializer. Instead of adding the YamlMember attribute to the property, you can configure it on the builders:

var deserializer = new DeserializerBuilder()
	.WithAttributeOverride<Data>(d => d.Text, new YamlMemberAttribute
	{
		Alias = "my_text",
		ApplyNamingConventions = false
	})
	.Build();
		
var serializer = new SerializerBuilder()
	.WithAttributeOverride<Data>(d => d.Text, new YamlMemberAttribute
	{
		Alias = "MY_TEXT",
		ApplyNamingConventions = false
	})
	.EnsureRoundtrip()
	.Build();

https://dotnetfiddle.net/8nGJ6z

aaubry avatar May 29 '20 11:05 aaubry

That makes sense! Thank you, Antoine!

xucian avatar May 29 '20 11:05 xucian