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
Maps with numeric keys in YAML files are not parsed correctly #21
Conversation
YAMLConfiguration conf(yaml); | ||
} | ||
|
||
CASE("JSON-style map with unquoted numeric keys") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JSON keys should always be strings, since objects are composed of name/value pairs.
Please see https://www.json.org/json-en.html
So this case invalid.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correction, this case is valid if we consider that you are parsing is as YAML. YAML does accept numeric values as keys
I think it is important to distinguish between parsing the YAML and the interface in the Configuration class. Configuration is meant to be used with keys that are strings, in the JSON sense of named values. If you are trying to use YAML configuration to input numeric data into an application, then Configuration is not the interface for that purpose. |
I have added a test to YAML parsing that quoted keys are correctly parsed as Numeric values. |
I agree.
But isn't input of numeric data precisely what methods such as I would agree that the Perhaps the most elegant way to retrieve such maps would be through dedicated methods such as |
No. There is a difference between the types for the keys and the types for the values.
See above for example of sequence of numbers.
Maps with strings as keys is a consequence of the named keys concept. Configuration is meant to be used with named entries. If you are trying to load a map of numbered index to values, I suggest use another abstraction, or create a new abstraction that loads numeric tables from files (which can be YAML). Again, please recall you can use the YAML parser separate from the Configuration class. |
I think
would be conceptually no different from the examples you've given if it was possible to tell However, I see that this feature is unlikely to be added to
instead. |
In any case, many thanks for your feedback on this issue. |
The
YAMLConfiguration
class does not handle correctly maps containing numeric keys. These keys are stored asValue
objects storing an instance ofNumberContent
; as a result, their values can't be accessed via theConfiguration::get()
function, since it expects the keys to be strings. Out of the four ways to write a map in a YAML file (in the block or flow style, with or without quoted keys), the only way that works is the JSON-like flow style with quoted keys.I'm not sure what the best way to fix this is; this pull request simply adds a few test cases demonstrating the problem.
The
test_eckit_yaml_21
test actually expects numeric keys to be treated as numbers, but I think this is undesirable, at least when YAML is parsed to build aConfiguration
object, since the latter requires keys to be strings.