diff options
| author | Amlal El Mahrouss <amlal.elmahrouss@icloud.com> | 2024-05-09 00:42:44 +0200 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal.elmahrouss@icloud.com> | 2024-05-09 00:42:44 +0200 |
| commit | af8a516fc22865abd80d6e26f1541fa3d6bebfdc (patch) | |
| tree | 96d42a10945fc03df022389aef54708383c1d616 /Kernel/NewKit/Json.hpp | |
| parent | a874e9cc98df994178d55996943fe81799c61d2f (diff) | |
MHR-23: :boom:, refactors.
- Move NewBoot to /Boot, thus making Kernel directory only containing the kernel.
Signed-off-by: Amlal El Mahrouss <amlal.elmahrouss@icloud.com>
Diffstat (limited to 'Kernel/NewKit/Json.hpp')
| -rw-r--r-- | Kernel/NewKit/Json.hpp | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/Kernel/NewKit/Json.hpp b/Kernel/NewKit/Json.hpp new file mode 100644 index 00000000..ebdcc848 --- /dev/null +++ b/Kernel/NewKit/Json.hpp @@ -0,0 +1,118 @@ + +/* ------------------------------------------- + + Copyright SoftwareLabs + +------------------------------------------- */ + +#pragma once + +// last-rev: 30/01/24 + +#include <CompilerKit/CompilerKit.hxx> +#include <NewKit/Defines.hpp> +#include <NewKit/Stream.hpp> +#include <NewKit/String.hpp> +#include <NewKit/Utils.hpp> + +namespace NewOS +{ + /// @brief Json value class + class JsonType final + { + public: + explicit JsonType() + : NewOS::JsonType(1, 1) + { + } + + explicit JsonType(SizeT lhsLen, SizeT rhsLen) + : fKey(lhsLen), fValue(rhsLen) + { + } + + ~JsonType() = default; + + NEWOS_COPY_DEFAULT(JsonType); + + private: + StringView fKey; + StringView fValue; + + public: + /// @brief returns the key of the json + /// @return the key as string view. + StringView& AsKey() + { + return fKey; + } + + /// @brief returns the value of the json. + /// @return the key as string view. + StringView& AsValue() + { + return fValue; + } + + static JsonType kUndefined; + }; + + /// @brief Json stream helper class. + struct JsonStreamTrait final + { + JsonType In(const char* full_array) + { + SizeT len = rt_string_len(full_array); + + if (full_array[0] == '\"' && full_array[len - 1] == ',' || + full_array[len - 1] == '\"') + { + Boolean probe_key = true; + + SizeT key_len = 0; + SizeT value_len = 0; + + for (SizeT i = 1; i < len; i++) + { + if (full_array[i] == ' ') + continue; + + JsonType type(kPathLen, kPathLen); + + if (probe_key) + { + type.AsKey().Data()[key_len] = full_array[i]; + ++key_len; + + if (full_array[i] == '\"') + { + probe_key = false; + type.AsKey().Data()[key_len] = 0; + + ++i; + } + } + else + { + type.AsValue().Data()[value_len] = full_array[i]; + ++value_len; + + if (full_array[i] == '\"') + { + type.AsValue().Data()[value_len] = 0; + } + } + } + } + + return JsonType::kUndefined; + } + + JsonType Out(JsonType& out) + { + return out; + } + }; + + using JsonStream = Stream<JsonStreamTrait, JsonType>; +} // namespace NewOS |
