diff options
author | pommicket <pommicket@gmail.com> | 2022-12-22 18:55:15 -0500 |
---|---|---|
committer | pommicket <pommicket@gmail.com> | 2022-12-22 18:56:41 -0500 |
commit | 91ff61cc22c08e2c247b6b689561e6d18cf276e7 (patch) | |
tree | f8025b141122ba219fbe45a1a9c2770e49d76905 /json.c | |
parent | 2667c71e71d77ecade1142c133ed7181ce38c664 (diff) |
detail text
Diffstat (limited to 'json.c')
-rw-r--r-- | json.c | 43 |
1 files changed, 43 insertions, 0 deletions
@@ -444,6 +444,49 @@ JSONValue json_object_get(const JSON *json, JSONObject object, const char *name) return (JSONValue){0}; } +// returns (JSONString){0} (which is interpreted as an empty string) if `name` does +// not exist or is not a string. +JSONString json_object_get_string(const JSON *json, JSONObject object, const char *name) { + JSONValue value = json_object_get(json, object, name); + if (value.type == JSON_STRING) { + return value.val.string; + } else { + return (JSONString){0}; + } +} + +// returns (JSONObject){0} (which is interpreted as an empty object) if `name` does +// not exist or is not an object. +JSONObject json_object_get_object(const JSON *json, JSONObject object, const char *name) { + JSONValue value = json_object_get(json, object, name); + if (value.type == JSON_OBJECT) { + return value.val.object; + } else { + return (JSONObject){0}; + } +} + +// returns (JSONArray){0} (which is interpreted as an empty array) if `name` does +// not exist or is not an array. +JSONArray json_object_get_array(const JSON *json, JSONObject object, const char *name) { + JSONValue value = json_object_get(json, object, name); + if (value.type == JSON_ARRAY) { + return value.val.array; + } else { + return (JSONArray){0}; + } +} + +// returns NaN if `name` does not exist or is not a number. +double json_object_get_number(const JSON *json, JSONObject object, const char *name) { + JSONValue value = json_object_get(json, object, name); + if (value.type == JSON_NUMBER) { + return value.val.number; + } else { + return NAN; + } +} + JSONValue json_array_get(const JSON *json, JSONArray array, u64 i) { if (i < array.len) { return json->values[array.elements + i]; |