FastLED 3.10.8
Loading...
Searching...
No Matches
type_to_json.h
Go to the documentation of this file.
1#pragma once
2
3#include "fl/stl/json.h"
4#include "fl/stl/noexcept.h"
5#include "fl/stl/string.h"
6#include "fl/stl/vector.h"
8namespace fl {
9namespace detail {
10
11// =============================================================================
12// Return type to JSON converter
13// =============================================================================
14
15template <typename T>
16struct TypeToJson {
17 static json convert(const T& value) FL_NO_EXCEPT {
18 return json(value);
19 }
20};
21
22template <>
25 return json(value.c_str());
26 }
27};
28
29// Explicit unsigned-int specialization. Without this, `json(u32_value)` in
30// the primary template above is ambiguous between `json(int)` and
31// `json(i64)` for a `u32` argument — GCC 15 reports:
32// error: call of overloaded 'json(const long unsigned int&)' is ambiguous
33// Route u32 → i64 explicitly so the RPC framework can carry unsigned 32-bit
34// return values / arg types (e.g. `fl::u32 rgb` in the LPC PWM+DMA
35// AutoResearch RPCs from #3517).
36template <>
37struct TypeToJson<fl::u32> {
38 static json convert(const fl::u32& value) FL_NO_EXCEPT {
39 return json(static_cast<fl::i64>(value));
40 }
41};
42
43// Sibling specialization for u16 which would hit the same ambiguity on
44// GCC 15 despite implicit promotion — safer to be explicit.
45template <>
46struct TypeToJson<fl::u16> {
47 static json convert(const fl::u16& value) FL_NO_EXCEPT {
48 return json(static_cast<fl::i64>(value));
49 }
50};
51
52// json identity conversion - pass json through unchanged
53template <>
54struct TypeToJson<fl::json> {
56 return value;
57 }
58};
59
60// fl::vector<fl::u8> specialization - encodes binary data as base64 string.
61template <>
62struct TypeToJson<fl::vector<fl::u8>> {
65 return json(encoded.c_str());
66 }
67};
68
69// fl::vector<T> conversion - converts vector to JSON array
70template <typename T>
71struct TypeToJson<fl::vector<T>> {
73 json arr = json::array();
74 for (fl::size i = 0; i < value.size(); i++) {
76 }
77 return arr;
78 }
79};
80
81template <>
82struct TypeToJson<void> {
84 return json(nullptr);
85 }
86};
87
88} // namespace detail
89} // namespace fl
const char * c_str() const FL_NO_EXCEPT
void push_back(const json &value) FL_NO_EXCEPT
Definition json.h:918
static json array() FL_NO_EXCEPT
Definition json.h:853
FastLED's Elegant JSON Library: fl::json
Compile-time linker keep-alive hook for a single fl::Bus.
Definition bus_info.h:57
u8 u8 fract8 blur_amount FL_NO_EXCEPT
Definition blur.h:186
constexpr int type_rank< T >::value
fl::string base64_encode(fl::span< const fl::u8 > data) FL_NO_EXCEPT
Base definition for an LED controller.
Definition crgb.hpp:179
static json convert(const json &value) FL_NO_EXCEPT
static json convert(const fl::string &value) FL_NO_EXCEPT
static json convert(const fl::u16 &value) FL_NO_EXCEPT
static json convert(const fl::u32 &value) FL_NO_EXCEPT
static json convert(const fl::vector< T > &value) FL_NO_EXCEPT
static json convert(const fl::vector< fl::u8 > &value) FL_NO_EXCEPT
static json convert() FL_NO_EXCEPT
static json convert(const T &value) FL_NO_EXCEPT