FastLED 3.10.6
Loading...
Searching...
No Matches
json_stream_writer.cpp.hpp
Go to the documentation of this file.
1#pragma once
2
3// IWYU pragma: private
4
6
7#include "fl/stl/charconv.h" // fl::itoa64
8#include "fl/stl/move.h" // fl::move
9
10namespace fl {
11
15
17
19 if (mLen > 0) {
20 if (mSink) {
21 mSink(mBuf, mLen);
22 }
23 mLen = 0;
24 }
25}
26
28 if (mLen >= kBufSize) {
29 flush();
30 }
31 mBuf[mLen++] = c;
32}
33
35 if (!s) {
36 return;
37 }
38 for (; *s; ++s) {
39 writeChar(*s);
40 }
41}
42
44 writeChar('"');
45 if (s) {
46 for (; *s; ++s) {
47 const unsigned char c = static_cast<unsigned char>(*s);
48 switch (c) {
49 case '"': puts("\\\""); break;
50 case '\\': puts("\\\\"); break;
51 case '\n': puts("\\n"); break;
52 case '\r': puts("\\r"); break;
53 case '\t': puts("\\t"); break;
54 case '\b': puts("\\b"); break;
55 case '\f': puts("\\f"); break;
56 default:
57 if (c < 0x20) {
58 // Control character → \u00XX
59 static const char *kHex = "0123456789abcdef";
60 puts("\\u00");
61 writeChar(kHex[(c >> 4) & 0xF]);
62 writeChar(kHex[c & 0xF]);
63 } else {
64 writeChar(static_cast<char>(c));
65 }
66 break;
67 }
68 }
69 }
70 writeChar('"');
71}
72
74 if (mPendingKey) {
75 // We just wrote a key + ':'; the value follows directly.
76 mPendingKey = false;
77 return;
78 }
79 // Element in an array (or a bare top-level value). Add a comma if this
80 // container already has a prior element.
81 if (mDepth > 0) {
82 if (mNeedComma[mDepth - 1]) {
83 writeChar(',');
84 }
85 mNeedComma[mDepth - 1] = true;
86 }
87}
88
90 if (mDepth > 0) {
91 if (mNeedComma[mDepth - 1]) {
92 writeChar(',');
93 }
94 mNeedComma[mDepth - 1] = true;
95 }
96}
97
99 if (mSuppressedDepth > 0) {
101 return;
102 }
103 if (mDepth == kMaxDepth) {
104 valueNull();
106 return;
107 }
109 writeChar('{');
110 mNeedComma[mDepth] = false;
111 ++mDepth;
112}
113
115 if (mSuppressedDepth > 0) {
117 return;
118 }
119 if (mDepth > 0) {
120 --mDepth;
121 }
122 writeChar('}');
123}
124
126 if (mSuppressedDepth > 0) {
128 return;
129 }
130 if (mDepth == kMaxDepth) {
131 valueNull();
133 return;
134 }
136 writeChar('[');
137 mNeedComma[mDepth] = false;
138 ++mDepth;
139}
140
142 if (mSuppressedDepth > 0) {
144 return;
145 }
146 if (mDepth > 0) {
147 --mDepth;
148 }
149 writeChar(']');
150}
151
153 if (mSuppressedDepth > 0) {
154 return;
155 }
156 prefixForKey();
157 putEscaped(k);
158 writeChar(':');
159 mPendingKey = true;
160}
161
163 if (mSuppressedDepth > 0) {
164 return;
165 }
167 putEscaped(s);
168}
169
171 if (mSuppressedDepth > 0) {
172 return;
173 }
175 char buf[24];
176 int len = fl::itoa64(n, buf, 10);
177 for (int i = 0; i < len; ++i) {
178 writeChar(buf[i]);
179 }
180}
181
183 if (mSuppressedDepth > 0) {
184 return;
185 }
187 puts(b ? "true" : "false");
188}
189
191 if (mSuppressedDepth > 0) {
192 return;
193 }
195 puts("null");
196}
197
198} // namespace fl
void writeChar(char c) FL_NO_EXCEPT
void prefixForValue() FL_NO_EXCEPT
fl::function< void(const char *data, fl::size len)> Sink
static constexpr fl::size kBufSize
static constexpr fl::size kMaxDepth
void prefixForKey() FL_NO_EXCEPT
void putEscaped(const char *s) FL_NO_EXCEPT
void beginObject() FL_NO_EXCEPT
void value(const char *s) FL_NO_EXCEPT
void key(const char *k) FL_NO_EXCEPT
bool mNeedComma[kMaxDepth]
void puts(const char *s) FL_NO_EXCEPT
JsonStreamWriter(Sink sink) FL_NO_EXCEPT
constexpr remove_reference< T >::type && move(T &&t) FL_NO_EXCEPT
Definition move.h:28
int itoa64(i64 value, char *sp, int radix)
Convert signed 64-bit integer to string buffer in given radix.
InputGamut g FL_NO_EXCEPT
Definition rgbw.h:121
bool flush(u32 timeoutMs)
Base definition for an LED controller.
Definition crgb.hpp:179