minimal-json icon indicating copy to clipboard operation
minimal-json copied to clipboard

Support custom JSON formatting

Open ralfstx opened this issue 12 years ago • 7 comments

ralfstx avatar Mar 03 '13 14:03 ralfstx

@ralfstx do you think you will have time to work on this issue?

In my case I need to generate JSON with indentation.

angelozerr avatar Feb 01 '15 14:02 angelozerr

Actually, I'm currently working on it ;-)

My plan is to start with a built-in pretty-printer that outputs each value on a separate line, indented by 2 spaces, for example:

{
  "an-array": [
    23,
    "foo",
    null
  ],
  "an-object": {
    "a": true,
    "b": "foo\\bar"
  }
}

I guess that will be okay in most cases. Would that be sufficient for you as well?

ralfstx avatar Feb 01 '15 16:02 ralfstx

@ralfstx very cool! I'm waiting for your release in order I will integrate it to tern.java. Thanks!

angelozerr avatar Feb 02 '15 08:02 angelozerr

A new optional parameter config has been added to the toString and writeTo methods. Example:

jsonValue.toString(WriterConfig.PRETTY_PRINT);
jsonValue.writeTo(writer, WriterConfig.PRETTY_PRINT);

More fine-grained parameters will be added to WriterConfig.

@angelozerr if you like to try it, you can build your own version from master (mvn clean install) rather than waiting for the release.

ralfstx avatar Feb 02 '15 21:02 ralfstx

Thanks @ralfstx!

I will try it, but I would like to wait the release of minimal-json because our update site is updated each time we push something in our git.

angelozerr avatar Feb 03 '15 10:02 angelozerr

I want to be able to fix the pretty print to suite my needs and it appears the JsonWriter is package protected. Could this be changed to public so that I can write my own print customizations please?

For example, My want to not newline arrays or only indent just the first level items. Being able to fully control would be nice.

blongstreth avatar May 11 '18 00:05 blongstreth

What about having a PrettyPrintConfig (for example!) interface and a default implementation which would allow users to create their own configurations without changing the JsonWriter visibility? I would think it would probably be the sort of complicated class in terms of number of possible settings which would be very amenable to a builder class with methods such as #setIndentString(String indent) which all return the builder to allow daisy-chaining method calls?

sroughley avatar May 11 '18 08:05 sroughley