diff options
author | Emmanuele Bassi <ebassi@openedhand.com> | 2008-06-13 11:06:15 +0100 |
---|---|---|
committer | Emmanuele Bassi <ebassi@openedhand.com> | 2008-06-13 11:06:15 +0100 |
commit | fdbad1cfd6ad6da9fab631f945ab8d2e85228fdf (patch) | |
tree | 700ec25c470d6a463db3d481b9872d897ce72d5f /json-glib/json-generator.c | |
parent | 81c02ef3db6901655f8a7117e5e2675d37096daf (diff) | |
download | json-glib-fdbad1cfd6ad6da9fab631f945ab8d2e85228fdf.tar.gz |
Escape to special characters in JsonGeneratorbug-958
When using json-glib to write a blog system, some deserialized objects
were not been interpreted by javascript because there were line breaks
in generated strings.
Patch from Lincoln de Sousa.
Bug #958 - JsonGenerator does not escape special characters
Signed-off-by: Emmanuele Bassi <ebassi@openedhand.com>
Diffstat (limited to 'json-glib/json-generator.c')
-rw-r--r-- | json-glib/json-generator.c | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/json-glib/json-generator.c b/json-glib/json-generator.c index 6b18bd9..6529ea6 100644 --- a/json-glib/json-generator.c +++ b/json-glib/json-generator.c @@ -257,7 +257,22 @@ dump_value (JsonGenerator *generator, break; case G_TYPE_STRING: - g_string_append_printf (buffer, "\"%s\"", g_value_get_string (&value)); + { + gchar *tmp; + gchar *exceptions = g_malloc (128); + gint chr, i; + + /* non-ascii characters can't be escaped, otherwise utf-8 + * chars will break */ + for (i = 0, chr = 0x7f; chr <= 0xff; chr++, i++) + exceptions[i] = chr; + + tmp = g_strescape (g_value_get_string (&value), exceptions); + g_free (exceptions); + + g_string_append_printf (buffer, "\"%s\"", tmp); + g_free (tmp); + } break; case G_TYPE_DOUBLE: |