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.
16 * @file gnunet_jsonapi_lib.h
17 * @brief functions to parse jsonapi objects
18 * @author Martin Schanzenbach
20 #ifndef GNUNET_JSONAPI_LIB_H
21 #define GNUNET_JSONAPI_LIB_H
23 #include "gnunet_util_lib.h"
24 #include "gnunet_json_lib.h"
26 #define GNUNET_JSONAPI_KEY_DATA "data"
28 #define GNUNET_JSONAPI_KEY_ID "id"
30 #define GNUNET_JSONAPI_KEY_TYPE "type"
32 #define GNUNET_JSONAPI_KEY_META "meta"
34 #define GNUNET_JSONAPI_KEY_ATTRIBUTES "attributes"
36 #define GNUNET_JSONAPI_KEY_CODE "code"
38 #define GNUNET_JSONAPI_KEY_TITLE "title"
40 #define GNUNET_JSONAPI_KEY_DETAIL "detail"
42 #define GNUNET_JSONAPI_KEY_SOURCE "source"
44 #define GNUNET_JSONAPI_KEY_LINKS "links"
46 #define GNUNET_JSONAPI_KEY_STATUS "status"
48 #define GNUNET_JSONAPI_KEY_ERRORS "errors"
50 /* ****************** JSONAPI parsing ******************* */
52 struct GNUNET_JSONAPI_Relationship;
54 struct GNUNET_JSONAPI_Error;
56 struct GNUNET_JSONAPI_Resource;
58 struct GNUNET_JSONAPI_Document;
61 * Specification for parsing a jsonapi relationship.
63 * @param jsonapi_obj where to store the jsonapi relationship
65 struct GNUNET_JSON_Specification
66 GNUNET_JSON_spec_jsonapi_relationship (struct GNUNET_JSONAPI_Relationship **jsonapi_obj);
69 * Specification for parsing a jsonapi error.
71 * @param jsonapi_obj where to store the jsonapi error
73 struct GNUNET_JSON_Specification
74 GNUNET_JSON_spec_jsonapi_error (struct GNUNET_JSONAPI_Error **jsonapi_obj);
77 * Specification for parsing a jsonapi resource.
79 * @param jsonapi_obj where to store the jsonapi resource
81 struct GNUNET_JSON_Specification
82 GNUNET_JSON_spec_jsonapi_resource (struct GNUNET_JSONAPI_Resource **jsonapi_obj);
85 * Specification for parsing a jsonapi object.
87 * @param jsonapi_obj where to store the jsonapi object
89 struct GNUNET_JSON_Specification
90 GNUNET_JSON_spec_jsonapi_document (struct GNUNET_JSONAPI_Document **jsonapi_obj);
93 * Delete a JSON API relationship
95 * @param res the JSON resource
96 * @param result Pointer where the resource should be stored
99 GNUNET_JSONAPI_relationship_delete (struct GNUNET_JSONAPI_Relationship *rel);
102 /****************** jsonapi Error API ********************/
105 * Create a JSON API error
107 * @param res the JSON error
109 struct GNUNET_JSONAPI_Error*
110 GNUNET_JSONAPI_error_new (const char *id,
120 * Delete a JSON API error
122 * @param res the JSON error
125 GNUNET_JSONAPI_error_delete (struct GNUNET_JSONAPI_Error *error);
129 * Add a JSON API error to document
131 * @param data The JSON API document to add to
132 * @param res the JSON API error to add
133 * @return the new number of resources
136 GNUNET_JSONAPI_document_error_add (struct GNUNET_JSONAPI_Document *doc,
137 struct GNUNET_JSONAPI_Error *err);
140 * String serialze jsonapi error to json
142 * @param data the JSON API error
143 * @param result where to store the result
144 * @return GNUNET_SYSERR on error else GNUNET_OK
147 GNUNET_JSONAPI_error_to_json (const struct GNUNET_JSONAPI_Error *err,
151 * Parse json to error object
153 * @param err_json JSON object
154 * @param[out] err error object
155 * @return GNUNET_OK on success
158 GNUNET_JSONAPI_json_to_error (json_t *err_json,
159 struct GNUNET_JSONAPI_Error **err);
161 /****************** jsonapi Resource API ********************/
164 * Create a JSON API resource
166 * @param type the JSON API resource type
167 * @param id the JSON API resource id
168 * @return a new JSON API resource or NULL on error.
170 struct GNUNET_JSONAPI_Resource*
171 GNUNET_JSONAPI_resource_new (const char *type, const char *id);
175 * Delete a JSON API resource
177 * @param res the JSON resource
178 * @param result Pointer where the resource should be stored
181 GNUNET_JSONAPI_resource_delete (struct GNUNET_JSONAPI_Resource *resource);
185 * String serialze jsonapi to json
187 * @param data the JSON API resource
188 * @param result where to store the result
189 * @return GNUNET_SYSERR on error else GNUNET_OK
192 GNUNET_JSONAPI_resource_to_json (const struct GNUNET_JSONAPI_Resource *res,
197 * Parse json to resource object
199 * @param res_json JSON object
200 * @param[out] res resource object
201 * @return GNUNET_OK on success
204 GNUNET_JSONAPI_json_to_resource (json_t *res_json,
205 struct GNUNET_JSONAPI_Resource **res);
209 * Add a JSON API attribute
211 * @param res the JSON resource
212 * @param key the key for the attribute
213 * @param json the json_t attribute to add
214 * @return #GNUNET_OK if added successfully
215 * #GNUNET_SYSERR if not
218 GNUNET_JSONAPI_resource_add_attr (struct GNUNET_JSONAPI_Resource *resource,
222 * Read a JSON API attribute
224 * @param res the JSON resource
225 * @param key the key for the attribute
226 * @return the json attr
229 GNUNET_JSONAPI_resource_read_attr (const struct GNUNET_JSONAPI_Resource *resource,
234 * Check a JSON API resource id
236 * @param res the JSON resource
237 * @param id the expected id
238 * @return GNUNET_YES if id matches
241 GNUNET_JSONAPI_resource_check_id (const struct GNUNET_JSONAPI_Resource *resource,
245 * Check a JSON API resource id
247 * @param res the JSON resource
248 * @return the resource id
251 GNUNET_JSONAPI_resource_get_id (const struct GNUNET_JSONAPI_Resource *resource);
255 * Check a JSON API resource type
257 * @param res the JSON resource
258 * @param type the expected type
259 * @return GNUNET_YES if id matches
262 GNUNET_JSONAPI_resource_check_type (const struct GNUNET_JSONAPI_Resource *resource,
265 /****************** jsonapi Document API ********************/
268 * Create a JSON API primary data
270 * @param type the JSON API resource type
271 * @param id the JSON API resource id
272 * @return a new JSON API resource or NULL on error.
274 struct GNUNET_JSONAPI_Document*
275 GNUNET_JSONAPI_document_new ();
279 * Delete a JSON API primary data
281 * @param type the JSON API resource type
282 * @param id the JSON API resource id
283 * @return a new JSON API resource or NULL on error.
286 GNUNET_JSONAPI_document_delete (struct GNUNET_JSONAPI_Document *resp);
289 * String serialze jsonapi primary data
291 * @param data the JSON API primary data
292 * @param result where to store the result
293 * @return GNUNET_SYSERR on error else GNUNET_OK
296 GNUNET_JSONAPI_document_to_json (const struct GNUNET_JSONAPI_Document *doc,
300 * Add a JSON API resource to primary data
302 * @param data The JSON API data to add to
303 * @param res the JSON API resource to add
304 * @return the new number of resources
307 GNUNET_JSONAPI_document_resource_add (struct GNUNET_JSONAPI_Document *resp,
308 struct GNUNET_JSONAPI_Resource *res);
310 * Get a JSON API object resource count
312 * @param resp the JSON API object
313 * @return the number of resources
316 GNUNET_JSONAPI_document_resource_count (struct GNUNET_JSONAPI_Document *resp);
319 * Get a JSON API object resource num
321 * @param resp the JSON API object
322 * @param num the number of the resource
323 * @return the resource
325 struct GNUNET_JSONAPI_Resource*
326 GNUNET_JSONAPI_document_get_resource (struct GNUNET_JSONAPI_Document *resp, int num);
330 * Add a JSON API resource to primary data
332 * @param resp The JSON API data to add to
333 * @param res the JSON API resource to add
334 * @return the new number of resources
337 GNUNET_JSONAPI_document_resource_remove (struct GNUNET_JSONAPI_Document *resp,
338 struct GNUNET_JSONAPI_Resource *res);
341 * String serialze jsonapi primary data
343 * @param data the JSON API primary data
344 * @param result where to store the result
345 * @return GNUNET_SYSERR on error else GNUNET_OK
348 GNUNET_JSONAPI_document_serialize (const struct GNUNET_JSONAPI_Document *resp,
351 /* end of gnunet_jsonapi_lib.h */