blob: 3aab902a240f265e0ca093e6686becbd32441868 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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
|