Uniot Core
0.8.1
Loading...
Searching...
No Matches
MicroJSON.h
Go to the documentation of this file.
1/*
2 * This is a part of the Uniot project.
3 * Copyright (C) 2016-2025 Uniot <contact@uniot.io>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#pragma once
20
21#include <Arduino.h>
22
30
60
61namespace uniot {
62namespace JSON {
63
64class Array;
65
73class Object {
74 public:
79 Object(String &out) : mOut(out), mFirst(true) {
80 mOut += "{";
81 }
82
90 inline Object &put(const String &key, const String &value, bool quote = true) {
91 _begin(key);
92 mOut += (quote ? "\"" + value + "\"" : value);
93 return *this;
94 }
95
102 inline Object &put(const String &key, int value) {
103 return put(key, String(value), false);
104 }
105
111 inline Array putArray(const String &key);
112
118 inline Object putObject(const String &key);
119
123 inline void close() {
124 mOut += "}";
125 }
126
127 private:
128 String &mOut;
129 bool mFirst;
130
135 inline void _begin(const String &key) {
136 if (!mFirst) {
137 mOut += ",";
138 }
139 mOut += "\"" + key + "\":";
140 mFirst = false;
141 }
142};
143
151class Array {
152 public:
157 Array(String &out) : mOut(out), mFirst(true) {
158 mOut += "[";
159 }
160
167 inline Array &append(const String &value, bool quote = true) {
168 _begin();
169 mOut += (quote ? "\"" + value + "\"" : value);
170 return *this;
171 }
172
178 inline Array &append(int value) {
179 return append(String(value), false);
180 }
181
186 inline Array appendArray();
187
193
197 inline void close() {
198 mOut += "]";
199 }
200
201 private:
202 String &mOut;
203 bool mFirst;
204
208 inline void _begin() {
209 if (!mFirst) mOut += ",";
210 mFirst = false;
211 }
212};
213
214inline Array Object::putArray(const String &key) {
215 _begin(key);
216 return Array(mOut);
217}
218
219inline Object Object::putObject(const String &key) {
220 _begin(key);
221 return Object(mOut);
222}
223
225 _begin();
226 return Array(mOut);
227}
228
230 _begin();
231 return Object(mOut);
232}
233
234} // namespace JSON
235} // namespace uniot
236
JSON array builder for creating ordered lists of values.
Definition MicroJSON.h:151
void close()
Close the JSON array by appending the closing bracket.
Definition MicroJSON.h:197
Array & append(const String &value, bool quote=true)
Append a string value to the array.
Definition MicroJSON.h:167
Array & append(int value)
Append an integer value to the array.
Definition MicroJSON.h:178
Array appendArray()
Create a nested array as an element.
Definition MicroJSON.h:224
Object appendObject()
Create a nested object as an element.
Definition MicroJSON.h:229
Array(String &out)
Construct a new JSON Array.
Definition MicroJSON.h:157
JSON object builder for creating key-value pairs.
Definition MicroJSON.h:73
Object & put(const String &key, const String &value, bool quote=true)
Add a string key-value pair to the object.
Definition MicroJSON.h:90
void close()
Close the JSON object by appending the closing brace.
Definition MicroJSON.h:123
Object & put(const String &key, int value)
Add an integer key-value pair to the object.
Definition MicroJSON.h:102
Object putObject(const String &key)
Create a nested object for the given key.
Definition MicroJSON.h:219
Object(String &out)
Construct a new JSON Object.
Definition MicroJSON.h:79
Array putArray(const String &key)
Create a nested array for the given key.
Definition MicroJSON.h:214
struct Obj * Object
A pointer to a Lisp object structure.
Definition LispHelper.h:61
Definition MicroJSON.h:62
Contains all classes and functions related to the Uniot Core.