From 91ff61cc22c08e2c247b6b689561e6d18cf276e7 Mon Sep 17 00:00:00 2001 From: pommicket Date: Thu, 22 Dec 2022 18:55:15 -0500 Subject: detail text --- json.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'json.c') diff --git a/json.c b/json.c index c11d907..e07b0ec 100644 --- a/json.c +++ b/json.c @@ -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]; -- cgit v1.2.3