]> mj.ucw.cz Git - libucw.git/commitdiff
JSON: Added numeric conversions
authorMartin Mares <mj@ucw.cz>
Fri, 17 Jul 2015 14:01:39 +0000 (16:01 +0200)
committerMartin Mares <mj@ucw.cz>
Fri, 17 Jul 2015 14:01:39 +0000 (16:01 +0200)
maint/libucw.abi
ucw-json/json.c
ucw-json/json.h

index 0e4deffd83cff302392048ce1686af1d22bc1ea2..3a68d417afb4766416e1c3f921f0bfc905830067 100644 (file)
@@ -891,6 +891,11 @@ json_reset
 json_push
 json_pop
 json_new_node
+json_new_number
+json_number_to_int
+json_number_to_uint
+json_number_to_s64
+json_number_to_u64
 json_new_array
 json_array_append
 json_new_object
index 28aad1b68d20a3940887abb6deedc38de24038a7..780478d661ed55cbcbede6e614036f187980bb1e 100644 (file)
@@ -12,7 +12,9 @@
 #include <ucw/mempool.h>
 #include <ucw-json/json.h>
 
+#include <limits.h>
 #include <math.h>
+#include <stdint.h>
 
 static void json_init(struct json_context *js)
 {
@@ -70,6 +72,23 @@ struct json_node *json_new_number(struct json_context *js, double value)
   return n;
 }
 
+#define JSON_NUM_TO(_type, _min, _max)                                 \
+  bool json_number_to_##_type(struct json_node *num, _type *dest)      \
+  {                                                                    \
+    if (num->type == JSON_NUMBER &&                                    \
+        num->number >= _min && num->number <= _max)                    \
+      {                                                                        \
+        *dest = num->number;                                           \
+        return 1;                                                      \
+      }                                                                        \
+    return 0;                                                          \
+  }
+
+JSON_NUM_TO(int, INT_MIN, INT_MAX)
+JSON_NUM_TO(uint, 0, UINT_MAX)
+JSON_NUM_TO(s64, INT64_MIN, INT64_MAX)
+JSON_NUM_TO(u64, 0, UINT64_MAX)
+
 struct json_node *json_new_array(struct json_context *js)
 {
   struct json_node *n = json_new_node(js, JSON_ARRAY);
index 95bf50e048512c1a28ce14122e421e3ad0237b48..799d88842642e1ac33f5e3d4fac42cd1337cf28c 100644 (file)
 #define json_new ucw_json_new
 #define json_new_array ucw_json_new_array
 #define json_new_node ucw_json_new_node
+#define json_new_number ucw_json_new_number
 #define json_new_object ucw_json_new_object
 #define json_next_token ucw_json_next_token
 #define json_next_value ucw_json_next_value
+#define json_number_to_int ucw_json_number_to_int
+#define json_number_to_s64 ucw_json_number_to_s64
+#define json_number_to_u64 ucw_json_number_to_u64
+#define json_number_to_uint ucw_json_number_to_uint
 #define json_object_get ucw_json_object_get
 #define json_object_set ucw_json_object_set
 #define json_parse ucw_json_parse
@@ -195,6 +200,21 @@ static inline struct json_node *json_new_bool(struct json_context *js UNUSED, bo
 /** Creates a new numeric value. The @value must be a finite number. **/
 struct json_node *json_new_number(struct json_context *js, double value);
 
+/**
+ * Convert a numeric value to an `int`. Returns false if the value
+ * is not numeric or if it is too large for an int.
+ **/
+bool json_number_to_int(struct json_node *num, int *dest);
+
+/** Same as above, but for `uint`. **/
+bool json_number_to_uint(struct json_node *num, uint *dest);
+
+/** Same as above, but for `s64`. **/
+bool json_number_to_s64(struct json_node *num, s64 *dest);
+
+/** Same as above, but for `u64`. **/
+bool json_number_to_u64(struct json_node *num, u64 *dest);
+
 /** Creates a new string value. The @value is kept only as a reference. **/
 static inline struct json_node *json_new_string_ref(struct json_context *js, const char *value)
 {