NeKernel dev
Loading...
Searching...
No Matches
Json.h
Go to the documentation of this file.
1
2/* ========================================
3
4 Copyright (C) 2024-2025, Amlal El Mahrouss, licensed under the Apache 2.0 license.
5
6======================================== */
7
8#pragma once
9
11
13#include <NeKit/Config.h>
14#include <NeKit/KString.h>
15#include <NeKit/Stream.h>
16#include <NeKit/Utils.h>
17
18#define kNeJsonMaxLen (8196)
19#define kNeJsonLen (256)
20#define kNeJsonNullArr "[]"
21#define kNeJsonNullObj "{}"
22#define kNeJsonNullKey "null"
23#define kNeJsonNullValue kNeJsonNullKey
24
25namespace Kernel {
29template <typename CharKind = Char>
30class JsonObject final {
31 public:
32 explicit JsonObject() : fUndefined(NO) {
34 key += kNeJsonNullValue;
35
36 this->AsKey() = key;
37 this->AsValue() = key;
38 }
39
40 explicit JsonObject(SizeT lhsLen, SizeT rhsLen) : fUndefined(NO), fKey(lhsLen), fValue(rhsLen) {
41 KBasicString<CharKind> key = KString(lhsLen);
42 this->AsKey() = key;
43
44 KBasicString<CharKind> value = KString(rhsLen);
45 this->AsValue() = value;
46 }
47
48 ~JsonObject() = default;
49
52
54
55 private:
56 Bool fUndefined; // is this instance undefined?
59
60 public:
64
68
70};
71
76 STATIC JsonObject<Char> In(const Char* full_array) {
77 auto start_val = '{';
78 auto end_val = '}';
79 Boolean probe_value = false;
80
81 if (full_array[0] != start_val) {
82 if (full_array[0] != '[') return JsonObject<Char>{0, 0};
83
84 start_val = '[';
85 end_val = ']';
86
87 probe_value = true;
88 }
89
90 SizeT len = rt_string_len(full_array);
91
92 SizeT key_len = 0;
93 SizeT value_len = 0;
94
96
97 for (SizeT i = 1; i < len; ++i) {
98 if (full_array[i] == '\r' || full_array[i] == '\n') continue;
99
100 if (probe_value) {
101 if (full_array[i] == end_val || full_array[i] == ',') {
102 probe_value = false;
103
104 ++value_len;
105 } else {
106 if (full_array[i] == '\'') {
107 type.AsValue().Data()[value_len] = 0;
108 break;
109 }
110
111 type.AsValue().Data()[value_len] = full_array[i];
112
113 ++value_len;
114 }
115 } else {
116 if (start_val == '[') continue;
117
118 if (full_array[i] == ':') {
119 type.AsKey().Data()[key_len] = 0;
120 ++key_len;
121
122 ++i;
123
124 while (full_array[i] == ' ' || full_array[i] == '\t') ++i;
125
126 probe_value = true;
127 } else {
128 type.AsKey().Data()[key_len] = full_array[i];
129
130 ++key_len;
131 }
132 }
133 }
134
135 type.AsValue().Data()[value_len] = 0;
136
137 return type;
138 }
139};
140
145} // namespace Kernel
#define NE_COPY_DEFAULT(KLASS)
Definition Detail.h:17
#define NE_MOVE_DEFAULT(KLASS)
Definition Detail.h:25
#define STATIC
#define NO
#define kNeJsonMaxLen
Kernel JSON API.
Definition Json.h:18
#define kNeJsonNullValue
Definition Json.h:23
JSON object representation.
Definition Json.h:30
KBasicString< CharKind > & AsValue()
returns the value of the json.
Definition Json.h:67
~JsonObject()=default
KBasicString< CharKind > fKey
Definition Json.h:57
Bool & IsUndefined()
Definition Json.h:53
KBasicString< CharKind > & AsKey()
returns the key of the json
Definition Json.h:63
JsonObject()
Definition Json.h:32
STATIC JsonObject< CharKind > kNull
Definition Json.h:69
JsonObject(SizeT lhsLen, SizeT rhsLen)
Definition Json.h:40
Bool fUndefined
Definition Json.h:56
KBasicString< CharKind > fValue
Definition Json.h:58
Kernel string class, not dynamic.
Definition KString.h:20
Definition Stream.h:15
UPS inline definitions.
Definition Device.h:12
char Char
Definition Config.h:51
Size rt_string_len(const Char *str)
Definition AsciiUtils.cc:23
__SIZE_TYPE__ SizeT
Definition Config.h:60
Stream< AsciiJsonStreamReader, JsonObject< Char > > AsciiJsonStream
AsciiJsonStream type definition.
Definition Json.h:144
bool Boolean
Definition Config.h:49
KBasicString<> KString
Definition KString.h:76
bool Bool
Definition Config.h:50
JsonObject stream reader helper for ASCII.
Definition Json.h:75
STATIC JsonObject< Char > In(const Char *full_array)
Definition Json.h:76