jshn: properly support JSON "null" type

Instead of abort parsing, properly deal with "null" values by implementing
support for reading and formatting such values.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
This commit is contained in:
Jo-Philipp Wich 2018-01-07 15:46:31 +01:00
parent 729f47fd52
commit 1c08e80313
2 changed files with 14 additions and 3 deletions

11
jshn.c
View file

@ -105,9 +105,6 @@ static int add_json_element(const char *key, json_object *obj)
{ {
char *type; char *type;
if (!obj)
return -1;
switch (json_object_get_type(obj)) { switch (json_object_get_type(obj)) {
case json_type_object: case json_type_object:
type = "object"; type = "object";
@ -127,6 +124,9 @@ static int add_json_element(const char *key, json_object *obj)
case json_type_double: case json_type_double:
type = "double"; type = "double";
break; break;
case json_type_null:
type = "null";
break;
default: default:
return -1; return -1;
} }
@ -159,6 +159,9 @@ static int add_json_element(const char *key, json_object *obj)
case json_type_double: case json_type_double:
fprintf(stdout, "' %lf;\n", json_object_get_double(obj)); fprintf(stdout, "' %lf;\n", json_object_get_double(obj));
break; break;
case json_type_null:
fprintf(stdout, "';\n");
break;
default: default:
return -1; return -1;
} }
@ -240,6 +243,8 @@ static void jshn_add_object_var(json_object *obj, bool array, const char *prefix
new = json_object_new_double(strtod(var, NULL)); new = json_object_new_double(strtod(var, NULL));
} else if (!strcmp(type, "boolean")) { } else if (!strcmp(type, "boolean")) {
new = json_object_new_boolean(!!atoi(var)); new = json_object_new_boolean(!!atoi(var));
} else if (!strcmp(type, "null")) {
new = NULL;
} else { } else {
return; return;
} }

View file

@ -168,6 +168,12 @@ json_add_double() {
_json_add_generic double "$1" "$2" "$cur" _json_add_generic double "$1" "$2" "$cur"
} }
json_add_null() {
local cur
_json_get_var cur JSON_CUR
_json_add_generic null "$1" "" "$cur"
}
# functions read access to json variables # functions read access to json variables
json_load() { json_load() {