-
-
Notifications
You must be signed in to change notification settings - Fork 485
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
Serializer: Need more control over how keys are emitted #644
Comments
I found a dirty workaround which does not involve modifying any code.
case TypeCode.String:
case TypeCode.Char:
eventInfo.Tag = FailsafeSchema.Tags.Str;
eventInfo.RenderedValue = value.ToString()!;
// Use dirty hack to determine if a key is being emitted.
if (emitter.IsEmittingKey() && eventInfo.RenderedValue.All(char.IsDigit))
{
// Emit numeric keys quoted.
suggestedStyle = ScalarStyle.SingleQuoted;
}
else
{
suggestedStyle = ScalarStyle.Any;
}
break; Key detection logic does this. I think it's right as I've tried it in various scenarios var state = GetState(emitter);
if (state == EmitterState.BlockMappingFirstKey || state == EmitterState.BlockMappingKey)
{
// State indicates it will emit a key next.
return true;
}
// If the next event in the queue is a mapping start, then a key will be emitted next.
return GetEventQueue(emitter).FirstOrDefault()?.GetType() == typeof(MappingStart); So it would be nice if |
I would also love to have a way to force all strings to be quoted. In my case, YAML is used for configuration files, but the fact that not every string is quoted means that users who don't know YAML very well frequently put characters in unquoted strings that are only allowed in quoted strings. |
I think this feature was added a few releases ago. Specify the default string quoting style on the serializerbuilder. If it’s still not working reopen this. |
Is your feature request related to a problem? Please describe.
Whilst building a serializer for CloudFormation (yeah, that old cookie), I find that mapping keys are always emitted as unquoted strings. In a CF template some keys may be numeric, e.g. an AWS account ID as a key in the
Mappings
section of the template. CloudFormation insists that these keys are quoted.Debugging through the code leads me to
Emitter.EmitBlockMappingKey
. I find that the scalar key to be emitted has the following properties...thus the mapping key is emitted without quotes.
If I put in the following hack, then the key is emitted with quotes
Describe the solution you'd like
Some mechanism for setting up the behaviour of the emitter to be added to
SerializerBuilder
such that the strategy for emitting keys can be controlled.This could also provide at least some of the solution for #591 since it could apply when a key has the string value
"null"
.Describe alternatives you've considered
None. I see no way to achieve the required behaviour without direct code changes in YamlDotNet.
I may attempt a PR, but would like guidance on how you think a change like this might be implemented.
The text was updated successfully, but these errors were encountered: