From: Eric S. Raymond Date: Wed, 17 Sep 2014 21:39:09 +0000 (-0400) Subject: Allow reals and bools as array elements. These cases need testing. X-Git-Tag: 1.0~24 X-Git-Url: http://thyrsus.com/gitweb/?a=commitdiff_plain;h=633677c5ac42a0c174fb577794808995636e3861;p=microjson.git Allow reals and bools as array elements. These cases need testing. --- diff --git a/microjson.c b/microjson.c index 58725ae..6472213 100644 --- a/microjson.c +++ b/microjson.c @@ -27,10 +27,9 @@ vs. boolean, but not signed integer vs. unsigned integer). The parser will match the right spec against the actual data. The dialect this parses has some limitations. First, it cannot -recognize the JSON "null" value. Secondly, arrays may only have -objects or strings - not reals or integers or floats - as elements -(this limitation could be easily removed if required). Third, all -elements of an array must be of the same type. +recognize the JSON "null" value. Second, all elements of an array must +be of the same type. Third, some types (t_time, t_character) may not +ve array elements (this restriction ciuld be lifted) There are separate entry points for beginning a parse of either JSON object or a JSON array. JSON "float" quantities are actually @@ -860,15 +859,30 @@ int json_read_array(const char *cp, const struct json_array_t *arr, cp = ep; break; case t_uinteger: - arr->arr.integers.store[offset] = (int)strtoul(cp, &ep, 0); + arr->arr.uintegers.store[offset] = (int)strtoul(cp, &ep, 0); if (ep == cp) return JSON_ERR_BADNUM; else cp = ep; break; - case t_time: case t_real: + arr->arr.real.store[offset] = (int)strtod(cp, &ep); + if (ep == cp) + return JSON_ERR_BADNUM; + else + cp = ep; + break; case t_boolean: + if (strncmp(cp, "true", 4) == 0) { + arr->arr.boolean.store[offset] = true; + cp += 4; + } + else if (strncmp(cp, "false", 5) == 0) { + arr->arr.boolean.store[offset] = false; + cp += 5; + } + break; + case t_time: case t_character: case t_array: case t_check: diff --git a/microjson.h b/microjson.h index 43b453e..1041e3d 100644 --- a/microjson.h +++ b/microjson.h @@ -34,8 +34,16 @@ struct json_array_t { } strings; struct { int *store; - int storelen; } integers; + struct { + unsigned int *store; + } uintegers; + struct { + double *store; + } real; + struct { + bool *store; + } boolean; } arr; int *count, maxlen; };