As far as I know, PetitParser is a parser which we can define grammers and actions to parse some text.
I've successfully use its built-in JSON parser to parse some JSON string, but I want to do more. I want to write a JSON formatter which can format the JSON to a pretty-print style.
But I don't know what to do. Do I have to create some nodes and build a tree for a parsed JSON? And how to do it?
I'm trying to look for some samples, but not found yet.
PetitParser does not directly help you with pretty printing.
In the particular example you could write code that would traverse the Maps and Lists the parser returns and print it as you wish.
Alternatively (and in general for more complex grammars) you could subclass JsonGrammar and produce an AST of custom parse nodes. Then you write a visitor or recursively traverse the nodes to format the data accordingly. The XML parser does exactly that.
PetitParser for Smalltalk comes with a little framework called PrettyPetit that helps you with a block layout engine to optimally place, wrap and align strings; but the process to generate such formatting instructions is the same as when doing it manually as described above.