diff options
| author | Amlal <amlal@nekernel.org> | 2025-04-25 13:08:33 +0200 |
|---|---|---|
| committer | Amlal <amlal@nekernel.org> | 2025-04-25 13:08:33 +0200 |
| commit | fb790b07aeba8e22e4190cf3e1834d11ecde6c96 (patch) | |
| tree | 4cec7d1b321307b1d5935577631dae116a658a37 /dev/kernel/NewKit/Json.h | |
| parent | 63a2d92c5dfe976175cda024ec01905d11b43738 (diff) | |
dev: better .clang-format, ran format command.
Signed-off-by: Amlal <amlal@nekernel.org>
Diffstat (limited to 'dev/kernel/NewKit/Json.h')
| -rw-r--r-- | dev/kernel/NewKit/Json.h | 258 |
1 files changed, 111 insertions, 147 deletions
diff --git a/dev/kernel/NewKit/Json.h b/dev/kernel/NewKit/Json.h index 8520dd71..6af71b72 100644 --- a/dev/kernel/NewKit/Json.h +++ b/dev/kernel/NewKit/Json.h @@ -1,7 +1,7 @@ /* ------------------------------------------- - Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. + Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. ------------------------------------------- */ @@ -11,155 +11,119 @@ #include <CompilerKit/CompilerKit.h> #include <NewKit/Defines.h> -#include <NewKit/Stream.h> #include <NewKit/KString.h> +#include <NewKit/Stream.h> #include <NewKit/Utils.h> -#define kJSONMaxLen (8196) -#define kJSONLen (256) +#define kJSONMaxLen (8196) +#define kJSONLen (256) #define kJSONNullArr "[]" #define kJSONNullObj "{}" -namespace Kernel -{ - /// @brief JavaScript object class - class Json final - { - public: - explicit Json() - { - auto len = kJSONMaxLen; - KString key = KString(len); - key += kJSONNullObj; - - this->AsKey() = key; - this->AsValue() = key; - } - - explicit Json(SizeT lhsLen, SizeT rhsLen) - : fKey(lhsLen), fValue(rhsLen) - { - } - - ~Json() = default; - - NE_COPY_DEFAULT(Json) - - 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(kJSONMaxLen, kJSONMaxLen); - - 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; - } - }; - - using JsonStream = Stream<JsonStreamReader, Json>; -} // namespace Kernel +namespace Kernel { +/// @brief JavaScript object class +class Json final { + public: + explicit Json() { + auto len = kJSONMaxLen; + KString key = KString(len); + key += kJSONNullObj; + + this->AsKey() = key; + this->AsValue() = key; + } + + explicit Json(SizeT lhsLen, SizeT rhsLen) : fKey(lhsLen), fValue(rhsLen) {} + + ~Json() = default; + + NE_COPY_DEFAULT(Json) + + 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(kJSONMaxLen, kJSONMaxLen); + + 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; + } +}; + +using JsonStream = Stream<JsonStreamReader, Json>; +} // namespace Kernel |
