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 | |
parent | e2e045f3fef2231c435ae7dacbb5e947a0193a20 (diff) | |
download | gnunet-9bfec4362916ca79314265af464a096706a1c963.tar.gz gnunet-9bfec4362916ca79314265af464a096706a1c963.zip |
- refactor jsonpi utils, add test
Diffstat (limited to 'src/jsonapi')
-rw-r--r-- | src/jsonapi/Makefile.am | 16 | ||||
-rw-r--r-- | src/jsonapi/jsonapi_document.c | 43 | ||||
-rw-r--r-- | src/jsonapi/jsonapi_error.c | 36 | ||||
-rw-r--r-- | src/jsonapi/jsonapi_resource.c | 2 | ||||
-rw-r--r-- | src/jsonapi/test_jsonapi.c | 44 |
5 files changed, 124 insertions, 17 deletions
diff --git a/src/jsonapi/Makefile.am b/src/jsonapi/Makefile.am index 7e881acbd..22ae7dac5 100644 --- a/src/jsonapi/Makefile.am +++ b/src/jsonapi/Makefile.am | |||
@@ -7,13 +7,24 @@ if USE_COVERAGE | |||
7 | endif | 7 | endif |
8 | 8 | ||
9 | lib_LTLIBRARIES = \ | 9 | lib_LTLIBRARIES = \ |
10 | libgnunetjsonapi.la | 10 | libgnunetjsonapi.la \ |
11 | libgnunetjsonapiutils.la | ||
12 | |||
13 | libgnunetjsonapiutils_la_LDFLAGS = \ | ||
14 | -version-info 0:0:0 \ | ||
15 | -no-undefined | ||
16 | libgnunetjsonapiutils_la_SOURCES = \ | ||
17 | jsonapi.c | ||
18 | libgnunetjsonapiutils_la_LIBADD = \ | ||
19 | $(top_builddir)/src/util/libgnunetutil.la \ | ||
20 | $(top_builddir)/src/jsonapi/libgnunetjsonapi.la \ | ||
21 | $(top_builddir)/src/rest/libgnunetrest.la \ | ||
22 | $(XLIB) | ||
11 | 23 | ||
12 | libgnunetjsonapi_la_LDFLAGS = \ | 24 | libgnunetjsonapi_la_LDFLAGS = \ |
13 | -version-info 0:0:0 \ | 25 | -version-info 0:0:0 \ |
14 | -no-undefined | 26 | -no-undefined |
15 | libgnunetjsonapi_la_SOURCES = \ | 27 | libgnunetjsonapi_la_SOURCES = \ |
16 | jsonapi.c \ | ||
17 | jsonapi_document.c \ | 28 | jsonapi_document.c \ |
18 | jsonapi_resource.c \ | 29 | jsonapi_resource.c \ |
19 | jsonapi_error.c \ | 30 | jsonapi_error.c \ |
@@ -21,7 +32,6 @@ libgnunetjsonapi_la_SOURCES = \ | |||
21 | libgnunetjsonapi_la_LIBADD = \ | 32 | libgnunetjsonapi_la_LIBADD = \ |
22 | $(top_builddir)/src/util/libgnunetutil.la \ | 33 | $(top_builddir)/src/util/libgnunetutil.la \ |
23 | $(top_builddir)/src/json/libgnunetjson.la \ | 34 | $(top_builddir)/src/json/libgnunetjson.la \ |
24 | $(top_builddir)/src/rest/libgnunetrest.la \ | ||
25 | -ljansson \ | 35 | -ljansson \ |
26 | $(XLIB) | 36 | $(XLIB) |
27 | 37 | ||
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 | ||
diff --git a/src/jsonapi/jsonapi_error.c b/src/jsonapi/jsonapi_error.c index d91f0a650..b7fc08d72 100644 --- a/src/jsonapi/jsonapi_error.c +++ b/src/jsonapi/jsonapi_error.c | |||
@@ -132,6 +132,42 @@ parse_jsonapierror (void *cls, | |||
132 | } | 132 | } |
133 | 133 | ||
134 | /** | 134 | /** |
135 | * Create a JSON API error | ||
136 | * | ||
137 | * @param res the JSON error | ||
138 | */ | ||
139 | struct GNUNET_JSONAPI_Error* | ||
140 | GNUNET_JSONAPI_error_new (const char *id, | ||
141 | const char *status, | ||
142 | const char *code, | ||
143 | const char *title, | ||
144 | const char *detail, | ||
145 | json_t *links, | ||
146 | json_t *source, | ||
147 | json_t *meta) | ||
148 | { | ||
149 | struct GNUNET_JSONAPI_Error *error; | ||
150 | error = GNUNET_new (struct GNUNET_JSONAPI_Error); | ||
151 | |||
152 | GNUNET_assert (NULL != id); | ||
153 | error->id = GNUNET_strdup (id); | ||
154 | GNUNET_assert (NULL != status); | ||
155 | error->status = GNUNET_strdup (status); | ||
156 | GNUNET_assert (NULL != code); | ||
157 | error->code = GNUNET_strdup (code); | ||
158 | GNUNET_assert (NULL != title); | ||
159 | error->title = GNUNET_strdup (title); | ||
160 | GNUNET_assert (NULL != detail); | ||
161 | error->detail = GNUNET_strdup (detail); | ||
162 | GNUNET_assert (NULL != links); | ||
163 | error->links = json_deep_copy (links); | ||
164 | GNUNET_assert (NULL != source); | ||
165 | error->source = json_deep_copy (source); | ||
166 | GNUNET_assert (NULL != meta); | ||
167 | error->meta = json_deep_copy (meta); | ||
168 | return error; | ||
169 | } | ||
170 | /** | ||
135 | * Delete a JSON API error | 171 | * Delete a JSON API error |
136 | * | 172 | * |
137 | * @param res the JSON error | 173 | * @param res the JSON error |
diff --git a/src/jsonapi/jsonapi_resource.c b/src/jsonapi/jsonapi_resource.c index 09217279a..d1d811482 100644 --- a/src/jsonapi/jsonapi_resource.c +++ b/src/jsonapi/jsonapi_resource.c | |||
@@ -145,7 +145,7 @@ GNUNET_JSONAPI_resource_add_attr (struct GNUNET_JSONAPI_Resource *resource, | |||
145 | return GNUNET_SYSERR; | 145 | return GNUNET_SYSERR; |
146 | if (NULL == resource->attr_obj) | 146 | if (NULL == resource->attr_obj) |
147 | resource->attr_obj = json_object (); | 147 | resource->attr_obj = json_object (); |
148 | json_object_set (resource->attr_obj, key, json); | 148 | json_object_set_new (resource->attr_obj, key, json); |
149 | return GNUNET_OK; | 149 | return GNUNET_OK; |
150 | } | 150 | } |
151 | 151 | ||
diff --git a/src/jsonapi/test_jsonapi.c b/src/jsonapi/test_jsonapi.c index 8b0b13566..b5f4d4cba 100644 --- a/src/jsonapi/test_jsonapi.c +++ b/src/jsonapi/test_jsonapi.c | |||
@@ -24,11 +24,47 @@ | |||
24 | #include "gnunet_jsonapi_lib.h" | 24 | #include "gnunet_jsonapi_lib.h" |
25 | #include "gnunet_json_lib.h" | 25 | #include "gnunet_json_lib.h" |
26 | 26 | ||
27 | #define TEST_JSONAPI_DOCUMENT "{\"data\":{\"id\":\"1\",\"type\":\"bar\",\"attributes\":{\"foo\":\"bar\"}}}" | ||
28 | |||
29 | static int | ||
30 | test_document () | ||
31 | { | ||
32 | struct GNUNET_JSONAPI_Document *obj; | ||
33 | struct GNUNET_JSONAPI_Resource *res; | ||
34 | json_t *doc_json; | ||
35 | json_t *data_js; | ||
36 | json_error_t err; | ||
37 | |||
38 | obj = GNUNET_JSONAPI_document_new (); | ||
39 | res = GNUNET_JSONAPI_resource_new ("bar", | ||
40 | "1"); | ||
41 | |||
42 | GNUNET_assert (GNUNET_OK == | ||
43 | GNUNET_JSONAPI_resource_add_attr (res, | ||
44 | "foo", | ||
45 | json_string ("bar"))); | ||
46 | |||
47 | GNUNET_JSONAPI_document_resource_add (obj, | ||
48 | res); | ||
49 | |||
50 | GNUNET_assert (GNUNET_OK == | ||
51 | GNUNET_JSONAPI_document_to_json (obj, | ||
52 | &doc_json)); | ||
53 | data_js = json_loads (TEST_JSONAPI_DOCUMENT, | ||
54 | JSON_DECODE_ANY, | ||
55 | &err); | ||
56 | GNUNET_assert (NULL != data_js); | ||
57 | GNUNET_assert (0 != json_equal (data_js, doc_json)); | ||
58 | GNUNET_JSONAPI_document_delete (obj); | ||
59 | json_decref (data_js); | ||
60 | json_decref (doc_json); | ||
61 | return 0; | ||
62 | } | ||
63 | |||
27 | static int | 64 | static int |
28 | test_serialize () | 65 | test_serialize () |
29 | { | 66 | { |
30 | struct GNUNET_JSONAPI_Document *obj; | 67 | struct GNUNET_JSONAPI_Document *obj; |
31 | char* data = "{\"data\":{\"id\":\"1\",\"type\":\"bar\", \"attributes\":{\"foo\":\"bar\"}}}"; | ||
32 | char* tmp_data; | 68 | char* tmp_data; |
33 | json_t* data_js; | 69 | json_t* data_js; |
34 | json_t* tmp_data_js; | 70 | json_t* tmp_data_js; |
@@ -37,7 +73,9 @@ test_serialize () | |||
37 | GNUNET_JSON_spec_jsonapi_document (&obj), | 73 | GNUNET_JSON_spec_jsonapi_document (&obj), |
38 | GNUNET_JSON_spec_end() | 74 | GNUNET_JSON_spec_end() |
39 | }; | 75 | }; |
40 | data_js = json_loads (data, JSON_DECODE_ANY, &err); | 76 | data_js = json_loads (TEST_JSONAPI_DOCUMENT, |
77 | JSON_DECODE_ANY, | ||
78 | &err); | ||
41 | GNUNET_assert (NULL != data_js); | 79 | GNUNET_assert (NULL != data_js); |
42 | GNUNET_assert (GNUNET_OK == | 80 | GNUNET_assert (GNUNET_OK == |
43 | GNUNET_JSON_parse (data_js, jsonapispec, | 81 | GNUNET_JSON_parse (data_js, jsonapispec, |
@@ -98,6 +136,8 @@ main(int argc, | |||
98 | return 1; | 136 | return 1; |
99 | if (0 != test_serialize ()) | 137 | if (0 != test_serialize ()) |
100 | return 1; | 138 | return 1; |
139 | if (0 != test_document ()) | ||
140 | return 1; | ||
101 | return 0; | 141 | return 0; |
102 | } | 142 | } |
103 | 143 | ||