Make real number encoding and decoding work under all locales
[jansson.git] / src / dump.c
index ba70f8d..d87d5c7 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2009 Petri Lehtinen <petri@digip.org>
+ * Copyright (c) 2009-2011 Petri Lehtinen <petri@digip.org>
  *
  * Jansson is free software; you can redistribute it and/or modify
  * it under the terms of the MIT license. See LICENSE for details.
@@ -9,7 +9,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <stdint.h>
 #include <assert.h>
 
 #include <jansson.h>
@@ -20,8 +19,6 @@
 #define MAX_INTEGER_STR_LENGTH  100
 #define MAX_REAL_STR_LENGTH     100
 
-typedef int (*dump_func)(const char *buffer, int size, void *data);
-
 struct string
 {
     char *buffer;
@@ -29,12 +26,12 @@ struct string
     int size;
 };
 
-static int dump_to_strbuffer(const char *buffer, int size, void *data)
+static int dump_to_strbuffer(const char *buffer, size_t size, void *data)
 {
     return strbuffer_append_bytes((strbuffer_t *)data, buffer, size);
 }
 
-static int dump_to_file(const char *buffer, int size, void *data)
+static int dump_to_file(const char *buffer, size_t size, void *data)
 {
     FILE *dest = (FILE *)data;
     if(fwrite(buffer, size, 1, dest) != 1)
@@ -42,10 +39,10 @@ static int dump_to_file(const char *buffer, int size, void *data)
     return 0;
 }
 
-/* 256 spaces (the maximum indentation size) */
-static char whitespace[] = "                                                                                                                                                                                                                                                                ";
+/* 32 spaces (the maximum indentation size) */
+static char whitespace[] = "                                ";
 
-static int dump_indent(unsigned long flags, int depth, int space, dump_func dump, void *data)
+static int dump_indent(size_t flags, int depth, int space, json_dump_callback_t dump, void *data)
 {
     if(JSON_INDENT(flags) > 0)
     {
@@ -67,7 +64,7 @@ static int dump_indent(unsigned long flags, int depth, int space, dump_func dump
     return 0;
 }
 
-static int dump_string(const char *str, int ascii, dump_func dump, void *data)
+static int dump_string(const char *str, int ascii, json_dump_callback_t dump, void *data)
 {
     const char *pos, *end;
     int32_t codepoint;
@@ -154,13 +151,20 @@ static int dump_string(const char *str, int ascii, dump_func dump, void *data)
     return dump("\"", 1, data);
 }
 
-static int object_key_cmp(const void *key1, const void *key2)
+static int object_key_compare_keys(const void *key1, const void *key2)
 {
-    return strcmp(*(const char **)key1, *(const char **)key2);
+    return strcmp((*(const object_key_t **)key1)->key,
+                  (*(const object_key_t **)key2)->key);
 }
 
-static int do_dump(const json_t *json, unsigned long flags, int depth,
-                   dump_func dump, void *data)
+static int object_key_compare_serials(const void *key1, const void *key2)
+{
+    return (*(const object_key_t **)key1)->serial -
+           (*(const object_key_t **)key2)->serial;
+}
+
+static int do_dump(const json_t *json, size_t flags, int depth,
+                   json_dump_callback_t dump, void *data)
 {
     int ascii = flags & JSON_ENSURE_ASCII ? 1 : 0;
 
@@ -179,7 +183,9 @@ static int do_dump(const json_t *json, unsigned long flags, int depth,
             char buffer[MAX_INTEGER_STR_LENGTH];
             int size;
 
-            size = snprintf(buffer, MAX_INTEGER_STR_LENGTH, "%d", json_integer_value(json));
+            size = snprintf(buffer, MAX_INTEGER_STR_LENGTH,
+                            "%" JSON_INTEGER_FORMAT,
+                            json_integer_value(json));
             if(size >= MAX_INTEGER_STR_LENGTH)
                 return -1;
 
@@ -190,26 +196,12 @@ static int do_dump(const json_t *json, unsigned long flags, int depth,
         {
             char buffer[MAX_REAL_STR_LENGTH];
             int size;
+            double value = json_real_value(json);
 
-            size = snprintf(buffer, MAX_REAL_STR_LENGTH, "%.17g",
-                            json_real_value(json));
-            if(size >= MAX_REAL_STR_LENGTH)
+            size = jsonp_dtostr(buffer, MAX_REAL_STR_LENGTH, value);
+            if(size < 0)
                 return -1;
 
-            /* Make sure there's a dot or 'e' in the output. Otherwise
-               a real is converted to an integer when decoding */
-            if(strchr(buffer, '.') == NULL &&
-               strchr(buffer, 'e') == NULL)
-            {
-                if(size + 2 >= MAX_REAL_STR_LENGTH) {
-                    /* No space to append ".0" */
-                    return -1;
-                }
-                buffer[size] = '.';
-                buffer[size + 1] = '0';
-                size += 2;
-            }
-
             return dump(buffer, size, data);
         }
 
@@ -225,38 +217,44 @@ static int do_dump(const json_t *json, unsigned long flags, int depth,
             /* detect circular references */
             array = json_to_array(json);
             if(array->visited)
-                return -1;
+                goto array_error;
             array->visited = 1;
 
             n = json_array_size(json);
 
             if(dump("[", 1, data))
-                return -1;
-            if(n == 0)
+                goto array_error;
+            if(n == 0) {
+                array->visited = 0;
                 return dump("]", 1, data);
+            }
             if(dump_indent(flags, depth + 1, 0, dump, data))
-                return -1;
+                goto array_error;
 
             for(i = 0; i < n; ++i) {
                 if(do_dump(json_array_get(json, i), flags, depth + 1,
                            dump, data))
-                    return -1;
+                    goto array_error;
 
                 if(i < n - 1)
                 {
                     if(dump(",", 1, data) ||
                        dump_indent(flags, depth + 1, 1, dump, data))
-                        return -1;
+                        goto array_error;
                 }
                 else
                 {
                     if(dump_indent(flags, depth, 0, dump, data))
-                        return -1;
+                        goto array_error;
                 }
             }
 
             array->visited = 0;
             return dump("]", 1, data);
+
+        array_error:
+            array->visited = 0;
+            return -1;
         }
 
         case JSON_OBJECT:
@@ -278,48 +276,53 @@ static int do_dump(const json_t *json, unsigned long flags, int depth,
             /* detect circular references */
             object = json_to_object(json);
             if(object->visited)
-                return -1;
+                goto object_error;
             object->visited = 1;
 
             iter = json_object_iter((json_t *)json);
 
             if(dump("{", 1, data))
-                return -1;
-            if(!iter)
+                goto object_error;
+            if(!iter) {
+                object->visited = 0;
                 return dump("}", 1, data);
+            }
             if(dump_indent(flags, depth + 1, 0, dump, data))
-                return -1;
+                goto object_error;
 
-            if(flags & JSON_SORT_KEYS)
+            if(flags & JSON_SORT_KEYS || flags & JSON_PRESERVE_ORDER)
             {
-                /* Sort keys */
-
-                const char **keys;
-                unsigned int size;
-                unsigned int i;
+                const object_key_t **keys;
+                size_t size, i;
+                int (*cmp_func)(const void *, const void *);
 
                 size = json_object_size(json);
-                keys = malloc(size * sizeof(const char *));
+                keys = jsonp_malloc(size * sizeof(object_key_t *));
                 if(!keys)
-                    return -1;
+                    goto object_error;
 
                 i = 0;
                 while(iter)
                 {
-                    keys[i] = json_object_iter_key(iter);
+                    keys[i] = jsonp_object_iter_fullkey(iter);
                     iter = json_object_iter_next((json_t *)json, iter);
                     i++;
                 }
                 assert(i == size);
 
-                qsort(keys, size, sizeof(const char *), object_key_cmp);
+                if(flags & JSON_SORT_KEYS)
+                    cmp_func = object_key_compare_keys;
+                else
+                    cmp_func = object_key_compare_serials;
+
+                qsort(keys, size, sizeof(object_key_t *), cmp_func);
 
                 for(i = 0; i < size; i++)
                 {
                     const char *key;
                     json_t *value;
 
-                    key = keys[i];
+                    key = keys[i]->key;
                     value = json_object_get(json, key);
                     assert(value);
 
@@ -327,8 +330,8 @@ static int do_dump(const json_t *json, unsigned long flags, int depth,
                     if(dump(separator, separator_length, data) ||
                        do_dump(value, flags, depth + 1, dump, data))
                     {
-                        free(keys);
-                        return -1;
+                        jsonp_free(keys);
+                        goto object_error;
                     }
 
                     if(i < size - 1)
@@ -336,21 +339,21 @@ static int do_dump(const json_t *json, unsigned long flags, int depth,
                         if(dump(",", 1, data) ||
                            dump_indent(flags, depth + 1, 1, dump, data))
                         {
-                            free(keys);
-                            return -1;
+                            jsonp_free(keys);
+                            goto object_error;
                         }
                     }
                     else
                     {
                         if(dump_indent(flags, depth, 0, dump, data))
                         {
-                            free(keys);
-                            return -1;
+                            jsonp_free(keys);
+                            goto object_error;
                         }
                     }
                 }
 
-                free(keys);
+                jsonp_free(keys);
             }
             else
             {
@@ -364,18 +367,18 @@ static int do_dump(const json_t *json, unsigned long flags, int depth,
                     if(dump(separator, separator_length, data) ||
                        do_dump(json_object_iter_value(iter), flags, depth + 1,
                                dump, data))
-                        return -1;
+                        goto object_error;
 
                     if(next)
                     {
                         if(dump(",", 1, data) ||
                            dump_indent(flags, depth + 1, 1, dump, data))
-                            return -1;
+                            goto object_error;
                     }
                     else
                     {
                         if(dump_indent(flags, depth, 0, dump, data))
-                            return -1;
+                            goto object_error;
                     }
 
                     iter = next;
@@ -384,6 +387,10 @@ static int do_dump(const json_t *json, unsigned long flags, int depth,
 
             object->visited = 0;
             return dump("}", 1, data);
+
+        object_error:
+            object->visited = 0;
+            return -1;
         }
 
         default:
@@ -392,38 +399,29 @@ static int do_dump(const json_t *json, unsigned long flags, int depth,
     }
 }
 
-
-char *json_dumps(const json_t *json, unsigned long flags)
+char *json_dumps(const json_t *json, size_t flags)
 {
     strbuffer_t strbuff;
     char *result;
 
-    if(!json_is_array(json) && !json_is_object(json))
-        return NULL;
-
     if(strbuffer_init(&strbuff))
         return NULL;
 
-    if(do_dump(json, flags, 0, dump_to_strbuffer, (void *)&strbuff)) {
-        strbuffer_close(&strbuff);
-        return NULL;
-    }
+    if(json_dump_callback(json, dump_to_strbuffer, (void *)&strbuff, flags))
+        result = NULL;
+    else
+        result = jsonp_strdup(strbuffer_value(&strbuff));
 
-    result = strdup(strbuffer_value(&strbuff));
     strbuffer_close(&strbuff);
-
     return result;
 }
 
-int json_dumpf(const json_t *json, FILE *output, unsigned long flags)
+int json_dumpf(const json_t *json, FILE *output, size_t flags)
 {
-    if(!json_is_array(json) && !json_is_object(json))
-        return -1;
-
-    return do_dump(json, flags, 0, dump_to_file, (void *)output);
+    return json_dump_callback(json, dump_to_file, (void *)output, flags);
 }
 
-int json_dump_file(const json_t *json, const char *path, unsigned long flags)
+int json_dump_file(const json_t *json, const char *path, size_t flags)
 {
     int result;
 
@@ -436,3 +434,13 @@ int json_dump_file(const json_t *json, const char *path, unsigned long flags)
     fclose(output);
     return result;
 }
+
+int json_dump_callback(const json_t *json, json_dump_callback_t callback, void *data, size_t flags)
+{
+    if(!(flags & JSON_ENCODE_ANY)) {
+        if(!json_is_array(json) && !json_is_object(json))
+           return -1;
+    }
+
+    return do_dump(json, flags, 0, callback, data);
+}