Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom JSON formatting #10

Open
ralfstx opened this issue Mar 3, 2013 · 7 comments
Open

Support custom JSON formatting #10

ralfstx opened this issue Mar 3, 2013 · 7 comments
Labels

Comments

@ralfstx
Copy link
Owner

ralfstx commented Mar 3, 2013

No description provided.

@angelozerr
Copy link
Contributor

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

In my case I need to generate JSON with indentation.

@ralfstx
Copy link
Owner Author

ralfstx commented Feb 1, 2015

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?

@angelozerr
Copy link
Contributor

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

@ralfstx
Copy link
Owner Author

ralfstx commented Feb 2, 2015

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.

@angelozerr
Copy link
Contributor

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.

@blongstreth
Copy link

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.

@sroughley
Copy link

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants