Skip to content

Commit

Permalink
implement null object
Browse files Browse the repository at this point in the history
* null object mapped to NULL
* use lowercase for json_t members
  • Loading branch information
recp committed Dec 8, 2019
1 parent 2d0fc2f commit 2604a27
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 16 deletions.
4 changes: 2 additions & 2 deletions include/json/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
21 changes: 17 additions & 4 deletions include/json/impl/impl_json.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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 */
Expand Down
9 changes: 6 additions & 3 deletions include/json/print.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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, ",");
Expand Down
14 changes: 7 additions & 7 deletions include/json/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 2604a27

Please sign in to comment.