Dump as JSON
It would be useful to be able to dump as JSON. SnakeYAML is almost, but not quite, able to do that with these DumperOptions
:
- defaultFlowStyle = DumperOptions.FlowStyle.FLOW
- defaultScalarStyle = DumperOptions.ScalarStyle.DOUBLE_QUOTED
- width = Int.MAX_VALUE
You can valid JSON with some post processing (Kotlin code, but you get the idea):
.replace("""!!null "null"""", "null")
.replace("""!!bool "true"""", "true")
.replace("""!!bool "false"""", "false")
.replace("""!!int "([+-]?[0-9]+)"""".toRegex(), "$1")
Maybe add DumperOptions.ScalarStyle.JSON
?
Comments (12)
-
-
- marked as minor
-
assigned issue to
-
- changed status to open
-
Please be aware that
“true”
and
true
are not the same
but
“!! bool 'true'”
and
true
are the same
-
reporter -
I got you now. I was thinking about kind of “flexible“ scalar style. I will check it next week.
If you have any solution - you are welcome ! I am not sureDumperOptions.ScalarStyle.JSON
would work… -
- marked as major
-
For you information. there is a proposal to make FlowStyle also flexible.
-
I will most probably try to implement the JSON style for SnakeYAML Engine (where we explicitly support JSON)
SnakeYAML is not fully JSON compatible.
-
Can you please help to identify which scalar style should be used for all the types in the spec ?
https://yaml.org/type/index.html
Double quoted - for !!str
Plain - for !!bool, !!float, !!int, !!null
What about all the others ?
-
reporter Good question. They cannot be
SINGLE_QUOTED
,LITERAL
orFOLDED
, that's not correct JSON.We can use
DOUBLE_QUOTED
for!!binary
and!!timestamp
.I don't think that
!!yaml
,!!merge
or!!value
can be supported at all when dumping to JSON, maybe just throw an exception if trying to.
-
I agree (started to work on it…)
- Log in to comment
a test is worth of thousand words. Can you please create a (failing) test ?