Dump as JSON

Issue #1084 open
Mikael Ståldal created an issue

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)

  1. Andrey Somov

    Please be aware that

    “true”
    

    and

    true
    

    are not the same

    but

    “!! bool 'true'”
    

    and

    true
    

    are the same

  2. Andrey Somov

    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 sure DumperOptions.ScalarStyle.JSON would work…

  3. Mikael Ståldal reporter

    Good question. They cannot be SINGLE_QUOTED, LITERAL or FOLDED, 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.

  4. Log in to comment