2 This file is part of GNUnet
3 Copyright (C) 2014, 2015, 2016 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * @file gnunet_jsonapi_lib.h
20 * @brief functions to parse jsonapi objects
21 * @author Martin Schanzenbach
23 #ifndef GNUNET_JSONAPI_LIB_H
24 #define GNUNET_JSONAPI_LIB_H
26 #include "gnunet_util_lib.h"
27 #include "gnunet_json_lib.h"
29 #define GNUNET_JSONAPI_KEY_DATA "data"
31 #define GNUNET_JSONAPI_KEY_ID "id"
33 #define GNUNET_JSONAPI_KEY_TYPE "type"
35 #define GNUNET_JSONAPI_KEY_META "meta"
37 #define GNUNET_JSONAPI_KEY_ATTRIBUTES "attributes"
39 #define GNUNET_JSONAPI_KEY_CODE "code"
41 #define GNUNET_JSONAPI_KEY_TITLE "title"
43 #define GNUNET_JSONAPI_KEY_DETAIL "detail"
45 #define GNUNET_JSONAPI_KEY_SOURCE "source"
47 #define GNUNET_JSONAPI_KEY_LINKS "links"
49 #define GNUNET_JSONAPI_KEY_STATUS "status"
51 #define GNUNET_JSONAPI_KEY_ERRORS "errors"
53 /* ****************** JSONAPI parsing ******************* */
55 struct GNUNET_JSONAPI_Relationship;
57 struct GNUNET_JSONAPI_Error;
59 struct GNUNET_JSONAPI_Resource;
61 struct GNUNET_JSONAPI_Document;
64 * Specification for parsing a jsonapi relationship.
66 * @param jsonapi_obj where to store the jsonapi relationship
68 struct GNUNET_JSON_Specification
69 GNUNET_JSON_spec_jsonapi_relationship (struct GNUNET_JSONAPI_Relationship **jsonapi_obj);
72 * Specification for parsing a jsonapi error.
74 * @param jsonapi_obj where to store the jsonapi error
76 struct GNUNET_JSON_Specification
77 GNUNET_JSON_spec_jsonapi_error (struct GNUNET_JSONAPI_Error **jsonapi_obj);
80 * Specification for parsing a jsonapi resource.
82 * @param jsonapi_obj where to store the jsonapi resource
84 struct GNUNET_JSON_Specification
85 GNUNET_JSON_spec_jsonapi_resource (struct GNUNET_JSONAPI_Resource **jsonapi_obj);
88 * Specification for parsing a jsonapi object.
90 * @param jsonapi_obj where to store the jsonapi object
92 struct GNUNET_JSON_Specification
93 GNUNET_JSON_spec_jsonapi_document (struct GNUNET_JSONAPI_Document **jsonapi_obj);
96 * Delete a JSON API relationship
98 * @param res the JSON resource
99 * @param result Pointer where the resource should be stored
102 GNUNET_JSONAPI_relationship_delete (struct GNUNET_JSONAPI_Relationship *rel);
105 /****************** jsonapi Error API ********************/
108 * Create a JSON API error
110 * @param res the JSON error
112 struct GNUNET_JSONAPI_Error*
113 GNUNET_JSONAPI_error_new (const char *id,
123 * Delete a JSON API error
125 * @param res the JSON error
128 GNUNET_JSONAPI_error_delete (struct GNUNET_JSONAPI_Error *error);
132 * Add a JSON API error to document
134 * @param data The JSON API document to add to
135 * @param res the JSON API error to add
136 * @return the new number of resources
139 GNUNET_JSONAPI_document_error_add (struct GNUNET_JSONAPI_Document *doc,
140 struct GNUNET_JSONAPI_Error *err);
143 * String serialze jsonapi error to json
145 * @param data the JSON API error
146 * @param result where to store the result
147 * @return GNUNET_SYSERR on error else GNUNET_OK
150 GNUNET_JSONAPI_error_to_json (const struct GNUNET_JSONAPI_Error *err,
154 * Parse json to error object
156 * @param err_json JSON object
157 * @param[out] err error object
158 * @return GNUNET_OK on success
161 GNUNET_JSONAPI_json_to_error (json_t *err_json,
162 struct GNUNET_JSONAPI_Error **err);
164 /****************** jsonapi Resource API ********************/
167 * Create a JSON API resource
169 * @param type the JSON API resource type
170 * @param id the JSON API resource id
171 * @return a new JSON API resource or NULL on error.
173 struct GNUNET_JSONAPI_Resource*
174 GNUNET_JSONAPI_resource_new (const char *type, const char *id);
178 * Delete a JSON API resource
180 * @param res the JSON resource
181 * @param result Pointer where the resource should be stored
184 GNUNET_JSONAPI_resource_delete (struct GNUNET_JSONAPI_Resource *resource);
188 * String serialze jsonapi to json
190 * @param data the JSON API resource
191 * @param result where to store the result
192 * @return GNUNET_SYSERR on error else GNUNET_OK
195 GNUNET_JSONAPI_resource_to_json (const struct GNUNET_JSONAPI_Resource *res,
200 * Parse json to resource object
202 * @param res_json JSON object
203 * @param[out] res resource object
204 * @return GNUNET_OK on success
207 GNUNET_JSONAPI_json_to_resource (json_t *res_json,
208 struct GNUNET_JSONAPI_Resource **res);
212 * Add a JSON API attribute
214 * @param res the JSON resource
215 * @param key the key for the attribute
216 * @param json the json_t attribute to add
217 * @return #GNUNET_OK if added successfully
218 * #GNUNET_SYSERR if not
221 GNUNET_JSONAPI_resource_add_attr (struct GNUNET_JSONAPI_Resource *resource,
225 * Read a JSON API attribute
227 * @param res the JSON resource
228 * @param key the key for the attribute
229 * @return the json attr
232 GNUNET_JSONAPI_resource_read_attr (const struct GNUNET_JSONAPI_Resource *resource,
237 * Check a JSON API resource id
239 * @param res the JSON resource
240 * @param id the expected id
241 * @return GNUNET_YES if id matches
244 GNUNET_JSONAPI_resource_check_id (const struct GNUNET_JSONAPI_Resource *resource,
248 * Check a JSON API resource id
250 * @param res the JSON resource
251 * @return the resource id
254 GNUNET_JSONAPI_resource_get_id (const struct GNUNET_JSONAPI_Resource *resource);
258 * Check a JSON API resource type
260 * @param res the JSON resource
261 * @param type the expected type
262 * @return GNUNET_YES if id matches
265 GNUNET_JSONAPI_resource_check_type (const struct GNUNET_JSONAPI_Resource *resource,
268 /****************** jsonapi Document API ********************/
271 * Create a JSON API primary data
273 * @param type the JSON API resource type
274 * @param id the JSON API resource id
275 * @return a new JSON API resource or NULL on error.
277 struct GNUNET_JSONAPI_Document*
278 GNUNET_JSONAPI_document_new ();
282 * Delete a JSON API primary data
284 * @param type the JSON API resource type
285 * @param id the JSON API resource id
286 * @return a new JSON API resource or NULL on error.
289 GNUNET_JSONAPI_document_delete (struct GNUNET_JSONAPI_Document *resp);
292 * String serialze jsonapi primary data
294 * @param data the JSON API primary data
295 * @param result where to store the result
296 * @return GNUNET_SYSERR on error else GNUNET_OK
299 GNUNET_JSONAPI_document_to_json (const struct GNUNET_JSONAPI_Document *doc,
303 * Add a JSON API resource to primary data
305 * @param data The JSON API data to add to
306 * @param res the JSON API resource to add
307 * @return the new number of resources
310 GNUNET_JSONAPI_document_resource_add (struct GNUNET_JSONAPI_Document *resp,
311 struct GNUNET_JSONAPI_Resource *res);
313 * Get a JSON API object resource count
315 * @param resp the JSON API object
316 * @return the number of resources
319 GNUNET_JSONAPI_document_resource_count (struct GNUNET_JSONAPI_Document *resp);
322 * Get a JSON API object resource num
324 * @param resp the JSON API object
325 * @param num the number of the resource
326 * @return the resource
328 struct GNUNET_JSONAPI_Resource*
329 GNUNET_JSONAPI_document_get_resource (struct GNUNET_JSONAPI_Document *resp, int num);
333 * Add a JSON API resource to primary data
335 * @param resp The JSON API data to add to
336 * @param res the JSON API resource to add
337 * @return the new number of resources
340 GNUNET_JSONAPI_document_resource_remove (struct GNUNET_JSONAPI_Document *resp,
341 struct GNUNET_JSONAPI_Resource *res);
344 * String serialze jsonapi primary data
346 * @param data the JSON API primary data
347 * @param result where to store the result
348 * @return GNUNET_SYSERR on error else GNUNET_OK
351 GNUNET_JSONAPI_document_serialize (const struct GNUNET_JSONAPI_Document *resp,
354 /* end of gnunet_jsonapi_lib.h */