Created flag to dump escaping slash
authorJuan Basso <jrbasso@gmail.com>
Fri, 29 Jun 2012 02:04:36 +0000 (22:04 -0400)
committerJuan Basso <jrbasso@gmail.com>
Fri, 29 Jun 2012 02:04:36 +0000 (22:04 -0400)
src/dump.c
src/jansson.h
test/suites/api/test_dump.c
test/suites/valid/string-escapes/output

index 7ff5708..43c135e 100644 (file)
@@ -62,7 +62,7 @@ static int dump_indent(size_t flags, int depth, int space, json_dump_callback_t
     return 0;
 }
 
-static int dump_string(const char *str, int ascii, json_dump_callback_t dump, void *data)
+static int dump_string(const char *str, int ascii, json_dump_callback_t dump, void *data, size_t flags)
 {
     const char *pos, *end;
     int32_t codepoint;
@@ -107,13 +107,20 @@ static int dump_string(const char *str, int ascii, json_dump_callback_t dump, vo
         switch(codepoint)
         {
             case '\\': text = "\\\\"; break;
-            case '/': text = "\\/"; break;
             case '\"': text = "\\\""; break;
             case '\b': text = "\\b"; break;
             case '\f': text = "\\f"; break;
             case '\n': text = "\\n"; break;
             case '\r': text = "\\r"; break;
             case '\t': text = "\\t"; break;
+            case '/':
+                if (flags & JSON_ESCAPE_SLASH)
+                    text = "\\/";
+                else {
+                    text = "/";
+                    length = 1;
+                }
+                break;
             default:
             {
                 /* codepoint is in BMP */
@@ -207,7 +214,7 @@ static int do_dump(const json_t *json, size_t flags, int depth,
         }
 
         case JSON_STRING:
-            return dump_string(json_string_value(json), ascii, dump, data);
+            return dump_string(json_string_value(json), ascii, dump, data, flags);
 
         case JSON_ARRAY:
         {
@@ -328,7 +335,7 @@ static int do_dump(const json_t *json, size_t flags, int depth,
                     value = json_object_get(json, key);
                     assert(value);
 
-                    dump_string(key, ascii, dump, data);
+                    dump_string(key, ascii, dump, data, flags);
                     if(dump(separator, separator_length, data) ||
                        do_dump(value, flags, depth + 1, dump, data))
                     {
@@ -365,7 +372,7 @@ static int do_dump(const json_t *json, size_t flags, int depth,
                 {
                     void *next = json_object_iter_next((json_t *)json, iter);
 
-                    dump_string(json_object_iter_key(iter), ascii, dump, data);
+                    dump_string(json_object_iter_key(iter), ascii, dump, data, flags);
                     if(dump(separator, separator_length, data) ||
                        do_dump(json_object_iter_value(iter), flags, depth + 1,
                                dump, data))
index cae2432..655f136 100644 (file)
@@ -246,6 +246,7 @@ json_t *json_load_file(const char *path, size_t flags, json_error_t *error);
 #define JSON_SORT_KEYS      0x80
 #define JSON_PRESERVE_ORDER 0x100
 #define JSON_ENCODE_ANY     0x200
+#define JSON_ESCAPE_SLASH   0x400
 
 typedef int (*json_dump_callback_t)(const char *buffer, size_t size, void *data);
 
index c1a3763..8cd659c 100644 (file)
@@ -133,9 +133,34 @@ static void encode_other_than_array_or_object()
 
 }
 
+static void escape_slashes()
+{
+    /* Test dump escaping slashes */
+
+    json_t *json;
+    char *result;
+
+    json = json_object();
+    json_object_set_new(json, "url", json_string("https://github.com/akheron/jansson"));
+
+    result = json_dumps(json, 0);
+    if(!result || strcmp(result, "{\"url\": \"https://github.com/akheron/jansson\"}"))
+        fail("json_dumps failed to not escape slashes");
+
+    free(result);
+
+    result = json_dumps(json, JSON_ESCAPE_SLASH);
+    if(!result || strcmp(result, "{\"url\": \"https:\\/\\/github.com\\/akheron\\/jansson\"}"))
+        fail("json_dumps failed to escape slashes");
+
+    free(result);
+    json_decref(json);
+}
+
 static void run_tests()
 {
     encode_twice();
     circular_references();
     encode_other_than_array_or_object();
+    escape_slashes();
 }
index 7f49553..ca5c1c6 100644 (file)
@@ -1 +1 @@
-["\"\\\/\b\f\n\r\t"]
\ No newline at end of file
+["\"\\/\b\f\n\r\t"]
\ No newline at end of file