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

Renaming a property via an attribute + better aliasing method #495

Closed
xucian opened this issue May 22, 2020 · 7 comments
Closed

Renaming a property via an attribute + better aliasing method #495

xucian opened this issue May 22, 2020 · 7 comments

Comments

@xucian
Copy link

xucian commented May 22, 2020

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.

@aaubry
Copy link
Owner

aaubry commented May 22, 2020

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.

@xucian
Copy link
Author

xucian commented May 26, 2020

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)?

@aaubry
Copy link
Owner

aaubry commented May 26, 2020

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.

@xucian
Copy link
Author

xucian commented May 29, 2020

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?

@aaubry
Copy link
Owner

aaubry commented May 29, 2020

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

@xucian
Copy link
Author

xucian commented May 29, 2020

That makes sense! Thank you, Antoine!

@EdwardCooke
Copy link
Collaborator

Looks like this was answered closing it.

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