diff options
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 4837ee2be..b99a7a4fe 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, | |||
286 | * @return GNUNET_SYSERR on error else GNUNET_OK | 286 | * @return GNUNET_SYSERR on error else GNUNET_OK |
287 | */ | 287 | */ |
288 | int | 288 | int |
289 | GNUNET_JSONAPI_document_serialize (const struct GNUNET_JSONAPI_Document *doc, | 289 | GNUNET_JSONAPI_document_to_json (const struct GNUNET_JSONAPI_Document *doc, |
290 | char **result) | 290 | json_t **root_json) |
291 | { | 291 | { |
292 | struct GNUNET_JSONAPI_Resource *res; | 292 | struct GNUNET_JSONAPI_Resource *res; |
293 | struct GNUNET_JSONAPI_Error *error; | 293 | struct GNUNET_JSONAPI_Error *error; |
294 | json_t *root_json; | ||
295 | json_t *res_json; | 294 | json_t *res_json; |
296 | json_t *res_json_tmp; | 295 | json_t *res_json_tmp; |
297 | 296 | ||
298 | if ((NULL == doc)) | 297 | if ((NULL == doc)) |
299 | return GNUNET_SYSERR; | 298 | return GNUNET_SYSERR; |
300 | 299 | ||
301 | root_json = json_object (); | 300 | *root_json = json_object (); |
302 | 301 | ||
303 | //Check for errors first | 302 | //Check for errors first |
304 | if (doc->err_count != 0) | 303 | if (doc->err_count != 0) |
@@ -313,7 +312,9 @@ GNUNET_JSONAPI_document_serialize (const struct GNUNET_JSONAPI_Document *doc, | |||
313 | &res_json_tmp)); | 312 | &res_json_tmp)); |
314 | json_array_append (res_json, res_json_tmp); | 313 | json_array_append (res_json, res_json_tmp); |
315 | } | 314 | } |
316 | json_object_set (root_json, GNUNET_JSONAPI_KEY_ERRORS, res_json); | 315 | json_object_set_new (*root_json, |
316 | GNUNET_JSONAPI_KEY_ERRORS, | ||
317 | res_json); | ||
317 | } else { | 318 | } else { |
318 | switch (doc->res_count) | 319 | switch (doc->res_count) |
319 | { | 320 | { |
@@ -338,14 +339,34 @@ GNUNET_JSONAPI_document_serialize (const struct GNUNET_JSONAPI_Document *doc, | |||
338 | } | 339 | } |
339 | break; | 340 | break; |
340 | } | 341 | } |
341 | json_object_set (root_json, GNUNET_JSONAPI_KEY_DATA, res_json); | 342 | json_object_set_new (*root_json, |
343 | GNUNET_JSONAPI_KEY_DATA, | ||
344 | res_json); | ||
342 | } | 345 | } |
346 | json_object_set (*root_json, | ||
347 | GNUNET_JSONAPI_KEY_META, | ||
348 | doc->meta); | ||
349 | return GNUNET_OK; | ||
350 | } | ||
351 | |||
352 | /** | ||
353 | * String serialze jsonapi primary data | ||
354 | * | ||
355 | * @param data the JSON API primary data | ||
356 | * @param result where to store the result | ||
357 | * @return GNUNET_SYSERR on error else GNUNET_OK | ||
358 | */ | ||
359 | int | ||
360 | GNUNET_JSONAPI_document_serialize (const struct GNUNET_JSONAPI_Document *doc, | ||
361 | char **result) | ||
362 | { | ||
363 | json_t *json_doc; | ||
364 | if (GNUNET_OK != GNUNET_JSONAPI_document_to_json (doc, | ||
365 | &json_doc)) | ||
366 | return GNUNET_SYSERR; | ||
343 | 367 | ||
344 | //Add meta | 368 | *result = json_dumps (json_doc, JSON_INDENT(2)); |
345 | json_object_set (root_json, GNUNET_JSONAPI_KEY_META, doc->meta); | 369 | json_decref (json_doc); |
346 | *result = json_dumps (root_json, JSON_INDENT(2)); | ||
347 | json_decref (root_json); | ||
348 | json_decref (res_json); | ||
349 | return GNUNET_OK; | 370 | return GNUNET_OK; |
350 | } | 371 | } |
351 | 372 | ||