diff --git a/include/json/common.h b/include/json/common.h index 68b64db..091cfc2 100644 --- a/include/json/common.h +++ b/include/json/common.h @@ -49,8 +49,8 @@ typedef struct json_t { struct json_t *next; const char *key; void *value; - int valSize; - int keySize; + int valsize; + int keysize; json_type_t type; } json_t; diff --git a/include/json/impl/impl_json.h b/include/json/impl/impl_json.h index b5d153c..6fcccf6 100644 --- a/include/json/impl/impl_json.h +++ b/include/json/impl/impl_json.h @@ -85,7 +85,7 @@ json_parse(const char * __restrict contents, bool reverse) { if (key) { obj->key = key; - obj->keySize = keysize; + obj->keysize = keysize; key = NULL; } @@ -194,7 +194,7 @@ json_parse(const char * __restrict contents, bool reverse) { if (key) { val->key = key; - val->keySize = keysize; + val->keysize = keysize; key = NULL; } @@ -237,9 +237,22 @@ json_parse(const char * __restrict contents, bool reverse) { c = *++p; } } + + val->valsize = (int)(end - (char *)val->value); + + if (!foundQuote && val->valsize == 4) { + char *n; + + /* check if it is null */ + n = val->value; + + if (n[0] == 'n' && n[1] == 'u' && n[2] == 'l' && n[3] == 'l') { + val->value = NULL; + val->valsize = 0; + } + } - val->valSize = (int)(end - (char *)val->value); - c = *p; + c = *p; goto again; } /* if lookingForKey */ diff --git a/include/json/print.h b/include/json/print.h index dbca96d..486403d 100644 --- a/include/json/print.h +++ b/include/json/print.h @@ -37,10 +37,10 @@ json_print_ex(FILE * __restrict ostream, fprintf(ostream, "\t"); if (json->key) - fprintf(ostream, "\"%.*s\": ", json->keySize, json->key); + fprintf(ostream, "\"%.*s\": ", json->keysize, json->key); } else { if (json->key) - fprintf(ostream, "\"%.*s\":", json->keySize, json->key); + fprintf(ostream, "\"%.*s\":", json->keysize, json->key); } switch (json->type) { @@ -62,7 +62,10 @@ json_print_ex(FILE * __restrict ostream, break; case JSON_STRING: - fprintf(ostream, "\"%.*s\"", json->valSize, json_string(json)); + if (json->value) + fprintf(ostream, "\"%.*s\"", json->valsize, json_string(json)); + else + fprintf(ostream, "null"); if (json->next) fprintf(ostream, ","); diff --git a/include/json/util.h b/include/json/util.h index faa47d4..d22a875 100644 --- a/include/json/util.h +++ b/include/json/util.h @@ -171,9 +171,9 @@ char* json_string_dup(const json_t * __restrict object) { char *s; - s = malloc(object->valSize + 1); - memcpy(s, object->value, object->valSize); - s[object->valSize] = '\0'; + s = malloc(object->valsize + 1); + memcpy(s, object->value, object->valsize); + s[object->valsize] = '\0'; return s; } @@ -190,7 +190,7 @@ bool json_key_eq(const json_t * __restrict obj, const char * __restrict str) { size_t strsize; - if ((strsize = strlen(str)) != (size_t)obj->keySize) + if ((strsize = strlen(str)) != (size_t)obj->keysize) return false; return strncmp(str, obj->key, strsize) == 0; @@ -209,7 +209,7 @@ bool json_key_eqsz(const json_t * __restrict obj, const char * __restrict str, size_t strsize) { - if (strsize != (size_t)obj->keySize) + if (strsize != (size_t)obj->keysize) return false; return strncmp(str, obj->key, strsize) == 0; @@ -227,7 +227,7 @@ bool json_val_eq(const json_t * __restrict obj, const char * __restrict str) { size_t strsize; - if ((strsize = strlen(str)) != (size_t)obj->valSize) + if ((strsize = strlen(str)) != (size_t)obj->valsize) return false; return strncmp(str, obj->value, strsize) == 0; @@ -246,7 +246,7 @@ bool json_val_eqsz(const json_t * __restrict obj, const char * __restrict str, size_t strsize) { - if (strsize != (size_t)obj->valSize) + if (strsize != (size_t)obj->valsize) return false; return strncmp(str, obj->value, strsize) == 0;