159#ifndef FASTLED_ENABLE_JSON
160#define FASTLED_ENABLE_JSON SKETCH_HAS_LOTS_OF_MEMORY
189 operator const T&()
const {
208 const T*
result =
nullptr;
215 void accept(
const U& value) {
230 storage =
static_cast<T
>(value);
239 storage =
static_cast<T
>(value);
248 storage =
static_cast<T
>(value);
257 storage =
static_cast<T
>(value);
282template<
typename IntType =
int64_t>
287 void accept(
const U& value) {
293 template<
typename T = IntType>
300 template<
typename T = IntType>
307 template<
typename T = IntType>
314 template<
typename T = IntType>
317 result =
static_cast<IntType
>(value);
321 result =
static_cast<IntType
>(value ? 1 : 0);
325 template<
typename T = IntType>
329 result =
static_cast<IntType
>(value);
336 bool isValidInt =
true;
337 fl::size startPos = 0;
340 if (str.
length() > 0 && (str[0] ==
'+' || str[0] ==
'-')) {
345 for (fl::size i = startPos; i < str.
length(); i++) {
353 if (isValidInt && str.
length() > 0) {
355 result =
static_cast<IntType
>(parsed);
371 void accept(
const U& value) {
386 result =
static_cast<int64_t
>(value);
393 bool isValidInt =
true;
394 fl::size startPos = 0;
397 if (str.
length() > 0 && (str[0] ==
'+' || str[0] ==
'-')) {
402 for (fl::size i = startPos; i < str.
length(); i++) {
410 if (isValidInt && str.
length() > 0) {
412 result =
static_cast<int64_t
>(parsed);
423template<
typename FloatType =
double>
428 void accept(
const U& value) {
438 template<
typename T = FloatType>
441 result =
static_cast<FloatType
>(value);
445 template<
typename T = FloatType>
448 result =
static_cast<FloatType
>(value);
453 result =
static_cast<FloatType
>(value);
457 result =
static_cast<FloatType
>(value ? 1.0 : 0.0);
464 bool isValidFloat =
true;
465 bool hasDecimal =
false;
466 fl::size startPos = 0;
469 if (str.
length() > 0 && (str[0] ==
'+' || str[0] ==
'-')) {
474 for (fl::size i = startPos; i < str.
length(); i++) {
479 isValidFloat =
false;
484 isValidFloat =
false;
490 if (isValidFloat && str.
length() > 0) {
493 bool isSimpleDecimal =
true;
494 for (fl::size i = startPos; i < str.
length(); i++) {
497 isSimpleDecimal =
false;
502 if (isSimpleDecimal) {
505 result =
static_cast<FloatType
>(parsed);
509 result =
static_cast<FloatType
>(parsed);
526 void accept(
const U& value) {
536 result =
static_cast<double>(value);
541 result =
static_cast<double>(value);
545 result = value ? 1.0 : 0.0;
552 bool isValidFloat =
true;
553 bool hasDecimal =
false;
554 fl::size startPos = 0;
557 if (str.
length() > 0 && (str[0] ==
'+' || str[0] ==
'-')) {
562 for (fl::size i = startPos; i < str.
length(); i++) {
567 isValidFloat =
false;
572 isValidFloat =
false;
578 if (isValidFloat && str.
length() > 0) {
581 bool isSimpleDecimal =
true;
582 for (fl::size i = startPos; i < str.
length(); i++) {
585 isSimpleDecimal =
false;
590 if (isSimpleDecimal) {
593 result =
static_cast<double>(parsed);
597 result =
static_cast<double>(parsed);
613 void accept(
const U& value) {
639 result = value ?
"true" :
"false";
756 data =
static_cast<float>(d);
798 template<
typename Visitor>
803 template<
typename Visitor>
809 bool is_null() const noexcept {
813 bool is_bool() const noexcept {
817 bool is_int() const noexcept {
837 void accept(
const T& value) {
869 IsArrayVisitor visitor;
871 return visitor.result;
900 auto ptr =
data.ptr<
bool>();
910 IntConversionVisitor<int64_t> visitor;
912 return visitor.result;
915 template<
typename IntType>
933 FloatConversionVisitor<double> visitor;
935 return visitor.result;
942 template<
typename FloatType>
960 StringConversionVisitor visitor;
962 return visitor.result;
973 for (
const auto& item : *audioPtr) {
982 for (
const auto& item : *bytePtr) {
991 for (
const auto& item : *floatPtr) {
1022 auto ptr =
data.ptr<
bool>();
1032 IntConversionVisitor<int64_t> visitor;
1034 return visitor.result;
1037 template<
typename IntType>
1045 data.visit(visitor);
1053 template<
typename FloatType>
1061 data.visit(visitor);
1071 StringConversionVisitor visitor;
1073 return visitor.result;
1084 for (
const auto& item : *audioPtr) {
1093 for (
const auto& item : *bytePtr) {
1102 for (
const auto& item : *floatPtr) {
1132 template<
typename T>
1134 auto ptr =
data.ptr<T>();
1138 template<
typename T>
1140 auto ptr =
data.ptr<T>();
1185 template<
typename T>
1198 return ptr ? ptr->size() : 0;
1203 return ptr ? ptr->size() : 0;
1208 return ptr ? ptr->size() : 0;
1213 return ptr ? ptr->size() : 0;
1228 auto& val = *((*ptr)[
m_index]);
1233 auto opt = val.as_bool();
1326 template<
typename T>
1334 template<
typename T>
1342 template<
typename T>
1350 template<
typename T>
1372 if (idx >= arr.size()) {
1374 for (
size_t i = arr.size(); i <= idx; i++) {
1392 auto &jsonArr = *ptr;
1393 if (idx >= jsonArr.size()) {
1395 for (
size_t i = jsonArr.size(); i <= idx; i++) {
1400 return *jsonArr[idx];
1411 if (obj.find(key) == obj.end()) {
1419 template<
typename T>
1422 data.visit(visitor);
1427 template<
typename T>
1428 T
as_or(
const T& fallback)
const {
1430 data.visit(visitor);
1439 return ptr && idx < ptr->size();
1445 return ptr && idx < ptr->size();
1449 return ptr && idx < ptr->size();
1453 return ptr && idx < ptr->size();
1461 return ptr && ptr->find(key) != ptr->end();
1468 for (
auto it =
begin(); it !=
end(); ++it) {
1469 auto keyValue = *it;
1470 result.push_back(keyValue.first);
1480 size_t size()
const {
1484 return ptr ? ptr->size() : 0;
1490 return ptr ? ptr->size() : 0;
1494 return ptr ? ptr->size() : 0;
1498 return ptr ? ptr->size() : 0;
1503 return ptr ? ptr->size() : 0;
1520 JsonObject::iterator
m_iter;
1566 JsonObject::const_iterator
m_iter;
1574 JsonObject::const_iterator const_iter(other.
get_iter());
1653 for (
const auto& item : vec) {
1675 if (
this != &other) {
1682 if (
this != &other) {
1724 for (
const auto& item : vec) {
1752 template<
typename IntType>
1768 template<
typename FloatType>
1786 template<
typename T>
1796 template<
typename T>
1803 template<
typename T>
1811 template<
typename T>
1818 template<
typename T>
1825 template<
typename T>
1833 template<
typename T>
1840 template<
typename T>
1847 template<
typename T>
1854 template<
typename T>
1860 template<
typename T>
1866 template<
typename T>
1873 template<
typename T>
1879 template<
typename T>
1885 template<
typename T>
1891 template<
typename T>
1897 template<
typename T>
1903 template<
typename T>
1909 template<
typename T>
1915 template<
typename T>
1921 template<
typename T>
1948 template<
typename T>
1949 typename JsonValue::template array_iterator<T>
begin_array() {
1950 if (!
m_value)
return typename JsonValue::template array_iterator<T>();
1954 template<
typename T>
1955 typename JsonValue::template array_iterator<T>
end_array() {
1956 if (!
m_value)
return typename JsonValue::template array_iterator<T>();
1960 template<
typename T>
1961 typename JsonValue::template array_iterator<T>
begin_array()
const {
1962 if (!
m_value)
return typename JsonValue::template array_iterator<T>();
1966 template<
typename T>
1967 typename JsonValue::template array_iterator<T>
end_array()
const {
1968 if (!
m_value)
return typename JsonValue::template array_iterator<T>();
1982 for (
auto it =
begin(); it !=
end(); ++it) {
1983 auto keyValue = *it;
1984 result.push_back(keyValue.first);
2003 auto arr =
m_value->as_array();
2010 auto arr =
m_value->as_array();
2013 if (idx >= arr->size()) {
2014 for (
size_t i = arr->size(); i <= idx; i++) {
2018 return Json((*arr)[idx]);
2021 return Json(
nullptr);
2026 return Json(
nullptr);
2030 auto arr =
m_value->as_array();
2031 if (arr && idx < arr->
size()) {
2032 return Json((*arr)[idx]);
2041 auto arr =
m_value->as_array();
2042 if (arr && idx < arr->
size()) {
2043 return Json((*arr)[idx]);
2046 return Json(
nullptr);
2057 if (objPtr->find(key) == objPtr->end()) {
2061 return Json((*objPtr)[key]);
2065 return Json(
nullptr);
2070 return Json(
nullptr);
2073 if (obj && obj->find(key) != obj->end()) {
2074 return Json((*obj)[key]);
2076 return Json(
nullptr);
2088 size_t size()
const {
2093 template<
typename T>
2095 if (!
m_value)
return fallback;
2102 template<
typename T>
2103 T
as_or(
const T& fallback)
const {
2135 return Json(
nullptr);
2159 (*objPtr)[key] =
value.m_value;
2170 template<typename T, typename = fl::enable_if_t<fl::is_same<T, char>::value>>
2184 auto arr =
m_value->as_array();
2193 ptr->push_back(
value.m_value);
UIAudio audio("Audio Input")
void push_back(const T &value)
fl::enable_if< fl::is_integral< T >::value &&!fl::is_same< T, bool >::value, fl::optional< T > >::type as_impl() const
fl::enable_if< fl::is_integral< T >::value &&!fl::is_same< T, bool >::value, T >::type get_default_value() const
static Json createObject()
fl::optional< JsonObject > as_object() const
fl::string to_string() const
void set(const fl::string &key, double value)
void set_value(const fl::shared_ptr< JsonValue > &value)
fl::optional< fl::string > as_string() const
static Json from_value(const JsonValue &value)
fl::optional< int64_t > as_int() const
fl::optional< fl::vector< uint8_t > > as_bytes() const
JsonValue::iterator begin()
JsonValue::template array_iterator< T > begin_array()
T operator|(const T &fallback) const
fl::optional< fl::vector< float > > as_floats() const
friend JsonValue::const_iterator begin(const Json &j)
bool is_generic_array() const
fl::shared_ptr< JsonValue > m_value
static Json from_char(char c)
static fl::string normalizeJsonString(const char *jsonStr)
Json(const fl::shared_ptr< JsonValue > &value)
Json(const fl::string &s)
Json & operator=(const Json &other)
JsonValue::template array_iterator< T > end_array()
void set(const fl::string &key, int64_t value)
bool contains(size_t idx) const
void set(const fl::string &key, const Json &value)
void push_back(const Json &value)
fl::string to_string_native_public() const
fl::optional< T > try_as() const
fl::optional< float > as_float() const
fl::vector< fl::string > keys() const
fl::optional< bool > as_bool() const
fl::optional< T > as() const
void set(const fl::string &key, const fl::string &value)
static Json createArray()
void set(const fl::string &key, T value)
T as_or(const T &fallback) const
void set(const fl::string &key, float value)
fl::optional< fl::vector< int16_t > > as_audio() const
fl::optional< JsonArray > as_array() const
fl::string to_string_native() const
static Json parse(const fl::string &txt)
fl::optional< double > as_double() const
JsonValue::iterator end()
void set(const fl::string &key, const char *value)
Json operator[](size_t idx)
fl::string serialize() const
friend JsonValue::const_iterator end(const Json &j)
fl::vector< fl::string > getObjectKeys() const
bool operator!=(const array_iterator &other) const
bool operator==(const array_iterator &other) const
ParseResult< T > operator*() const
ParseResult< T > get_value() const
array_iterator & operator++()
typename JsonValue::variant_t variant_t
KeyValue operator*() const
bool operator!=(const const_iterator &other) const
const_iterator & operator++()
JsonObject::const_iterator m_iter
bool operator==(const const_iterator &other) const
static const_iterator from_iterator(JsonObject::const_iterator iter)
static const_iterator from_object_iterator(const iterator &other)
KeyValue operator*() const
bool operator!=(const iterator &other) const
JsonObject::iterator m_iter
bool operator==(const iterator &other) const
JsonObject::iterator get_iter() const
const char * c_str() const
void visit(Visitor &visitor)
bool empty() const noexcept
Result type for promise operations.
constexpr remove_reference< T >::type && move(T &&t) noexcept
JsonObject & get_empty_json_object()
string to_string(T value)
decltype(nullptr) nullptr_t
shared_ptr< T > make_shared(Args &&... args)
fl::HashMap< fl::string, fl::shared_ptr< JsonValue > > JsonObject
constexpr nullopt_t nullopt
fl::vector< fl::shared_ptr< JsonValue > > JsonArray
HeapVector< T, Allocator > vector
FL_DISABLE_WARNING_POP JsonValue & get_null_value()
constexpr T && forward(typename remove_reference< T >::type &t) noexcept
Promise-based fluent API for FastLED - standalone async primitives.
void accept(const U &value)
DefaultValueVisitor(const T &fb)
void operator()(const T &value)
fl::optional< double > result
fl::optional< FloatType > result
void accept(const U &value)
void operator()(const FloatType &value)
fl::optional< int64_t > result
void accept(const U &value)
fl::optional< IntType > result
fl::enable_if<!fl::is_same< T, int64_t >::value &&!fl::is_same< T, double >::value, void >::type operator()(const IntType &value)
void operator()(const JsonArray &)
void accept(const T &value)
KeyValue(const fl::string &key, const fl::shared_ptr< JsonValue > &value_ptr)
KeyValue(const fl::string &key, const fl::shared_ptr< JsonValue > &value_ptr)
fl::optional< double > as_double() const
JsonValue(int64_t i) noexcept
fl::optional< T > get() const
bool contains(size_t idx) const
bool is_audio() const noexcept
bool is_object() const noexcept
bool is_float() const noexcept
JsonValue & operator=(const JsonValue &other)
bool is_bool() const noexcept
T operator|(const T &fallback) const
bool is_double() const noexcept
bool is_int() const noexcept
friend const_iterator begin(const JsonValue &v)
bool is_floats() const noexcept
auto visit(Visitor &&visitor) -> decltype(visitor(fl::nullptr_t{}))
bool is_string() const noexcept
static fl::shared_ptr< JsonValue > from_char(char c)
JsonValue & operator[](size_t idx)
bool is_generic_array() const noexcept
bool is_array() const noexcept
friend class SerializerVisitor
fl::optional< JsonObject > as_object()
JsonValue(bool b) noexcept
JsonValue::const_iterator const_iterator
fl::optional< float > as_float()
fl::vector< fl::string > getObjectKeys() const
array_iterator< T > begin_array()
fl::optional< fl::vector< float > > as_floats()
static fl::shared_ptr< JsonValue > parse(const fl::string &txt)
JsonValue::iterator iterator
fl::vector< fl::string > keys() const
bool is_null() const noexcept
friend const_iterator end(const JsonValue &v)
fl::optional< fl::vector< int16_t > > as_audio()
fl::optional< JsonArray > as_array()
fl::optional< fl::vector< uint8_t > > as_bytes()
fl::optional< fl::string > as_string()
fl::string to_string() const
fl::Variant< fl::nullptr_t, bool, int64_t, float, fl::string, JsonArray, JsonObject, fl::vector< int16_t >, fl::vector< uint8_t >, fl::vector< float > > variant_t
JsonValue(float f) noexcept
fl::optional< bool > as_bool()
JsonValue(const JsonArray &a)
T as_or(const T &fallback) const
bool is_bytes() const noexcept
JsonValue(const fl::string &s)
array_iterator< T > end_array()
fl::optional< int64_t > as_int()
const Error & get_error() const
ParseResult(const T &val)
const T & get_value() const
void operator()(const fl::string &value)
void accept(const U &value)
fl::optional< fl::string > result
static constexpr bool value
static constexpr bool value
static constexpr bool value
static constexpr bool value