Using pprint to write into file or string

Hi,
Is there any way to use pprint to format object to file or at least to string?

I know I can save a table with:

sys.save(path, table)

which is not very readable,
or as a json:

file = io.open(path, "w")
file:write(json.encode(table))
file.close()

which is better but still in just one line.
It is much more space efficient without extra spaces I get it, but I want it to be still more readable. And since pprint does exactly what I need I am wondering whether there is any way to redirrect its output to file or at least to string.

I hoped the “pprint” it uses “print” to print so I overrode it but to my disappointment it seems it does not use it. :\

Thank you in advance.

Regards
Azurac

No, I’m afraid there’s no way to redirect pprint(). Your best bet to pretty print a table to a string is to roll your own solution or use something like inspect.lua to generate the string:

1 Like

If you want a prettified string with newlines and indentations, an alternative is to modify the output from json.encode(table). There might be lua prettifiers out there.

1 Like

Thanks for your advices.
I just wanted to know if there is some “native” way to do it that I am missing.

Another dependency then… I’ll reconsider whether I really need it :smiley:
Thanks.