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

Add custom indent for pretty printing #1279

Closed
WasylF opened this issue Apr 3, 2018 · 9 comments
Closed

Add custom indent for pretty printing #1279

WasylF opened this issue Apr 3, 2018 · 9 comments

Comments

@WasylF
Copy link

WasylF commented Apr 3, 2018

It would be great if we can specify custom indent while call pretty printing. Currently pretty printing always use 2 spaces for indent. This customization also allows to reduce size of resulting json.
Comparison table and code for generating json below:

Indent \ setsCount 100 2000
2 spaces 99,7 kb 50,9 MB
1 tab 79,5 kb 42,9 MB
nothing 59,3 kb 34,9 MB
        List<Set<Integer>> sets = new ArrayList<Set<Integer>>();
        int setsCount = 2000;
        for (int setN = 0; setN < setsCount; setN++) {
            TreeSet<Integer> set = new TreeSet<Integer>();
            for (int i = 0; i < setsCount; i++) {
                set.add(setN * setsCount + i);
            }
            sets.add(set);
        }

        String json = gsonManager.getGson().toJson(sets);

@JakeWharton
Copy link
Contributor

What's the motivation? If you care about size you aren't using pretty printing.

@WasylF
Copy link
Author

WasylF commented May 17, 2018

My team's project stores some large configuration data in the git repository as JSON. If I don't use pretty option the resulting file contains a single line with the whole JSON, so git couldn't show a difference between different versions. Currently, I use pretty JSON, but in my case, it increases the size of the JSON from 30 - 40 MB up to 100+ MB. Bigger resulting JSON also requires more RAM for converting. So, I suppose it would be cool to have such feature as a custom indent.
And the other reason is that war between people using tabs and spaces is endless:) So, it is great to have both options.

@bonoboris
Copy link

I agree, if you are appending to an existing Json with either 2, 4 spaces or tab indent, it would be nice to have such a feature. I'm actually suprised there isn't.

@yurymann
Copy link

+1 for configurable indent size

@JakeWharton , my motivation is being able to compare jsons generated with gson against json files formatted in other tools, in my case IntelliJ Idea.

@nickgeorge
Copy link

+1 for configurability!

@Conrad-T-Pino
Copy link

Already implemented in 2.8.7 release.

Class com.google.gson.stream.JsonWriter has public final void setIndent​(String indent); which:

Sets the indentation string to be repeated for each level of indentation in the encoded document. If indent.isEmpty() the encoded document will be compact. Otherwise the encoded document will be more human-readable.

This implementation indents with tab and appends final newline:

void run(final File file, final JsonElement element) {
	final Gson gsonPrettyPrinting = new GsonBuilder().setPrettyPrinting().create();
	try (final PrintWriter pWriter = new PrintWriter(file)) {
		final JsonWriter jWriter = gsonPrettyPrinting.newJsonWriter(pWriter);
		jWriter.setIndent("\t");
		gsonPrettyPrinting.toJson(element, jWriter);
		pWriter.println();
	} catch (final IOException e) {
	}
}

Best regards!

@eamonnmcmanus
Copy link
Member

I believe @Conrad-T-Pino is right that this feature has been implemented, so the issue can be closed.

@Conrad-T-Pino
Copy link

Untested JasonElement to String case:

String run(final JsonElement element) {
	final Gson gsonPrettyPrinting = new GsonBuilder().setPrettyPrinting().create();
	try (final StringWriter sWriter = new StringWriter(file)) {
		final JsonWriter jWriter = gsonPrettyPrinting.newJsonWriter(sWriter);
		jWriter.setIndent("\t");
		gsonPrettyPrinting.toJson(element, jWriter);
		sWriter.write(System.lineSeparator());
		return sWriter.toString();
	} catch (final IOException e) {
		return null;
	}
}

Best regards.

@xresch
Copy link

xresch commented Feb 22, 2022

I use the pretty print for debug purposes to be able to actually read the JSON.
I think it is interesting that we cannot just pass the separator to the method as a parameter.
Here is the code I use for DEBUG PURPOSES ONLY.
I just replace any occurence of two blanks with a tab.

public static String toJSONPretty(Object object) {
    return gsonInstancePretty.toJson(object).replaceAll("  ", "\t");
}

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

No branches or pull requests

8 participants