summaryrefslogtreecommitdiffhomepage
path: root/src/kernel/NeKit/Json.h
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal@nekernel.org>2025-11-24 03:02:43 +0100
committerAmlal El Mahrouss <amlal@nekernel.org>2025-11-24 03:02:43 +0100
commit83d870e58457a1d335a1d9b9966a6a1887cc297b (patch)
tree72888f88c7728c82f3f6df1f4f70591de15eab36 /src/kernel/NeKit/Json.h
parentab37adbacf0f33845804c788b39680cd754752a8 (diff)
feat! breaking changes on kernel sources.
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
Diffstat (limited to 'src/kernel/NeKit/Json.h')
-rw-r--r--src/kernel/NeKit/Json.h146
1 files changed, 146 insertions, 0 deletions
diff --git a/src/kernel/NeKit/Json.h b/src/kernel/NeKit/Json.h
new file mode 100644
index 00000000..1e804354
--- /dev/null
+++ b/src/kernel/NeKit/Json.h
@@ -0,0 +1,146 @@
+
+/* ========================================
+
+ Copyright (C) 2024-2025, Amlal El Mahrouss, licensed under the Apache 2.0 license.
+
+======================================== */
+
+#pragma once
+
+/// @brief Kernel JSON API.
+
+#include <CompilerKit/CompilerKit.h>
+#include <NeKit/Defines.h>
+#include <NeKit/KString.h>
+#include <NeKit/Stream.h>
+#include <NeKit/Utils.h>
+
+#define kNeJsonMaxLen (8196)
+#define kNeJsonLen (256)
+#define kNeJsonNullArr "[]"
+#define kNeJsonNullObj "{}"
+#define kNeJsonNullKey "null"
+#define kNeJsonNullValue kNeJsonNullKey
+
+namespace Kernel {
+/// ================================================================================
+/// @brief JSON object representation.
+/// ================================================================================
+template <typename CharKind = Char>
+class JsonObject final {
+ public:
+ explicit JsonObject() {
+ KBasicString<CharKind> key = KString(kNeJsonMaxLen);
+ key += kNeJsonNullValue;
+
+ this->AsKey() = key;
+ this->AsValue() = key;
+ }
+
+ explicit JsonObject(SizeT lhsLen, SizeT rhsLen) : fKey(lhsLen), fValue(rhsLen) {
+
+ KBasicString<CharKind> key = KString(lhsLen);
+ this->AsKey() = key;
+
+ KBasicString<CharKind> value = KString(rhsLen);
+ this->AsValue() = value;
+ }
+
+ ~JsonObject() = default;
+
+ NE_COPY_DEFAULT(JsonObject)
+ NE_MOVE_DEFAULT(JsonObject)
+
+ Bool& IsUndefined() { return fUndefined; }
+
+ private:
+ Bool fUndefined; // is this instance undefined?
+ KBasicString<CharKind> fKey;
+ KBasicString<CharKind> fValue;
+
+ public:
+ /// @brief returns the key of the json
+ /// @return the key as string view.
+ KBasicString<CharKind>& AsKey() { return fKey; }
+
+ /// @brief returns the value of the json.
+ /// @return the key as string view.
+ KBasicString<CharKind>& AsValue() { return fValue; }
+
+ STATIC JsonObject<CharKind> kNull;
+};
+
+/// ================================================================================
+/// @brief JsonObject stream reader helper for ASCII.
+/// ================================================================================
+struct AsciiJsonStreamReader final {
+ STATIC JsonObject<Char> 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 JsonObject<Char>{0, 0};
+
+ start_val = '[';
+ end_val = ']';
+
+ probe_value = true;
+ }
+
+ SizeT len = rt_string_len(full_array);
+
+ SizeT key_len = 0;
+ SizeT value_len = 0;
+
+ JsonObject<Char> type(kNeJsonMaxLen, kNeJsonMaxLen);
+
+ 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 {
+ if (full_array[i] == '\'') {
+ type.AsValue().Data()[value_len] = 0;
+ break;
+ }
+
+ type.AsValue().Data()[value_len] = full_array[i];
+
+ ++value_len;
+ }
+ } else {
+ if (start_val == '[') continue;
+
+ if (full_array[i] == ':') {
+ type.AsKey().Data()[key_len] = 0;
+ ++key_len;
+
+ ++i;
+
+ while (full_array[i] == ' ' || full_array[i] == '\t') ++i;
+
+ probe_value = true;
+ } else {
+ type.AsKey().Data()[key_len] = full_array[i];
+
+ ++key_len;
+ }
+ }
+ }
+
+ type.AsValue().Data()[value_len] = 0;
+
+ return type;
+ }
+};
+
+/// ================================================================================
+/// @brief AsciiJsonStream type definition.
+/// ================================================================================
+using AsciiJsonStream = Stream<AsciiJsonStreamReader, JsonObject<Char>>;
+} // namespace Kernel