2020-09-24 07:51:43 +01:00
|
|
|
/*
|
|
|
|
JsonGenerator.h - lightweight JSON generator
|
|
|
|
|
2021-01-01 12:57:04 +00:00
|
|
|
Copyright (C) 2021 Stephan Hadinger
|
2020-09-24 07:51:43 +01:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __JSON_GENERATOR__
|
|
|
|
#define __JSON_GENERATOR__
|
|
|
|
|
|
|
|
#include <Arduino.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
extern String EscapeJSONString(const char *str);
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* JSON Generator for Arrays
|
|
|
|
\*********************************************************************************************/
|
|
|
|
class JsonGeneratorArray {
|
|
|
|
public:
|
|
|
|
|
2021-01-18 20:48:04 +00:00
|
|
|
JsonGeneratorArray(): val(F("[]")) {} // start with empty array
|
2020-09-24 07:51:43 +01:00
|
|
|
|
|
|
|
void add(uint32_t uval32);
|
|
|
|
void add(int32_t uval32);
|
|
|
|
void addStrRaw(const char * sval);
|
|
|
|
void addStr(const char * sval);
|
|
|
|
|
|
|
|
inline String &toString(void) { return val; }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void pre(void);
|
|
|
|
void post(void) { val += ']'; }
|
|
|
|
String val;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* JSON Generator for Objects
|
|
|
|
\*********************************************************************************************/
|
|
|
|
class JsonGeneratorObject {
|
|
|
|
public:
|
|
|
|
|
2021-01-18 20:48:04 +00:00
|
|
|
JsonGeneratorObject(): val(F("{}")) {} // start with empty object
|
2020-09-24 07:51:43 +01:00
|
|
|
|
|
|
|
void add(const char* key, uint32_t uval32);
|
|
|
|
void add(const char* key, int32_t uval32);
|
|
|
|
void add(const char* key, const String & str);
|
2020-10-07 19:04:33 +01:00
|
|
|
void addHex32(const char* key, uint32_t uval32);
|
2020-09-24 07:51:43 +01:00
|
|
|
void addStrRaw(const char* key, const char * sval);
|
|
|
|
void addStr(const char* key, const char * sval);
|
|
|
|
|
|
|
|
inline String &toString(void) { return val; }
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void pre(const char * key);
|
|
|
|
void post(void) { val += '}'; }
|
|
|
|
String val;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // __JSON_PARSER__
|