gnunetutil: add 2d and 3d allocation including tests
[oweals/gnunet.git] / src / include / gnunet_jsonapi_lib.h
index 18dfbdf950584b9d45de1727cdc8cd69e1f704cd..f95bff8362e33a8cec957dc9c6591b5a4f0a8698 100644 (file)
 #include "gnunet_util_lib.h"
 #include "gnunet_json_lib.h"
 
+#define GNUNET_JSONAPI_KEY_DATA "data"
+
+#define GNUNET_JSONAPI_KEY_ID "id"
+
+#define GNUNET_JSONAPI_KEY_TYPE "type"
+
+#define GNUNET_JSONAPI_KEY_META "meta"
+
+#define GNUNET_JSONAPI_KEY_ATTRIBUTES "attributes"
+
+#define GNUNET_JSONAPI_KEY_CODE "code"
+
+#define GNUNET_JSONAPI_KEY_TITLE "title"
+
+#define GNUNET_JSONAPI_KEY_DETAIL "detail"
+
+#define GNUNET_JSONAPI_KEY_SOURCE "source"
+
+#define GNUNET_JSONAPI_KEY_LINKS "links"
+
+#define GNUNET_JSONAPI_KEY_STATUS "status"
+
+#define GNUNET_JSONAPI_KEY_ERRORS "errors"
 
 /* ****************** JSONAPI parsing ******************* */
 
+struct GNUNET_JSONAPI_Relationship;
+
+struct GNUNET_JSONAPI_Error;
+
 struct GNUNET_JSONAPI_Resource;
 
-struct GNUNET_JSONAPI_Object;
+struct GNUNET_JSONAPI_Document;
+
+/**
+ * Specification for parsing a jsonapi relationship.
+ *
+ * @param jsonapi_obj where to store the jsonapi relationship
+ */
+struct GNUNET_JSON_Specification
+GNUNET_JSON_spec_jsonapi_relationship (struct GNUNET_JSONAPI_Relationship **jsonapi_obj);
+
+/**
+ * Specification for parsing a jsonapi error.
+ *
+ * @param jsonapi_obj where to store the jsonapi error
+ */
+struct GNUNET_JSON_Specification
+GNUNET_JSON_spec_jsonapi_error (struct GNUNET_JSONAPI_Error **jsonapi_obj);
+
+/**
+ * Specification for parsing a jsonapi resource.
+ *
+ * @param jsonapi_obj where to store the jsonapi resource
+ */
+struct GNUNET_JSON_Specification
+GNUNET_JSON_spec_jsonapi_resource (struct GNUNET_JSONAPI_Resource **jsonapi_obj);
 
 /**
  * Specification for parsing a jsonapi object.
  *
  * @param jsonapi_obj where to store the jsonapi object
  */
-struct GNUNET_JSONAPI_Specification
-GNUNET_JSONAPI_spec_jsonapi (struct GNUNET_JSONAPI_Object **jsonapi_obj);
+struct GNUNET_JSON_Specification
+GNUNET_JSON_spec_jsonapi_document (struct GNUNET_JSONAPI_Document **jsonapi_obj);
+
+/**
+ * Delete a JSON API relationship
+ *
+ * @param res the JSON resource
+ * @param result Pointer where the resource should be stored
+ */
+void
+GNUNET_JSONAPI_relationship_delete (struct GNUNET_JSONAPI_Relationship *rel);
+
+
+/****************** jsonapi Error API ********************/
+
+/**
+ * Create a JSON API error
+ *
+ * @param res the JSON error
+ */
+struct GNUNET_JSONAPI_Error*
+GNUNET_JSONAPI_error_new (const char *id,
+                          const char *status,
+                          const char *code,
+                          const char *title,
+                          const char *detail,
+                          json_t *links,
+                          json_t *source,
+                          json_t *meta);
+
+/**
+ * Delete a JSON API error
+ *
+ * @param res the JSON error
+ */
+void
+GNUNET_JSONAPI_error_delete (struct GNUNET_JSONAPI_Error *error);
+
+
+/**
+ * Add a JSON API error to document
+ *
+ * @param data The JSON API document to add to
+ * @param res the JSON API error to add
+ * @return the new number of resources
+ */
+void
+GNUNET_JSONAPI_document_error_add (struct GNUNET_JSONAPI_Document *doc,
+                                      struct GNUNET_JSONAPI_Error *err);
+
+/**
+ * String serialze jsonapi error to json
+ *
+ * @param data the JSON API error
+ * @param result where to store the result
+ * @return GNUNET_SYSERR on error else GNUNET_OK
+ */
+int
+GNUNET_JSONAPI_error_to_json (const struct GNUNET_JSONAPI_Error *err,
+                              json_t **result);
+
+/**
+ * Parse json to error object
+ *
+ * @param err_json JSON object
+ * @param[out] err error object
+ * @return GNUNET_OK on success
+ */
+int
+GNUNET_JSONAPI_json_to_error (json_t *err_json,
+                              struct GNUNET_JSONAPI_Error **err);
+
+/****************** jsonapi Resource API ********************/
 
 /**
  * Create a JSON API resource
@@ -49,6 +171,7 @@ GNUNET_JSONAPI_spec_jsonapi (struct GNUNET_JSONAPI_Object **jsonapi_obj);
 struct GNUNET_JSONAPI_Resource*
 GNUNET_JSONAPI_resource_new (const char *type, const char *id);
 
+
 /**
  * Delete a JSON API resource
  *
@@ -58,6 +181,31 @@ GNUNET_JSONAPI_resource_new (const char *type, const char *id);
 void
 GNUNET_JSONAPI_resource_delete (struct GNUNET_JSONAPI_Resource *resource);
 
+
+/**
+ * String serialze jsonapi to json
+ *
+ * @param data the JSON API resource
+ * @param result where to store the result
+ * @return GNUNET_SYSERR on error else GNUNET_OK
+ */
+int
+GNUNET_JSONAPI_resource_to_json (const struct GNUNET_JSONAPI_Resource *res,
+                                 json_t **result);
+
+
+/**
+ * Parse json to resource object
+ *
+ * @param res_json JSON object
+ * @param[out] res resource object
+ * @return GNUNET_OK on success
+ */
+int
+GNUNET_JSONAPI_json_to_resource (json_t *res_json,
+                                 struct GNUNET_JSONAPI_Resource **res);
+
+
 /**
  * Add a JSON API attribute
  *
@@ -68,9 +216,9 @@ GNUNET_JSONAPI_resource_delete (struct GNUNET_JSONAPI_Resource *resource);
  *         #GNUNET_SYSERR if not
  */
 int
-GNUNET_JSONAPI_resource_add_attr (const struct GNUNET_JSONAPI_Resource *resource,
-                                       const char* key,
-                                       json_t *json);
+GNUNET_JSONAPI_resource_add_attr (struct GNUNET_JSONAPI_Resource *resource,
+                                  const char* key,
+                                  json_t *json);
 /**
  * Read a JSON API attribute
  *
@@ -80,7 +228,7 @@ GNUNET_JSONAPI_resource_add_attr (const struct GNUNET_JSONAPI_Resource *resource
  */
 json_t*
 GNUNET_JSONAPI_resource_read_attr (const struct GNUNET_JSONAPI_Resource *resource,
-                                       const char* key);
+                                   const char* key);
 
 
 /**
@@ -92,7 +240,16 @@ GNUNET_JSONAPI_resource_read_attr (const struct GNUNET_JSONAPI_Resource *resourc
  */
 int
 GNUNET_JSONAPI_resource_check_id (const struct GNUNET_JSONAPI_Resource *resource,
-                                       const char* id);
+                                  const char* id);
+
+/**
+ * Check a JSON API resource id
+ *
+ * @param res the JSON resource
+ * @return the resource id
+ */
+char*
+GNUNET_JSONAPI_resource_get_id (const struct GNUNET_JSONAPI_Resource *resource);
 
 
 /**
@@ -104,8 +261,9 @@ GNUNET_JSONAPI_resource_check_id (const struct GNUNET_JSONAPI_Resource *resource
  */
 int
 GNUNET_JSONAPI_resource_check_type (const struct GNUNET_JSONAPI_Resource *resource,
-                                         const char* type);
+                                    const char* type);
 
+/****************** jsonapi Document API ********************/
 
 /**
  * Create a JSON API primary data
@@ -114,18 +272,8 @@ GNUNET_JSONAPI_resource_check_type (const struct GNUNET_JSONAPI_Resource *resour
  * @param id the JSON API resource id
  * @return a new JSON API resource or NULL on error.
  */
-struct GNUNET_JSONAPI_Object*
-GNUNET_JSONAPI_object_new ();
-
-
-/**
- * Create a JSON API primary data from a string
- *
- * @param data the string of the JSON API data
- * @return a new JSON API resource or NULL on error.
- */
-struct GNUNET_JSONAPI_Object*
-GNUNET_JSONAPI_object_parse (const char* data);
+struct GNUNET_JSONAPI_Document*
+GNUNET_JSONAPI_document_new ();
 
 
 /**
@@ -136,7 +284,18 @@ GNUNET_JSONAPI_object_parse (const char* data);
  * @return a new JSON API resource or NULL on error.
  */
 void
-GNUNET_JSONAPI_object_delete (struct GNUNET_JSONAPI_Object *resp);
+GNUNET_JSONAPI_document_delete (struct GNUNET_JSONAPI_Document *resp);
+
+/**
+ * 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_to_json (const struct GNUNET_JSONAPI_Document *doc,
+                                 json_t **root_json);
 
 /**
  * Add a JSON API resource to primary data
@@ -146,8 +305,8 @@ GNUNET_JSONAPI_object_delete (struct GNUNET_JSONAPI_Object *resp);
  * @return the new number of resources
  */
 void
-GNUNET_JSONAPI_object_resource_add (struct GNUNET_JSONAPI_Object *resp,
-                                           struct GNUNET_JSONAPI_Resource *res);
+GNUNET_JSONAPI_document_resource_add (struct GNUNET_JSONAPI_Document *resp,
+                                      struct GNUNET_JSONAPI_Resource *res);
 /**
  * Get a JSON API object resource count
  *
@@ -155,7 +314,7 @@ GNUNET_JSONAPI_object_resource_add (struct GNUNET_JSONAPI_Object *resp,
  * @return the number of resources
  */
 int
-GNUNET_JSONAPI_object_resource_count (struct GNUNET_JSONAPI_Object *resp);
+GNUNET_JSONAPI_document_resource_count (struct GNUNET_JSONAPI_Document *resp);
 
 /**
  * Get a JSON API object resource num
@@ -165,7 +324,7 @@ GNUNET_JSONAPI_object_resource_count (struct GNUNET_JSONAPI_Object *resp);
  * @return the resource
  */
 struct GNUNET_JSONAPI_Resource*
-GNUNET_JSONAPI_object_get_resource (struct GNUNET_JSONAPI_Object *resp, int num);
+GNUNET_JSONAPI_document_get_resource (struct GNUNET_JSONAPI_Document *resp, int num);
 
 
 /**
@@ -176,8 +335,8 @@ GNUNET_JSONAPI_object_get_resource (struct GNUNET_JSONAPI_Object *resp, int num)
  * @return the new number of resources
  */
 void
-GNUNET_JSONAPI_data_resource_remove (struct GNUNET_JSONAPI_Object *resp,
-                                          struct GNUNET_JSONAPI_Resource *res);
+GNUNET_JSONAPI_document_resource_remove (struct GNUNET_JSONAPI_Document *resp,
+                                         struct GNUNET_JSONAPI_Resource *res);
 
 /**
  * String serialze jsonapi primary data
@@ -187,16 +346,8 @@ GNUNET_JSONAPI_data_resource_remove (struct GNUNET_JSONAPI_Object *resp,
  * @return GNUNET_SYSERR on error else GNUNET_OK
  */
 int
-GNUNET_JSONAPI_data_serialize (const struct GNUNET_JSONAPI_Object *resp,
-                                    char **result);
+GNUNET_JSONAPI_document_serialize (const struct GNUNET_JSONAPI_Document *resp,
+                                   char **result);
 
-/**
- * Check a JSON API resource id
- *
- * @param res the JSON resource
- * @return the resource id
- */
-json_t*
-GNUNET_JSONAPI_resource_get_id (const struct GNUNET_JSONAPI_Resource *resource);
 /* end of gnunet_jsonapi_lib.h */
 #endif