FastLED 3.10.6
Loading...
Searching...
No Matches

◆ crgbToJson()

optional< json > fl::crgbToJson ( span< const CRGB > pixels)

Encode a CRGB pixel buffer as a versioned JSON object.

Parameters
pixelsPixel buffer to encode.
Returns
JSON object with version and pixels fields, or nullopt if the document could not be built (for example, due to allocation failure).

Definition at line 63 of file crgb_json.cpp.hpp.

63 {
64 json encodedPixels = json::array();
65 if (!encodedPixels.is_array()) {
66 return fl::nullopt;
67 }
68
69 for (const CRGB& pixel : pixels) {
70 json encodedPixel = json::array();
71 if (!encodedPixel.is_array()) {
72 return fl::nullopt;
73 }
74
75 encodedPixel.push_back(json(static_cast<int>(pixel.r)));
76 encodedPixel.push_back(json(static_cast<int>(pixel.g)));
77 encodedPixel.push_back(json(static_cast<int>(pixel.b)));
78 if (encodedPixel.size() != 3 || !crgbJsonPixel(encodedPixel)) {
79 return fl::nullopt;
80 }
81
82 const size oldSize = encodedPixels.size();
83 encodedPixels.push_back(encodedPixel);
84 if (encodedPixels.size() != oldSize + 1) {
85 return fl::nullopt;
86 }
87 }
88
89 json document = json::object();
90 if (!document.is_object()) {
91 return fl::nullopt;
92 }
93 document.set("version", 1);
94 document.set("pixels", encodedPixels);
95 if (!crgbJsonMatches(document, pixels)) {
96 return fl::nullopt;
97 }
98 return document;
99}
size_t size() const FL_NO_EXCEPT
Definition json.h:798
bool is_array() const FL_NO_EXCEPT
Definition json.h:389
void push_back(const json &value) FL_NO_EXCEPT
Definition json.h:918
static json object() FL_NO_EXCEPT
Definition json.h:857
static json array() FL_NO_EXCEPT
Definition json.h:853
fl::CRGB CRGB
Definition crgb.h:25
optional< CRGB > crgbJsonPixel(const json &value) FL_NO_EXCEPT
bool crgbJsonMatches(const json &document, span< const CRGB > pixels) FL_NO_EXCEPT
constexpr nullopt_t nullopt
Definition optional.h:13

References fl::json::array(), FL_NO_EXCEPT, fl::json::is_array(), fl::json::is_object(), nullopt, fl::json::object(), fl::json::push_back(), fl::json::set(), and fl::json::size().

+ Here is the call graph for this function: