summaryrefslogtreecommitdiff
path: root/json.c
diff options
context:
space:
mode:
authorpommicket <pommicket@gmail.com>2022-12-22 18:55:15 -0500
committerpommicket <pommicket@gmail.com>2022-12-22 18:56:41 -0500
commit91ff61cc22c08e2c247b6b689561e6d18cf276e7 (patch)
treef8025b141122ba219fbe45a1a9c2770e49d76905 /json.c
parent2667c71e71d77ecade1142c133ed7181ce38c664 (diff)
detail text
Diffstat (limited to 'json.c')
-rw-r--r--json.c43
1 files changed, 43 insertions, 0 deletions
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];