FastLED 3.9.15
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/string.h"
5#include "fl/stl/vector.h"
7namespace fl {
8namespace detail {
9
10// =============================================================================
11// Return type to JSON converter
12// =============================================================================
13
14template <typename T>
15struct TypeToJson {
16 static json convert(const T& value) {
17 return json(value);
18 }
19};
20
21template <>
23 static json convert(const fl::string& value) {
24 return json(value.c_str());
25 }
26};
27
28// json identity conversion - pass json through unchanged
29template <>
30struct TypeToJson<fl::json> {
31 static json convert(const json& value) {
32 return value;
33 }
34};
35
36// fl::vector<fl::u8> specialization - encodes binary data as base64 string.
37template <>
38struct TypeToJson<fl::vector<fl::u8>> {
41 return json(encoded.c_str());
42 }
43};
44
45// fl::vector<T> conversion - converts vector to JSON array
46template <typename T>
47struct TypeToJson<fl::vector<T>> {
49 json arr = json::array();
50 for (fl::size i = 0; i < value.size(); i++) {
52 }
53 return arr;
54 }
55};
56
57template <>
58struct TypeToJson<void> {
59 static json convert() {
60 return json(nullptr);
61 }
62};
63
64} // namespace detail
65} // namespace fl
const char * c_str() const FL_NOEXCEPT
void push_back(const json &value) FL_NOEXCEPT
Definition json.h:745
static json array() FL_NOEXCEPT
Definition json.h:688
FastLED's Elegant JSON Library: fl::json
Compile-time linker keep-alive hook for a single fl::Bus.
Definition bus_traits.h:48
constexpr int type_rank< T >::value
fl::string base64_encode(fl::span< const fl::u8 > data)
Base definition for an LED controller.
Definition crgb.hpp:179
static json convert(const json &value)
static json convert(const fl::string &value)
static json convert(const fl::vector< T > &value)
static json convert(const fl::vector< fl::u8 > &value)
static json convert(const T &value)