apr-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From minf...@apache.org
Subject svn commit: r1835979 - in /apr/apr/trunk: include/apr_json.h json/apr_json.c json/apr_json_decode.c json/apr_json_encode.c
Date Sun, 15 Jul 2018 14:00:04 GMT
Author: minfrin
Date: Sun Jul 15 14:00:04 2018
New Revision: 1835979

URL: http://svn.apache.org/viewvc?rev=1835979&view=rev
Log:
Rework the create functions to create a JSON object for each
JSON type. Simplifies code that uses the library.

Modified:
    apr/apr/trunk/include/apr_json.h
    apr/apr/trunk/json/apr_json.c
    apr/apr/trunk/json/apr_json_decode.c
    apr/apr/trunk/json/apr_json_encode.c

Modified: apr/apr/trunk/include/apr_json.h
URL: http://svn.apache.org/viewvc/apr/apr/trunk/include/apr_json.h?rev=1835979&r1=1835978&r2=1835979&view=diff
==============================================================================
--- apr/apr/trunk/include/apr_json.h (original)
+++ apr/apr/trunk/include/apr_json.h Sun Jul 15 14:00:04 2018
@@ -101,8 +101,8 @@ typedef enum apr_json_type_e {
 typedef struct apr_json_string_t {
     /** pointer to the string */
     const char *p;
-    /** string length */
-    apr_size_t len;
+    /** string length, or APR_JSON_VALUE_STRING to compute length automatically */
+    apr_ssize_t len;
 } apr_json_string_t;
 
 /**
@@ -170,29 +170,98 @@ APR_DECLARE(apr_json_value_t *) apr_json
         __attribute__((nonnull(1)));
 
 /**
- * Allocate and return a apr_json_object_t structure.
+ * Allocate and return a JSON string with the given value.
  *
  * @param pool The pool to allocate from.
- * @return The apr_json_object_t structure.
+ * @param val The UTF-8 encoded string value.
+ * @param len The length of the string, or APR_JSON_VALUE_STRING.
+ * @return The apr_json_value_t structure.
+ */
+APR_DECLARE(apr_json_value_t *)
+		apr_json_string_create(apr_pool_t *pool, const char *val,
+				apr_ssize_t len) __attribute__((nonnull(1)));
+
+/**
+ * Allocate and return a JSON array.
+ *
+ * @param pool The pool to allocate from.
+ * @param nelts the number of elements in the initial array
+ * @return The apr_json_value_t structure.
+ */
+APR_DECLARE(apr_json_value_t *)
+		apr_json_array_create(apr_pool_t *pool, int nelts)
+		__attribute__((nonnull(1)));
+
+/**
+ * Allocate and return a JSON object.
+ *
+ * @param pool The pool to allocate from.
+ * @return The apr_json_value_t structure.
  */
-APR_DECLARE(apr_json_object_t *) apr_json_object_create(apr_pool_t *pool)
+APR_DECLARE(apr_json_value_t *) apr_json_object_create(apr_pool_t *pool)
         __attribute__((nonnull(1)));
 
 /**
+ * Allocate and return a JSON long.
+ *
+ * @param pool The pool to allocate from.
+ * @param lnumber The long value.
+ * @return The apr_json_value_t structure.
+ */
+APR_DECLARE(apr_json_value_t *)
+		apr_json_long_create(apr_pool_t *pool, apr_int64_t lnumber)
+		__attribute__((nonnull(1)));
+
+/**
+ * Allocate and return a JSON double.
+ *
+ * @param pool The pool to allocate from.
+ * @param dnumber The double value.
+ * @return The apr_json_value_t structure.
+ */
+APR_DECLARE(apr_json_value_t *)
+		apr_json_double_create(apr_pool_t *pool, double dnumber)
+		__attribute__((nonnull(1)));
+
+/**
+ * Allocate and return a JSON boolean.
+ *
+ * @param pool The pool to allocate from.
+ * @param boolean The boolean value.
+ * @return The apr_json_value_t structure.
+ */
+APR_DECLARE(apr_json_value_t *)
+		apr_json_boolean_create(apr_pool_t *pool, int boolean)
+		__attribute__((nonnull(1)));
+
+/**
+ * Allocate and return a JSON null.
+ *
+ * @param pool The pool to allocate from.
+ * @return The apr_json_value_t structure.
+ */
+APR_DECLARE(apr_json_value_t *)
+		apr_json_null_create(apr_pool_t *pool)
+		__attribute__((nonnull(1)));
+
+/**
  * Associate a value with a key in a JSON object.
  * @param obj The JSON object.
- * @param key Pointer to the key.
+ * @param key Pointer to the key string.
  * @param val Value to associate with the key.
+ * @param pool Pool to use.
+ * @return APR_SUCCESS on success, APR_EINVAL if the key is
+ *   NULL or not a string, or the object is not an APR_JSON_OBJECT.
  * @remark If the value is NULL the key value pair is deleted.
  */
-APR_DECLARE(void) apr_json_object_set(apr_json_object_t *obj,
+APR_DECLARE(apr_status_t) apr_json_object_set(apr_json_value_t *obj,
         apr_json_value_t *key, apr_json_value_t *val,
-        apr_pool_t *pool) __attribute__((nonnull(1, 2, 4)));
+        apr_pool_t *pool) __attribute__((nonnull(1, 4)));
 
 /**
  * Look up the value associated with a key in a JSON object.
- * @param ht The hash table
- * @param key Pointer to the key
+ * @param obj The JSON object.
+ * @param key Pointer to the key.
  * @return Returns NULL if the key is not present.
  */
 APR_DECLARE(apr_json_kv_t *)

Modified: apr/apr/trunk/json/apr_json.c
URL: http://svn.apache.org/viewvc/apr/apr/trunk/json/apr_json.c?rev=1835979&r1=1835978&r2=1835979&view=diff
==============================================================================
--- apr/apr/trunk/json/apr_json.c (original)
+++ apr/apr/trunk/json/apr_json.c Sun Jul 15 14:00:04 2018
@@ -30,42 +30,133 @@ apr_json_value_t *apr_json_value_create(
     return apr_pcalloc(pool, sizeof(apr_json_value_t));
 }
 
-apr_json_object_t *apr_json_object_create(apr_pool_t *pool)
+apr_json_value_t *apr_json_object_create(apr_pool_t *pool)
 {
-    apr_json_object_t *object = apr_pcalloc(pool,
-            sizeof(apr_json_object_t));
+	apr_json_object_t *object;
+
+	apr_json_value_t *json = apr_json_value_create(pool);
+
+	json->type = APR_JSON_OBJECT;
+	json->value.object = object = apr_pcalloc(pool, sizeof(apr_json_object_t));
     APR_RING_INIT(&object->list, apr_json_kv_t, link);
     object->hash = apr_hash_make(pool);
 
-    return object;
+    return json;
+}
+
+apr_json_value_t *apr_json_string_create(apr_pool_t *pool, const char *val,
+		apr_ssize_t len) {
+	apr_json_value_t *json = apr_json_value_create(pool);
+
+	if (json) {
+		if (val) {
+			json->type = APR_JSON_STRING;
+			json->value.string.p = val;
+			json->value.string.len = len;
+		} else {
+			json->type = APR_JSON_NULL;
+		}
+	}
+
+	return json;
 }
 
-void apr_json_object_set(apr_json_object_t *object, apr_json_value_t *key,
+apr_json_value_t *apr_json_array_create(apr_pool_t *pool, int nelts)
+{
+	apr_json_value_t *json = apr_json_value_create(pool);
+
+	if (json) {
+		json->type = APR_JSON_ARRAY;
+		json->value.array = apr_array_make(pool, nelts,
+				sizeof(apr_json_value_t *));
+	}
+
+	return json;
+}
+
+apr_json_value_t *apr_json_long_create(apr_pool_t *pool, apr_int64_t lnumber)
+{
+	apr_json_value_t *json = apr_json_value_create(pool);
+
+	if (json) {
+		json->type = APR_JSON_LONG;
+		json->value.lnumber = lnumber;
+	}
+
+	return json;
+}
+
+apr_json_value_t *apr_json_double_create(apr_pool_t *pool, double dnumber)
+{
+	apr_json_value_t *json = apr_json_value_create(pool);
+
+	if (json) {
+		json->type = APR_JSON_DOUBLE;
+		json->value.lnumber = dnumber;
+	}
+
+	return json;
+}
+
+apr_json_value_t *apr_json_boolean_create(apr_pool_t *pool, int boolean)
+{
+	apr_json_value_t *json = apr_json_value_create(pool);
+
+	if (json) {
+		json->type = APR_JSON_BOOLEAN;
+		json->value.boolean = boolean;
+	}
+
+	return json;
+}
+
+apr_json_value_t *apr_json_null_create(apr_pool_t *pool)
+{
+	apr_json_value_t *json = apr_json_value_create(pool);
+
+	if (json) {
+		json->type = APR_JSON_NULL;
+	}
+
+	return json;
+}
+
+apr_status_t apr_json_object_set(apr_json_value_t *object, apr_json_value_t *key,
         apr_json_value_t *val, apr_pool_t *pool)
 {
     apr_json_kv_t *kv;
+    apr_hash_t *hash;
+
+	if (object->type != APR_JSON_OBJECT || !key
+			|| key->type != APR_JSON_STRING) {
+		return APR_EINVAL;
+	}
 
-    kv = apr_hash_get(object->hash, key->value.string.p, key->value.string.len);
+	hash = object->value.object->hash;
+
+    kv = apr_hash_get(hash, key->value.string.p, key->value.string.len);
 
     if (!val) {
         if (kv) {
-            apr_hash_set(object->hash, key->value.string.p, key->value.string.len,
+            apr_hash_set(hash, key->value.string.p, key->value.string.len,
                     NULL);
             APR_RING_REMOVE((kv), link);
         }
-        return;
+        return APR_SUCCESS;
     }
 
     if (!kv) {
         kv = apr_palloc(pool, sizeof(apr_json_kv_t));
         APR_RING_ELEM_INIT(kv, link);
-        APR_JSON_OBJECT_INSERT_TAIL(object, kv);
-        apr_hash_set(object->hash, key->value.string.p, key->value.string.len,
+        APR_JSON_OBJECT_INSERT_TAIL(object->value.object, kv);
+        apr_hash_set(hash, key->value.string.p, key->value.string.len,
                 kv);
     }
 
     kv->k = key;
     kv->v = val;
+
+    return APR_SUCCESS;
 }
 
 apr_json_kv_t *apr_json_object_get(apr_json_object_t *object, const char *key)

Modified: apr/apr/trunk/json/apr_json_decode.c
URL: http://svn.apache.org/viewvc/apr/apr/trunk/json/apr_json_decode.c?rev=1835979&r1=1835978&r2=1835979&view=diff
==============================================================================
--- apr/apr/trunk/json/apr_json_decode.c (original)
+++ apr/apr/trunk/json/apr_json_decode.c Sun Jul 15 14:00:04 2018
@@ -83,7 +83,7 @@ static apr_status_t apr_json_decode_stri
     const char *p = self->p;
     const char *e;
     char *q;
-    apr_size_t len;
+    apr_ssize_t len;
 
     if (self->p >= self->e) {
         status = APR_EOF;
@@ -432,11 +432,16 @@ out:
 }
 
 static apr_status_t apr_json_decode_object(apr_json_scanner_t * self,
-        apr_json_object_t ** retval)
+        apr_json_value_t *json, apr_json_object_t ** retval)
 {
     apr_status_t status = APR_SUCCESS;
 
-    apr_json_object_t *object = apr_json_object_create(self->pool);
+	apr_json_object_t *object = apr_pcalloc(self->pool,
+			sizeof(apr_json_object_t));
+    APR_RING_INIT(&object->list, apr_json_kv_t, link);
+    object->hash = apr_hash_make(self->pool);
+
+    *retval = object;
 
     if (self->p >= self->e) {
         return APR_EOF;
@@ -491,7 +496,7 @@ static apr_status_t apr_json_decode_obje
         if ((status = apr_json_decode_value(self, &value)))
             goto out;
 
-        apr_json_object_set(object, key, value, self->pool);
+        apr_json_object_set(json, key, value, self->pool);
 
         if (self->p == self->e) {
             status = APR_EOF;
@@ -509,7 +514,6 @@ static apr_status_t apr_json_decode_obje
 
     self->level++;
 
-    *retval = object;
 out:
     return status;
 }
@@ -733,7 +737,7 @@ static apr_status_t apr_json_decode_valu
             break;
         case '{':
             value.type = APR_JSON_OBJECT;
-            status = apr_json_decode_object(self, &value.value.object);
+            status = apr_json_decode_object(self, &value, &value.value.object);
             break;
         case 'n':
             value.type = APR_JSON_NULL;

Modified: apr/apr/trunk/json/apr_json_encode.c
URL: http://svn.apache.org/viewvc/apr/apr/trunk/json/apr_json_encode.c?rev=1835979&r1=1835978&r2=1835979&view=diff
==============================================================================
--- apr/apr/trunk/json/apr_json_encode.c (original)
+++ apr/apr/trunk/json/apr_json_encode.c Sun Jul 15 14:00:04 2018
@@ -73,7 +73,9 @@ static apr_status_t apr_json_encode_stri
         return status;
     }
 
-    for (p = chunk = string->p, e = string->p + string->len; p < e; p++) {
+    for (p = chunk = string->p, e = string->p
+            + (APR_JSON_VALUE_STRING == string->len ?
+                    strlen(string->p) : string->len); p < e; p++) {
         switch (*p) {
         case '\n':
             status = apr_json_brigade_write(self, chunk, p - chunk, "\\n");



Mime
View raw message