FastLED 3.9.15
Loading...
Searching...
No Matches

◆ test_json_response()

void test_json_response ( )

APPROACH 3: JSON Response Handling with FastLED's ideal JSON API This demonstrates fetch responses with automatic JSON parsing.

Definition at line 221 of file NetTestReal.h.

221 {
222 FL_WARN("APPROACH 3: JSON Response handling with fl::Json integration");
223
224 // TUTORIAL: Fetch a JSON API endpoint (httpbin.org provides test JSON)
225 // This endpoint returns JSON with request information
226 fl::fetch_get("https://httpbin.org/json").then([](const fl::response& response) {
227 if (response.ok()) {
228 FL_WARN("SUCCESS [JSON Promise] HTTP fetch successful! Status: "
229 << response.status() << " " << response.status_text());
230
231 // TUTORIAL: Check if response contains JSON content
232 // is_json() checks Content-Type header and body content
233 if (response.is_json()) {
234 FL_WARN("DETECTED [JSON Promise] Response contains JSON data");
235
236 // TUTORIAL: response.json() returns fl::Json with FastLED's ideal API
237 // Automatic parsing, caching, and safe access with defaults using operator|
238 fl::Json data = response.json();
239
240 // TUTORIAL: Safe JSON access with defaults - never crashes!
241 // Uses FastLED's proven pattern: json["path"]["to"]["key"] | default_value
242 fl::string slideshow_url = data["slideshow"]["author"] | fl::string("unknown");
243 fl::string slideshow_title = data["slideshow"]["title"] | fl::string("untitled");
244 int slide_count = data["slideshow"]["slides"].size();
245
246 FL_WARN("JSON [Promise] Slideshow Author: " << slideshow_url);
247 FL_WARN("JSON [Promise] Slideshow Title: " << slideshow_title);
248 FL_WARN("JSON [Promise] Slide Count: " << slide_count);
249
250 // TUTORIAL: Access nested arrays safely
251 if (data.contains("slideshow") && data["slideshow"].contains("slides")) {
252 fl::Json slides = data["slideshow"]["slides"];
253 if (slides.is_array() && slides.size() > 0) {
254 // Get first slide information with safe defaults
255 fl::string first_slide_title = slides[0]["title"] | fl::string("no title");
256 fl::string first_slide_type = slides[0]["type"] | fl::string("unknown");
257 FL_WARN("JSON [Promise] First slide: " << first_slide_title << " (" << first_slide_type << ")");
258 }
259 }
260
261 // Visual feedback: Blue LEDs for successful JSON parsing
262 fill_solid(leds, NUM_LEDS, CRGB(0, 0, 128)); // Blue for JSON success
263
264 } else {
265 FL_WARN("INFO [JSON Promise] Response is not JSON format");
266 // Visual feedback: Yellow for non-JSON response
267 fill_solid(leds, NUM_LEDS, CRGB(64, 64, 0)); // Yellow for non-JSON
268 }
269 } else {
270 FL_WARN("ERROR [JSON Promise] HTTP error: " << response.status()
271 << " " << response.status_text());
272 // Visual feedback: Red LEDs for HTTP error
273 fill_solid(leds, NUM_LEDS, CRGB(64, 0, 0)); // Red for HTTP error
274 }
275 }).catch_([](const fl::Error& error) {
276 FL_WARN("ERROR [JSON Promise] Network error: " << error.message);
277 // Visual feedback: Purple LEDs for network error
278 fill_solid(leds, NUM_LEDS, CRGB(64, 0, 64)); // Purple for network error
279 });
280
281 FastLED.show();
282}
CRGB leds[NUM_LEDS]
#define NUM_LEDS
FL_DISABLE_WARNING_PUSH FL_DISABLE_WARNING_GLOBAL_CONSTRUCTORS CFastLED FastLED
Global LED strip management instance.
Definition FastLED.cpp:74
bool is_array() const
Definition json.h:1736
bool contains(size_t idx) const
Definition json.h:2078
size_t size() const
Definition json.h:2086
fl::Json json() const
Response body parsed as JSON (JavaScript-like API)
Definition fetch.cpp:319
const fl::string & status_text() const
HTTP status text (like JavaScript response.statusText)
Definition fetch.h:94
int status() const
HTTP status code (like JavaScript response.status)
Definition fetch.h:91
bool ok() const
Check if response is successful (like JavaScript response.ok)
Definition fetch.h:97
bool is_json() const
Check if response appears to contain JSON content.
Definition fetch.h:127
HTTP response class (unified interface)
Definition fetch.h:83
void fill_solid(struct CRGB *targetArray, int numToFill, const struct CRGB &color)
Fill a range of LEDs with a solid color.
Definition fill.cpp:9
fl::promise< response > fetch_get(const fl::string &url, const fetch_options &request)
HTTP GET request.
Definition fetch.cpp:180
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:86
fl::string message
Definition promise.h:54
Error type for promises.
Definition promise.h:53
#define FL_WARN
Definition warn.h:12

References fl::Json::contains(), FastLED, fl::fetch_get(), fl::fill_solid(), FL_WARN, fl::Json::is_array(), fl::response::is_json(), fl::response::json(), leds, fl::Error::message, NUM_LEDS, fl::response::ok(), fl::Json::size(), fl::response::status(), and fl::response::status_text().

Referenced by loop().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: