YamlDotNet icon indicating copy to clipboard operation
YamlDotNet copied to clipboard

Wrap all string values in quotes when serializing, if possible

Open PintTheDragon opened this issue 3 years ago • 1 comments

I'd like to wrap string values in quotes during serialization, if it's possible to do so safely. YamlDotNet is being used to handle configuration files, and one of the biggest issues I've encountered is that the default value for a key is serialized without quotes, so if a user modifies it to include a character which requires surrounding quotes, it results in a parsing error. I've looked into #315, although it seems as though the method described there doesn't give as much control as is desired and surrounds keys in quotes as well as values. Any help with solving this would be greatly appreciated!

For reference, this is for https://github.com/Exiled-Team/EXILED/issues/563.

PintTheDragon avatar Aug 16 '22 20:08 PintTheDragon

Right now the only way of doing this would be to add the YamlMember attribute to all of your properties, which may not be feasible in your case, one thing we could do is add a feature to the serializer where you can specify the default string quoting behavior.

Without digging in to this too much, I think the place we would need to do this is adding a constructor property (defaulted to null or a new constructor) with the type of quoting to use here:

https://github.com/aaubry/YamlDotNet/blob/fd6731d4ab6bb4f3bec79a47b9ecb60f0d8e415f/YamlDotNet/Serialization/EventEmitters/TypeAssigningEventEmitter.cs#L46

and implement the feature here:

https://github.com/aaubry/YamlDotNet/blob/fd6731d4ab6bb4f3bec79a47b9ecb60f0d8e415f/YamlDotNet/Serialization/EventEmitters/TypeAssigningEventEmitter.cs#L111

EdwardCooke avatar Sep 08 '22 18:09 EdwardCooke

Hello, I'd just like to 👍 this. I've been having an issue when serializing foreign data that frequently has has strings that are formatted as valid YAML ints (e.g. 01). Naturally, this is an issue when reifying into a collection with a homogeneous type.

I'd be happy to contribute a PR if no one else is planning on taking it on.

tymokvo avatar Jul 13 '23 18:07 tymokvo

Go for it, I think it would be great to have something on the serializerbuilder that would be something like WithDefaultScalarStyle

EdwardCooke avatar Jul 13 '23 18:07 EdwardCooke

I opened #827 on this topic. Looking forward to everyone's feedback!

tymokvo avatar Jul 17 '23 15:07 tymokvo