summaryrefslogtreecommitdiffhomepage
path: root/dev/Kernel/NewKit/Json.h
diff options
context:
space:
mode:
authorAmlal <amlal.elmahrouss@icloud.com>2025-01-24 10:38:36 +0100
committerAmlal <amlal.elmahrouss@icloud.com>2025-01-24 10:38:36 +0100
commit7b4bd3577a31d0f0adc7371840642791ae1567f4 (patch)
tree1a8afc973aaa739d0d763315cad2fd376d1cea9c /dev/Kernel/NewKit/Json.h
ADD: Open version, with important changes kept out.
Signed-off-by: Amlal <amlal.elmahrouss@icloud.com>
Diffstat (limited to 'dev/Kernel/NewKit/Json.h')
-rw-r--r--dev/Kernel/NewKit/Json.h151
1 files changed, 151 insertions, 0 deletions
diff --git a/dev/Kernel/NewKit/Json.h b/dev/Kernel/NewKit/Json.h
new file mode 100644
index 00000000..250bc716
--- /dev/null
+++ b/dev/Kernel/NewKit/Json.h
@@ -0,0 +1,151 @@
+
+/* -------------------------------------------
+
+ Copyright (C) 2024, Amlal EL Mahrouss, all rights reserved.
+
+------------------------------------------- */
+
+#pragma once
+
+// last-rev: 30/01/24
+
+#include <CompilerKit/CompilerKit.h>
+#include <NewKit/Defines.h>
+#include <NewKit/Stream.h>
+#include <NewKit/KString.h>
+#include <NewKit/Utils.h>
+
+#define kMaxJsonPath 4096
+#define kJSONLen 32
+#define kJSONNull "null"
+
+namespace Kernel
+{
+ /// @brief Json class
+ class JSON final
+ {
+ public:
+ explicit JSON()
+ {
+ auto len = kJSONLen;
+ KString key = KString(len);
+ key += kJSONNull;
+
+ this->AsKey() = key;
+ this->AsValue() = key;
+ }
+
+ explicit JSON(SizeT lhsLen, SizeT rhsLen)
+ : fKey(lhsLen), fValue(rhsLen)
+ {
+ }
+
+ ~JSON() = default;
+
+ ZKA_COPY_DEFAULT(JSON);
+
+ const Bool& IsUndefined()
+ {
+ return fUndefined;
+ }
+
+ private:
+ Bool fUndefined; // is this instance undefined?
+ KString fKey;
+ KString fValue;
+
+ public:
+ /// @brief returns the key of the json
+ /// @return the key as string view.
+ KString& AsKey()
+ {
+ return fKey;
+ }
+
+ /// @brief returns the value of the json.
+ /// @return the key as string view.
+ KString& AsValue()
+ {
+ return fValue;
+ }
+
+ static JSON kNull;
+ };
+
+ /// @brief Json stream reader helper.
+ struct JsonStreamReader final
+ {
+ STATIC JSON In(const Char* full_array)
+ {
+ auto start_val = '{';
+ auto end_val = '}';
+ Boolean probe_value = false;
+
+ if (full_array[0] != start_val)
+ {
+ if (full_array[0] != '[')
+ return JSON::kNull;
+
+ start_val = '[';
+ end_val = ']';
+
+ probe_value = true;
+ }
+
+ SizeT len = rt_string_len(full_array);
+
+ SizeT key_len = 0;
+ SizeT value_len = 0;
+
+ JSON type(kMaxJsonPath, kMaxJsonPath);
+
+ for (SizeT i = 1; i < len; ++i)
+ {
+ if (full_array[i] == '\r' ||
+ full_array[i] == '\n')
+ continue;
+
+ if (probe_value)
+ {
+ if (full_array[i] == end_val ||
+ full_array[i] == ',')
+ {
+ probe_value = false;
+
+ ++value_len;
+ }
+ else
+ {
+ type.AsValue().Data()[value_len] = full_array[i];
+
+ ++value_len;
+ }
+ }
+ else
+ {
+ if (start_val == '[')
+ continue;
+
+ if (full_array[i] == ':')
+ {
+ probe_value = true;
+ type.AsKey().Data()[key_len] = 0;
+ ++key_len;
+ }
+ else
+ {
+ type.AsKey().Data()[key_len] = full_array[i];
+
+ ++key_len;
+ }
+ }
+ }
+
+ type.AsValue().Data()[value_len] = 0;
+
+ return type;
+ }
+ };
+
+ using JsonStream = Stream<JsonStreamReader, JSON>;
+} // namespace Kernel