summaryrefslogtreecommitdiffstats
path: root/util/json.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-29 04:41:05 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-29 04:41:05 +0000
commit1cc8413aaf5f8fa6595aece1933462c096e88639 (patch)
treee97b4f25c511372d73bdd96c389c5f468d99138a /util/json.c
parentInitial commit. (diff)
downloadnvme-cli-5cfa46883ee24249a66010b27ade5caf2917916a.tar.xz
nvme-cli-5cfa46883ee24249a66010b27ade5caf2917916a.zip
Adding upstream version 2.4+really2.3.upstream/2.4+really2.3upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--util/json.c60
1 files changed, 60 insertions, 0 deletions
diff --git a/util/json.c b/util/json.c
new file mode 100644
index 0000000..84d43e5
--- /dev/null
+++ b/util/json.c
@@ -0,0 +1,60 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+#include <stdio.h>
+#include <errno.h>
+
+#include "json.h"
+#include "types.h"
+
+struct json_object *util_json_object_new_double(long double d)
+{
+ struct json_object *obj;
+ char *str;
+
+ if (asprintf(&str, "%Lf", d) < 0)
+ return NULL;
+
+ obj = json_object_new_string(str);
+
+ free(str);
+ return obj;
+
+}
+
+struct json_object *util_json_object_new_uint64(uint64_t i)
+{
+ struct json_object *obj;
+ char *str;
+
+ if (asprintf(&str, "%" PRIu64, i) < 0)
+ return NULL;
+
+ obj = json_object_new_string(str);
+
+ free(str);
+ return obj;
+
+}
+
+struct json_object *util_json_object_new_uint128(nvme_uint128_t val)
+{
+ struct json_object *obj;
+ obj = json_object_new_string(uint128_t_to_string(val));
+ return obj;
+}
+
+uint64_t util_json_object_get_uint64(struct json_object *obj)
+{
+ uint64_t val = 0;
+
+ if (json_object_is_type(obj, json_type_string)) {
+ char *end = NULL;
+ const char *buf;
+
+ buf = json_object_get_string(obj);
+ val = strtoull(buf, &end, 10);
+ if ((val == 0 && errno != 0) || (end == buf))
+ return 0;
+ }
+
+ return val;
+}