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

◆ operator[]() [2/2]

JsonValue & fl::JsonValue::operator[] ( size_t idx)
inline

Definition at line 1363 of file json.h.

1365 {
1366 if (!is_array()) data = JsonArray{};
1367 // Handle regular JsonArray
1368 if (data.is<JsonArray>()) {
1369 auto ptr = data.ptr<JsonArray>();
1370 if (!ptr) return get_null_value(); // Handle error case
1371 auto &arr = *ptr;
1372 if (idx >= arr.size()) {
1373 // Resize array and fill with null values
1374 for (size_t i = arr.size(); i <= idx; i++) {
1375 arr.push_back(fl::make_shared<JsonValue>());
1376 }
1377 }
1378 if (idx >= arr.size()) return get_null_value(); // Handle error case
1379 return *arr[idx];
1380 }
1381 // For packed arrays, we need to convert them to regular arrays first
1382 // This is needed for compatibility with existing code that expects JsonArray
1383 if (data.is<fl::vector<int16_t>>() ||
1384 data.is<fl::vector<uint8_t>>() ||
1385 data.is<fl::vector<float>>()) {
1386 // Convert to regular JsonArray
1387 auto arr = as_array();
1388 if (arr) {
1389 data = fl::move(*arr);
1390 auto ptr = data.ptr<JsonArray>();
1391 if (!ptr) return get_null_value();
1392 auto &jsonArr = *ptr;
1393 if (idx >= jsonArr.size()) {
1394 // Resize array and fill with null values
1395 for (size_t i = jsonArr.size(); i <= idx; i++) {
1396 jsonArr.push_back(fl::make_shared<JsonValue>());
1397 }
1398 }
1399 if (idx >= jsonArr.size()) return get_null_value();
1400 return *jsonArr[idx];
1401 }
1402 }
constexpr remove_reference< T >::type && move(T &&t) noexcept
Definition move.h:27
shared_ptr< T > make_shared(Args &&... args)
Definition shared_ptr.h:348
fl::vector< fl::shared_ptr< JsonValue > > JsonArray
Definition json.h:170
HeapVector< T, Allocator > vector
Definition vector.h:1214
FL_DISABLE_WARNING_POP JsonValue & get_null_value()
Definition json.cpp:70
bool is_array() const noexcept
Definition json.h:865
variant_t data
Definition json.h:678
fl::optional< JsonArray > as_array()
Definition json.h:963

References JsonValue(), as_array(), data, fl::get_null_value(), is_array(), fl::make_shared(), and fl::move().

+ Here is the call graph for this function: