Skip to content

Commit 2604a27

Browse files
committed
implement null object
* null object mapped to NULL * use lowercase for json_t members
1 parent 2d0fc2f commit 2604a27

File tree

4 files changed

+32
-16
lines changed

4 files changed

+32
-16
lines changed

include/json/common.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ typedef struct json_t {
4949
struct json_t *next;
5050
const char *key;
5151
void *value;
52-
int valSize;
53-
int keySize;
52+
int valsize;
53+
int keysize;
5454
json_type_t type;
5555
} json_t;
5656

include/json/impl/impl_json.h

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ json_parse(const char * __restrict contents, bool reverse) {
8585

8686
if (key) {
8787
obj->key = key;
88-
obj->keySize = keysize;
88+
obj->keysize = keysize;
8989
key = NULL;
9090
}
9191

@@ -194,7 +194,7 @@ json_parse(const char * __restrict contents, bool reverse) {
194194

195195
if (key) {
196196
val->key = key;
197-
val->keySize = keysize;
197+
val->keysize = keysize;
198198
key = NULL;
199199
}
200200

@@ -237,9 +237,22 @@ json_parse(const char * __restrict contents, bool reverse) {
237237
c = *++p;
238238
}
239239
}
240+
241+
val->valsize = (int)(end - (char *)val->value);
242+
243+
if (!foundQuote && val->valsize == 4) {
244+
char *n;
245+
246+
/* check if it is null */
247+
n = val->value;
248+
249+
if (n[0] == 'n' && n[1] == 'u' && n[2] == 'l' && n[3] == 'l') {
250+
val->value = NULL;
251+
val->valsize = 0;
252+
}
253+
}
240254

241-
val->valSize = (int)(end - (char *)val->value);
242-
c = *p;
255+
c = *p;
243256

244257
goto again;
245258
} /* if lookingForKey */

include/json/print.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@ json_print_ex(FILE * __restrict ostream,
3737
fprintf(ostream, "\t");
3838

3939
if (json->key)
40-
fprintf(ostream, "\"%.*s\": ", json->keySize, json->key);
40+
fprintf(ostream, "\"%.*s\": ", json->keysize, json->key);
4141
} else {
4242
if (json->key)
43-
fprintf(ostream, "\"%.*s\":", json->keySize, json->key);
43+
fprintf(ostream, "\"%.*s\":", json->keysize, json->key);
4444
}
4545

4646
switch (json->type) {
@@ -62,7 +62,10 @@ json_print_ex(FILE * __restrict ostream,
6262
break;
6363

6464
case JSON_STRING:
65-
fprintf(ostream, "\"%.*s\"", json->valSize, json_string(json));
65+
if (json->value)
66+
fprintf(ostream, "\"%.*s\"", json->valsize, json_string(json));
67+
else
68+
fprintf(ostream, "null");
6669

6770
if (json->next)
6871
fprintf(ostream, ",");

include/json/util.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,9 +171,9 @@ char*
171171
json_string_dup(const json_t * __restrict object) {
172172
char *s;
173173

174-
s = malloc(object->valSize + 1);
175-
memcpy(s, object->value, object->valSize);
176-
s[object->valSize] = '\0';
174+
s = malloc(object->valsize + 1);
175+
memcpy(s, object->value, object->valsize);
176+
s[object->valsize] = '\0';
177177

178178
return s;
179179
}
@@ -190,7 +190,7 @@ bool
190190
json_key_eq(const json_t * __restrict obj, const char * __restrict str) {
191191
size_t strsize;
192192

193-
if ((strsize = strlen(str)) != (size_t)obj->keySize)
193+
if ((strsize = strlen(str)) != (size_t)obj->keysize)
194194
return false;
195195

196196
return strncmp(str, obj->key, strsize) == 0;
@@ -209,7 +209,7 @@ bool
209209
json_key_eqsz(const json_t * __restrict obj,
210210
const char * __restrict str,
211211
size_t strsize) {
212-
if (strsize != (size_t)obj->keySize)
212+
if (strsize != (size_t)obj->keysize)
213213
return false;
214214

215215
return strncmp(str, obj->key, strsize) == 0;
@@ -227,7 +227,7 @@ bool
227227
json_val_eq(const json_t * __restrict obj, const char * __restrict str) {
228228
size_t strsize;
229229

230-
if ((strsize = strlen(str)) != (size_t)obj->valSize)
230+
if ((strsize = strlen(str)) != (size_t)obj->valsize)
231231
return false;
232232

233233
return strncmp(str, obj->value, strsize) == 0;
@@ -246,7 +246,7 @@ bool
246246
json_val_eqsz(const json_t * __restrict obj,
247247
const char * __restrict str,
248248
size_t strsize) {
249-
if (strsize != (size_t)obj->valSize)
249+
if (strsize != (size_t)obj->valsize)
250250
return false;
251251

252252
return strncmp(str, obj->value, strsize) == 0;

0 commit comments

Comments
 (0)