X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;ds=inline;f=ucw-json%2Fjson.c;h=780478d661ed55cbcbede6e614036f187980bb1e;hb=a1a614d36f4b00dd72e8cbd63dafffc455520e0c;hp=fbcd5278c389056c0a8ab09a06c84b5df95f84db;hpb=e1aae876430ba9497ba1a524f315c5dee6cf285a;p=libucw.git diff --git a/ucw-json/json.c b/ucw-json/json.c index fbcd5278..780478d6 100644 --- a/ucw-json/json.c +++ b/ucw-json/json.c @@ -12,6 +12,10 @@ #include #include +#include +#include +#include + static void json_init(struct json_context *js) { mp_save(js->pool, &js->init_state); @@ -60,6 +64,31 @@ struct json_node *json_new_node(struct json_context *js, enum json_node_type typ return n; } +struct json_node *json_new_number(struct json_context *js, double value) +{ + ASSERT(isfinite(value)); + struct json_node *n = json_new_node(js, JSON_NUMBER); + n->number = 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);