yaml2json
yaml2json copied to clipboard
YAML Specific symbols support: .inf, .nan, ~, and multi-documents
I have looked inside this repo to find if it able to convert some YAML specific symbols like .inf
, .-inf
, .nan
, and ~
.
In one of my own projects, when I have needed to same conversion from YAML to JSON it was resolved as:
-
.inf
=2147483647
(math.MaxInt32) -
-.inf
=-2147483648
(math.MinInt32) -
.nan
=0
-
~
=null
And what about YAML multi-document files? It might be 2 functions where first will fetch only first document (by compatibility reasons) and processes it as JSON, and second fetches all documents and returns array of JSON converted docs.
some examples?
some examples?
According to the YAML Spec 1.2.2 section "10.2.1.4. Floating Point", and "2.2. Structures".
Input YAML:
key1: .inf
key2: .nan
key3: ~
---
key4: Second
key5: Document
Output JSONs:
{
"key1": 2147483647,
"key2": 0,
"key3": null
}
{
"key4": "Second",
"key5": "Document"
}
you change the value of the data, it not looks good for me. (I know json do not allow +Inf/-Inf/Nan )
you change the value of the data, it not looks good for me. (I know json do not allow +Inf/-Inf/Nan )
You made this great Go lib, thank you. Anyway, I would like to use converter without any worries about unsuported YAML-to-JSON values. Right now, I can't be sure about successful convert even if I have valid YAML.
It's always up to you. But this lib might be better with this little changes. Maybe, it can be applied as an additional optional parameters for converter function to force convert YAML specific symbols?
Thanks for your example. I will make it convertible. I think we should make any valid yaml example convertible to valid json and document any abnormal staff. (like https://github.com/golang/go/issues/59627)
.inf
.nan
~
Supported in v1.3.2
v1.3.2 will translate .inf to "+Inf" .nan to "NaN" ~ to null in order to not loss any value infomation. (but will lose type infomation...)
In future version (may be tag as v1.3.3):
- Add an option to match the behavior to what your asked here .
- Add an option to match the behavior to returns array of JSON converted docs. (https://github.com/bronze1man/yaml2json/issues/19)
- Add an option to match the behavior to returns first yaml object.