FastLED 3.10.9
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/json.h"
9#include "fl/stl/move.h" // fl::move
10
11namespace fl {
12
16
18
20 if (mLen > 0) {
21 if (mSink) {
22 mSink(mBuf, mLen);
23 }
24 mLen = 0;
25 }
26}
27
29 if (mLen >= kBufSize) {
30 flush();
31 }
32 mBuf[mLen++] = c;
33}
34
36 if (!s) {
37 return;
38 }
39 for (; *s; ++s) {
40 writeChar(*s);
41 }
42}
43
45 writeChar('"');
46 if (s) {
47 for (; *s; ++s) {
48 const unsigned char c = static_cast<unsigned char>(*s);
49 switch (c) {
50 case '"': puts("\\\""); break;
51 case '\\': puts("\\\\"); break;
52 case '\n': puts("\\n"); break;
53 case '\r': puts("\\r"); break;
54 case '\t': puts("\\t"); break;
55 case '\b': puts("\\b"); break;
56 case '\f': puts("\\f"); break;
57 default:
58 if (c < 0x20) {
59 // Control character → \u00XX
60 static const char *kHex = "0123456789abcdef";
61 puts("\\u00");
62 writeChar(kHex[(c >> 4) & 0xF]);
63 writeChar(kHex[c & 0xF]);
64 } else {
65 writeChar(static_cast<char>(c));
66 }
67 break;
68 }
69 }
70 }
71 writeChar('"');
72}
73
75 if (mPendingKey) {
76 // We just wrote a key + ':'; the value follows directly.
77 mPendingKey = false;
78 return;
79 }
80 // Element in an array (or a bare top-level value). Add a comma if this
81 // container already has a prior element.
82 if (mDepth > 0) {
83 if (mNeedComma[mDepth - 1]) {
84 writeChar(',');
85 }
86 mNeedComma[mDepth - 1] = true;
87 }
88}
89
91 if (mDepth > 0) {
92 if (mNeedComma[mDepth - 1]) {
93 writeChar(',');
94 }
95 mNeedComma[mDepth - 1] = true;
96 }
97}
98
100 if (mSuppressedDepth > 0) {
102 return;
103 }
104 if (mDepth == kMaxDepth) {
105 valueNull();
107 return;
108 }
110 writeChar('{');
111 mNeedComma[mDepth] = false;
112 ++mDepth;
113}
114
116 if (mSuppressedDepth > 0) {
118 return;
119 }
120 if (mDepth > 0) {
121 --mDepth;
122 }
123 writeChar('}');
124}
125
127 if (mSuppressedDepth > 0) {
129 return;
130 }
131 if (mDepth == kMaxDepth) {
132 valueNull();
134 return;
135 }
137 writeChar('[');
138 mNeedComma[mDepth] = false;
139 ++mDepth;
140}
141
143 if (mSuppressedDepth > 0) {
145 return;
146 }
147 if (mDepth > 0) {
148 --mDepth;
149 }
150 writeChar(']');
151}
152
154 if (mSuppressedDepth > 0) {
155 return;
156 }
157 prefixForKey();
158 putEscaped(k);
159 writeChar(':');
160 mPendingKey = true;
161}
162
164 if (mSuppressedDepth > 0) {
165 return;
166 }
168 putEscaped(s);
169}
170
172 if (mSuppressedDepth > 0) {
173 return;
174 }
176 char buf[24];
177 int len = fl::itoa64(n, buf, 10);
178 for (int i = 0; i < len; ++i) {
179 writeChar(buf[i]);
180 }
181}
182
184 if (mSuppressedDepth > 0) {
185 return;
186 }
188 puts(b ? "true" : "false");
189}
190
192 if (mSuppressedDepth > 0) {
193 return;
194 }
196 j.write_to(
197 [](void *ctx, const char *data, fl::size len) {
198 JsonStreamWriter *self = static_cast<JsonStreamWriter *>(ctx);
199 for (fl::size i = 0; i < len; ++i) {
200 self->writeChar(data[i]);
201 }
202 },
203 this);
204}
205
207 if (mSuppressedDepth > 0) {
208 return;
209 }
211 puts("null");
212}
213
214} // 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
FastLED's Elegant JSON Library: fl::json
constexpr remove_reference< T >::type && move(T &&t) FL_NO_EXCEPT
Definition s16x16x4.h:30
u8 u8 fract8 blur_amount FL_NO_EXCEPT
Definition blur.h:186
int itoa64(i64 value, char *sp, int radix) FL_NO_EXCEPT
Convert signed 64-bit integer to string buffer in given radix.
fl::i64 i64
Definition s16x16x4.h:222
bool flush(u32 timeoutMs) FL_NO_EXCEPT
Base definition for an LED controller.