yaml2json icon indicating copy to clipboard operation
yaml2json copied to clipboard

YAML Specific symbols support: .inf, .nan, ~, and multi-documents

Open jkulvich opened this issue 1 year ago • 6 comments

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.

jkulvich avatar May 17 '23 09:05 jkulvich

some examples?

bronze1man avatar May 18 '23 01:05 bronze1man

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"
}

jkulvich avatar May 19 '23 07:05 jkulvich

you change the value of the data, it not looks good for me. (I know json do not allow +Inf/-Inf/Nan )

bronze1man avatar May 21 '23 07:05 bronze1man

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?

jkulvich avatar May 21 '23 19:05 jkulvich

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)

bronze1man avatar May 22 '23 02:05 bronze1man

.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.

bronze1man avatar May 23 '23 16:05 bronze1man