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

◆ json_convert_to_float_core()

FL_NO_INLINE TypeConversionResult fl::detail::json_convert_to_float_core ( const json_value & val,
float * out_value )

Definition at line 116 of file json_dispatch.cpp.hpp.

117 {
119 if (auto* p = val.data.template ptr<float>()) {
120 *out_value = *p;
121 return result;
122 }
123 if (auto* p = val.data.template ptr<i64>()) {
124#if FL_PLATFORM_HAS_LARGE_MEMORY
125 *out_value = static_cast<float>(*p);
126 if (sizeof(float) < 8 && (*p > 16777216 || *p < -16777216)) {
127 result.addWarning(fl::string("large int ") + fl::to_string(*p) +
128 " may lose precision as float");
129 }
130#else
131 // Avoid __aeabi_l2f via i32 route (same trick as #3076).
132 if (*p > 2147483647LL) {
133 *out_value = static_cast<float>(2147483647);
134 } else if (*p < -2147483648LL) {
135 *out_value = static_cast<float>(-2147483648);
136 } else {
137 *out_value = static_cast<float>(static_cast<fl::i32>(*p));
138 }
139#endif
140 return result;
141 }
142 if (auto* p = val.data.template ptr<bool>()) {
143 *out_value = *p ? 1.0f : 0.0f;
144 result.addWarning(fl::string("bool converted to float ") +
145 (*p ? "1.0" : "0.0"));
146 return result;
147 }
148 if (auto* p = val.data.template ptr<fl::string>()) {
149#if FL_PLATFORM_HAS_LARGE_MEMORY
150 char* end = nullptr;
151 double parsed = fl::strtod(p->c_str(), &end);
152 if (end != p->c_str() && *end == '\0') {
153 *out_value = static_cast<float>(parsed);
154 result.addWarning(fl::string("string '") + *p + "' parsed to float");
155 } else {
156 result.setError(fl::string("cannot parse string '") + *p + "' as float");
157 *out_value = 0.0f;
158 }
159#else
160 (void)p;
161 *out_value = 0.0f;
162 result.setError("string -> float not supported on Low-memory");
163#endif
164 return result;
165 }
166 if (val.data.template ptr<fl::nullptr_t>()) {
167 result.setError("cannot convert null to float");
168 *out_value = 0.0f;
169 return result;
170 }
171 result.setError("cannot convert array or object to float");
172 *out_value = 0.0f;
173 return result;
174}
expected< T, E > result
Alias for expected (Rust-style naming)
Definition result.h:31
string to_string(T value) FL_NO_EXCEPT
Definition string.h:459
double strtod(const char *str, char **endptr)
variant_t data
Definition types.h:760

References fl::end(), fl::FL_NO_EXCEPT, fl::strtod(), and fl::to_string().

Referenced by fl::detail::JsonToType< T, typename fl::enable_if< fl::is_floating_point< T >::value >::type >::convert().

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