diff options
author | Martin Schanzenbach <mschanzenbach@posteo.de> | 2016-05-05 10:17:37 +0000 |
---|---|---|
committer | Martin Schanzenbach <mschanzenbach@posteo.de> | 2016-05-05 10:17:37 +0000 |
commit | 9bfec4362916ca79314265af464a096706a1c963 (patch) | |
tree | b3ce3a417842bff6df2496c0f4ebd979836d4dcb /src/jsonapi/jsonapi_document.c | |
parent | e2e045f3fef2231c435ae7dacbb5e947a0193a20 (diff) |
- refactor jsonpi utils, add test
Diffstat (limited to 'src/jsonapi/jsonapi_document.c')
-rw-r--r-- | src/jsonapi/jsonapi_document.c | 43 |
1 files changed, 32 insertions, 11 deletions
diff --git a/src/jsonapi/jsonapi_document.c b/src/jsonapi/jsonapi_document.c index 4837ee2be6..b99a7a4fef 100644 --- a/src/jsonapi/jsonapi_document.c +++ b/src/jsonapi/jsonapi_document.c @@ -286,19 +286,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) @@ -313,7 +312,9 @@ GNUNET_JSONAPI_document_serialize (const struct GNUNET_JSONAPI_Document *doc, &res_json_tmp)); json_array_append (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 +339,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; } |