{
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;)
GNUNET_JSONAPI_resource_delete (res);
res = res_next;
}
+
+ if (NULL != doc->meta)
+ json_decref (doc->meta);
GNUNET_free (doc);
doc = NULL;
}
*/
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,
*/
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,
* @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)
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)
{
}
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;
}