question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add custom indent for pretty printing

See original GitHub issue

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);

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:21
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

8reactions
WasylFcommented, 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.

4reactions
bonoboriscommented, Aug 2, 2018

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to implement custom indentation when pretty-printing ...
(Note: The code in this answer only works with json.dumps() which returns a JSON formatted string, but not with json.dump() which writes directly...
Read more >
Pretty-printing JSON with custom indent - Andreas Möller
Recently I was in need of pretty-printing JSON in PHP with custom indentation. While PHP 5.4 has added the constants JSON_PRETTY_PRINT ...
Read more >
Pretty Printing — Rich 12.6.0 documentation
Rich can draw indent guides to highlight the indent level of a data structure. These can make it easier to read more deeply...
Read more >
Python PrettyPrint JSON Data - PYnative
Python Write Indented and Pretty-printed JSON into a file. Prettyprint JSON file and JSON string. Use pprint module to pretty-print JSON.
Read more >
pprint — Data pretty printer — Python 3.11.1 documentation
indent (default 1) specifies the amount of indentation added for each nesting level. depth controls the number of nesting levels which may be...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found