REDAC HybridController
Firmware for LUCIDAC/REDAC Teensy
Loading...
Searching...
No Matches
streaming_json.h
Go to the documentation of this file.
1// Copyright (c) 2024 anabrid GmbH
2// Contact: https://www.anabrid.com/licensing/
3// SPDX-License-Identifier: MIT OR GPL-2.0-or-later
4
5#pragma once
6
7#include <Arduino.h>
8
9namespace utils {
10
11
45 bool _needs_comma = false;
46
47 public:
48 Print& output;
50
51 void check_comma() {
52 if(_needs_comma) output.print(',');
53 _needs_comma = false;
54 }
55 void needs_comma() { _needs_comma = true; }
56
57 void begin_dict() { output.print('{'); _needs_comma = false; }
58 void end_dict() { output.print('}'); needs_comma(); }
59 void begin_list() { output.print('['); _needs_comma = false; }
60 void end_list() { output.print(']'); needs_comma(); }
61
62 void begin_str(char quote='"') { output.print(quote); }
63 void end_str(char quote='"') { output.print(quote); }
64
65 void key(const char* str, char quote='"') {
67 begin_str(quote);
68 output.print(str);
69 end_str(quote);
70 output.print(':');
71 }
72
73 void key(const std::string& str) { key(str.c_str()); }
74
75 // value types
76
77 void val(const char* str, char quote='"') {
79 begin_str(quote);
80 output.print(str);
81 end_str(quote);
83 }
84
85 void val(const std::string& str) { val(str.c_str()); }
86
87 void val(bool b) {
89 output.print(b ? "true" : "false");
91 }
92
93 // should ensure things do not behave as strings...
94 template<typename V>
95 void val(V i) {
97 output.print(i);
99 }
100
101 void null() {
102 check_comma();
103 output.print("null");
104 needs_comma();
105 }
106
108 void json(const char* str) {
109 check_comma();
110 output.print(str);
111 needs_comma();
112 }
113
114 // Shorthands
115
116 template<typename V> void kv(const char* _key, V _val) { key(_key); val(_val); }
117
118 void endln() {
119 output.println();
120 output.flush();
121 }
122
123 // scope handlers
124 };
125
126}
The Streaming JSON API provides a way of constructing (writing) JSON messages without RAM overhead.
void end_str(char quote='"')
void json(const char *str)
An embedded Raw json structure.
void kv(const char *_key, V _val)
StreamingJson(Print &output)
void val(const char *str, char quote='"')
void val(const std::string &str)
void key(const char *str, char quote='"')
void begin_str(char quote='"')
void key(const std::string &str)