Use Gson for generating JSON output
This PR uses Gson for generating the JSON output instead of using a StringBuilder. It is more versatile and also fixes some issues with texts containing special characters or newlines that would break the JSON output.