src/json.c

changeset 1014
56eb7da4f3e1
parent 1012
21884374edbb
child 1020
e78e65405c56
equal deleted inserted replaced
1013:add8358fc3c3 1014:56eb7da4f3e1
28 28
29 #include <string.h> 29 #include <string.h>
30 #include <ctype.h> 30 #include <ctype.h>
31 #include <assert.h> 31 #include <assert.h>
32 #include <stdio.h> 32 #include <stdio.h>
33 #include <errno.h>
33 34
34 #include "cx/json.h" 35 #include "cx/json.h"
35 36
36 /* 37 /*
37 * RFC 8259 38 * RFC 8259
38 * https://tools.ietf.org/html/rfc8259 39 * https://tools.ietf.org/html/rfc8259
39 */ 40 */
40
41 #define PARSER_READVALUE_ALLOC 32
42 41
43 static CxJsonValue cx_json_value_nothing = {.type = CX_JSON_NOTHING}; 42 static CxJsonValue cx_json_value_nothing = {.type = CX_JSON_NOTHING};
44 43
45 static void token_destroy(CxJsonToken *token) { 44 static void token_destroy(CxJsonToken *token) {
46 if (token->allocated) { 45 if (token->allocated) {
298 // the buffer guarantees that we are working on a copied string 297 // the buffer guarantees that we are working on a copied string
299 char c = str.ptr[str.length]; 298 char c = str.ptr[str.length];
300 str.ptr[str.length] = 0; 299 str.ptr[str.length] = 0;
301 300
302 if (asint) { 301 if (asint) {
302 errno = 0;
303 long long v = strtoll(str.ptr, &endptr, 10); 303 long long v = strtoll(str.ptr, &endptr, 10);
304 if (errno == ERANGE) {
305 return 1;
306 }
304 *((int64_t*)value) = (int64_t) v; 307 *((int64_t*)value) = (int64_t) v;
305 } else { 308 } else {
306 // TODO: proper JSON spec number parser 309 // TODO: proper JSON spec number parser
310 // TODO: also return an error when loss of precision is high
307 double v = strtod(str.ptr, &endptr); 311 double v = strtod(str.ptr, &endptr);
308 *((double*)value) = v; 312 *((double*)value) = v;
309 } 313 }
310 314
311 // recover from the hack 315 // recover from the hack

mercurial