Controlling the type of a map key value

109 views
Skip to first unread message

Tim Quinn

unread,
Jan 11, 2021, 7:29:05 PM1/11/21
to SnakeYAML
I feel as if the solution to this is in front of me using a custom TypeDefinition or something similar but I'm just not seeing it. (I did some customizing of our SnakeYAML parsing some time ago and I'm having to relearn what I have forgotten.)

To process part of the YAML input, I need to translate some input YAML to a `Map<String, MyType>` on the Java object.

Each key in the input YAML can be either an integer or a string. 

When SnakeYAML parses those keys that happen to be integers, it understandably creates an integer.

As a result, in `BaseConstructor.constructMapping2ndStep`, the code fails when it tries to `put` the key and value to the mapping because the key type of the map on `MyType` is `String` but the key value SnakeYAML has created is an `Integer`.

What's the cleanest way for me to make sure that, when processing this property on MyType, the key value is created as a `String`?

Thanks.

mfried...@gmx.de

unread,
Jan 12, 2021, 7:52:23 AM1/12/21
to snakeya...@googlegroups.com
Hello Timm,

maybe your YAML is invalid? If you want to express a numeric as string you must put it into quotes, same goes for booleans like true or false or the nil value.

Best Regards
Mirko Friedenhagen

Sent from my mobile

Am 12.01.21 um 01:29 schrieb Tim Quinn

--
You received this message because you are subscribed to the Google Groups "SnakeYAML" group.
To unsubscribe from this group and stop receiving emails from it, send an email to snakeyaml-cor...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/snakeyaml-core/092dd3e7-601b-445e-9294-abdcda69341cn%40googlegroups.com.

Tim Quinn

unread,
Jan 12, 2021, 9:07:19 AM1/12/21
to SnakeYAML
Oh, yes. The code in question is reading an OpenAPI document, and the problem was with a test YAML file that was incorrect.

Thanks!

Reply all
Reply to author
Forward
0 new messages