-
-
Notifications
You must be signed in to change notification settings - Fork 498
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
Pass ScalarStyle to members of Dictionary #478
Comments
The problem with this is that each element of a Dictionary will potentially produce two scalars. One for the key and another for the value. This means that, the following dictionary: new Dictionary<string, object> {
{ "one", 1 },
{ "two", 2 }
} would serialize as: "one": "1"
"two": "2" While this may be desirable in some cases, I'm sure that many people would want to control the scalar style of the keys and values independently. Also, it may not be appropriate to use the |
Yes, in fact it is not that easy. What we needed is quotes around every string value in a Dictionary<string, object>, which can again contain Dictionary<string, object>s or Lists. We hard-coded it by putting this code in TraverseDictionary after
We did the same in TraverseList:
Of course we had to make ScalarStyle public settable. Perhaps it would make sense to create a global serializer settings to achieve this behaviour. It makes a big difference if the values are quoted or not e. g. for Helm (https://helm.sh/). |
This is just what I needed. Thanks. I then just used a custom INodeTypeResolver to assume that any scalar with the DoubleQuoted style is a string. private class ScalarNodeTypeResolver : INodeTypeResolver
{
bool INodeTypeResolver.Resolve(NodeEvent nodeEvent, ref Type currentType)
{
if (currentType != typeof(object))
return false;
if (nodeEvent is Scalar scalar)
{
if (scalar.Style == ScalarStyle.DoubleQuoted)
{
currentType = typeof(string);
return true;
}
}
}
} |
There’s an option to set default quoting style now. Closing this since that should solve the problem mentioned. |
The ScalarStyle of a Dictionary is not used for members of the Dictionary.
The problem seems to be that in TraverseDictionary there is a call to GetObjectDescriptor that creates a new ObjectDescriptor with ScalarStyle = ScalarStyle.Any.
The text was updated successfully, but these errors were encountered: