-fix off-by-1
[oweals/gnunet.git] / src / jsonapi / jsonapi_document.c
index 4837ee2be688f3d61bd6b0e593ba14e6ea862c12..600b7ee6aa46a1684ca4b679b8f7d159e6066db9 100644 (file)
@@ -53,7 +53,20 @@ GNUNET_JSONAPI_document_delete (struct GNUNET_JSONAPI_Document *doc)
 {
   struct GNUNET_JSONAPI_Resource *res;
   struct GNUNET_JSONAPI_Resource *res_next;
-  
+  struct GNUNET_JSONAPI_Error *err;
+  struct GNUNET_JSONAPI_Error *err_next;
+
+
+  for (err = doc->err_list_head;
+       err != NULL;)
+  {
+    err_next = err->next;
+    GNUNET_CONTAINER_DLL_remove (doc->err_list_head,
+                                 doc->err_list_tail,
+                                 err);
+    GNUNET_JSONAPI_error_delete (err);
+    err = err_next;
+  }
 
   for (res = doc->res_list_head;
        res != NULL;)
@@ -65,6 +78,9 @@ GNUNET_JSONAPI_document_delete (struct GNUNET_JSONAPI_Document *doc)
     GNUNET_JSONAPI_resource_delete (res);
     res = res_next;
   }
+
+  if (NULL != doc->meta)
+    json_decref (doc->meta);
   GNUNET_free (doc);
   doc = NULL;
 }
@@ -95,7 +111,7 @@ GNUNET_JSONAPI_document_new ()
  */
 void
 GNUNET_JSONAPI_document_error_add (struct GNUNET_JSONAPI_Document *doc,
-                                      struct GNUNET_JSONAPI_Error *err)
+                                   struct GNUNET_JSONAPI_Error *err)
 {
   GNUNET_CONTAINER_DLL_insert (doc->err_list_head,
                                doc->err_list_tail,
@@ -113,7 +129,7 @@ GNUNET_JSONAPI_document_error_add (struct GNUNET_JSONAPI_Document *doc,
  */
 void
 GNUNET_JSONAPI_document_resource_add (struct GNUNET_JSONAPI_Document *doc,
-                                         struct GNUNET_JSONAPI_Resource *res)
+                                      struct GNUNET_JSONAPI_Resource *res)
 {
   GNUNET_CONTAINER_DLL_insert (doc->res_list_head,
                                doc->res_list_tail,
@@ -286,19 +302,18 @@ GNUNET_JSONAPI_document_resource_remove (struct GNUNET_JSONAPI_Document *resp,
  * @return GNUNET_SYSERR on error else GNUNET_OK
  */
 int
-GNUNET_JSONAPI_document_serialize (const struct GNUNET_JSONAPI_Document *doc,
-                                   char **result)
+GNUNET_JSONAPI_document_to_json (const struct GNUNET_JSONAPI_Document *doc,
+                                 json_t **root_json)
 {
   struct GNUNET_JSONAPI_Resource *res;
   struct GNUNET_JSONAPI_Error *error;
-  json_t *root_json;
   json_t *res_json;
   json_t *res_json_tmp;
 
   if ((NULL == doc))
     return GNUNET_SYSERR;
 
-  root_json = json_object ();
+  *root_json = json_object ();
 
   //Check for errors first 
   if (doc->err_count != 0)
@@ -311,9 +326,11 @@ GNUNET_JSONAPI_document_serialize (const struct GNUNET_JSONAPI_Document *doc,
       GNUNET_assert (GNUNET_OK ==
                      GNUNET_JSONAPI_error_to_json (error,
                                                    &res_json_tmp));
-      json_array_append (res_json, res_json_tmp);
+      json_array_append_new (res_json, res_json_tmp);
     }
-    json_object_set (root_json, GNUNET_JSONAPI_KEY_ERRORS, res_json);
+    json_object_set_new (*root_json,
+                         GNUNET_JSONAPI_KEY_ERRORS,
+                         res_json);
   } else {
     switch (doc->res_count)
     {
@@ -338,14 +355,34 @@ GNUNET_JSONAPI_document_serialize (const struct GNUNET_JSONAPI_Document *doc,
         }
         break;
     }
-    json_object_set (root_json, GNUNET_JSONAPI_KEY_DATA, res_json);
+    json_object_set_new (*root_json,
+                         GNUNET_JSONAPI_KEY_DATA,
+                         res_json);
   }
+  json_object_set (*root_json,
+                   GNUNET_JSONAPI_KEY_META,
+                   doc->meta);
+  return GNUNET_OK;
+}
+
+/**
+ * String serialze jsonapi primary data
+ *
+ * @param data the JSON API primary data
+ * @param result where to store the result
+ * @return GNUNET_SYSERR on error else GNUNET_OK
+ */
+int
+GNUNET_JSONAPI_document_serialize (const struct GNUNET_JSONAPI_Document *doc,
+                                   char **result)
+{
+  json_t *json_doc;
+  if (GNUNET_OK != GNUNET_JSONAPI_document_to_json (doc,
+                                                    &json_doc))
+    return GNUNET_SYSERR;
 
-  //Add meta
-  json_object_set (root_json, GNUNET_JSONAPI_KEY_META, doc->meta);
-  *result = json_dumps (root_json, JSON_INDENT(2));
-  json_decref (root_json);
-  json_decref (res_json);
+  *result = json_dumps (json_doc, JSON_INDENT(2));
+  json_decref (json_doc);
   return GNUNET_OK;
 }
 
@@ -371,4 +408,3 @@ GNUNET_JSON_spec_jsonapi_document (struct GNUNET_JSONAPI_Document **jsonapi_obje
   return ret;
 }
 
-