forked from bblanchon/ArduinoJson
Compare commits
48 Commits
Author | SHA1 | Date | |
---|---|---|---|
7e7074502f | |||
aa306d5573 | |||
55669e306e | |||
3f96e070ce | |||
8032a4b564 | |||
b923e8f4df | |||
c5388cf8e7 | |||
cb908a2373 | |||
cecbcd1929 | |||
90e0cc6091 | |||
45bbf6db86 | |||
432476c98a | |||
1f3e227a8b | |||
66c28020c5 | |||
6cfe2a58eb | |||
fc6ad51e68 | |||
d3bc52951a | |||
2f7232859e | |||
4a7232ac99 | |||
72d78432c9 | |||
f6cd42d916 | |||
542dff2a08 | |||
e75e843c88 | |||
146a76247c | |||
f28157cab7 | |||
1ce16ce449 | |||
c310e7e8b7 | |||
aa2ef79e55 | |||
7ad57f1c33 | |||
cbfd331e50 | |||
e6f55b1f6f | |||
bb805e93cb | |||
deb57b960b | |||
8a9b918bf4 | |||
2f6f3d0629 | |||
a60b35f41c | |||
6757f35a3a | |||
ffb9b6d1ba | |||
e401498e4a | |||
d30e940b3b | |||
05ea5e04c8 | |||
a7ef99d0fe | |||
f2a8b52c2c | |||
409ca7ee4e | |||
387b565705 | |||
96f486001d | |||
a498abc14a | |||
c64340a9bb |
5
.mbedignore
Normal file
5
.mbedignore
Normal file
@ -0,0 +1,5 @@
|
||||
.github/
|
||||
examples/
|
||||
scripts/
|
||||
test/
|
||||
third-party/
|
22
.travis.yml
22
.travis.yml
@ -70,20 +70,14 @@ matrix:
|
||||
sources: ['ubuntu-toolchain-r-test','llvm-toolchain-precise-3.8']
|
||||
packages: ['clang-3.8']
|
||||
env: SCRIPT=cmake CMAKE_CXX_COMPILER=clang++-3.8
|
||||
- compiler: clang
|
||||
addons:
|
||||
apt:
|
||||
sources: ['ubuntu-toolchain-r-test','llvm-toolchain-precise-3.8']
|
||||
packages: ['clang-3.8']
|
||||
env: SCRIPT=sanitize CMAKE_CXX_COMPILER=clang++-3.8
|
||||
- compiler: gcc
|
||||
env: SCRIPT=coverage
|
||||
- os: osx
|
||||
osx_image: beta-xcode6.1
|
||||
compiler: clang
|
||||
env: SCRIPT=cmake-osx
|
||||
- os: osx
|
||||
osx_image: beta-xcode6.2
|
||||
compiler: clang
|
||||
env: SCRIPT=cmake-osx
|
||||
- os: osx
|
||||
osx_image: beta-xcode6.3
|
||||
compiler: clang
|
||||
env: SCRIPT=cmake-osx
|
||||
- os: osx
|
||||
osx_image: xcode6.4
|
||||
compiler: clang
|
||||
@ -104,10 +98,8 @@ matrix:
|
||||
osx_image: xcode7.3
|
||||
compiler: clang
|
||||
env: SCRIPT=cmake-osx
|
||||
- env: SCRIPT=arduino VERSION=1.5.8 BOARD=arduino:avr:uno
|
||||
- env: SCRIPT=arduino VERSION=1.6.7 BOARD=arduino:avr:uno
|
||||
- env: SCRIPT=arduino VERSION=1.6.8 BOARD=arduino:avr:uno
|
||||
- env: SCRIPT=arduino VERSION=1.6.9 BOARD=arduino:avr:uno
|
||||
- env: SCRIPT=arduino VERSION=1.6.12 BOARD=arduino:avr:uno
|
||||
- env: SCRIPT=platformio BOARD=uno
|
||||
- env: SCRIPT=platformio BOARD=due
|
||||
- env: SCRIPT=platformio BOARD=esp01
|
||||
|
93
CHANGELOG.md
93
CHANGELOG.md
@ -1,6 +1,91 @@
|
||||
ArduinoJson: change log
|
||||
=======================
|
||||
|
||||
v5.8.0
|
||||
------
|
||||
|
||||
* Added operator `==` to compare `JsonVariant` and strings (issue #402)
|
||||
* Added support for `Stream` (issue #300)
|
||||
* Reduced memory consumption by not duplicating spaces and comments
|
||||
|
||||
v5.7.3
|
||||
------
|
||||
|
||||
* Added an `printTo(char[N])` and `prettyPrintTo(char[N])` (issue #292)
|
||||
* Added ability to set a nested value like this: `root["A"]["B"] = "C"` (issue #352)
|
||||
* Renamed `*.ipp` to `*Impl.hpp` because they were ignored by Arduino IDE (issue #396)
|
||||
|
||||
v5.7.2
|
||||
------
|
||||
|
||||
* Made PROGMEM available on more platforms (issue #381)
|
||||
* Fixed PROGMEM causing an exception on ESP8266 (issue #383)
|
||||
|
||||
v5.7.1
|
||||
------
|
||||
|
||||
* Added support for PROGMEM (issue #76)
|
||||
* Fixed compilation error when index is not an `int` (issue #381)
|
||||
|
||||
v5.7.0
|
||||
------
|
||||
|
||||
* Templatized all functions using `String` or `std::string`
|
||||
* Removed `ArduinoJson::String`
|
||||
* Removed `JsonVariant::defaultValue<T>()`
|
||||
* Removed non-template `JsonObject::get()` and `JsonArray.get()`
|
||||
* Fixed support for `StringSumHelper` (issue #184)
|
||||
* Replaced `ARDUINOJSON_USE_ARDUINO_STRING` by `ARDUINOJSON_ENABLE_STD_STRING` and `ARDUINOJSON_ENABLE_ARDUINO_STRING` (issue #378)
|
||||
* Added example `StringExample.ino` to show where `String` can be used
|
||||
* Increased default nesting limit to 50 when compiled for a computer (issue #349)
|
||||
|
||||
**BREAKING CHANGES**:
|
||||
|
||||
The non-template functions `JsonObject::get()` and `JsonArray.get()` have been removed. This means that you need to explicitely tell the type you expect in return.
|
||||
|
||||
Old code:
|
||||
|
||||
```c++
|
||||
#define ARDUINOJSON_USE_ARDUINO_STRING 0
|
||||
JsonVariant value1 = myObject.get("myKey");
|
||||
JsonVariant value2 = myArray.get(0);
|
||||
```
|
||||
|
||||
New code:
|
||||
|
||||
```c++
|
||||
#define ARDUINOJSON_ENABLE_ARDUINO_STRING 0
|
||||
#define ARDUINOJSON_ENABLE_STD_STRING 1
|
||||
JsonVariant value1 = myObject.get<JsonVariant>("myKey");
|
||||
JsonVariant value2 = myArray.get<JsonVariant>(0);
|
||||
```
|
||||
|
||||
|
||||
v5.6.7
|
||||
------
|
||||
|
||||
* Fixed `array[idx].as<JsonVariant>()` and `object[key].as<JsonVariant>()`
|
||||
* Fixed return value of `JsonObject::set()` (issue #350)
|
||||
* Fixed undefined behavior in `Prettyfier` and `Print` (issue #354)
|
||||
* Fixed parser that incorrectly rejected floats containing a `+` (issue #349)
|
||||
|
||||
v5.6.6
|
||||
------
|
||||
|
||||
* Fixed `-Wparentheses` warning introduced in v5.6.5 (PR #335 by @nuket)
|
||||
* Added `.mbedignore` for ARM mbdeb (PR #334 by @nuket)
|
||||
* Fixed `JsonVariant::success()` which didn't propagate `JsonArray::success()` nor `JsonObject::success()` (issue #342).
|
||||
|
||||
v5.6.5
|
||||
------
|
||||
|
||||
* `as<char*>()` now returns `true` when input is `null` (issue #330)
|
||||
|
||||
v5.6.4
|
||||
------
|
||||
|
||||
* Fixed error in float serialization (issue #324)
|
||||
|
||||
v5.6.3
|
||||
------
|
||||
|
||||
@ -112,7 +197,7 @@ v5.0.3
|
||||
v5.0.2
|
||||
------
|
||||
|
||||
* Fixed segmentation fault in `parseObject(String)` and `parseArray(String)`, when the
|
||||
* Fixed segmentation fault in `parseObject(String)` and `parseArray(String)`, when the
|
||||
`StaticJsonBuffer` is too small to hold a copy of the string
|
||||
* Fixed Clang warning "register specifier is deprecated" (issue #102)
|
||||
* Fixed GCC warning "declaration shadows a member" (issue #103)
|
||||
@ -228,14 +313,14 @@ v3.1
|
||||
|
||||
Old generator API:
|
||||
|
||||
JsonObject<3> root;
|
||||
JsonObject<3> root;
|
||||
root.add("sensor", "gps");
|
||||
root.add("time", 1351824120);
|
||||
root.add("data", array);
|
||||
|
||||
New generator API:
|
||||
|
||||
JsonObject<3> root;
|
||||
JsonObject<3> root;
|
||||
root["sensor"] = "gps";
|
||||
root["time"] = 1351824120;
|
||||
root["data"] = array;
|
||||
@ -292,7 +377,7 @@ v1.1
|
||||
* Example: changed `char* json` into `char[] json` so that the bytes are not write protected
|
||||
* Fixed parsing bug when the JSON contains multi-dimensional arrays
|
||||
|
||||
v1.0
|
||||
v1.0
|
||||
----
|
||||
|
||||
Initial release
|
||||
|
@ -18,4 +18,8 @@ if(${COVERAGE})
|
||||
set(CMAKE_CXX_FLAGS "-g -O0 -fprofile-arcs -ftest-coverage")
|
||||
endif()
|
||||
|
||||
if(${SANITIZE})
|
||||
set(CMAKE_CXX_FLAGS "-fsanitize=address,undefined")
|
||||
endif()
|
||||
|
||||
add_subdirectory(test)
|
||||
|
17
README.md
17
README.md
@ -5,9 +5,10 @@ Arduino JSON library
|
||||
|
||||
*An elegant and efficient JSON library for embedded systems.*
|
||||
|
||||
It's designed to have the most intuitive API, the smallest footprint and works without any allocation on the heap (no malloc).
|
||||
It's designed to have the most intuitive API, the smallest footprint and is able to work without any allocation on the heap (no malloc).
|
||||
|
||||
It has been written with Arduino in mind, but it isn't linked to Arduino libraries so you can use this library in any other C++ project.
|
||||
For instance, it supports Aduino's `String` and `Stream`, but also `std::string`, `std::istream` and `std::ostream`.
|
||||
|
||||
Features
|
||||
--------
|
||||
@ -55,6 +56,8 @@ double latitude = root["data"][0];
|
||||
double longitude = root["data"][1];
|
||||
```
|
||||
|
||||
[See JsonParserExample.ino](examples/JsonParserExample/JsonParserExample.ino)
|
||||
|
||||
#### Encoding / Generating
|
||||
|
||||
```c++
|
||||
@ -73,6 +76,8 @@ root.printTo(Serial);
|
||||
// {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]}
|
||||
```
|
||||
|
||||
[See JsonGeneratorExample.ino](examples/JsonGeneratorExample/JsonGeneratorExample.ino)
|
||||
|
||||
|
||||
Documentation
|
||||
-------------
|
||||
@ -86,9 +91,6 @@ From Arduino's Forum user `jflaplante`:
|
||||
> I tried aJson json-arduino before trying your library. I always ran into memory problem after a while.
|
||||
> I have no such problem so far with your library. It is working perfectly with my web services.
|
||||
|
||||
From Arduino's Forum user `gbathree`:
|
||||
> Thanks so much - this is an awesome library! If you want to see what we're doing with it - the project is located at www.photosynq.org.
|
||||
|
||||
From StackOverflow user `thegreendroid`:
|
||||
> It has a really elegant, simple API and it works like a charm on embedded and Windows/Linux platforms. We recently started using this on an embedded project and I can vouch for its quality.
|
||||
|
||||
@ -106,6 +108,9 @@ From GitHub user `zacsketches`:
|
||||
[From Twitter user `@hemalchevli`](https://twitter.com/hemalchevli/status/715788439397011456):
|
||||
> ArduinoJson library should be used as a benchmark/reference for making libraries. Truly elegant.
|
||||
|
||||
[From GitHub user `sticilface`](https://github.com/bblanchon/ArduinoJson/issues/381#issuecomment-260203594):
|
||||
> its a great lib:) and i use it in everything!
|
||||
|
||||
Donators
|
||||
--------
|
||||
|
||||
@ -124,6 +129,10 @@ Special thanks to the following persons and companies who made generous donation
|
||||
* Nick Koumaris <img alt='Greece' src='https://cdnjs.cloudflare.com/ajax/libs/emojione/2.1.4/assets/svg/1f1ec-1f1f7.svg' width='18' height='18'>
|
||||
* Jon Williams <img alt='USA' src='https://cdnjs.cloudflare.com/ajax/libs/emojione/2.1.4/assets/svg/1f1fa-1f1f8.svg' width='18' height='18'>
|
||||
* Kestutis Liaugminas <img alt='Lithuania' src='https://cdnjs.cloudflare.com/ajax/libs/emojione/2.1.4/assets/svg/1f1f1-1f1f9.svg' width='18' height='18'>
|
||||
* Darlington Adibe <img alt='Nigeria' src='https://cdnjs.cloudflare.com/ajax/libs/emojione/2.1.4/assets/svg/1f1f3-1f1ec.svg' width='18' height='18'>
|
||||
* Yoeri Kroon <img alt='Netherlands' src='https://cdnjs.cloudflare.com/ajax/libs/emojione/2.1.4/assets/svg/1f1f3-1f1f1.svg' width='18' height='18'>
|
||||
* Andrew Melvin <img alt='United Kingdom' src='https://cdnjs.cloudflare.com/ajax/libs/emojione/2.1.4/assets/svg/1f1ec-1f1e7.svg' width='18' height='18'>
|
||||
* Doanh Luong <img alt ='Vietnam' src='https://cdnjs.cloudflare.com/ajax/libs/emojione/2.1.4/assets/svg/1f1fb-1f1f3.svg' width='18' height='18'>
|
||||
|
||||
---
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
version: 5.6.3.{build}
|
||||
version: 5.8.0.{build}
|
||||
environment:
|
||||
matrix:
|
||||
- CMAKE_GENERATOR: Visual Studio 14 2015
|
||||
|
@ -9,8 +9,8 @@
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <SPI.h>
|
||||
#include <Ethernet.h>
|
||||
#include <SPI.h>
|
||||
|
||||
EthernetClient client;
|
||||
|
||||
@ -36,11 +36,8 @@ void setup() {
|
||||
void loop() {
|
||||
if (connect(server)) {
|
||||
if (sendRequest(server, resource) && skipResponseHeaders()) {
|
||||
char response[MAX_CONTENT_SIZE];
|
||||
readReponseContent(response, sizeof(response));
|
||||
|
||||
UserData userData;
|
||||
if (parseUserData(response, &userData)) {
|
||||
if (readReponseContent(&userData)) {
|
||||
printUserData(&userData);
|
||||
}
|
||||
}
|
||||
@ -87,9 +84,9 @@ bool sendRequest(const char* host, const char* resource) {
|
||||
|
||||
client.print("GET ");
|
||||
client.print(resource);
|
||||
client.println(" HTTP/1.1");
|
||||
client.println(" HTTP/1.0");
|
||||
client.print("Host: ");
|
||||
client.println(server);
|
||||
client.println(host);
|
||||
client.println("Connection: close");
|
||||
client.println();
|
||||
|
||||
@ -111,13 +108,6 @@ bool skipResponseHeaders() {
|
||||
return ok;
|
||||
}
|
||||
|
||||
// Read the body of the response from the HTTP server
|
||||
void readReponseContent(char* content, size_t maxSize) {
|
||||
size_t length = client.readBytes(content, maxSize);
|
||||
content[length] = 0;
|
||||
Serial.println(content);
|
||||
}
|
||||
|
||||
// Parse the JSON from the input string and extract the interesting values
|
||||
// Here is the JSON we need to parse
|
||||
// {
|
||||
@ -143,21 +133,20 @@ void readReponseContent(char* content, size_t maxSize) {
|
||||
// "bs": "harness real-time e-markets"
|
||||
// }
|
||||
// }
|
||||
bool parseUserData(char* content, struct UserData* userData) {
|
||||
bool readReponseContent(struct UserData* userData) {
|
||||
// Compute optimal size of the JSON buffer according to what we need to parse.
|
||||
// This is only required if you use StaticJsonBuffer.
|
||||
const size_t BUFFER_SIZE =
|
||||
JSON_OBJECT_SIZE(8) // the root object has 8 elements
|
||||
+ JSON_OBJECT_SIZE(5) // the "address" object has 5 elements
|
||||
+ JSON_OBJECT_SIZE(2) // the "geo" object has 2 elements
|
||||
+ JSON_OBJECT_SIZE(3); // the "company" object has 3 elements
|
||||
JSON_OBJECT_SIZE(8) // the root object has 8 elements
|
||||
+ JSON_OBJECT_SIZE(5) // the "address" object has 5 elements
|
||||
+ JSON_OBJECT_SIZE(2) // the "geo" object has 2 elements
|
||||
+ JSON_OBJECT_SIZE(3) // the "company" object has 3 elements
|
||||
+ MAX_CONTENT_SIZE; // additional space for strings
|
||||
|
||||
// Allocate a temporary memory pool on the stack
|
||||
StaticJsonBuffer<BUFFER_SIZE> jsonBuffer;
|
||||
// If the memory pool is too big for the stack, use this instead:
|
||||
// DynamicJsonBuffer jsonBuffer;
|
||||
// Allocate a temporary memory pool
|
||||
DynamicJsonBuffer jsonBuffer(BUFFER_SIZE);
|
||||
|
||||
JsonObject& root = jsonBuffer.parseObject(content);
|
||||
JsonObject& root = jsonBuffer.parseObject(client);
|
||||
|
||||
if (!root.success()) {
|
||||
Serial.println("JSON parsing failed!");
|
||||
|
56
examples/ProgmemExample/ProgmemExample.ino
Normal file
56
examples/ProgmemExample/ProgmemExample.ino
Normal file
@ -0,0 +1,56 @@
|
||||
// Copyright Benoit Blanchon 2014-2016
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
// About
|
||||
// -----
|
||||
// This example shows the different ways you can use PROGMEM with ArduinoJson.
|
||||
// Please don't see this as an invitation to use PROGMEM.
|
||||
// On the contrary, you should always use char[] when possible, it's much more
|
||||
// efficient in term of code size, speed and memory usage.
|
||||
|
||||
void setup() {
|
||||
#ifdef PROGMEM
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
|
||||
// You can use a Flash String as your JSON input.
|
||||
// WARNING: the content of the Flash String will be duplicated in the
|
||||
// JsonBuffer.
|
||||
JsonObject& root =
|
||||
jsonBuffer.parseObject(F("{\"sensor\":\"gps\",\"time\":1351824120,"
|
||||
"\"data\":[48.756080,2.302038]}"));
|
||||
|
||||
// You can use a Flash String to get an element of a JsonObject
|
||||
// No duplication is done.
|
||||
long time = root[F("time")];
|
||||
|
||||
// You can use a Flash String to set an element of a JsonObject
|
||||
// WARNING: the content of the Flash String will be duplicated in the
|
||||
// JsonBuffer.
|
||||
root[F("time")] = time;
|
||||
|
||||
// You can set a Flash String to a JsonObject or JsonArray:
|
||||
// WARNING: the content of the Flash String will be duplicated in the
|
||||
// JsonBuffer.
|
||||
root["sensor"] = F("gps");
|
||||
|
||||
// You can compare the content of a JsonVariant to a Flash String
|
||||
if (root["sensor"] == F("gps")) {
|
||||
// ...
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#warning PROGMEM is not supported on this platform
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// not used in this example
|
||||
}
|
63
examples/StringExample/StringExample.ino
Normal file
63
examples/StringExample/StringExample.ino
Normal file
@ -0,0 +1,63 @@
|
||||
// Copyright Benoit Blanchon 2014-2016
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
// About
|
||||
// -----
|
||||
// This example shows the different ways you can use String with ArduinoJson.
|
||||
// Please don't see this as an invitation to use String.
|
||||
// On the contrary, you should always use char[] when possible, it's much more
|
||||
// efficient in term of code size, speed and memory usage.
|
||||
|
||||
void setup() {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
|
||||
// You can use a String as your JSON input.
|
||||
// WARNING: the content of the String will be duplicated in the JsonBuffer.
|
||||
String input =
|
||||
"{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
|
||||
JsonObject& root = jsonBuffer.parseObject(input);
|
||||
|
||||
// You can use a String to get an element of a JsonObject
|
||||
// No duplication is done.
|
||||
long time = root[String("time")];
|
||||
|
||||
// You can use a String to set an element of a JsonObject
|
||||
// WARNING: the content of the String will be duplicated in the JsonBuffer.
|
||||
root[String("time")] = time;
|
||||
|
||||
// You can get a String from a JsonObject or JsonArray:
|
||||
// No duplication is done, at least not in the JsonBuffer.
|
||||
String sensor = root["sensor"];
|
||||
|
||||
// Unfortunately, the following doesn't work (issue #118):
|
||||
// sensor = root["sensor"]; // <- error "ambiguous overload for 'operator='"
|
||||
// As a workaround, you need to replace by:
|
||||
sensor = root["sensor"].as<String>();
|
||||
|
||||
// You can set a String to a JsonObject or JsonArray:
|
||||
// WARNING: the content of the String will be duplicated in the JsonBuffer.
|
||||
root["sensor"] = sensor;
|
||||
|
||||
// You can also concatenate strings
|
||||
// WARNING: the content of the String will be duplicated in the JsonBuffer.
|
||||
root[String("sen") + "sor"] = String("gp") + "s";
|
||||
|
||||
// You can compare the content of a JsonObject with a String
|
||||
if (root["sensor"] == sensor) {
|
||||
// ...
|
||||
}
|
||||
|
||||
// Lastly, you can print the resulting JSON to a String
|
||||
String output;
|
||||
root.printTo(output);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// not used in this example
|
||||
}
|
@ -10,12 +10,14 @@
|
||||
#include "ArduinoJson/DynamicJsonBuffer.hpp"
|
||||
#include "ArduinoJson/JsonArray.hpp"
|
||||
#include "ArduinoJson/JsonObject.hpp"
|
||||
#include "ArduinoJson/JsonVariantComparisons.hpp"
|
||||
#include "ArduinoJson/StaticJsonBuffer.hpp"
|
||||
|
||||
#include "ArduinoJson/Internals/JsonParser.ipp"
|
||||
#include "ArduinoJson/JsonArray.ipp"
|
||||
#include "ArduinoJson/JsonBuffer.ipp"
|
||||
#include "ArduinoJson/JsonObject.ipp"
|
||||
#include "ArduinoJson/JsonVariant.ipp"
|
||||
#include "ArduinoJson/Deserialization/JsonParserImpl.hpp"
|
||||
#include "ArduinoJson/JsonArrayImpl.hpp"
|
||||
#include "ArduinoJson/JsonBufferImpl.hpp"
|
||||
#include "ArduinoJson/JsonObjectImpl.hpp"
|
||||
#include "ArduinoJson/JsonVariantImpl.hpp"
|
||||
#include "ArduinoJson/Serialization/JsonSerializerImpl.hpp"
|
||||
|
||||
using namespace ArduinoJson;
|
||||
|
@ -22,12 +22,30 @@
|
||||
#define ARDUINOJSON_USE_INT64 0
|
||||
#endif
|
||||
|
||||
// arduino has its own implementation of String to replace std::string
|
||||
#ifndef ARDUINOJSON_USE_ARDUINO_STRING
|
||||
#define ARDUINOJSON_USE_ARDUINO_STRING 1
|
||||
// Arduino has its own implementation of String to replace std::string
|
||||
#ifndef ARDUINOJSON_ENABLE_ARDUINO_STRING
|
||||
#define ARDUINOJSON_ENABLE_ARDUINO_STRING 1
|
||||
#endif
|
||||
|
||||
// arduino doesn't support STL stream
|
||||
#ifndef ARDUINOJSON_ENABLE_ARDUINO_STREAM
|
||||
#define ARDUINOJSON_ENABLE_ARDUINO_STREAM 1
|
||||
#endif
|
||||
|
||||
// On AVR archiecture, we can use PROGMEM
|
||||
#ifndef ARDUINOJSON_ENABLE_PROGMEM
|
||||
#ifdef PROGMEM
|
||||
#define ARDUINOJSON_ENABLE_PROGMEM 1
|
||||
#else
|
||||
#define ARDUINOJSON_ENABLE_PROGMEM 0
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// Arduino doesn't have std::string
|
||||
#ifndef ARDUINOJSON_ENABLE_STD_STRING
|
||||
#define ARDUINOJSON_ENABLE_STD_STRING 0
|
||||
#endif
|
||||
|
||||
// Arduino doesn't support STL stream
|
||||
#ifndef ARDUINOJSON_ENABLE_STD_STREAM
|
||||
#define ARDUINOJSON_ENABLE_STD_STREAM 0
|
||||
#endif
|
||||
@ -42,6 +60,11 @@
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// low value to prevent stack overflow
|
||||
#ifndef ARDUINOJSON_DEFAULT_NESTING_LIMIT
|
||||
#define ARDUINOJSON_DEFAULT_NESTING_LIMIT 10
|
||||
#endif
|
||||
|
||||
#else // assume this is a computer
|
||||
|
||||
// on a computer we have plenty of memory so we can use doubles
|
||||
@ -68,8 +91,18 @@
|
||||
#endif
|
||||
|
||||
// on a computer, we can use std::string
|
||||
#ifndef ARDUINOJSON_USE_ARDUINO_STRING
|
||||
#define ARDUINOJSON_USE_ARDUINO_STRING 0
|
||||
#ifndef ARDUINOJSON_ENABLE_STD_STRING
|
||||
#define ARDUINOJSON_ENABLE_STD_STRING 1
|
||||
#endif
|
||||
|
||||
// on a computer, there is no reason to beleive Arduino String is available
|
||||
#ifndef ARDUINOJSON_ENABLE_ARDUINO_STRING
|
||||
#define ARDUINOJSON_ENABLE_ARDUINO_STRING 0
|
||||
#endif
|
||||
|
||||
// PROGMEM is only available on AVR architecture
|
||||
#ifndef ARDUINOJSON_ENABLE_PROGMEM
|
||||
#define ARDUINOJSON_ENABLE_PROGMEM 0
|
||||
#endif
|
||||
|
||||
// on a computer, we can assume that the STL is there
|
||||
@ -77,11 +110,21 @@
|
||||
#define ARDUINOJSON_ENABLE_STD_STREAM 1
|
||||
#endif
|
||||
|
||||
// on a computer, there is no reason to beleive Arduino Stream is available
|
||||
#ifndef ARDUINOJSON_ENABLE_ARDUINO_STREAM
|
||||
#define ARDUINOJSON_ENABLE_ARDUINO_STREAM 0
|
||||
#endif
|
||||
|
||||
#ifndef ARDUINOJSON_ENABLE_ALIGNMENT
|
||||
// even if not required, most cpu's are faster with aligned pointers
|
||||
#define ARDUINOJSON_ENABLE_ALIGNMENT 1
|
||||
#endif
|
||||
|
||||
// on a computer, we should have a lot of space on the stack
|
||||
#ifndef ARDUINOJSON_DEFAULT_NESTING_LIMIT
|
||||
#define ARDUINOJSON_DEFAULT_NESTING_LIMIT 50
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
#if ARDUINOJSON_USE_LONG_LONG && ARDUINOJSON_USE_INT64
|
||||
|
@ -8,17 +8,19 @@
|
||||
#pragma once
|
||||
|
||||
namespace ArduinoJson {
|
||||
namespace TypeTraits {
|
||||
namespace Internals {
|
||||
|
||||
// A meta-function that returns true if T is a reference
|
||||
template <typename T>
|
||||
struct IsReference {
|
||||
static const bool value = false;
|
||||
struct JsonVariantDefault {
|
||||
static T get() {
|
||||
return T();
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct IsReference<T&> {
|
||||
static const bool value = true;
|
||||
};
|
||||
struct JsonVariantDefault<const T> : JsonVariantDefault<T> {};
|
||||
|
||||
template <typename T>
|
||||
struct JsonVariantDefault<T&> : JsonVariantDefault<T> {};
|
||||
}
|
||||
}
|
41
include/ArduinoJson/Data/ValueSetter.hpp
Normal file
41
include/ArduinoJson/Data/ValueSetter.hpp
Normal file
@ -0,0 +1,41 @@
|
||||
// Copyright Benoit Blanchon 2014-2016
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../JsonBuffer.hpp"
|
||||
#include "../JsonVariant.hpp"
|
||||
#include "../StringTraits/StringTraits.hpp"
|
||||
#include "../TypeTraits/EnableIf.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
namespace Internals {
|
||||
|
||||
template <typename TSource, typename Enable = void>
|
||||
struct ValueSetter {
|
||||
template <typename TDestination>
|
||||
static bool set(JsonBuffer*, TDestination& destination,
|
||||
const TSource& source) {
|
||||
destination = source;
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename TSource>
|
||||
struct ValueSetter<TSource, typename TypeTraits::EnableIf<
|
||||
StringFuncs<TSource>::should_duplicate>::type> {
|
||||
template <typename TDestination>
|
||||
static bool set(JsonBuffer* buffer, TDestination& destination,
|
||||
const TSource& source) {
|
||||
const char* copy = buffer->strdup(source);
|
||||
if (!copy) return false;
|
||||
destination = copy;
|
||||
return true;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
76
include/ArduinoJson/Deserialization/Comments.hpp
Normal file
76
include/ArduinoJson/Deserialization/Comments.hpp
Normal file
@ -0,0 +1,76 @@
|
||||
// Copyright Benoit Blanchon 2014-2016
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace ArduinoJson {
|
||||
namespace Internals {
|
||||
template <typename TInput>
|
||||
void skipSpacesAndComments(TInput& input) {
|
||||
for (;;) {
|
||||
switch (input.current()) {
|
||||
// spaces
|
||||
case ' ':
|
||||
case '\t':
|
||||
case '\r':
|
||||
case '\n':
|
||||
input.move();
|
||||
continue;
|
||||
|
||||
// comments
|
||||
case '/':
|
||||
switch (input.next()) {
|
||||
// C-style block comment
|
||||
case '*':
|
||||
input.move(); // skip '/'
|
||||
input.move(); // skip '*'
|
||||
for (;;) {
|
||||
switch (input.current()) {
|
||||
case '\0':
|
||||
return;
|
||||
case '*':
|
||||
input.move(); // skip '*'
|
||||
if (input.current() == '/') {
|
||||
input.move(); // skip '/'
|
||||
return;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
input.move();
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
// C++-style line comment
|
||||
case '/':
|
||||
input.move(); // skip '/'
|
||||
for (;;) {
|
||||
switch (input.current()) {
|
||||
case '\0':
|
||||
return;
|
||||
case '\n':
|
||||
input.move();
|
||||
return;
|
||||
default:
|
||||
input.move();
|
||||
}
|
||||
}
|
||||
return;
|
||||
|
||||
// not a comment, just a '/'
|
||||
default:
|
||||
return;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
108
include/ArduinoJson/Deserialization/JsonParser.hpp
Normal file
108
include/ArduinoJson/Deserialization/JsonParser.hpp
Normal file
@ -0,0 +1,108 @@
|
||||
// Copyright Benoit Blanchon 2014-2016
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../JsonBuffer.hpp"
|
||||
#include "../JsonVariant.hpp"
|
||||
#include "StringReader.hpp"
|
||||
#include "StringWriter.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
namespace Internals {
|
||||
|
||||
// Parse JSON string to create JsonArrays and JsonObjects
|
||||
// This internal class is not indended to be used directly.
|
||||
// Instead, use JsonBuffer.parseArray() or .parseObject()
|
||||
template <typename TReader, typename TWriter>
|
||||
class JsonParser {
|
||||
public:
|
||||
JsonParser(JsonBuffer *buffer, TReader reader, TWriter writer,
|
||||
uint8_t nestingLimit)
|
||||
: _buffer(buffer),
|
||||
_reader(reader),
|
||||
_writer(writer),
|
||||
_nestingLimit(nestingLimit) {}
|
||||
|
||||
JsonArray &parseArray();
|
||||
JsonObject &parseObject();
|
||||
|
||||
JsonVariant parseVariant() {
|
||||
JsonVariant result;
|
||||
parseAnythingTo(&result);
|
||||
return result;
|
||||
}
|
||||
|
||||
private:
|
||||
JsonParser &operator=(const JsonParser &); // non-copiable
|
||||
|
||||
static bool eat(TReader &, char charToSkip);
|
||||
FORCE_INLINE bool eat(char charToSkip) {
|
||||
return eat(_reader, charToSkip);
|
||||
}
|
||||
|
||||
const char *parseString();
|
||||
bool parseAnythingTo(JsonVariant *destination);
|
||||
FORCE_INLINE bool parseAnythingToUnsafe(JsonVariant *destination);
|
||||
|
||||
inline bool parseArrayTo(JsonVariant *destination);
|
||||
inline bool parseObjectTo(JsonVariant *destination);
|
||||
inline bool parseStringTo(JsonVariant *destination);
|
||||
|
||||
static inline bool isInRange(char c, char min, char max) {
|
||||
return min <= c && c <= max;
|
||||
}
|
||||
|
||||
static inline bool isLetterOrNumber(char c) {
|
||||
return isInRange(c, '0', '9') || isInRange(c, 'a', 'z') ||
|
||||
isInRange(c, 'A', 'Z') || c == '+' || c == '-' || c == '.';
|
||||
}
|
||||
|
||||
static inline bool isQuote(char c) {
|
||||
return c == '\'' || c == '\"';
|
||||
}
|
||||
|
||||
JsonBuffer *_buffer;
|
||||
TReader _reader;
|
||||
TWriter _writer;
|
||||
uint8_t _nestingLimit;
|
||||
};
|
||||
|
||||
template <typename TJsonBuffer, typename TString>
|
||||
struct JsonParserBuilder {
|
||||
typedef typename Internals::StringFuncs<TString>::Iterator InputIterator;
|
||||
typedef JsonParser<StringReader<InputIterator>, TJsonBuffer &> TParser;
|
||||
|
||||
static TParser makeParser(TJsonBuffer *buffer, TString &json,
|
||||
uint8_t nestingLimit) {
|
||||
return TParser(buffer, InputIterator(json), *buffer, nestingLimit);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename TJsonBuffer>
|
||||
struct JsonParserBuilder<TJsonBuffer, char *> {
|
||||
typedef typename Internals::StringFuncs<char *>::Iterator InputIterator;
|
||||
typedef JsonParser<StringReader<InputIterator>, StringWriter> TParser;
|
||||
|
||||
static TParser makeParser(TJsonBuffer *buffer, char *json,
|
||||
uint8_t nestingLimit) {
|
||||
return TParser(buffer, InputIterator(json), json, nestingLimit);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename TJsonBuffer, size_t N>
|
||||
struct JsonParserBuilder<TJsonBuffer, char[N]>
|
||||
: JsonParserBuilder<TJsonBuffer, char *> {};
|
||||
|
||||
template <typename TJsonBuffer, typename TString>
|
||||
inline typename JsonParserBuilder<TJsonBuffer, TString>::TParser makeParser(
|
||||
TJsonBuffer *buffer, TString &json, uint8_t nestingLimit) {
|
||||
return JsonParserBuilder<TJsonBuffer, TString>::makeParser(buffer, json,
|
||||
nestingLimit);
|
||||
}
|
||||
}
|
||||
}
|
@ -10,15 +10,19 @@
|
||||
#include "Comments.hpp"
|
||||
#include "JsonParser.hpp"
|
||||
|
||||
inline bool ArduinoJson::Internals::JsonParser::skip(char charToSkip) {
|
||||
const char *ptr = skipSpacesAndComments(_readPtr);
|
||||
if (*ptr != charToSkip) return false;
|
||||
ptr++;
|
||||
_readPtr = skipSpacesAndComments(ptr);
|
||||
template <typename TReader, typename TWriter>
|
||||
inline bool ArduinoJson::Internals::JsonParser<TReader, TWriter>::eat(
|
||||
TReader &reader, char charToSkip) {
|
||||
skipSpacesAndComments(reader);
|
||||
if (reader.current() != charToSkip) return false;
|
||||
reader.move();
|
||||
skipSpacesAndComments(reader);
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool ArduinoJson::Internals::JsonParser::parseAnythingTo(
|
||||
template <typename TReader, typename TWriter>
|
||||
inline bool
|
||||
ArduinoJson::Internals::JsonParser<TReader, TWriter>::parseAnythingTo(
|
||||
JsonVariant *destination) {
|
||||
if (_nestingLimit == 0) return false;
|
||||
_nestingLimit--;
|
||||
@ -27,11 +31,13 @@ inline bool ArduinoJson::Internals::JsonParser::parseAnythingTo(
|
||||
return success;
|
||||
}
|
||||
|
||||
inline bool ArduinoJson::Internals::JsonParser::parseAnythingToUnsafe(
|
||||
template <typename TReader, typename TWriter>
|
||||
inline bool
|
||||
ArduinoJson::Internals::JsonParser<TReader, TWriter>::parseAnythingToUnsafe(
|
||||
JsonVariant *destination) {
|
||||
_readPtr = skipSpacesAndComments(_readPtr);
|
||||
skipSpacesAndComments(_reader);
|
||||
|
||||
switch (*_readPtr) {
|
||||
switch (_reader.current()) {
|
||||
case '[':
|
||||
return parseArrayTo(destination);
|
||||
|
||||
@ -43,14 +49,15 @@ inline bool ArduinoJson::Internals::JsonParser::parseAnythingToUnsafe(
|
||||
}
|
||||
}
|
||||
|
||||
template <typename TReader, typename TWriter>
|
||||
inline ArduinoJson::JsonArray &
|
||||
ArduinoJson::Internals::JsonParser::parseArray() {
|
||||
ArduinoJson::Internals::JsonParser<TReader, TWriter>::parseArray() {
|
||||
// Create an empty array
|
||||
JsonArray &array = _buffer->createArray();
|
||||
|
||||
// Check opening braket
|
||||
if (!skip('[')) goto ERROR_MISSING_BRACKET;
|
||||
if (skip(']')) goto SUCCESS_EMPTY_ARRAY;
|
||||
if (!eat('[')) goto ERROR_MISSING_BRACKET;
|
||||
if (eat(']')) goto SUCCESS_EMPTY_ARRAY;
|
||||
|
||||
// Read each value
|
||||
for (;;) {
|
||||
@ -60,8 +67,8 @@ ArduinoJson::Internals::JsonParser::parseArray() {
|
||||
if (!array.add(value)) goto ERROR_NO_MEMORY;
|
||||
|
||||
// 2 - More values?
|
||||
if (skip(']')) goto SUCCES_NON_EMPTY_ARRAY;
|
||||
if (!skip(',')) goto ERROR_MISSING_COMMA;
|
||||
if (eat(']')) goto SUCCES_NON_EMPTY_ARRAY;
|
||||
if (!eat(',')) goto ERROR_MISSING_COMMA;
|
||||
}
|
||||
|
||||
SUCCESS_EMPTY_ARRAY:
|
||||
@ -75,7 +82,8 @@ ERROR_NO_MEMORY:
|
||||
return JsonArray::invalid();
|
||||
}
|
||||
|
||||
inline bool ArduinoJson::Internals::JsonParser::parseArrayTo(
|
||||
template <typename TReader, typename TWriter>
|
||||
inline bool ArduinoJson::Internals::JsonParser<TReader, TWriter>::parseArrayTo(
|
||||
JsonVariant *destination) {
|
||||
JsonArray &array = parseArray();
|
||||
if (!array.success()) return false;
|
||||
@ -84,21 +92,22 @@ inline bool ArduinoJson::Internals::JsonParser::parseArrayTo(
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename TReader, typename TWriter>
|
||||
inline ArduinoJson::JsonObject &
|
||||
ArduinoJson::Internals::JsonParser::parseObject() {
|
||||
ArduinoJson::Internals::JsonParser<TReader, TWriter>::parseObject() {
|
||||
// Create an empty object
|
||||
JsonObject &object = _buffer->createObject();
|
||||
|
||||
// Check opening brace
|
||||
if (!skip('{')) goto ERROR_MISSING_BRACE;
|
||||
if (skip('}')) goto SUCCESS_EMPTY_OBJECT;
|
||||
if (!eat('{')) goto ERROR_MISSING_BRACE;
|
||||
if (eat('}')) goto SUCCESS_EMPTY_OBJECT;
|
||||
|
||||
// Read each key value pair
|
||||
for (;;) {
|
||||
// 1 - Parse key
|
||||
const char *key = parseString();
|
||||
if (!key) goto ERROR_INVALID_KEY;
|
||||
if (!skip(':')) goto ERROR_MISSING_COLON;
|
||||
if (!eat(':')) goto ERROR_MISSING_COLON;
|
||||
|
||||
// 2 - Parse value
|
||||
JsonVariant value;
|
||||
@ -106,8 +115,8 @@ ArduinoJson::Internals::JsonParser::parseObject() {
|
||||
if (!object.set(key, value)) goto ERROR_NO_MEMORY;
|
||||
|
||||
// 3 - More keys/values?
|
||||
if (skip('}')) goto SUCCESS_NON_EMPTY_OBJECT;
|
||||
if (!skip(',')) goto ERROR_MISSING_COMMA;
|
||||
if (eat('}')) goto SUCCESS_NON_EMPTY_OBJECT;
|
||||
if (!eat(',')) goto ERROR_MISSING_COMMA;
|
||||
}
|
||||
|
||||
SUCCESS_EMPTY_OBJECT:
|
||||
@ -123,7 +132,8 @@ ERROR_NO_MEMORY:
|
||||
return JsonObject::invalid();
|
||||
}
|
||||
|
||||
inline bool ArduinoJson::Internals::JsonParser::parseObjectTo(
|
||||
template <typename TReader, typename TWriter>
|
||||
inline bool ArduinoJson::Internals::JsonParser<TReader, TWriter>::parseObjectTo(
|
||||
JsonVariant *destination) {
|
||||
JsonObject &object = parseObject();
|
||||
if (!object.success()) return false;
|
||||
@ -132,54 +142,49 @@ inline bool ArduinoJson::Internals::JsonParser::parseObjectTo(
|
||||
return true;
|
||||
}
|
||||
|
||||
inline const char *ArduinoJson::Internals::JsonParser::parseString() {
|
||||
const char *readPtr = _readPtr;
|
||||
char *writePtr = _writePtr;
|
||||
template <typename TReader, typename TWriter>
|
||||
inline const char *
|
||||
ArduinoJson::Internals::JsonParser<TReader, TWriter>::parseString() {
|
||||
typename TypeTraits::RemoveReference<TWriter>::type::String str =
|
||||
_writer.startString();
|
||||
|
||||
char c = *readPtr;
|
||||
char c = _reader.current();
|
||||
|
||||
if (isQuote(c)) { // quotes
|
||||
_reader.move();
|
||||
char stopChar = c;
|
||||
for (;;) {
|
||||
c = *++readPtr;
|
||||
c = _reader.current();
|
||||
if (c == '\0') break;
|
||||
_reader.move();
|
||||
|
||||
if (c == stopChar) {
|
||||
readPtr++;
|
||||
break;
|
||||
}
|
||||
if (c == stopChar) break;
|
||||
|
||||
if (c == '\\') {
|
||||
// replace char
|
||||
c = Encoding::unescapeChar(*++readPtr);
|
||||
c = Encoding::unescapeChar(_reader.current());
|
||||
if (c == '\0') break;
|
||||
_reader.move();
|
||||
}
|
||||
|
||||
*writePtr++ = c;
|
||||
str.append(c);
|
||||
}
|
||||
} else { // no quotes
|
||||
for (;;) {
|
||||
if (!isLetterOrNumber(c)) break;
|
||||
*writePtr++ = c;
|
||||
c = *++readPtr;
|
||||
_reader.move();
|
||||
str.append(c);
|
||||
c = _reader.current();
|
||||
}
|
||||
}
|
||||
// end the string here
|
||||
*writePtr++ = '\0';
|
||||
|
||||
const char *startPtr = _writePtr;
|
||||
|
||||
// update end ptr
|
||||
_readPtr = readPtr;
|
||||
_writePtr = writePtr;
|
||||
|
||||
// return pointer to unquoted string
|
||||
return startPtr;
|
||||
return str.c_str();
|
||||
}
|
||||
|
||||
inline bool ArduinoJson::Internals::JsonParser::parseStringTo(
|
||||
template <typename TReader, typename TWriter>
|
||||
inline bool ArduinoJson::Internals::JsonParser<TReader, TWriter>::parseStringTo(
|
||||
JsonVariant *destination) {
|
||||
bool hasQuotes = isQuote(_readPtr[0]);
|
||||
bool hasQuotes = isQuote(_reader.current());
|
||||
const char *value = parseString();
|
||||
if (value == NULL) return false;
|
||||
if (hasQuotes) {
|
41
include/ArduinoJson/Deserialization/StringReader.hpp
Normal file
41
include/ArduinoJson/Deserialization/StringReader.hpp
Normal file
@ -0,0 +1,41 @@
|
||||
// Copyright Benoit Blanchon 2014-2016
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace ArduinoJson {
|
||||
namespace Internals {
|
||||
|
||||
// Parse JSON string to create JsonArrays and JsonObjects
|
||||
// This internal class is not indended to be used directly.
|
||||
// Instead, use JsonBuffer.parseArray() or .parseObject()
|
||||
template <typename TIterator>
|
||||
class StringReader {
|
||||
TIterator _input;
|
||||
char _current, _next;
|
||||
|
||||
public:
|
||||
StringReader(const TIterator& input) : _input(input) {
|
||||
_current = _input.next();
|
||||
_next = _input.next();
|
||||
}
|
||||
|
||||
void move() {
|
||||
_current = _next;
|
||||
_next = _input.next();
|
||||
}
|
||||
|
||||
char current() const {
|
||||
return _current;
|
||||
}
|
||||
|
||||
char next() const {
|
||||
return _next;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
43
include/ArduinoJson/Deserialization/StringWriter.hpp
Normal file
43
include/ArduinoJson/Deserialization/StringWriter.hpp
Normal file
@ -0,0 +1,43 @@
|
||||
// Copyright Benoit Blanchon 2014-2016
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace ArduinoJson {
|
||||
namespace Internals {
|
||||
|
||||
class StringWriter {
|
||||
public:
|
||||
class String {
|
||||
public:
|
||||
String(char** ptr) : _writePtr(ptr), _startPtr(*ptr) {}
|
||||
|
||||
void append(char c) {
|
||||
*(*_writePtr)++ = c;
|
||||
}
|
||||
|
||||
const char* c_str() const {
|
||||
*(*_writePtr)++ = 0;
|
||||
return _startPtr;
|
||||
}
|
||||
|
||||
private:
|
||||
char** _writePtr;
|
||||
char* _startPtr;
|
||||
};
|
||||
|
||||
StringWriter(char* buffer) : _ptr(buffer) {}
|
||||
|
||||
String startString() {
|
||||
return String(&_ptr);
|
||||
}
|
||||
|
||||
private:
|
||||
char* _ptr;
|
||||
};
|
||||
}
|
||||
}
|
@ -7,12 +7,153 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Internals/BlockJsonBuffer.hpp"
|
||||
#include "JsonBufferBase.hpp"
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#if defined(__clang__)
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wnon-virtual-dtor"
|
||||
#elif defined(__GNUC__)
|
||||
#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
|
||||
#pragma GCC diagnostic push
|
||||
#endif
|
||||
#pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
|
||||
#endif
|
||||
|
||||
namespace ArduinoJson {
|
||||
class DefaultAllocator {
|
||||
public:
|
||||
void* allocate(size_t size) {
|
||||
return malloc(size);
|
||||
}
|
||||
void deallocate(void* pointer) {
|
||||
free(pointer);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename TAllocator>
|
||||
class DynamicJsonBufferBase
|
||||
: public JsonBufferBase<DynamicJsonBufferBase<TAllocator> > {
|
||||
struct Block;
|
||||
struct EmptyBlock {
|
||||
Block* next;
|
||||
size_t capacity;
|
||||
size_t size;
|
||||
};
|
||||
struct Block : EmptyBlock {
|
||||
uint8_t data[1];
|
||||
};
|
||||
|
||||
public:
|
||||
DynamicJsonBufferBase(size_t initialSize = 256)
|
||||
: _head(NULL), _nextBlockCapacity(initialSize) {}
|
||||
|
||||
~DynamicJsonBufferBase() {
|
||||
Block* currentBlock = _head;
|
||||
|
||||
while (currentBlock != NULL) {
|
||||
Block* nextBlock = currentBlock->next;
|
||||
_allocator.deallocate(currentBlock);
|
||||
currentBlock = nextBlock;
|
||||
}
|
||||
}
|
||||
|
||||
size_t size() const {
|
||||
size_t total = 0;
|
||||
for (const Block* b = _head; b; b = b->next) total += b->size;
|
||||
return total;
|
||||
}
|
||||
|
||||
virtual void* alloc(size_t bytes) {
|
||||
alignNextAlloc();
|
||||
return canAllocInHead(bytes) ? allocInHead(bytes) : allocInNewBlock(bytes);
|
||||
}
|
||||
|
||||
class String {
|
||||
public:
|
||||
String(DynamicJsonBufferBase* parent)
|
||||
: _parent(parent), _start(NULL), _length(0) {}
|
||||
|
||||
void append(char c) {
|
||||
if (_parent->canAllocInHead(1)) {
|
||||
char* end = static_cast<char*>(_parent->allocInHead(1));
|
||||
*end = c;
|
||||
if (_length == 0) _start = end;
|
||||
} else {
|
||||
char* newStart =
|
||||
static_cast<char*>(_parent->allocInNewBlock(_length + 1));
|
||||
if (_start && newStart) memcpy(newStart, _start, _length);
|
||||
newStart[_length] = c;
|
||||
_start = newStart;
|
||||
}
|
||||
_length++;
|
||||
}
|
||||
|
||||
const char* c_str() {
|
||||
append(0);
|
||||
return _start;
|
||||
}
|
||||
|
||||
private:
|
||||
DynamicJsonBufferBase* _parent;
|
||||
char* _start;
|
||||
int _length;
|
||||
};
|
||||
|
||||
String startString() {
|
||||
return String(this);
|
||||
}
|
||||
|
||||
private:
|
||||
void alignNextAlloc() {
|
||||
if (_head) _head->size = this->round_size_up(_head->size);
|
||||
}
|
||||
|
||||
bool canAllocInHead(size_t bytes) const {
|
||||
return _head != NULL && _head->size + bytes <= _head->capacity;
|
||||
}
|
||||
|
||||
void* allocInHead(size_t bytes) {
|
||||
void* p = _head->data + _head->size;
|
||||
_head->size += bytes;
|
||||
return p;
|
||||
}
|
||||
|
||||
void* allocInNewBlock(size_t bytes) {
|
||||
size_t capacity = _nextBlockCapacity;
|
||||
if (bytes > capacity) capacity = bytes;
|
||||
if (!addNewBlock(capacity)) return NULL;
|
||||
_nextBlockCapacity *= 2;
|
||||
return allocInHead(bytes);
|
||||
}
|
||||
|
||||
bool addNewBlock(size_t capacity) {
|
||||
size_t bytes = sizeof(EmptyBlock) + capacity;
|
||||
Block* block = static_cast<Block*>(_allocator.allocate(bytes));
|
||||
if (block == NULL) return false;
|
||||
block->capacity = capacity;
|
||||
block->size = 0;
|
||||
block->next = _head;
|
||||
_head = block;
|
||||
return true;
|
||||
}
|
||||
|
||||
TAllocator _allocator;
|
||||
Block* _head;
|
||||
size_t _nextBlockCapacity;
|
||||
};
|
||||
|
||||
#if defined(__clang__)
|
||||
#pragma clang diagnostic pop
|
||||
#elif defined(__GNUC__)
|
||||
#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// Implements a JsonBuffer with dynamic memory allocation.
|
||||
// You are strongly encouraged to consider using StaticJsonBuffer which is much
|
||||
// more suitable for embedded systems.
|
||||
typedef Internals::BlockJsonBuffer<Internals::DefaultAllocator>
|
||||
DynamicJsonBuffer;
|
||||
typedef DynamicJsonBufferBase<DefaultAllocator> DynamicJsonBuffer;
|
||||
}
|
||||
|
@ -1,115 +0,0 @@
|
||||
// Copyright Benoit Blanchon 2014-2016
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../JsonBuffer.hpp"
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#if defined(__clang__)
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wnon-virtual-dtor"
|
||||
#elif defined(__GNUC__)
|
||||
#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
|
||||
#pragma GCC diagnostic push
|
||||
#endif
|
||||
#pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
|
||||
#endif
|
||||
|
||||
namespace ArduinoJson {
|
||||
namespace Internals {
|
||||
class DefaultAllocator {
|
||||
public:
|
||||
void* allocate(size_t size) {
|
||||
return malloc(size);
|
||||
}
|
||||
void deallocate(void* pointer) {
|
||||
free(pointer);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename TAllocator>
|
||||
class BlockJsonBuffer : public JsonBuffer {
|
||||
struct Block;
|
||||
struct EmptyBlock {
|
||||
Block* next;
|
||||
size_t capacity;
|
||||
size_t size;
|
||||
};
|
||||
struct Block : EmptyBlock {
|
||||
uint8_t data[1];
|
||||
};
|
||||
|
||||
public:
|
||||
BlockJsonBuffer(size_t initialSize = 256)
|
||||
: _head(NULL), _nextBlockSize(initialSize) {}
|
||||
|
||||
~BlockJsonBuffer() {
|
||||
Block* currentBlock = _head;
|
||||
|
||||
while (currentBlock != NULL) {
|
||||
Block* nextBlock = currentBlock->next;
|
||||
_allocator.deallocate(currentBlock);
|
||||
currentBlock = nextBlock;
|
||||
}
|
||||
}
|
||||
|
||||
size_t size() const {
|
||||
size_t total = 0;
|
||||
for (const Block* b = _head; b; b = b->next) total += b->size;
|
||||
return total;
|
||||
}
|
||||
|
||||
virtual void* alloc(size_t bytes) {
|
||||
return canAllocInHead(bytes) ? allocInHead(bytes) : allocInNewBlock(bytes);
|
||||
}
|
||||
|
||||
private:
|
||||
bool canAllocInHead(size_t bytes) const {
|
||||
return _head != NULL && _head->size + bytes <= _head->capacity;
|
||||
}
|
||||
|
||||
void* allocInHead(size_t bytes) {
|
||||
void* p = _head->data + _head->size;
|
||||
_head->size += round_size_up(bytes);
|
||||
return p;
|
||||
}
|
||||
|
||||
void* allocInNewBlock(size_t bytes) {
|
||||
size_t capacity = _nextBlockSize;
|
||||
if (bytes > capacity) capacity = bytes;
|
||||
if (!addNewBlock(capacity)) return NULL;
|
||||
_nextBlockSize *= 2;
|
||||
return allocInHead(bytes);
|
||||
}
|
||||
|
||||
bool addNewBlock(size_t capacity) {
|
||||
size_t bytes = sizeof(EmptyBlock) + capacity;
|
||||
Block* block = static_cast<Block*>(_allocator.allocate(bytes));
|
||||
if (block == NULL) return false;
|
||||
block->capacity = capacity;
|
||||
block->size = 0;
|
||||
block->next = _head;
|
||||
_head = block;
|
||||
return true;
|
||||
}
|
||||
|
||||
TAllocator _allocator;
|
||||
Block* _head;
|
||||
size_t _nextBlockSize;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#if defined(__clang__)
|
||||
#pragma clang diagnostic pop
|
||||
#elif defined(__GNUC__)
|
||||
#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
#endif
|
@ -1,56 +0,0 @@
|
||||
// Copyright Benoit Blanchon 2014-2016
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace ArduinoJson {
|
||||
namespace Internals {
|
||||
inline const char *skipCStyleComment(const char *ptr) {
|
||||
ptr += 2;
|
||||
for (;;) {
|
||||
if (ptr[0] == '\0') return ptr;
|
||||
if (ptr[0] == '*' && ptr[1] == '/') return ptr + 2;
|
||||
ptr++;
|
||||
}
|
||||
}
|
||||
|
||||
inline const char *skipCppStyleComment(const char *ptr) {
|
||||
ptr += 2;
|
||||
for (;;) {
|
||||
if (ptr[0] == '\0' || ptr[0] == '\n') return ptr;
|
||||
ptr++;
|
||||
}
|
||||
}
|
||||
|
||||
inline const char *skipSpacesAndComments(const char *ptr) {
|
||||
for (;;) {
|
||||
switch (ptr[0]) {
|
||||
case ' ':
|
||||
case '\t':
|
||||
case '\r':
|
||||
case '\n':
|
||||
ptr++;
|
||||
continue;
|
||||
case '/':
|
||||
switch (ptr[1]) {
|
||||
case '*':
|
||||
ptr = skipCStyleComment(ptr);
|
||||
break;
|
||||
case '/':
|
||||
ptr = skipCppStyleComment(ptr);
|
||||
break;
|
||||
default:
|
||||
return ptr;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
return ptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,66 +0,0 @@
|
||||
// Copyright Benoit Blanchon 2014-2016
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../JsonBuffer.hpp"
|
||||
#include "../JsonVariant.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
namespace Internals {
|
||||
|
||||
// Parse JSON string to create JsonArrays and JsonObjects
|
||||
// This internal class is not indended to be used directly.
|
||||
// Instead, use JsonBuffer.parseArray() or .parseObject()
|
||||
class JsonParser {
|
||||
public:
|
||||
JsonParser(JsonBuffer *buffer, char *json, uint8_t nestingLimit)
|
||||
: _buffer(buffer),
|
||||
_readPtr(json ? json : ""),
|
||||
_writePtr(json),
|
||||
_nestingLimit(nestingLimit) {}
|
||||
|
||||
JsonArray &parseArray();
|
||||
JsonObject &parseObject();
|
||||
|
||||
JsonVariant parseVariant() {
|
||||
JsonVariant result;
|
||||
parseAnythingTo(&result);
|
||||
return result;
|
||||
}
|
||||
|
||||
private:
|
||||
bool skip(char charToSkip);
|
||||
|
||||
const char *parseString();
|
||||
bool parseAnythingTo(JsonVariant *destination);
|
||||
FORCE_INLINE bool parseAnythingToUnsafe(JsonVariant *destination);
|
||||
|
||||
inline bool parseArrayTo(JsonVariant *destination);
|
||||
inline bool parseObjectTo(JsonVariant *destination);
|
||||
inline bool parseStringTo(JsonVariant *destination);
|
||||
|
||||
static inline bool isInRange(char c, char min, char max) {
|
||||
return min <= c && c <= max;
|
||||
}
|
||||
|
||||
static inline bool isLetterOrNumber(char c) {
|
||||
return isInRange(c, '0', '9') || isInRange(c, 'a', 'z') ||
|
||||
isInRange(c, 'A', 'Z') || c == '-' || c == '.';
|
||||
}
|
||||
|
||||
static inline bool isQuote(char c) {
|
||||
return c == '\'' || c == '\"';
|
||||
}
|
||||
|
||||
JsonBuffer *_buffer;
|
||||
const char *_readPtr;
|
||||
char *_writePtr;
|
||||
uint8_t _nestingLimit;
|
||||
};
|
||||
}
|
||||
}
|
@ -7,14 +7,16 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Internals/JsonBufferAllocated.hpp"
|
||||
#include "Internals/JsonPrintable.hpp"
|
||||
#include "Internals/List.hpp"
|
||||
#include "Internals/ReferenceType.hpp"
|
||||
#include "Data/JsonBufferAllocated.hpp"
|
||||
#include "Data/List.hpp"
|
||||
#include "Data/ReferenceType.hpp"
|
||||
#include "Data/ValueSetter.hpp"
|
||||
#include "JsonVariant.hpp"
|
||||
#include "Serialization/JsonPrintable.hpp"
|
||||
#include "StringTraits/StringTraits.hpp"
|
||||
#include "TypeTraits/ConstRefOrConstPtr.hpp"
|
||||
#include "TypeTraits/EnableIf.hpp"
|
||||
#include "TypeTraits/IsFloatingPoint.hpp"
|
||||
#include "TypeTraits/IsReference.hpp"
|
||||
#include "TypeTraits/IsSame.hpp"
|
||||
|
||||
// Returns the size (in bytes) of an array with n elements.
|
||||
@ -40,15 +42,6 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
|
||||
public Internals::List<JsonVariant>,
|
||||
public Internals::JsonBufferAllocated {
|
||||
public:
|
||||
// A meta-function that returns true if type T can be used in
|
||||
// JsonArray::set()
|
||||
template <typename T>
|
||||
struct CanSet {
|
||||
static const bool value = JsonVariant::IsConstructibleFrom<T>::value ||
|
||||
TypeTraits::IsSame<T, String &>::value ||
|
||||
TypeTraits::IsSame<T, const String &>::value;
|
||||
};
|
||||
|
||||
// Create an empty JsonArray attached to the specified JsonBuffer.
|
||||
// You should not call this constructor directly.
|
||||
// Instead, use JsonBuffer::createArray() or JsonBuffer::parseArray().
|
||||
@ -57,7 +50,7 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
|
||||
|
||||
// Gets the value at the specified index
|
||||
JsonVariant operator[](size_t index) const {
|
||||
return get(index);
|
||||
return get<JsonVariant>(index);
|
||||
}
|
||||
|
||||
// Gets or sets the value at specified index
|
||||
@ -73,29 +66,24 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
|
||||
// bool add(float value);
|
||||
// bool add(double value);
|
||||
// bool add(const char*);
|
||||
template <typename T>
|
||||
bool add(
|
||||
T value,
|
||||
typename TypeTraits::EnableIf<
|
||||
CanSet<T>::value && !TypeTraits::IsReference<T>::value>::type * = 0) {
|
||||
return addNode<T>(value);
|
||||
}
|
||||
// bool add(const char[]);
|
||||
// bool add(const char[N]);
|
||||
// bool add(RawJson);
|
||||
// bool add(const std::string&)
|
||||
// bool add(const String&)
|
||||
// bool add(const JsonVariant&);
|
||||
// bool add(JsonArray&);
|
||||
// bool add(JsonObject&);
|
||||
template <typename T>
|
||||
bool add(const T &value,
|
||||
typename TypeTraits::EnableIf<CanSet<T &>::value>::type * = 0) {
|
||||
return addNode<T &>(const_cast<T &>(value));
|
||||
bool add(const T &value) {
|
||||
// reduce the number of template function instanciation to reduce code size
|
||||
return addNodeImpl<typename TypeTraits::ConstRefOrConstPtr<T>::type>(value);
|
||||
}
|
||||
// bool add(float value, uint8_t decimals);
|
||||
// bool add(double value, uint8_t decimals);
|
||||
template <typename T>
|
||||
bool add(T value, uint8_t decimals,
|
||||
typename TypeTraits::EnableIf<
|
||||
TypeTraits::IsFloatingPoint<T>::value>::type * = 0) {
|
||||
return addNode<JsonVariant>(JsonVariant(value, decimals));
|
||||
bool add(T value, uint8_t decimals) {
|
||||
return add(JsonVariant(value, decimals));
|
||||
}
|
||||
|
||||
// Sets the value at specified index.
|
||||
@ -104,42 +92,33 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
|
||||
// bool set(size_t index, long value);
|
||||
// bool set(size_t index, int value);
|
||||
// bool set(size_t index, short value);
|
||||
template <typename T>
|
||||
bool set(
|
||||
size_t index, T value,
|
||||
typename TypeTraits::EnableIf<
|
||||
CanSet<T>::value && !TypeTraits::IsReference<T>::value>::type * = 0) {
|
||||
return setNodeAt<T>(index, value);
|
||||
}
|
||||
// bool set(size_t index, const std::string&)
|
||||
// bool set(size_t index, const String&)
|
||||
// bool set(size_t index, const JsonVariant&);
|
||||
// bool set(size_t index, JsonArray&);
|
||||
// bool set(size_t index, JsonObject&);
|
||||
template <typename T>
|
||||
bool set(size_t index, const T &value,
|
||||
typename TypeTraits::EnableIf<CanSet<T &>::value>::type * = 0) {
|
||||
return setNodeAt<T &>(index, const_cast<T &>(value));
|
||||
bool set(size_t index, const T &value) {
|
||||
// reduce the number of template function instanciation to reduce code size
|
||||
return setNodeAt<typename TypeTraits::ConstRefOrConstPtr<T>::type>(index,
|
||||
value);
|
||||
}
|
||||
// bool set(size_t index, float value, uint8_t decimals = 2);
|
||||
// bool set(size_t index, double value, uint8_t decimals = 2);
|
||||
template <typename T>
|
||||
bool set(size_t index, T value, uint8_t decimals,
|
||||
typename TypeTraits::EnableIf<
|
||||
TypeTraits::IsFloatingPoint<T>::value>::type * = 0) {
|
||||
return setNodeAt<const JsonVariant &>(index, JsonVariant(value, decimals));
|
||||
}
|
||||
|
||||
// Gets the value at the specified index.
|
||||
JsonVariant get(size_t index) const {
|
||||
node_type *node = getNodeAt(index);
|
||||
return node ? node->content : JsonVariant();
|
||||
typename TypeTraits::EnableIf<TypeTraits::IsFloatingPoint<T>::value,
|
||||
bool>::type
|
||||
set(size_t index, T value, uint8_t decimals) {
|
||||
return set(index, JsonVariant(value, decimals));
|
||||
}
|
||||
|
||||
// Gets the value at the specified index.
|
||||
template <typename T>
|
||||
typename Internals::JsonVariantAs<T>::type get(size_t index) const {
|
||||
node_type *node = getNodeAt(index);
|
||||
return node ? node->content.as<T>() : JsonVariant::defaultValue<T>();
|
||||
return node ? node->content.as<T>()
|
||||
: Internals::JsonVariantDefault<T>::get();
|
||||
;
|
||||
}
|
||||
|
||||
// Check the type of the value at specified index.
|
||||
@ -170,26 +149,9 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
|
||||
return instance;
|
||||
}
|
||||
|
||||
// Serialize the array to the specified JsonWriter.
|
||||
void writeTo(Internals::JsonWriter &writer) const {
|
||||
writer.beginArray();
|
||||
|
||||
const node_type *child = _firstNode;
|
||||
while (child) {
|
||||
child->content.writeTo(writer);
|
||||
|
||||
child = child->next;
|
||||
if (!child) break;
|
||||
|
||||
writer.writeComma();
|
||||
}
|
||||
|
||||
writer.endArray();
|
||||
}
|
||||
|
||||
// Imports a 1D array
|
||||
template <typename T, size_t N>
|
||||
bool copyFrom(T(&array)[N]) {
|
||||
bool copyFrom(T (&array)[N]) {
|
||||
return copyFrom(array, N);
|
||||
}
|
||||
|
||||
@ -205,7 +167,7 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
|
||||
|
||||
// Imports a 2D array
|
||||
template <typename T, size_t N1, size_t N2>
|
||||
bool copyFrom(T(&array)[N1][N2]) {
|
||||
bool copyFrom(T (&array)[N1][N2]) {
|
||||
bool ok = true;
|
||||
for (size_t i = 0; i < N1; i++) {
|
||||
JsonArray &nestedArray = createNestedArray();
|
||||
@ -218,7 +180,7 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
|
||||
|
||||
// Exports a 1D array
|
||||
template <typename T, size_t N>
|
||||
size_t copyTo(T(&array)[N]) const {
|
||||
size_t copyTo(T (&array)[N]) const {
|
||||
return copyTo(array, N);
|
||||
}
|
||||
|
||||
@ -233,7 +195,7 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
|
||||
|
||||
// Exports a 2D array
|
||||
template <typename T, size_t N1, size_t N2>
|
||||
void copyTo(T(&array)[N1][N2]) const {
|
||||
void copyTo(T (&array)[N1][N2]) const {
|
||||
size_t i = 0;
|
||||
for (const_iterator it = begin(); it != end() && i < N1; ++it) {
|
||||
it->asArray().copyTo(array[i++]);
|
||||
@ -247,22 +209,31 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
|
||||
return node;
|
||||
}
|
||||
|
||||
template <typename TValue>
|
||||
bool setNodeAt(size_t index, TValue value) {
|
||||
template <typename TValueRef>
|
||||
bool setNodeAt(size_t index, TValueRef value) {
|
||||
node_type *node = getNodeAt(index);
|
||||
return node != NULL && setNodeValue<TValue>(node, value);
|
||||
if (!node) return false;
|
||||
|
||||
return Internals::ValueSetter<TValueRef>::set(_buffer, node->content,
|
||||
value);
|
||||
}
|
||||
|
||||
template <typename TValue>
|
||||
bool addNode(TValue value) {
|
||||
template <typename TValueRef>
|
||||
bool addNodeImpl(TValueRef value) {
|
||||
node_type *node = addNewNode();
|
||||
return node != NULL && setNodeValue<TValue>(node, value);
|
||||
}
|
||||
if (!node) return false;
|
||||
|
||||
template <typename T>
|
||||
bool setNodeValue(node_type *node, T value) {
|
||||
node->content = value;
|
||||
return true;
|
||||
return Internals::ValueSetter<TValueRef>::set(_buffer, node->content,
|
||||
value);
|
||||
}
|
||||
};
|
||||
|
||||
namespace Internals {
|
||||
template <>
|
||||
struct JsonVariantDefault<JsonArray> {
|
||||
static JsonArray &get() {
|
||||
return JsonArray::invalid();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -1,62 +0,0 @@
|
||||
// Copyright Benoit Blanchon 2014-2016
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "JsonArray.hpp"
|
||||
#include "JsonObject.hpp"
|
||||
#include "JsonArraySubscript.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
|
||||
template <>
|
||||
inline bool JsonArray::setNodeValue(node_type *node, String &value) {
|
||||
const char *copy = _buffer->strdup(value);
|
||||
if (!copy) return false;
|
||||
node->content = copy;
|
||||
return true;
|
||||
}
|
||||
|
||||
template <>
|
||||
inline JsonArray &JsonVariant::defaultValue<JsonArray>() {
|
||||
return JsonArray::invalid();
|
||||
}
|
||||
|
||||
template <>
|
||||
inline JsonArray &JsonVariant::defaultValue<JsonArray &>() {
|
||||
return JsonArray::invalid();
|
||||
}
|
||||
|
||||
template <>
|
||||
inline const JsonArray &JsonVariant::defaultValue<const JsonArray>() {
|
||||
return JsonArray::invalid();
|
||||
}
|
||||
|
||||
template <>
|
||||
inline const JsonArray &JsonVariant::defaultValue<const JsonArray &>() {
|
||||
return JsonArray::invalid();
|
||||
}
|
||||
|
||||
inline JsonArray &JsonVariant::asArray() const {
|
||||
if (_type == Internals::JSON_ARRAY) return *_content.asArray;
|
||||
return JsonArray::invalid();
|
||||
}
|
||||
|
||||
inline JsonArray &JsonArray::createNestedArray() {
|
||||
if (!_buffer) return JsonArray::invalid();
|
||||
JsonArray &array = _buffer->createArray();
|
||||
add(array);
|
||||
return array;
|
||||
}
|
||||
|
||||
inline JsonArray &JsonObject::createNestedArray(JsonObjectKey key) {
|
||||
if (!_buffer) return JsonArray::invalid();
|
||||
JsonArray &array = _buffer->createArray();
|
||||
setNodeAt<const JsonVariant &>(key, array);
|
||||
return array;
|
||||
}
|
||||
}
|
29
include/ArduinoJson/JsonArrayImpl.hpp
Normal file
29
include/ArduinoJson/JsonArrayImpl.hpp
Normal file
@ -0,0 +1,29 @@
|
||||
// Copyright Benoit Blanchon 2014-2016
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "JsonArray.hpp"
|
||||
#include "JsonArraySubscript.hpp"
|
||||
#include "JsonObject.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
|
||||
inline JsonArray &JsonArray::createNestedArray() {
|
||||
if (!_buffer) return JsonArray::invalid();
|
||||
JsonArray &array = _buffer->createArray();
|
||||
add(array);
|
||||
return array;
|
||||
}
|
||||
|
||||
inline JsonObject &JsonArray::createNestedObject() {
|
||||
if (!_buffer) return JsonObject::invalid();
|
||||
JsonObject &object = _buffer->createObject();
|
||||
add(object);
|
||||
return object;
|
||||
}
|
||||
}
|
@ -21,24 +21,14 @@ class JsonArraySubscript : public JsonVariantBase<JsonArraySubscript> {
|
||||
FORCE_INLINE JsonArraySubscript(JsonArray& array, size_t index)
|
||||
: _array(array), _index(index) {}
|
||||
|
||||
JsonArraySubscript& operator=(const JsonArraySubscript& src) {
|
||||
_array.set<const JsonVariant&>(_index, src);
|
||||
FORCE_INLINE JsonArraySubscript& operator=(const JsonArraySubscript& src) {
|
||||
_array.set(_index, src);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename TypeTraits::EnableIf<JsonArray::CanSet<T&>::value,
|
||||
JsonArraySubscript>::type&
|
||||
operator=(const T& src) {
|
||||
_array.set<T&>(_index, const_cast<T&>(src));
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename TypeTraits::EnableIf<JsonArray::CanSet<T>::value,
|
||||
JsonArraySubscript>::type&
|
||||
operator=(T src) {
|
||||
_array.set<T>(_index, src);
|
||||
FORCE_INLINE JsonArraySubscript& operator=(const T& src) {
|
||||
_array.set(_index, src);
|
||||
return *this;
|
||||
}
|
||||
|
||||
@ -46,10 +36,6 @@ class JsonArraySubscript : public JsonVariantBase<JsonArraySubscript> {
|
||||
return _index < _array.size();
|
||||
}
|
||||
|
||||
FORCE_INLINE operator JsonVariant() const {
|
||||
return _array.get(_index);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
FORCE_INLINE typename Internals::JsonVariantAs<T>::type as() const {
|
||||
return _array.get<T>(_index);
|
||||
@ -60,12 +46,8 @@ class JsonArraySubscript : public JsonVariantBase<JsonArraySubscript> {
|
||||
return _array.is<T>(_index);
|
||||
}
|
||||
|
||||
void writeTo(Internals::JsonWriter& writer) const {
|
||||
_array.get(_index).writeTo(writer);
|
||||
}
|
||||
|
||||
template <typename TValue>
|
||||
void set(TValue value) {
|
||||
FORCE_INLINE void set(const TValue& value) {
|
||||
_array.set(_index, value);
|
||||
}
|
||||
|
||||
@ -85,6 +67,11 @@ inline JsonArraySubscript JsonArray::operator[](size_t index) {
|
||||
return JsonArraySubscript(*this, index);
|
||||
}
|
||||
|
||||
template <typename TImplem>
|
||||
inline JsonArraySubscript JsonVariantBase<TImplem>::operator[](int index) {
|
||||
return asArray()[index];
|
||||
}
|
||||
|
||||
template <typename TImplem>
|
||||
inline const JsonArraySubscript JsonVariantBase<TImplem>::operator[](
|
||||
int index) const {
|
||||
|
@ -12,7 +12,6 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "JsonVariant.hpp"
|
||||
#include "String.hpp"
|
||||
|
||||
#if defined(__clang__)
|
||||
#pragma clang diagnostic push
|
||||
@ -52,79 +51,18 @@ class JsonBuffer {
|
||||
// allocation fails.
|
||||
JsonObject &createObject();
|
||||
|
||||
// Allocates and populate a JsonArray from a JSON string.
|
||||
//
|
||||
// The First argument is a pointer to the JSON string, the memory must be
|
||||
// writable
|
||||
// because the parser will insert null-terminators and replace escaped chars.
|
||||
//
|
||||
// The second argument set the nesting limit (see comment on DEFAULT_LIMIT)
|
||||
//
|
||||
// Returns a reference to the new JsonObject or JsonObject::invalid() if the
|
||||
// allocation fails.
|
||||
JsonArray &parseArray(char *json, uint8_t nestingLimit = DEFAULT_LIMIT);
|
||||
|
||||
// Same with a const char*.
|
||||
// With this overload, the JsonBuffer will make a copy of the string
|
||||
JsonArray &parseArray(const char *json, uint8_t nesting = DEFAULT_LIMIT) {
|
||||
return parseArray(strdup(json), nesting);
|
||||
}
|
||||
|
||||
// Same as above with a String class
|
||||
JsonArray &parseArray(const String &json, uint8_t nesting = DEFAULT_LIMIT) {
|
||||
return parseArray(json.c_str(), nesting);
|
||||
}
|
||||
|
||||
// Allocates and populate a JsonObject from a JSON string.
|
||||
//
|
||||
// The First argument is a pointer to the JSON string, the memory must be
|
||||
// writable
|
||||
// because the parser will insert null-terminators and replace escaped chars.
|
||||
//
|
||||
// The second argument set the nesting limit (see comment on DEFAULT_LIMIT)
|
||||
//
|
||||
// Returns a reference to the new JsonObject or JsonObject::invalid() if the
|
||||
// allocation fails.
|
||||
JsonObject &parseObject(char *json, uint8_t nestingLimit = DEFAULT_LIMIT);
|
||||
|
||||
// Same with a const char*.
|
||||
// With this overload, the JsonBuffer will make a copy of the string
|
||||
JsonObject &parseObject(const char *json, uint8_t nesting = DEFAULT_LIMIT) {
|
||||
return parseObject(strdup(json), nesting);
|
||||
}
|
||||
|
||||
// Same as above with a String class
|
||||
JsonObject &parseObject(const String &json, uint8_t nesting = DEFAULT_LIMIT) {
|
||||
return parseObject(json.c_str(), nesting);
|
||||
}
|
||||
|
||||
// Generalized version of parseArray() and parseObject(), also works for
|
||||
// integral types.
|
||||
JsonVariant parse(char *json, uint8_t nestingLimit = DEFAULT_LIMIT);
|
||||
|
||||
// Same with a const char*.
|
||||
// With this overload, the JsonBuffer will make a copy of the string
|
||||
JsonVariant parse(const char *json, uint8_t nesting = DEFAULT_LIMIT) {
|
||||
return parse(strdup(json), nesting);
|
||||
}
|
||||
|
||||
// Same as above with a String class
|
||||
JsonVariant parse(const String &json, uint8_t nesting = DEFAULT_LIMIT) {
|
||||
return parse(json.c_str(), nesting);
|
||||
}
|
||||
|
||||
// Duplicate a string
|
||||
char *strdup(const char *src) {
|
||||
return src ? strdup(src, strlen(src)) : NULL;
|
||||
template <typename TString>
|
||||
char *strdup(const TString &src) {
|
||||
return Internals::StringFuncs<TString>::duplicate(src, this);
|
||||
}
|
||||
char *strdup(const String &src) { return strdup(src.c_str(), src.length()); }
|
||||
|
||||
// Allocates n bytes in the JsonBuffer.
|
||||
// Return a pointer to the allocated memory or NULL if allocation fails.
|
||||
virtual void *alloc(size_t size) = 0;
|
||||
|
||||
protected:
|
||||
// Preserve aligment if nessary
|
||||
// Preserve aligment if necessary
|
||||
static FORCE_INLINE size_t round_size_up(size_t bytes) {
|
||||
#if ARDUINOJSON_ENABLE_ALIGNMENT
|
||||
const size_t x = sizeof(void *) - 1;
|
||||
@ -133,24 +71,6 @@ class JsonBuffer {
|
||||
return bytes;
|
||||
#endif
|
||||
}
|
||||
|
||||
private:
|
||||
char *strdup(const char *, size_t);
|
||||
|
||||
// Default value of nesting limit of parseArray() and parseObject().
|
||||
//
|
||||
// The nesting limit is a contain on the level of nesting allowed in the
|
||||
// JSON
|
||||
// string.
|
||||
// If set to 0, only a flat array or objects can be parsed.
|
||||
// If set to 1, the object can contain nested arrays or objects but only 1
|
||||
// level deep.
|
||||
// And bigger values will allow more level of nesting.
|
||||
//
|
||||
// The purpose of this feature is to prevent stack overflow that could
|
||||
// lead to
|
||||
// a security risk.
|
||||
static const uint8_t DEFAULT_LIMIT = 10;
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -1,46 +0,0 @@
|
||||
// Copyright Benoit Blanchon 2014-2016
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Internals/JsonParser.hpp"
|
||||
|
||||
inline ArduinoJson::JsonArray &ArduinoJson::JsonBuffer::createArray() {
|
||||
JsonArray *ptr = new (this) JsonArray(this);
|
||||
return ptr ? *ptr : JsonArray::invalid();
|
||||
}
|
||||
|
||||
inline ArduinoJson::JsonObject &ArduinoJson::JsonBuffer::createObject() {
|
||||
JsonObject *ptr = new (this) JsonObject(this);
|
||||
return ptr ? *ptr : JsonObject::invalid();
|
||||
}
|
||||
|
||||
inline ArduinoJson::JsonArray &ArduinoJson::JsonBuffer::parseArray(
|
||||
char *json, uint8_t nestingLimit) {
|
||||
Internals::JsonParser parser(this, json, nestingLimit);
|
||||
return parser.parseArray();
|
||||
}
|
||||
|
||||
inline ArduinoJson::JsonObject &ArduinoJson::JsonBuffer::parseObject(
|
||||
char *json, uint8_t nestingLimit) {
|
||||
Internals::JsonParser parser(this, json, nestingLimit);
|
||||
return parser.parseObject();
|
||||
}
|
||||
|
||||
inline ArduinoJson::JsonVariant ArduinoJson::JsonBuffer::parse(
|
||||
char *json, uint8_t nestingLimit) {
|
||||
Internals::JsonParser parser(this, json, nestingLimit);
|
||||
return parser.parseVariant();
|
||||
}
|
||||
|
||||
inline char *ArduinoJson::JsonBuffer::strdup(const char *source,
|
||||
size_t length) {
|
||||
size_t size = length + 1;
|
||||
char *dest = static_cast<char *>(alloc(size));
|
||||
if (dest != NULL) memcpy(dest, source, size);
|
||||
return dest;
|
||||
}
|
97
include/ArduinoJson/JsonBufferBase.hpp
Normal file
97
include/ArduinoJson/JsonBufferBase.hpp
Normal file
@ -0,0 +1,97 @@
|
||||
// Copyright Benoit Blanchon 2014-2016
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Deserialization/JsonParser.hpp"
|
||||
|
||||
#if defined(__clang__)
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wnon-virtual-dtor"
|
||||
#elif defined(__GNUC__)
|
||||
#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
|
||||
#pragma GCC diagnostic push
|
||||
#endif
|
||||
#pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
|
||||
#endif
|
||||
|
||||
namespace ArduinoJson {
|
||||
template <typename TDerived>
|
||||
class JsonBufferBase : public JsonBuffer {
|
||||
public:
|
||||
// Allocates and populate a JsonArray from a JSON string.
|
||||
//
|
||||
// The First argument is a pointer to the JSON string, the memory must be
|
||||
// writable
|
||||
// because the parser will insert null-terminators and replace escaped chars.
|
||||
//
|
||||
// The second argument set the nesting limit
|
||||
//
|
||||
// Returns a reference to the new JsonObject or JsonObject::invalid() if the
|
||||
// allocation fails.
|
||||
// With this overload, the JsonBuffer will make a copy of the string
|
||||
template <typename TString>
|
||||
JsonArray &parseArray(
|
||||
const TString &json,
|
||||
uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) {
|
||||
return Internals::makeParser(that(), json, nestingLimit).parseArray();
|
||||
}
|
||||
template <typename TString>
|
||||
JsonArray &parseArray(
|
||||
TString &json, uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) {
|
||||
return Internals::makeParser(that(), json, nestingLimit).parseArray();
|
||||
}
|
||||
|
||||
// Allocates and populate a JsonObject from a JSON string.
|
||||
//
|
||||
// The First argument is a pointer to the JSON string, the memory must be
|
||||
// writable
|
||||
// because the parser will insert null-terminators and replace escaped chars.
|
||||
//
|
||||
// The second argument set the nesting limit
|
||||
//
|
||||
// Returns a reference to the new JsonObject or JsonObject::invalid() if the
|
||||
// allocation fails.
|
||||
template <typename TString>
|
||||
JsonObject &parseObject(
|
||||
const TString &json,
|
||||
uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) {
|
||||
return Internals::makeParser(that(), json, nestingLimit).parseObject();
|
||||
}
|
||||
template <typename TString>
|
||||
JsonObject &parseObject(
|
||||
TString &json, uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) {
|
||||
return Internals::makeParser(that(), json, nestingLimit).parseObject();
|
||||
}
|
||||
|
||||
// Generalized version of parseArray() and parseObject(), also works for
|
||||
// integral types.
|
||||
template <typename TString>
|
||||
JsonVariant parse(const TString &json,
|
||||
uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) {
|
||||
return Internals::makeParser(that(), json, nestingLimit).parseVariant();
|
||||
}
|
||||
template <typename TString>
|
||||
JsonVariant parse(TString &json,
|
||||
uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) {
|
||||
return Internals::makeParser(that(), json, nestingLimit).parseVariant();
|
||||
}
|
||||
|
||||
private:
|
||||
TDerived *that() {
|
||||
return static_cast<TDerived *>(this);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#if defined(__clang__)
|
||||
#pragma clang diagnostic pop
|
||||
#elif defined(__GNUC__)
|
||||
#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
#endif
|
20
include/ArduinoJson/JsonBufferImpl.hpp
Normal file
20
include/ArduinoJson/JsonBufferImpl.hpp
Normal file
@ -0,0 +1,20 @@
|
||||
// Copyright Benoit Blanchon 2014-2016
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Deserialization/JsonParser.hpp"
|
||||
|
||||
inline ArduinoJson::JsonArray &ArduinoJson::JsonBuffer::createArray() {
|
||||
JsonArray *ptr = new (this) JsonArray(this);
|
||||
return ptr ? *ptr : JsonArray::invalid();
|
||||
}
|
||||
|
||||
inline ArduinoJson::JsonObject &ArduinoJson::JsonBuffer::createObject() {
|
||||
JsonObject *ptr = new (this) JsonObject(this);
|
||||
return ptr ? *ptr : JsonObject::invalid();
|
||||
}
|
@ -7,15 +7,16 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "String.hpp"
|
||||
#include "Internals/JsonBufferAllocated.hpp"
|
||||
#include "Internals/JsonPrintable.hpp"
|
||||
#include "Internals/List.hpp"
|
||||
#include "Internals/ReferenceType.hpp"
|
||||
#include "Data/JsonBufferAllocated.hpp"
|
||||
#include "Data/List.hpp"
|
||||
#include "Data/ReferenceType.hpp"
|
||||
#include "Data/ValueSetter.hpp"
|
||||
#include "JsonPair.hpp"
|
||||
#include "Serialization/JsonPrintable.hpp"
|
||||
#include "StringTraits/StringTraits.hpp"
|
||||
#include "TypeTraits/ConstRefOrConstPtr.hpp"
|
||||
#include "TypeTraits/EnableIf.hpp"
|
||||
#include "TypeTraits/IsFloatingPoint.hpp"
|
||||
#include "TypeTraits/IsReference.hpp"
|
||||
#include "TypeTraits/IsSame.hpp"
|
||||
|
||||
// Returns the size (in bytes) of an object with n elements.
|
||||
@ -40,27 +41,19 @@ class JsonObject : public Internals::JsonPrintable<JsonObject>,
|
||||
public Internals::List<JsonPair>,
|
||||
public Internals::JsonBufferAllocated {
|
||||
public:
|
||||
// A meta-function that returns true if type T can be used in
|
||||
// JsonObject::set()
|
||||
template <typename T>
|
||||
struct CanSet {
|
||||
static const bool value = JsonVariant::IsConstructibleFrom<T>::value ||
|
||||
TypeTraits::IsSame<T, String&>::value ||
|
||||
TypeTraits::IsSame<T, const String&>::value;
|
||||
};
|
||||
|
||||
// Create an empty JsonArray attached to the specified JsonBuffer.
|
||||
// You should not use this constructor directly.
|
||||
// Instead, use JsonBuffer::createObject() or JsonBuffer.parseObject().
|
||||
explicit JsonObject(JsonBuffer* buffer) : Internals::List<JsonPair>(buffer) {}
|
||||
|
||||
// Gets or sets the value associated with the specified key.
|
||||
JsonObjectSubscript<const char*> operator[](const char* key);
|
||||
JsonObjectSubscript<const String&> operator[](const String& key);
|
||||
template <typename TString>
|
||||
JsonObjectSubscript<TString> operator[](const TString& key);
|
||||
|
||||
// Gets the value associated with the specified key.
|
||||
JsonVariant operator[](JsonObjectKey key) const {
|
||||
return get(key);
|
||||
template <typename TString>
|
||||
JsonVariant operator[](const TString& key) const {
|
||||
return get<JsonVariant>(key);
|
||||
}
|
||||
|
||||
// Sets the specified key with the specified value.
|
||||
@ -73,67 +66,62 @@ class JsonObject : public Internals::JsonPrintable<JsonObject>,
|
||||
// bool set(TKey key, double value);
|
||||
// bool set(TKey key, const char* value);
|
||||
// bool set(TKey key, RawJson value);
|
||||
template <typename T>
|
||||
bool set(
|
||||
JsonObjectKey key, T value,
|
||||
typename TypeTraits::EnableIf<
|
||||
CanSet<T>::value && !TypeTraits::IsReference<T>::value>::type* = 0) {
|
||||
return setNodeAt<T>(key, value);
|
||||
}
|
||||
// bool set(Key, String&);
|
||||
// bool set(Key, JsonArray&);
|
||||
// bool set(Key, JsonObject&);
|
||||
// bool set(Key, JsonVariant&);
|
||||
template <typename T>
|
||||
bool set(JsonObjectKey key, const T& value,
|
||||
typename TypeTraits::EnableIf<CanSet<T&>::value>::type* = 0) {
|
||||
return setNodeAt<T&>(key, const_cast<T&>(value));
|
||||
template <typename TValue, typename TString>
|
||||
bool set(const TString& key, const TValue& value) {
|
||||
// reduce the number of template function instanciation to reduce code size
|
||||
return setNodeAt<typename TypeTraits::ConstRefOrConstPtr<TString>::type,
|
||||
typename TypeTraits::ConstRefOrConstPtr<TValue>::type>(
|
||||
key, value);
|
||||
}
|
||||
// bool set(Key, float value, uint8_t decimals);
|
||||
// bool set(Key, double value, uint8_t decimals);
|
||||
template <typename TValue>
|
||||
bool set(JsonObjectKey key, TValue value, uint8_t decimals,
|
||||
typename TypeTraits::EnableIf<
|
||||
TypeTraits::IsFloatingPoint<TValue>::value>::type* = 0) {
|
||||
return setNodeAt<const JsonVariant&>(key, JsonVariant(value, decimals));
|
||||
template <typename TValue, typename TString>
|
||||
typename TypeTraits::EnableIf<TypeTraits::IsFloatingPoint<TValue>::value,
|
||||
bool>::type
|
||||
set(const TString& key, TValue value, uint8_t decimals) {
|
||||
return set(key, JsonVariant(value, decimals));
|
||||
}
|
||||
|
||||
// Gets the value associated with the specified key.
|
||||
JsonVariant get(JsonObjectKey key) const {
|
||||
node_type* node = getNodeAt(key.c_str());
|
||||
return node ? node->content.value : JsonVariant();
|
||||
}
|
||||
|
||||
// Gets the value associated with the specified key.
|
||||
template <typename T>
|
||||
typename Internals::JsonVariantAs<T>::type get(JsonObjectKey key) const {
|
||||
node_type* node = getNodeAt(key.c_str());
|
||||
return node ? node->content.value.as<T>() : JsonVariant::defaultValue<T>();
|
||||
template <typename TValue, typename TString>
|
||||
typename Internals::JsonVariantAs<TValue>::type get(
|
||||
const TString& key) const {
|
||||
node_type* node = getNodeAt(key);
|
||||
return node ? node->content.value.as<TValue>()
|
||||
: Internals::JsonVariantDefault<TValue>::get();
|
||||
}
|
||||
|
||||
// Checks the type of the value associated with the specified key.
|
||||
template <typename T>
|
||||
bool is(JsonObjectKey key) const {
|
||||
node_type* node = getNodeAt(key.c_str());
|
||||
return node ? node->content.value.is<T>() : false;
|
||||
template <typename TValue, typename TString>
|
||||
bool is(const TString& key) const {
|
||||
node_type* node = getNodeAt(key);
|
||||
return node ? node->content.value.is<TValue>() : false;
|
||||
}
|
||||
|
||||
// Creates and adds a JsonArray.
|
||||
// This is a shortcut for JsonBuffer::createArray() and JsonObject::add().
|
||||
JsonArray& createNestedArray(JsonObjectKey key);
|
||||
template <typename TString>
|
||||
JsonArray& createNestedArray(const TString& key);
|
||||
|
||||
// Creates and adds a JsonObject.
|
||||
// This is a shortcut for JsonBuffer::createObject() and JsonObject::add().
|
||||
JsonObject& createNestedObject(JsonObjectKey key);
|
||||
template <typename TString>
|
||||
JsonObject& createNestedObject(const TString& key);
|
||||
|
||||
// Tells weither the specified key is present and associated with a value.
|
||||
bool containsKey(JsonObjectKey key) const {
|
||||
return getNodeAt(key.c_str()) != NULL;
|
||||
template <typename TString>
|
||||
bool containsKey(const TString& key) const {
|
||||
return getNodeAt(key) != NULL;
|
||||
}
|
||||
|
||||
// Removes the specified key and the associated value.
|
||||
void remove(JsonObjectKey key) {
|
||||
removeNode(getNodeAt(key.c_str()));
|
||||
template <typename TString>
|
||||
void remove(const TString& key) {
|
||||
removeNode(getNodeAt(key));
|
||||
}
|
||||
|
||||
// Returns a reference an invalid JsonObject.
|
||||
@ -144,58 +132,46 @@ class JsonObject : public Internals::JsonPrintable<JsonObject>,
|
||||
return instance;
|
||||
}
|
||||
|
||||
// Serialize the object to the specified JsonWriter
|
||||
void writeTo(Internals::JsonWriter& writer) const {
|
||||
writer.beginObject();
|
||||
|
||||
const node_type* node = _firstNode;
|
||||
while (node) {
|
||||
writer.writeString(node->content.key);
|
||||
writer.writeColon();
|
||||
node->content.value.writeTo(writer);
|
||||
|
||||
node = node->next;
|
||||
if (!node) break;
|
||||
|
||||
writer.writeComma();
|
||||
}
|
||||
|
||||
writer.endObject();
|
||||
}
|
||||
|
||||
private:
|
||||
// Returns the list node that matches the specified key.
|
||||
node_type* getNodeAt(const char* key) const {
|
||||
template <typename TString>
|
||||
node_type* getNodeAt(const TString& key) const {
|
||||
// reduce the number of template function instanciation to reduce code size
|
||||
return getNodeAtImpl<
|
||||
typename TypeTraits::ConstRefOrConstPtr<TString>::type>(key);
|
||||
}
|
||||
|
||||
template <typename TStringRef>
|
||||
node_type* getNodeAtImpl(TStringRef key) const {
|
||||
for (node_type* node = _firstNode; node; node = node->next) {
|
||||
if (!strcmp(node->content.key, key)) return node;
|
||||
if (Internals::StringFuncs<TStringRef>::equals(key, node->content.key))
|
||||
return node;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool setNodeAt(JsonObjectKey key, T value) {
|
||||
node_type* node = getNodeAt(key.c_str());
|
||||
template <typename TStringRef, typename TValueRef>
|
||||
bool setNodeAt(TStringRef key, TValueRef value) {
|
||||
node_type* node = getNodeAtImpl<TStringRef>(key);
|
||||
if (!node) {
|
||||
node = addNewNode();
|
||||
if (!node || !setNodeKey(node, key)) return false;
|
||||
}
|
||||
return setNodeValue<T>(node, value);
|
||||
}
|
||||
if (!node) return false;
|
||||
|
||||
bool setNodeKey(node_type* node, JsonObjectKey key) {
|
||||
if (key.needs_copy()) {
|
||||
node->content.key = _buffer->strdup(key.c_str());
|
||||
if (node->content.key == NULL) return false;
|
||||
} else {
|
||||
node->content.key = key.c_str();
|
||||
bool key_ok = Internals::ValueSetter<TStringRef>::set(
|
||||
_buffer, node->content.key, key);
|
||||
if (!key_ok) return false;
|
||||
}
|
||||
return true;
|
||||
return Internals::ValueSetter<TValueRef>::set(_buffer, node->content.value,
|
||||
value);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
bool setNodeValue(node_type* node, T value) {
|
||||
node->content.value = value;
|
||||
return true;
|
||||
namespace Internals {
|
||||
template <>
|
||||
struct JsonVariantDefault<JsonObject> {
|
||||
static JsonObject& get() {
|
||||
return JsonObject::invalid();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -1,66 +0,0 @@
|
||||
// Copyright Benoit Blanchon 2014-2016
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "JsonArray.hpp"
|
||||
#include "JsonObject.hpp"
|
||||
#include "JsonObjectSubscript.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
|
||||
template <>
|
||||
inline bool JsonObject::setNodeValue(node_type *node, String &value) {
|
||||
node->content.value = _buffer->strdup(value);
|
||||
return node->content.value;
|
||||
}
|
||||
|
||||
template <>
|
||||
inline bool JsonObject::setNodeValue(node_type *node, const String &value) {
|
||||
node->content.value = _buffer->strdup(value);
|
||||
return node->content.value;
|
||||
}
|
||||
|
||||
template <>
|
||||
inline const JsonObject &JsonVariant::defaultValue<const JsonObject &>() {
|
||||
return JsonObject::invalid();
|
||||
}
|
||||
|
||||
template <>
|
||||
inline const JsonObject &JsonVariant::defaultValue<const JsonObject>() {
|
||||
return JsonObject::invalid();
|
||||
}
|
||||
|
||||
template <>
|
||||
inline JsonObject &JsonVariant::defaultValue<JsonObject &>() {
|
||||
return JsonObject::invalid();
|
||||
}
|
||||
|
||||
template <>
|
||||
inline JsonObject &JsonVariant::defaultValue<JsonObject>() {
|
||||
return JsonObject::invalid();
|
||||
}
|
||||
|
||||
inline JsonObject &JsonVariant::asObject() const {
|
||||
if (_type == Internals::JSON_OBJECT) return *_content.asObject;
|
||||
return JsonObject::invalid();
|
||||
}
|
||||
|
||||
inline JsonObject &JsonObject::createNestedObject(JsonObjectKey key) {
|
||||
if (!_buffer) return JsonObject::invalid();
|
||||
JsonObject &array = _buffer->createObject();
|
||||
setNodeAt<const JsonVariant &>(key, array);
|
||||
return array;
|
||||
}
|
||||
|
||||
inline JsonObject &JsonArray::createNestedObject() {
|
||||
if (!_buffer) return JsonObject::invalid();
|
||||
JsonObject &object = _buffer->createObject();
|
||||
add(object);
|
||||
return object;
|
||||
}
|
||||
}
|
31
include/ArduinoJson/JsonObjectImpl.hpp
Normal file
31
include/ArduinoJson/JsonObjectImpl.hpp
Normal file
@ -0,0 +1,31 @@
|
||||
// Copyright Benoit Blanchon 2014-2016
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "JsonArray.hpp"
|
||||
#include "JsonObject.hpp"
|
||||
#include "JsonObjectSubscript.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
|
||||
template <typename TString>
|
||||
inline JsonObject &JsonObject::createNestedObject(const TString &key) {
|
||||
if (!_buffer) return JsonObject::invalid();
|
||||
JsonObject &object = _buffer->createObject();
|
||||
set(key, object);
|
||||
return object;
|
||||
}
|
||||
|
||||
template <typename TString>
|
||||
inline JsonArray &JsonObject::createNestedArray(const TString &key) {
|
||||
if (!_buffer) return JsonArray::invalid();
|
||||
JsonArray &array = _buffer->createArray();
|
||||
set(key, array);
|
||||
return array;
|
||||
}
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
// Copyright Benoit Blanchon 2014-2016
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "String.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
|
||||
// Represents a key in a JsonObject
|
||||
class JsonObjectKey {
|
||||
public:
|
||||
JsonObjectKey(const char* key) : _value(key), _needs_copy(false) {}
|
||||
JsonObjectKey(const String& key) : _value(key.c_str()), _needs_copy(true) {}
|
||||
|
||||
const char* c_str() const { return _value; }
|
||||
bool needs_copy() const { return _needs_copy; }
|
||||
|
||||
private:
|
||||
const char* _value;
|
||||
bool _needs_copy;
|
||||
};
|
||||
}
|
@ -9,6 +9,7 @@
|
||||
|
||||
#include "Configuration.hpp"
|
||||
#include "JsonVariantBase.hpp"
|
||||
#include "TypeTraits/ConstRefOrConstPtr.hpp"
|
||||
#include "TypeTraits/EnableIf.hpp"
|
||||
|
||||
#ifdef _MSC_VER
|
||||
@ -18,30 +19,27 @@
|
||||
|
||||
namespace ArduinoJson {
|
||||
|
||||
template <typename TKey>
|
||||
class JsonObjectSubscript : public JsonVariantBase<JsonObjectSubscript<TKey> > {
|
||||
template <typename TString>
|
||||
class JsonObjectSubscript
|
||||
: public JsonVariantBase<JsonObjectSubscript<TString> > {
|
||||
// const String&
|
||||
// const std::string&
|
||||
// const char*
|
||||
typedef typename TypeTraits::ConstRefOrConstPtr<TString>::type TStringRef;
|
||||
|
||||
public:
|
||||
FORCE_INLINE JsonObjectSubscript(JsonObject& object, TKey key)
|
||||
FORCE_INLINE JsonObjectSubscript(JsonObject& object, TStringRef key)
|
||||
: _object(object), _key(key) {}
|
||||
|
||||
JsonObjectSubscript<TKey>& operator=(const JsonObjectSubscript<TKey>& src) {
|
||||
_object.set<const JsonVariant&>(_key, src);
|
||||
FORCE_INLINE JsonObjectSubscript<TString>& operator=(
|
||||
const JsonObjectSubscript<TString>& src) {
|
||||
_object.set(_key, src);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename TypeTraits::EnableIf<JsonObject::CanSet<T&>::value,
|
||||
JsonObjectSubscript<TKey> >::type&
|
||||
operator=(const T& src) {
|
||||
_object.set<T&>(_key, const_cast<T&>(src));
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename TypeTraits::EnableIf<JsonObject::CanSet<T>::value,
|
||||
JsonObjectSubscript<TKey> >::type&
|
||||
operator=(T src) {
|
||||
_object.set<T>(_key, src);
|
||||
FORCE_INLINE JsonObjectSubscript<TString>& operator=(const T& src) {
|
||||
_object.set(_key, src);
|
||||
return *this;
|
||||
}
|
||||
|
||||
@ -49,13 +47,9 @@ class JsonObjectSubscript : public JsonVariantBase<JsonObjectSubscript<TKey> > {
|
||||
return _object.containsKey(_key);
|
||||
}
|
||||
|
||||
FORCE_INLINE operator JsonVariant() const {
|
||||
return _object.get(_key);
|
||||
}
|
||||
|
||||
template <typename TValue>
|
||||
FORCE_INLINE typename Internals::JsonVariantAs<TValue>::type as() const {
|
||||
return _object.get<TValue>(_key);
|
||||
return _object.get<TValue, TStringRef>(_key);
|
||||
}
|
||||
|
||||
template <typename TValue>
|
||||
@ -64,62 +58,32 @@ class JsonObjectSubscript : public JsonVariantBase<JsonObjectSubscript<TKey> > {
|
||||
}
|
||||
|
||||
template <typename TValue>
|
||||
FORCE_INLINE bool set(TValue value) {
|
||||
return _object.set<TValue>(_key, value);
|
||||
FORCE_INLINE bool set(const TValue& value) {
|
||||
return _object.set(_key, value);
|
||||
}
|
||||
|
||||
template <typename TValue>
|
||||
FORCE_INLINE bool set(TValue value, uint8_t decimals) {
|
||||
FORCE_INLINE bool set(const TValue& value, uint8_t decimals) {
|
||||
return _object.set(_key, value, decimals);
|
||||
}
|
||||
|
||||
FORCE_INLINE JsonVariant get() {
|
||||
return _object.get(_key);
|
||||
}
|
||||
|
||||
void writeTo(Internals::JsonWriter& writer) const {
|
||||
_object.get(_key).writeTo(writer);
|
||||
}
|
||||
|
||||
private:
|
||||
JsonObject& _object;
|
||||
TKey _key;
|
||||
TStringRef _key;
|
||||
};
|
||||
|
||||
#if ARDUINOJSON_ENABLE_STD_STREAM
|
||||
inline std::ostream& operator<<(
|
||||
std::ostream& os, const JsonObjectSubscript<const String&>& source) {
|
||||
return source.printTo(os);
|
||||
}
|
||||
|
||||
inline std::ostream& operator<<(
|
||||
std::ostream& os, const JsonObjectSubscript<const char*>& source) {
|
||||
template <typename TString>
|
||||
inline std::ostream& operator<<(std::ostream& os,
|
||||
const JsonObjectSubscript<TString>& source) {
|
||||
return source.printTo(os);
|
||||
}
|
||||
#endif
|
||||
|
||||
inline JsonObjectSubscript<const char*> JsonObject::operator[](
|
||||
const char* key) {
|
||||
return JsonObjectSubscript<const char*>(*this, key);
|
||||
template <typename TString>
|
||||
inline JsonObjectSubscript<TString> JsonObject::operator[](const TString& key) {
|
||||
return JsonObjectSubscript<TString>(*this, key);
|
||||
}
|
||||
|
||||
inline JsonObjectSubscript<const String&> JsonObject::operator[](
|
||||
const String& key) {
|
||||
return JsonObjectSubscript<const String&>(*this, key);
|
||||
}
|
||||
|
||||
template <typename TImplem>
|
||||
inline const JsonObjectSubscript<const char*> JsonVariantBase<TImplem>::
|
||||
operator[](const char* key) const {
|
||||
return asObject()[key];
|
||||
}
|
||||
|
||||
template <typename TImplem>
|
||||
inline const JsonObjectSubscript<const String&> JsonVariantBase<TImplem>::
|
||||
operator[](const String& key) const {
|
||||
return asObject()[key];
|
||||
}
|
||||
|
||||
} // namespace ArduinoJson
|
||||
|
||||
#ifdef _MSC_VER
|
||||
|
@ -7,7 +7,6 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "JsonObjectKey.hpp"
|
||||
#include "JsonVariant.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
|
@ -10,15 +10,18 @@
|
||||
#include <stddef.h>
|
||||
#include <stdint.h> // for uint8_t
|
||||
|
||||
#include "Internals/JsonPrintable.hpp"
|
||||
#include "Internals/JsonVariantContent.hpp"
|
||||
#include "Internals/JsonVariantType.hpp"
|
||||
#include "Data/JsonVariantContent.hpp"
|
||||
#include "Data/JsonVariantDefault.hpp"
|
||||
#include "Data/JsonVariantType.hpp"
|
||||
#include "JsonVariantBase.hpp"
|
||||
#include "RawJson.hpp"
|
||||
#include "Serialization/JsonPrintable.hpp"
|
||||
#include "TypeTraits/EnableIf.hpp"
|
||||
#include "TypeTraits/IsFloatingPoint.hpp"
|
||||
#include "TypeTraits/IsIntegral.hpp"
|
||||
#include "TypeTraits/IsSame.hpp"
|
||||
#include "TypeTraits/IsSignedIntegral.hpp"
|
||||
#include "TypeTraits/IsUnsignedIntegral.hpp"
|
||||
#include "TypeTraits/RemoveConst.hpp"
|
||||
#include "TypeTraits/RemoveReference.hpp"
|
||||
|
||||
@ -36,10 +39,10 @@ class JsonObject;
|
||||
// - a string (const char*)
|
||||
// - a reference to a JsonArray or JsonObject
|
||||
class JsonVariant : public JsonVariantBase<JsonVariant> {
|
||||
public:
|
||||
template <typename T>
|
||||
struct IsConstructibleFrom;
|
||||
friend void Internals::JsonSerializer::serialize(const JsonVariant &,
|
||||
JsonWriter &);
|
||||
|
||||
public:
|
||||
// Creates an uninitialized JsonVariant
|
||||
JsonVariant() : _type(Internals::JSON_UNDEFINED) {}
|
||||
|
||||
@ -107,16 +110,14 @@ class JsonVariant : public JsonVariantBase<JsonVariant> {
|
||||
}
|
||||
|
||||
// Create a JsonVariant containing a reference to an array.
|
||||
JsonVariant(JsonArray &array) {
|
||||
_type = Internals::JSON_ARRAY;
|
||||
_content.asArray = &array;
|
||||
}
|
||||
// CAUTION: we are lying about constness, because the array can be modified if
|
||||
// the variant is converted back to a JsonArray&
|
||||
JsonVariant(const JsonArray &array);
|
||||
|
||||
// Create a JsonVariant containing a reference to an object.
|
||||
JsonVariant(JsonObject &object) {
|
||||
_type = Internals::JSON_OBJECT;
|
||||
_content.asObject = &object;
|
||||
}
|
||||
// CAUTION: we are lying about constness, because the object can be modified
|
||||
// if the variant is converted back to a JsonObject&
|
||||
JsonVariant(const JsonObject &object);
|
||||
|
||||
// Get the variant as the specified type.
|
||||
//
|
||||
@ -149,14 +150,6 @@ class JsonVariant : public JsonVariantBase<JsonVariant> {
|
||||
return static_cast<T>(asFloat());
|
||||
}
|
||||
//
|
||||
// const String as<String>() const;
|
||||
template <typename T>
|
||||
const typename TypeTraits::EnableIf<TypeTraits::IsSame<T, String>::value,
|
||||
T>::type
|
||||
as() const {
|
||||
return toString();
|
||||
}
|
||||
//
|
||||
// const char* as<const char*>() const;
|
||||
// const char* as<char*>() const;
|
||||
template <typename T>
|
||||
@ -167,6 +160,18 @@ class JsonVariant : public JsonVariantBase<JsonVariant> {
|
||||
return asString();
|
||||
}
|
||||
//
|
||||
// std::string as<std::string>() const;
|
||||
// String as<String>() const;
|
||||
template <typename T>
|
||||
typename TypeTraits::EnableIf<Internals::StringFuncs<T>::has_append, T>::type
|
||||
as() const {
|
||||
const char *cstr = asString();
|
||||
if (cstr) return T(cstr);
|
||||
T s;
|
||||
printTo(s);
|
||||
return s;
|
||||
}
|
||||
//
|
||||
// const bool as<bool>() const
|
||||
template <typename T>
|
||||
const typename TypeTraits::EnableIf<TypeTraits::IsSame<T, bool>::value,
|
||||
@ -217,6 +222,14 @@ class JsonVariant : public JsonVariantBase<JsonVariant> {
|
||||
as() const {
|
||||
return asObject();
|
||||
}
|
||||
//
|
||||
// JsonVariant as<JsonVariant> const;
|
||||
template <typename T>
|
||||
typename TypeTraits::EnableIf<TypeTraits::IsSame<T, JsonVariant>::value,
|
||||
T>::type
|
||||
as() const {
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Tells weither the variant has the specified type.
|
||||
// Returns true if the variant has type type T, false otherwise.
|
||||
@ -225,7 +238,8 @@ class JsonVariant : public JsonVariantBase<JsonVariant> {
|
||||
// int as<int>() const;
|
||||
// long as<long>() const;
|
||||
template <typename T>
|
||||
const typename TypeTraits::EnableIf<TypeTraits::IsIntegral<T>::value,
|
||||
const typename TypeTraits::EnableIf<TypeTraits::IsIntegral<T>::value &&
|
||||
!TypeTraits::IsSame<T, bool>::value,
|
||||
bool>::type
|
||||
is() const {
|
||||
return isInteger();
|
||||
@ -272,9 +286,9 @@ class JsonVariant : public JsonVariantBase<JsonVariant> {
|
||||
return isArray();
|
||||
}
|
||||
//
|
||||
// JsonObject& as<JsonObject> const;
|
||||
// JsonObject& as<JsonObject&> const;
|
||||
// JsonObject& as<const JsonObject&> const;
|
||||
// bool is<JsonObject> const;
|
||||
// bool is<JsonObject&> const;
|
||||
// bool is<const JsonObject&> const;
|
||||
template <typename T>
|
||||
typename TypeTraits::EnableIf<
|
||||
TypeTraits::IsSame<
|
||||
@ -291,17 +305,13 @@ class JsonVariant : public JsonVariantBase<JsonVariant> {
|
||||
return _type != Internals::JSON_UNDEFINED;
|
||||
}
|
||||
|
||||
// Serialize the variant to a JsonWriter
|
||||
void writeTo(Internals::JsonWriter &writer) const;
|
||||
|
||||
// Value returned if the variant has an incompatible type
|
||||
template <typename T>
|
||||
static typename Internals::JsonVariantAs<T>::type defaultValue() {
|
||||
return T();
|
||||
}
|
||||
|
||||
// DEPRECATED: use as<char*>() instead
|
||||
const char *asString() const;
|
||||
|
||||
// DEPRECATED: use as<JsonArray>() instead
|
||||
JsonArray &asArray() const;
|
||||
|
||||
// DEPRECATED: use as<JsonObject>() instead
|
||||
JsonObject &asObject() const;
|
||||
|
||||
private:
|
||||
@ -310,7 +320,6 @@ class JsonVariant : public JsonVariantBase<JsonVariant> {
|
||||
JsonVariant(T value, typename TypeTraits::EnableIf<
|
||||
TypeTraits::IsSame<T, char>::value>::type * = 0);
|
||||
|
||||
String toString() const;
|
||||
Internals::JsonFloat asFloat() const;
|
||||
Internals::JsonInteger asInteger() const;
|
||||
Internals::JsonUInt asUnsignedInteger() const;
|
||||
@ -324,7 +333,9 @@ class JsonVariant : public JsonVariantBase<JsonVariant> {
|
||||
return _type == Internals::JSON_OBJECT;
|
||||
}
|
||||
bool isString() const {
|
||||
return _type == Internals::JSON_STRING;
|
||||
return _type == Internals::JSON_STRING ||
|
||||
(_type == Internals::JSON_UNPARSED && _content.asString &&
|
||||
!strcmp("null", _content.asString));
|
||||
}
|
||||
|
||||
// The current type of the variant
|
||||
@ -341,27 +352,4 @@ inline JsonVariant float_with_n_digits(float value, uint8_t digits) {
|
||||
inline JsonVariant double_with_n_digits(double value, uint8_t digits) {
|
||||
return JsonVariant(value, digits);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
struct JsonVariant::IsConstructibleFrom {
|
||||
static const bool value =
|
||||
TypeTraits::IsIntegral<T>::value ||
|
||||
TypeTraits::IsFloatingPoint<T>::value ||
|
||||
TypeTraits::IsSame<T, bool>::value ||
|
||||
TypeTraits::IsSame<T, char *>::value ||
|
||||
TypeTraits::IsSame<T, const char *>::value ||
|
||||
TypeTraits::IsSame<T, RawJson>::value ||
|
||||
TypeTraits::IsSame<T, JsonArray &>::value ||
|
||||
TypeTraits::IsSame<T, const JsonArray &>::value ||
|
||||
TypeTraits::IsSame<T, JsonArraySubscript &>::value ||
|
||||
TypeTraits::IsSame<T, const JsonArraySubscript &>::value ||
|
||||
TypeTraits::IsSame<T, JsonObject &>::value ||
|
||||
TypeTraits::IsSame<T, const JsonObject &>::value ||
|
||||
TypeTraits::IsSame<T, JsonObjectSubscript<const char *> &>::value ||
|
||||
TypeTraits::IsSame<T, const JsonObjectSubscript<const char *> &>::value ||
|
||||
TypeTraits::IsSame<T, JsonObjectSubscript<String> &>::value ||
|
||||
TypeTraits::IsSame<T, const JsonObjectSubscript<String> &>::value ||
|
||||
TypeTraits::IsSame<T, JsonVariant &>::value ||
|
||||
TypeTraits::IsSame<T, const JsonVariant &>::value;
|
||||
};
|
||||
}
|
||||
|
@ -7,9 +7,9 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Internals/JsonVariantAs.hpp"
|
||||
#include "JsonObjectKey.hpp"
|
||||
#include "Data/JsonVariantAs.hpp"
|
||||
#include "Polyfills/attributes.hpp"
|
||||
#include "Serialization/JsonPrintable.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
|
||||
@ -72,82 +72,30 @@ class JsonVariantBase : public Internals::JsonPrintable<TImpl> {
|
||||
// Returns the element at specified index if the variant is an array.
|
||||
// Returns JsonVariant::invalid() if the variant is not an array.
|
||||
FORCE_INLINE const JsonArraySubscript operator[](int index) const;
|
||||
FORCE_INLINE JsonArraySubscript operator[](int index);
|
||||
|
||||
// Mimics an object.
|
||||
// Returns the value associated with the specified key if the variant is
|
||||
// an object.
|
||||
// Return JsonVariant::invalid() if the variant is not an object.
|
||||
FORCE_INLINE const JsonObjectSubscript<const char *> operator[](
|
||||
const char *key) const;
|
||||
FORCE_INLINE const JsonObjectSubscript<const String &> operator[](
|
||||
const String &key) const;
|
||||
|
||||
// Serialize the variant to a JsonWriter
|
||||
void writeTo(Internals::JsonWriter &writer) const;
|
||||
template <typename TString>
|
||||
FORCE_INLINE
|
||||
typename TypeTraits::EnableIf<Internals::StringFuncs<TString>::has_equals,
|
||||
const JsonObjectSubscript<TString> >::type
|
||||
operator[](const TString &key) const {
|
||||
return asObject()[key];
|
||||
}
|
||||
template <typename TString>
|
||||
FORCE_INLINE
|
||||
typename TypeTraits::EnableIf<Internals::StringFuncs<TString>::has_equals,
|
||||
JsonObjectSubscript<TString> >::type
|
||||
operator[](const TString &key) {
|
||||
return asObject()[key];
|
||||
}
|
||||
|
||||
private:
|
||||
const TImpl *impl() const {
|
||||
return static_cast<const TImpl *>(this);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename TImpl, typename TComparand>
|
||||
inline bool operator==(const JsonVariantBase<TImpl> &left, TComparand right) {
|
||||
return left.template as<TComparand>() == right;
|
||||
}
|
||||
|
||||
template <typename TImpl, typename TComparand>
|
||||
inline bool operator==(TComparand left, const JsonVariantBase<TImpl> &right) {
|
||||
return left == right.template as<TComparand>();
|
||||
}
|
||||
|
||||
template <typename TImpl, typename TComparand>
|
||||
inline bool operator!=(const JsonVariantBase<TImpl> &left, TComparand right) {
|
||||
return left.template as<TComparand>() != right;
|
||||
}
|
||||
|
||||
template <typename TImpl, typename TComparand>
|
||||
inline bool operator!=(TComparand left, const JsonVariantBase<TImpl> &right) {
|
||||
return left != right.template as<TComparand>();
|
||||
}
|
||||
|
||||
template <typename TImpl, typename TComparand>
|
||||
inline bool operator<=(const JsonVariantBase<TImpl> &left, TComparand right) {
|
||||
return left.template as<TComparand>() <= right;
|
||||
}
|
||||
|
||||
template <typename TImpl, typename TComparand>
|
||||
inline bool operator<=(TComparand left, const JsonVariantBase<TImpl> &right) {
|
||||
return left <= right.template as<TComparand>();
|
||||
}
|
||||
|
||||
template <typename TImpl, typename TComparand>
|
||||
inline bool operator>=(const JsonVariantBase<TImpl> &left, TComparand right) {
|
||||
return left.template as<TComparand>() >= right;
|
||||
}
|
||||
|
||||
template <typename TImpl, typename TComparand>
|
||||
inline bool operator>=(TComparand left, const JsonVariantBase<TImpl> &right) {
|
||||
return left >= right.template as<TComparand>();
|
||||
}
|
||||
|
||||
template <typename TImpl, typename TComparand>
|
||||
inline bool operator<(const JsonVariantBase<TImpl> &left, TComparand right) {
|
||||
return left.template as<TComparand>() < right;
|
||||
}
|
||||
|
||||
template <typename TImpl, typename TComparand>
|
||||
inline bool operator<(TComparand left, const JsonVariantBase<TImpl> &right) {
|
||||
return left < right.template as<TComparand>();
|
||||
}
|
||||
|
||||
template <typename TImpl, typename TComparand>
|
||||
inline bool operator>(const JsonVariantBase<TImpl> &left, TComparand right) {
|
||||
return left.template as<TComparand>() > right;
|
||||
}
|
||||
|
||||
template <typename TImpl, typename TComparand>
|
||||
inline bool operator>(TComparand left, const JsonVariantBase<TImpl> &right) {
|
||||
return left > right.template as<TComparand>();
|
||||
}
|
||||
}
|
||||
|
106
include/ArduinoJson/JsonVariantComparisons.hpp
Normal file
106
include/ArduinoJson/JsonVariantComparisons.hpp
Normal file
@ -0,0 +1,106 @@
|
||||
// Copyright Benoit Blanchon 2014-2016
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "JsonVariantBase.hpp"
|
||||
#include "StringTraits/StringTraits.hpp"
|
||||
#include "TypeTraits/EnableIf.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
template <typename TVariant, typename TComparand, typename Enable = void>
|
||||
struct JsonVariantComparer {
|
||||
static bool equals(const TVariant &variant, const TComparand &comparand) {
|
||||
return variant.template as<TComparand>() == comparand;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename TVariant, typename TString>
|
||||
struct JsonVariantComparer<
|
||||
TVariant, TString, typename TypeTraits::EnableIf<
|
||||
Internals::StringFuncs<TString>::has_equals>::type> {
|
||||
static bool equals(const TVariant &variant, const TString &comparand) {
|
||||
const char *value = variant.template as<const char *>();
|
||||
return Internals::StringFuncs<TString>::equals(comparand, value);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename TImpl, typename TComparand>
|
||||
inline bool operator==(const JsonVariantBase<TImpl> &variant,
|
||||
TComparand comparand) {
|
||||
typedef JsonVariantBase<TImpl> TVariant;
|
||||
return JsonVariantComparer<TVariant, TComparand>::equals(variant, comparand);
|
||||
}
|
||||
|
||||
template <typename TImpl, typename TComparand>
|
||||
inline bool operator==(TComparand comparand,
|
||||
const JsonVariantBase<TImpl> &variant) {
|
||||
typedef JsonVariantBase<TImpl> TVariant;
|
||||
return JsonVariantComparer<TVariant, TComparand>::equals(variant, comparand);
|
||||
}
|
||||
|
||||
template <typename TImpl, typename TComparand>
|
||||
inline bool operator!=(const JsonVariantBase<TImpl> &variant,
|
||||
TComparand comparand) {
|
||||
typedef JsonVariantBase<TImpl> TVariant;
|
||||
return !JsonVariantComparer<TVariant, TComparand>::equals(variant, comparand);
|
||||
}
|
||||
|
||||
template <typename TImpl, typename TComparand>
|
||||
inline bool operator!=(TComparand comparand,
|
||||
const JsonVariantBase<TImpl> &variant) {
|
||||
typedef JsonVariantBase<TImpl> TVariant;
|
||||
return !JsonVariantComparer<TVariant, TComparand>::equals(variant, comparand);
|
||||
}
|
||||
|
||||
template <typename TImpl, typename TComparand>
|
||||
inline bool operator<=(const JsonVariantBase<TImpl> &left, TComparand right) {
|
||||
return left.template as<TComparand>() <= right;
|
||||
}
|
||||
|
||||
template <typename TImpl, typename TComparand>
|
||||
inline bool operator<=(TComparand comparand,
|
||||
const JsonVariantBase<TImpl> &variant) {
|
||||
return comparand <= variant.template as<TComparand>();
|
||||
}
|
||||
|
||||
template <typename TImpl, typename TComparand>
|
||||
inline bool operator>=(const JsonVariantBase<TImpl> &variant,
|
||||
TComparand comparand) {
|
||||
return variant.template as<TComparand>() >= comparand;
|
||||
}
|
||||
|
||||
template <typename TImpl, typename TComparand>
|
||||
inline bool operator>=(TComparand comparand,
|
||||
const JsonVariantBase<TImpl> &variant) {
|
||||
return comparand >= variant.template as<TComparand>();
|
||||
}
|
||||
|
||||
template <typename TImpl, typename TComparand>
|
||||
inline bool operator<(const JsonVariantBase<TImpl> &varian,
|
||||
TComparand comparand) {
|
||||
return varian.template as<TComparand>() < comparand;
|
||||
}
|
||||
|
||||
template <typename TImpl, typename TComparand>
|
||||
inline bool operator<(TComparand comparand,
|
||||
const JsonVariantBase<TImpl> &variant) {
|
||||
return comparand < variant.template as<TComparand>();
|
||||
}
|
||||
|
||||
template <typename TImpl, typename TComparand>
|
||||
inline bool operator>(const JsonVariantBase<TImpl> &variant,
|
||||
TComparand comparand) {
|
||||
return variant.template as<TComparand>() > comparand;
|
||||
}
|
||||
|
||||
template <typename TImpl, typename TComparand>
|
||||
inline bool operator>(TComparand comparand,
|
||||
const JsonVariantBase<TImpl> &variant) {
|
||||
return comparand > variant.template as<TComparand>();
|
||||
}
|
||||
}
|
@ -8,17 +8,45 @@
|
||||
#pragma once
|
||||
|
||||
#include "Configuration.hpp"
|
||||
#include "JsonVariant.hpp"
|
||||
#include "Internals/Parse.hpp"
|
||||
#include "Data/Parse.hpp"
|
||||
#include "JsonArray.hpp"
|
||||
#include "JsonObject.hpp"
|
||||
#include "JsonVariant.hpp"
|
||||
|
||||
#include <string.h> // for strcmp
|
||||
#include <errno.h> // for errno
|
||||
#include <stdlib.h> // for strtol, strtod
|
||||
#include <string.h> // for strcmp
|
||||
|
||||
namespace ArduinoJson {
|
||||
|
||||
inline JsonVariant::JsonVariant(const JsonArray &array) {
|
||||
if (array.success()) {
|
||||
_type = Internals::JSON_ARRAY;
|
||||
_content.asArray = const_cast<JsonArray *>(&array);
|
||||
} else {
|
||||
_type = Internals::JSON_UNDEFINED;
|
||||
}
|
||||
}
|
||||
|
||||
inline JsonVariant::JsonVariant(const JsonObject &object) {
|
||||
if (object.success()) {
|
||||
_type = Internals::JSON_OBJECT;
|
||||
_content.asObject = const_cast<JsonObject *>(&object);
|
||||
} else {
|
||||
_type = Internals::JSON_UNDEFINED;
|
||||
}
|
||||
}
|
||||
|
||||
inline JsonArray &JsonVariant::asArray() const {
|
||||
if (_type == Internals::JSON_ARRAY) return *_content.asArray;
|
||||
return JsonArray::invalid();
|
||||
}
|
||||
|
||||
inline JsonObject &JsonVariant::asObject() const {
|
||||
if (_type == Internals::JSON_OBJECT) return *_content.asObject;
|
||||
return JsonObject::invalid();
|
||||
}
|
||||
|
||||
inline Internals::JsonInteger JsonVariant::asInteger() const {
|
||||
using namespace Internals;
|
||||
switch (_type) {
|
||||
@ -85,17 +113,6 @@ inline Internals::JsonFloat JsonVariant::asFloat() const {
|
||||
}
|
||||
}
|
||||
|
||||
inline String JsonVariant::toString() const {
|
||||
using namespace Internals;
|
||||
String s;
|
||||
if ((_type == JSON_STRING || _type == JSON_UNPARSED) &&
|
||||
_content.asString != NULL)
|
||||
s = _content.asString;
|
||||
else
|
||||
printTo(s);
|
||||
return s;
|
||||
}
|
||||
|
||||
inline bool JsonVariant::isBoolean() const {
|
||||
using namespace Internals;
|
||||
if (_type == JSON_BOOLEAN) return true;
|
||||
@ -133,44 +150,6 @@ inline bool JsonVariant::isFloat() const {
|
||||
return *end == '\0' && errno == 0 && !is<long>();
|
||||
}
|
||||
|
||||
inline void JsonVariant::writeTo(Internals::JsonWriter &writer) const {
|
||||
using namespace Internals;
|
||||
switch (_type) {
|
||||
case JSON_UNDEFINED:
|
||||
return;
|
||||
|
||||
case JSON_ARRAY:
|
||||
_content.asArray->writeTo(writer);
|
||||
return;
|
||||
|
||||
case JSON_OBJECT:
|
||||
_content.asObject->writeTo(writer);
|
||||
return;
|
||||
|
||||
case JSON_STRING:
|
||||
writer.writeString(_content.asString);
|
||||
return;
|
||||
|
||||
case JSON_UNPARSED:
|
||||
writer.writeRaw(_content.asString);
|
||||
return;
|
||||
|
||||
case JSON_NEGATIVE_INTEGER:
|
||||
writer.writeRaw('-');
|
||||
case JSON_POSITIVE_INTEGER:
|
||||
writer.writeInteger(_content.asInteger);
|
||||
return;
|
||||
|
||||
case JSON_BOOLEAN:
|
||||
writer.writeBoolean(_content.asInteger != 0);
|
||||
return;
|
||||
|
||||
default:
|
||||
uint8_t decimals = static_cast<uint8_t>(_type - JSON_FLOAT_0_DECIMALS);
|
||||
writer.writeFloat(_content.asFloat, decimals);
|
||||
}
|
||||
}
|
||||
|
||||
#if ARDUINOJSON_ENABLE_STD_STREAM
|
||||
inline std::ostream &operator<<(std::ostream &os, const JsonVariant &source) {
|
||||
return source.printTo(os);
|
@ -23,12 +23,17 @@ class Print {
|
||||
size_t print(const char* s) {
|
||||
size_t n = 0;
|
||||
while (*s) {
|
||||
n += write(*s++);
|
||||
n += write(static_cast<uint8_t>(*s++));
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
size_t println() { return write('\r') + write('\n'); }
|
||||
size_t println() {
|
||||
size_t n = 0;
|
||||
n += write('\r');
|
||||
n += write('\n');
|
||||
return n;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -13,7 +13,9 @@ namespace ArduinoJson {
|
||||
class RawJson {
|
||||
public:
|
||||
explicit RawJson(const char* str) : _str(str) {}
|
||||
operator const char*() const { return _str; }
|
||||
operator const char*() const {
|
||||
return _str;
|
||||
}
|
||||
|
||||
private:
|
||||
const char* _str;
|
||||
|
@ -8,26 +8,26 @@
|
||||
#pragma once
|
||||
|
||||
#include "../Print.hpp"
|
||||
#include "../String.hpp"
|
||||
#include "../StringTraits/StringTraits.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
namespace Internals {
|
||||
|
||||
// A Print implementation that allows to write in a String
|
||||
template <typename TString>
|
||||
class DynamicStringBuilder : public Print {
|
||||
public:
|
||||
DynamicStringBuilder(String &str) : _str(str) {}
|
||||
DynamicStringBuilder(TString &str) : _str(str) {}
|
||||
|
||||
virtual size_t write(uint8_t c) {
|
||||
// Need to cast to char, otherwise String will print a number (issue #120)
|
||||
_str += static_cast<char>(c);
|
||||
StringFuncs<TString>::append(_str, static_cast<char>(c));
|
||||
return 1;
|
||||
}
|
||||
|
||||
private:
|
||||
DynamicStringBuilder &operator=(const DynamicStringBuilder &);
|
||||
|
||||
String &_str;
|
||||
TString &_str;
|
||||
};
|
||||
}
|
||||
}
|
@ -8,12 +8,14 @@
|
||||
#pragma once
|
||||
|
||||
#include "../Configuration.hpp"
|
||||
#include "../TypeTraits/EnableIf.hpp"
|
||||
#include "DummyPrint.hpp"
|
||||
#include "DynamicStringBuilder.hpp"
|
||||
#include "IndentedPrint.hpp"
|
||||
#include "JsonSerializer.hpp"
|
||||
#include "JsonWriter.hpp"
|
||||
#include "Prettyfier.hpp"
|
||||
#include "StaticStringBuilder.hpp"
|
||||
#include "DynamicStringBuilder.hpp"
|
||||
|
||||
#if ARDUINOJSON_ENABLE_STD_STREAM
|
||||
#include "StreamPrintAdapter.hpp"
|
||||
@ -31,7 +33,7 @@ class JsonPrintable {
|
||||
public:
|
||||
size_t printTo(Print &print) const {
|
||||
JsonWriter writer(print);
|
||||
downcast().writeTo(writer);
|
||||
JsonSerializer::serialize(downcast(), writer);
|
||||
return writer.bytesWritten();
|
||||
}
|
||||
|
||||
@ -48,8 +50,15 @@ class JsonPrintable {
|
||||
return printTo(sb);
|
||||
}
|
||||
|
||||
size_t printTo(String &str) const {
|
||||
DynamicStringBuilder sb(str);
|
||||
template <size_t N>
|
||||
size_t printTo(char (&buffer)[N]) const {
|
||||
return printTo(buffer, N);
|
||||
}
|
||||
|
||||
template <typename TString>
|
||||
typename TypeTraits::EnableIf<StringFuncs<TString>::has_append, size_t>::type
|
||||
printTo(TString &str) const {
|
||||
DynamicStringBuilder<TString> sb(str);
|
||||
return printTo(sb);
|
||||
}
|
||||
|
||||
@ -63,13 +72,20 @@ class JsonPrintable {
|
||||
return prettyPrintTo(sb);
|
||||
}
|
||||
|
||||
template <size_t N>
|
||||
size_t prettyPrintTo(char (&buffer)[N]) const {
|
||||
return prettyPrintTo(buffer, N);
|
||||
}
|
||||
|
||||
size_t prettyPrintTo(Print &print) const {
|
||||
IndentedPrint indentedPrint = IndentedPrint(print);
|
||||
return prettyPrintTo(indentedPrint);
|
||||
}
|
||||
|
||||
size_t prettyPrintTo(String &str) const {
|
||||
DynamicStringBuilder sb(str);
|
||||
template <typename TString>
|
||||
typename TypeTraits::EnableIf<StringFuncs<TString>::has_append, size_t>::type
|
||||
prettyPrintTo(TString &str) const {
|
||||
DynamicStringBuilder<TString> sb(str);
|
||||
return prettyPrintTo(sb);
|
||||
}
|
||||
|
||||
@ -84,7 +100,9 @@ class JsonPrintable {
|
||||
}
|
||||
|
||||
private:
|
||||
const T &downcast() const { return *static_cast<const T *>(this); }
|
||||
const T &downcast() const {
|
||||
return *static_cast<const T *>(this);
|
||||
}
|
||||
};
|
||||
|
||||
#if ARDUINOJSON_ENABLE_STD_STREAM
|
33
include/ArduinoJson/Serialization/JsonSerializer.hpp
Normal file
33
include/ArduinoJson/Serialization/JsonSerializer.hpp
Normal file
@ -0,0 +1,33 @@
|
||||
// Copyright Benoit Blanchon 2014-2016
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "JsonWriter.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
|
||||
class JsonArray;
|
||||
class JsonArraySubscript;
|
||||
class JsonObject;
|
||||
template <typename TKey>
|
||||
class JsonObjectSubscript;
|
||||
class JsonVariant;
|
||||
|
||||
namespace Internals {
|
||||
|
||||
class JsonSerializer {
|
||||
public:
|
||||
static void serialize(const JsonArray &, JsonWriter &);
|
||||
static void serialize(const JsonArraySubscript &, JsonWriter &);
|
||||
static void serialize(const JsonObject &, JsonWriter &);
|
||||
template <typename TKey>
|
||||
static void serialize(const JsonObjectSubscript<TKey> &, JsonWriter &);
|
||||
static void serialize(const JsonVariant &, JsonWriter &);
|
||||
};
|
||||
}
|
||||
}
|
101
include/ArduinoJson/Serialization/JsonSerializerImpl.hpp
Normal file
101
include/ArduinoJson/Serialization/JsonSerializerImpl.hpp
Normal file
@ -0,0 +1,101 @@
|
||||
// Copyright Benoit Blanchon 2014-2016
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../JsonArray.hpp"
|
||||
#include "../JsonArraySubscript.hpp"
|
||||
#include "../JsonObject.hpp"
|
||||
#include "../JsonObjectSubscript.hpp"
|
||||
#include "../JsonVariant.hpp"
|
||||
#include "JsonSerializer.hpp"
|
||||
|
||||
inline void ArduinoJson::Internals::JsonSerializer::serialize(
|
||||
const JsonArray& array, JsonWriter& writer) {
|
||||
writer.beginArray();
|
||||
|
||||
JsonArray::const_iterator it = array.begin();
|
||||
while (it != array.end()) {
|
||||
serialize(*it, writer);
|
||||
|
||||
++it;
|
||||
if (it == array.end()) break;
|
||||
|
||||
writer.writeComma();
|
||||
}
|
||||
|
||||
writer.endArray();
|
||||
}
|
||||
|
||||
inline void ArduinoJson::Internals::JsonSerializer::serialize(
|
||||
const JsonArraySubscript& arraySubscript, JsonWriter& writer) {
|
||||
serialize(arraySubscript.as<JsonVariant>(), writer);
|
||||
}
|
||||
|
||||
inline void ArduinoJson::Internals::JsonSerializer::serialize(
|
||||
const JsonObject& object, JsonWriter& writer) {
|
||||
writer.beginObject();
|
||||
|
||||
JsonObject::const_iterator it = object.begin();
|
||||
while (it != object.end()) {
|
||||
writer.writeString(it->key);
|
||||
writer.writeColon();
|
||||
serialize(it->value, writer);
|
||||
|
||||
++it;
|
||||
if (it == object.end()) break;
|
||||
|
||||
writer.writeComma();
|
||||
}
|
||||
|
||||
writer.endObject();
|
||||
}
|
||||
|
||||
template <typename TKey>
|
||||
inline void ArduinoJson::Internals::JsonSerializer::serialize(
|
||||
const JsonObjectSubscript<TKey>& objectSubscript, JsonWriter& writer) {
|
||||
serialize(objectSubscript.template as<JsonVariant>(), writer);
|
||||
}
|
||||
|
||||
inline void ArduinoJson::Internals::JsonSerializer::serialize(
|
||||
const JsonVariant& variant, JsonWriter& writer) {
|
||||
switch (variant._type) {
|
||||
case JSON_UNDEFINED:
|
||||
return;
|
||||
|
||||
case JSON_ARRAY:
|
||||
serialize(*variant._content.asArray, writer);
|
||||
return;
|
||||
|
||||
case JSON_OBJECT:
|
||||
serialize(*variant._content.asObject, writer);
|
||||
return;
|
||||
|
||||
case JSON_STRING:
|
||||
writer.writeString(variant._content.asString);
|
||||
return;
|
||||
|
||||
case JSON_UNPARSED:
|
||||
writer.writeRaw(variant._content.asString);
|
||||
return;
|
||||
|
||||
case JSON_NEGATIVE_INTEGER:
|
||||
writer.writeRaw('-');
|
||||
case JSON_POSITIVE_INTEGER:
|
||||
writer.writeInteger(variant._content.asInteger);
|
||||
return;
|
||||
|
||||
case JSON_BOOLEAN:
|
||||
writer.writeBoolean(variant._content.asInteger != 0);
|
||||
return;
|
||||
|
||||
default:
|
||||
uint8_t decimals =
|
||||
static_cast<uint8_t>(variant._type - JSON_FLOAT_0_DECIMALS);
|
||||
writer.writeFloat(variant._content.asFloat, decimals);
|
||||
}
|
||||
}
|
@ -7,13 +7,13 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../Data/Encoding.hpp"
|
||||
#include "../Data/JsonFloat.hpp"
|
||||
#include "../Data/JsonInteger.hpp"
|
||||
#include "../Polyfills/attributes.hpp"
|
||||
#include "../Polyfills/math.hpp"
|
||||
#include "../Polyfills/normalize.hpp"
|
||||
#include "../Print.hpp"
|
||||
#include "Encoding.hpp"
|
||||
#include "JsonFloat.hpp"
|
||||
#include "JsonInteger.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
namespace Internals {
|
||||
@ -81,7 +81,7 @@ class JsonWriter {
|
||||
}
|
||||
}
|
||||
|
||||
void writeFloat(JsonFloat value, int digits = 2) {
|
||||
void writeFloat(JsonFloat value, uint8_t digits = 2) {
|
||||
if (Polyfills::isNaN(value)) return writeRaw("NaN");
|
||||
|
||||
if (value < 0.0) {
|
||||
@ -98,6 +98,9 @@ class JsonWriter {
|
||||
powersOf10 = 0;
|
||||
}
|
||||
|
||||
// Round up last digit (so that print(1.999, 2) prints as "2.00")
|
||||
value += getRoundingBias(digits);
|
||||
|
||||
// Extract the integer part of the value and print it
|
||||
JsonUInt int_part = static_cast<JsonUInt>(value);
|
||||
JsonFloat remainder = value - static_cast<JsonFloat>(int_part);
|
||||
@ -115,9 +118,6 @@ class JsonWriter {
|
||||
char currentDigit = char(remainder);
|
||||
remainder -= static_cast<JsonFloat>(currentDigit);
|
||||
|
||||
// Round up last digit (so that print(1.999, 2) prints as "2.00")
|
||||
if (digits == 0 && remainder >= 0.5) currentDigit++;
|
||||
|
||||
// Print
|
||||
writeRaw(char('0' + currentDigit));
|
||||
}
|
||||
@ -135,16 +135,15 @@ class JsonWriter {
|
||||
|
||||
void writeInteger(JsonUInt value) {
|
||||
char buffer[22];
|
||||
char *ptr = buffer + sizeof(buffer) - 1;
|
||||
|
||||
uint8_t i = 0;
|
||||
*ptr = 0;
|
||||
do {
|
||||
buffer[i++] = static_cast<char>(value % 10 + '0');
|
||||
*--ptr = static_cast<char>(value % 10 + '0');
|
||||
value /= 10;
|
||||
} while (value);
|
||||
|
||||
while (i > 0) {
|
||||
writeRaw(buffer[--i]);
|
||||
}
|
||||
writeRaw(ptr);
|
||||
}
|
||||
|
||||
void writeRaw(const char *s) {
|
||||
@ -160,6 +159,26 @@ class JsonWriter {
|
||||
|
||||
private:
|
||||
JsonWriter &operator=(const JsonWriter &); // cannot be assigned
|
||||
|
||||
static JsonFloat getLastDigit(uint8_t digits) {
|
||||
// Designed as a compromise between code size and speed
|
||||
switch (digits) {
|
||||
case 0:
|
||||
return 1e-0;
|
||||
case 1:
|
||||
return 1e-1;
|
||||
case 2:
|
||||
return 1e-2;
|
||||
case 3:
|
||||
return 1e-3;
|
||||
default:
|
||||
return getLastDigit(uint8_t(digits - 4)) * 1e-4;
|
||||
}
|
||||
}
|
||||
|
||||
FORCE_INLINE static JsonFloat getRoundingBias(uint8_t digits) {
|
||||
return 0.5 * getLastDigit(digits);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
@ -66,28 +66,46 @@ class Prettyfier : public Print {
|
||||
}
|
||||
|
||||
size_t writeBlockClose(uint8_t c) {
|
||||
return unindentIfNeeded() + _sink.write(c);
|
||||
size_t n = 0;
|
||||
n += unindentIfNeeded();
|
||||
n += _sink.write(c);
|
||||
return n;
|
||||
}
|
||||
|
||||
size_t writeBlockOpen(uint8_t c) {
|
||||
return indentIfNeeded() + _sink.write(c);
|
||||
size_t n = 0;
|
||||
n += indentIfNeeded();
|
||||
n += _sink.write(c);
|
||||
return n;
|
||||
}
|
||||
|
||||
size_t writeColon() {
|
||||
return _sink.write(':') + _sink.write(' ');
|
||||
size_t n = 0;
|
||||
n += _sink.write(':');
|
||||
n += _sink.write(' ');
|
||||
return n;
|
||||
}
|
||||
|
||||
size_t writeComma() {
|
||||
return _sink.write(',') + _sink.println();
|
||||
size_t n = 0;
|
||||
n += _sink.write(',');
|
||||
n += _sink.println();
|
||||
return n;
|
||||
}
|
||||
|
||||
size_t writeQuoteOpen() {
|
||||
_inString = true;
|
||||
return indentIfNeeded() + _sink.write('"');
|
||||
size_t n = 0;
|
||||
n += indentIfNeeded();
|
||||
n += _sink.write('"');
|
||||
return n;
|
||||
}
|
||||
|
||||
size_t writeNormalChar(uint8_t c) {
|
||||
return indentIfNeeded() + _sink.write(c);
|
||||
size_t n = 0;
|
||||
n += indentIfNeeded();
|
||||
n += _sink.write(c);
|
||||
return n;
|
||||
}
|
||||
|
||||
size_t indentIfNeeded() {
|
@ -7,7 +7,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "JsonBuffer.hpp"
|
||||
#include "JsonBufferBase.hpp"
|
||||
|
||||
#if defined(__clang__)
|
||||
#pragma clang diagnostic push
|
||||
@ -21,32 +21,87 @@
|
||||
|
||||
namespace ArduinoJson {
|
||||
|
||||
// Implements a JsonBuffer with fixed memory allocation.
|
||||
// The template paramenter CAPACITY specifies the capacity of the buffer in
|
||||
// bytes.
|
||||
template <size_t CAPACITY>
|
||||
class StaticJsonBuffer : public JsonBuffer {
|
||||
class StaticJsonBufferBase : public JsonBufferBase<StaticJsonBufferBase> {
|
||||
public:
|
||||
explicit StaticJsonBuffer() : _size(0) {}
|
||||
class String {
|
||||
public:
|
||||
String(StaticJsonBufferBase* parent) : _parent(parent) {
|
||||
_start = parent->_buffer + parent->_size;
|
||||
}
|
||||
|
||||
void append(char c) {
|
||||
if (_parent->canAlloc(1)) {
|
||||
char* last = static_cast<char*>(_parent->doAlloc(1));
|
||||
*last = c;
|
||||
}
|
||||
}
|
||||
|
||||
const char* c_str() const {
|
||||
if (_parent->canAlloc(1)) {
|
||||
char* last = static_cast<char*>(_parent->doAlloc(1));
|
||||
*last = '\0';
|
||||
return _start;
|
||||
} else {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
StaticJsonBufferBase* _parent;
|
||||
char* _start;
|
||||
};
|
||||
|
||||
StaticJsonBufferBase(char* buffer, size_t capa)
|
||||
: _buffer(buffer), _capacity(capa), _size(0) {}
|
||||
|
||||
size_t capacity() const {
|
||||
return CAPACITY;
|
||||
return _capacity;
|
||||
}
|
||||
size_t size() const {
|
||||
return _size;
|
||||
}
|
||||
|
||||
virtual void* alloc(size_t bytes) {
|
||||
if (_size + bytes > CAPACITY) return NULL;
|
||||
void* p = &_buffer[_size];
|
||||
_size += round_size_up(bytes);
|
||||
return p;
|
||||
alignNextAlloc();
|
||||
if (!canAlloc(bytes)) return NULL;
|
||||
return doAlloc(bytes);
|
||||
}
|
||||
|
||||
String startString() {
|
||||
return String(this);
|
||||
}
|
||||
|
||||
private:
|
||||
uint8_t _buffer[CAPACITY];
|
||||
void alignNextAlloc() {
|
||||
_size = round_size_up(_size);
|
||||
}
|
||||
|
||||
bool canAlloc(size_t bytes) const {
|
||||
return _size + bytes <= _capacity;
|
||||
}
|
||||
|
||||
void* doAlloc(size_t bytes) {
|
||||
void* p = &_buffer[_size];
|
||||
_size += bytes;
|
||||
return p;
|
||||
}
|
||||
|
||||
char* _buffer;
|
||||
size_t _capacity;
|
||||
size_t _size;
|
||||
};
|
||||
|
||||
// Implements a JsonBuffer with fixed memory allocation.
|
||||
// The template paramenter CAPACITY specifies the capacity of the buffer in
|
||||
// bytes.
|
||||
template <size_t CAPACITY>
|
||||
class StaticJsonBuffer : public StaticJsonBufferBase {
|
||||
public:
|
||||
explicit StaticJsonBuffer() : StaticJsonBufferBase(_buffer, CAPACITY) {}
|
||||
|
||||
private:
|
||||
char _buffer[CAPACITY];
|
||||
};
|
||||
}
|
||||
|
||||
#if defined(__clang__)
|
||||
|
@ -1,24 +0,0 @@
|
||||
// Copyright Benoit Blanchon 2014-2016
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Configuration.hpp"
|
||||
|
||||
#if ARDUINOJSON_USE_ARDUINO_STRING
|
||||
|
||||
#include <WString.h>
|
||||
|
||||
#else
|
||||
|
||||
#include <string>
|
||||
|
||||
namespace ArduinoJson {
|
||||
typedef std::string String;
|
||||
}
|
||||
|
||||
#endif
|
39
include/ArduinoJson/StringTraits/ArduinoStream.hpp
Normal file
39
include/ArduinoJson/StringTraits/ArduinoStream.hpp
Normal file
@ -0,0 +1,39 @@
|
||||
// Copyright Benoit Blanchon 2014-2016
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../TypeTraits/EnableIf.hpp"
|
||||
#include "../TypeTraits/IsBaseOf.hpp"
|
||||
#include "../TypeTraits/RemoveReference.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
namespace Internals {
|
||||
|
||||
struct StdStreamFuncs {
|
||||
class Iterator {
|
||||
Stream& _stream;
|
||||
|
||||
public:
|
||||
Iterator(Stream& stream) : _stream(stream) {}
|
||||
|
||||
char next() {
|
||||
int n = _stream.read();
|
||||
return n >= 0 ? static_cast<char>(n) : '\0';
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
template <typename TStream>
|
||||
struct StringFuncs<TStream,
|
||||
// match any type that is derived from std::istream:
|
||||
typename TypeTraits::EnableIf<TypeTraits::IsBaseOf<
|
||||
Stream, typename TypeTraits::RemoveReference<
|
||||
TStream>::type>::value>::type>
|
||||
: StdStreamFuncs {};
|
||||
}
|
||||
}
|
57
include/ArduinoJson/StringTraits/CharPointer.hpp
Normal file
57
include/ArduinoJson/StringTraits/CharPointer.hpp
Normal file
@ -0,0 +1,57 @@
|
||||
// Copyright Benoit Blanchon 2014-2016
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace ArduinoJson {
|
||||
namespace Internals {
|
||||
|
||||
struct CharPtrFuncs {
|
||||
class Iterator {
|
||||
const char* _ptr;
|
||||
|
||||
public:
|
||||
Iterator(const char* ptr) : _ptr(ptr ? ptr : "") {}
|
||||
|
||||
char next() {
|
||||
char c = *_ptr;
|
||||
if (c) ++_ptr;
|
||||
return c;
|
||||
}
|
||||
};
|
||||
|
||||
static bool equals(const char* str, const char* expected) {
|
||||
return strcmp(str, expected) == 0;
|
||||
}
|
||||
|
||||
template <typename Buffer>
|
||||
static char* duplicate(const char* str, Buffer* buffer) {
|
||||
if (!str) return NULL;
|
||||
size_t size = strlen(str) + 1;
|
||||
void* dup = buffer->alloc(size);
|
||||
if (dup != NULL) memcpy(dup, str, size);
|
||||
return static_cast<char*>(dup);
|
||||
}
|
||||
|
||||
static const bool has_append = false;
|
||||
static const bool has_equals = true;
|
||||
static const bool should_duplicate = false;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct StringFuncs<const char*, void> : CharPtrFuncs {};
|
||||
|
||||
template <>
|
||||
struct StringFuncs<char*, void> : CharPtrFuncs {};
|
||||
|
||||
template <size_t N>
|
||||
struct StringFuncs<char[N], void> : CharPtrFuncs {};
|
||||
|
||||
template <size_t N>
|
||||
struct StringFuncs<const char[N], void> : CharPtrFuncs {};
|
||||
}
|
||||
}
|
44
include/ArduinoJson/StringTraits/FlashString.hpp
Normal file
44
include/ArduinoJson/StringTraits/FlashString.hpp
Normal file
@ -0,0 +1,44 @@
|
||||
// Copyright Benoit Blanchon 2014-2016
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace ArduinoJson {
|
||||
namespace Internals {
|
||||
template <>
|
||||
struct StringFuncs<const __FlashStringHelper*, void> {
|
||||
class Iterator {
|
||||
const char* _ptr;
|
||||
|
||||
public:
|
||||
Iterator(const __FlashStringHelper* ptr)
|
||||
: _ptr(reinterpret_cast<const char*>(ptr)) {}
|
||||
|
||||
char next() {
|
||||
return pgm_read_byte_near(_ptr++);
|
||||
}
|
||||
};
|
||||
|
||||
static bool equals(const __FlashStringHelper* str, const char* expected) {
|
||||
return strcmp_P(expected, (PGM_P)str) == 0;
|
||||
}
|
||||
|
||||
template <typename Buffer>
|
||||
static char* duplicate(const __FlashStringHelper* str, Buffer* buffer) {
|
||||
if (!str) return NULL;
|
||||
size_t size = strlen_P((PGM_P)str) + 1;
|
||||
void* dup = buffer->alloc(size);
|
||||
if (dup != NULL) memcpy_P(dup, (PGM_P)str, size);
|
||||
return static_cast<char*>(dup);
|
||||
}
|
||||
|
||||
static const bool has_append = false;
|
||||
static const bool has_equals = true;
|
||||
static const bool should_duplicate = true;
|
||||
};
|
||||
}
|
||||
}
|
42
include/ArduinoJson/StringTraits/StdStream.hpp
Normal file
42
include/ArduinoJson/StringTraits/StdStream.hpp
Normal file
@ -0,0 +1,42 @@
|
||||
// Copyright Benoit Blanchon 2014-2016
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <istream>
|
||||
#include "../TypeTraits/EnableIf.hpp"
|
||||
#include "../TypeTraits/IsBaseOf.hpp"
|
||||
#include "../TypeTraits/RemoveReference.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
namespace Internals {
|
||||
|
||||
struct StdStreamFuncs {
|
||||
class Iterator {
|
||||
std::istream& _stream;
|
||||
|
||||
public:
|
||||
Iterator(std::istream& stream) : _stream(stream) {}
|
||||
|
||||
char next() {
|
||||
return _stream.eof() ? '\0' : static_cast<char>(_stream.get());
|
||||
}
|
||||
|
||||
private:
|
||||
Iterator& operator=(const Iterator&); // Visual Studio C4512
|
||||
};
|
||||
};
|
||||
|
||||
template <typename TStream>
|
||||
struct StringFuncs<TStream,
|
||||
// match any type that is derived from std::istream:
|
||||
typename TypeTraits::EnableIf<TypeTraits::IsBaseOf<
|
||||
std::istream, typename TypeTraits::RemoveReference<
|
||||
TStream>::type>::value>::type>
|
||||
: StdStreamFuncs {};
|
||||
}
|
||||
}
|
61
include/ArduinoJson/StringTraits/StdString.hpp
Normal file
61
include/ArduinoJson/StringTraits/StdString.hpp
Normal file
@ -0,0 +1,61 @@
|
||||
// Copyright Benoit Blanchon 2014-2016
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#pragma once
|
||||
|
||||
#if ARDUINOJSON_ENABLE_ARDUINO_STRING
|
||||
#include <WString.h>
|
||||
#endif
|
||||
|
||||
#if ARDUINOJSON_ENABLE_STD_STRING
|
||||
#include <string>
|
||||
#endif
|
||||
|
||||
namespace ArduinoJson {
|
||||
namespace Internals {
|
||||
|
||||
template <typename TString>
|
||||
struct StdStringFuncs {
|
||||
template <typename Buffer>
|
||||
static char* duplicate(const TString& str, Buffer* buffer) {
|
||||
if (!str.c_str()) return NULL; // <- Arduino string can return NULL
|
||||
size_t size = str.length() + 1;
|
||||
void* dup = buffer->alloc(size);
|
||||
if (dup != NULL) memcpy(dup, str.c_str(), size);
|
||||
return static_cast<char*>(dup);
|
||||
}
|
||||
|
||||
struct Iterator : CharPtrFuncs::Iterator {
|
||||
Iterator(const TString& str) : CharPtrFuncs::Iterator(str.c_str()) {}
|
||||
};
|
||||
|
||||
static bool equals(const TString& str, const char* expected) {
|
||||
return str == expected;
|
||||
}
|
||||
|
||||
static void append(TString& str, char c) {
|
||||
str += c;
|
||||
}
|
||||
|
||||
static const bool has_append = true;
|
||||
static const bool has_equals = true;
|
||||
static const bool should_duplicate = true;
|
||||
};
|
||||
|
||||
#if ARDUINOJSON_ENABLE_ARDUINO_STRING
|
||||
template <>
|
||||
struct StringFuncs<String, void> : StdStringFuncs<String> {};
|
||||
template <>
|
||||
struct StringFuncs<StringSumHelper, void> : StdStringFuncs<StringSumHelper> {};
|
||||
#endif
|
||||
|
||||
#if ARDUINOJSON_ENABLE_STD_STRING
|
||||
template <>
|
||||
struct StringFuncs<std::string, void> : StdStringFuncs<std::string> {};
|
||||
#endif
|
||||
}
|
||||
}
|
42
include/ArduinoJson/StringTraits/StringTraits.hpp
Normal file
42
include/ArduinoJson/StringTraits/StringTraits.hpp
Normal file
@ -0,0 +1,42 @@
|
||||
// Copyright Benoit Blanchon 2014-2016
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../Configuration.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
namespace Internals {
|
||||
|
||||
template <typename TString, typename Enable = void>
|
||||
struct StringFuncs {};
|
||||
|
||||
template <typename TString>
|
||||
struct StringFuncs<const TString, void> : StringFuncs<TString> {};
|
||||
|
||||
template <typename TString>
|
||||
struct StringFuncs<TString&, void> : StringFuncs<TString> {};
|
||||
}
|
||||
}
|
||||
|
||||
#include "CharPointer.hpp"
|
||||
|
||||
#if ARDUINOJSON_ENABLE_STD_STRING || ARDUINOJSON_ENABLE_ARDUINO_STRING
|
||||
#include "StdString.hpp"
|
||||
#endif
|
||||
|
||||
#if ARDUINOJSON_ENABLE_STD_STREAM
|
||||
#include "StdStream.hpp"
|
||||
#endif
|
||||
|
||||
#if ARDUINOJSON_ENABLE_ARDUINO_STREAM
|
||||
#include "ArduinoStream.hpp"
|
||||
#endif
|
||||
|
||||
#if ARDUINOJSON_ENABLE_PROGMEM
|
||||
#include "FlashString.hpp"
|
||||
#endif
|
31
include/ArduinoJson/TypeTraits/ConstRefOrConstPtr.hpp
Normal file
31
include/ArduinoJson/TypeTraits/ConstRefOrConstPtr.hpp
Normal file
@ -0,0 +1,31 @@
|
||||
// Copyright Benoit Blanchon 2014-2016
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace ArduinoJson {
|
||||
namespace TypeTraits {
|
||||
|
||||
// A meta-function that return the type T without the const modifier
|
||||
template <typename T>
|
||||
struct ConstRefOrConstPtr {
|
||||
typedef const T& type;
|
||||
};
|
||||
template <typename T>
|
||||
struct ConstRefOrConstPtr<T*> {
|
||||
typedef const T* type;
|
||||
};
|
||||
template <typename T>
|
||||
struct ConstRefOrConstPtr<T[]> {
|
||||
typedef const T* type;
|
||||
};
|
||||
template <typename T, size_t N>
|
||||
struct ConstRefOrConstPtr<T[N]> {
|
||||
typedef const T* type;
|
||||
};
|
||||
}
|
||||
}
|
30
include/ArduinoJson/TypeTraits/IsBaseOf.hpp
Normal file
30
include/ArduinoJson/TypeTraits/IsBaseOf.hpp
Normal file
@ -0,0 +1,30 @@
|
||||
// Copyright Benoit Blanchon 2014-2016
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace ArduinoJson {
|
||||
namespace TypeTraits {
|
||||
|
||||
// A meta-function that returns true if Derived inherits from TBase is an
|
||||
// integral type.
|
||||
template <typename TBase, typename TDerived>
|
||||
class IsBaseOf {
|
||||
protected: // <- to avoid GCC's "all member functions in class are private"
|
||||
typedef char Yes[1];
|
||||
typedef char No[2];
|
||||
|
||||
static Yes &probe(const TBase *);
|
||||
static No &probe(const void *);
|
||||
|
||||
public:
|
||||
enum {
|
||||
value = sizeof(probe(reinterpret_cast<TDerived *>(0))) == sizeof(Yes)
|
||||
};
|
||||
};
|
||||
}
|
||||
}
|
@ -7,7 +7,6 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../Configuration.hpp"
|
||||
#include "IsSame.hpp"
|
||||
#include "IsSignedIntegral.hpp"
|
||||
#include "IsUnsignedIntegral.hpp"
|
||||
@ -20,7 +19,11 @@ template <typename T>
|
||||
struct IsIntegral {
|
||||
static const bool value = TypeTraits::IsSignedIntegral<T>::value ||
|
||||
TypeTraits::IsUnsignedIntegral<T>::value ||
|
||||
TypeTraits::IsSame<T, char>::value;
|
||||
TypeTraits::IsSame<T, char>::value ||
|
||||
TypeTraits::IsSame<T, bool>::value;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct IsIntegral<const T> : IsIntegral<T> {};
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ JsonArray KEYWORD1
|
||||
JsonObject KEYWORD1
|
||||
JsonVariant KEYWORD1
|
||||
StaticJsonBuffer KEYWORD1
|
||||
DynamicJsonBuffer KEYWORD1
|
||||
add KEYWORD2
|
||||
createArray KEYWORD2
|
||||
createNestedArray KEYWORD2
|
||||
|
@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "Json",
|
||||
"name": "ArduinoJson",
|
||||
"keywords": "json, rest, http, web",
|
||||
"description": "An elegant and efficient JSON library for embedded systems",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/bblanchon/ArduinoJson.git"
|
||||
},
|
||||
"version": "5.6.3",
|
||||
"version": "5.8.0",
|
||||
"authors": {
|
||||
"name": "Benoit Blanchon",
|
||||
"url": "http://blog.benoitblanchon.fr"
|
||||
|
@ -1,5 +1,5 @@
|
||||
name=ArduinoJson
|
||||
version=5.6.3
|
||||
version=5.8.0
|
||||
author=Benoit Blanchon <blog.benoitblanchon.fr>
|
||||
maintainer=Benoit Blanchon <blog.benoitblanchon.fr>
|
||||
sentence=An efficient and elegant JSON library for Arduino.
|
||||
|
@ -6,5 +6,5 @@ curl https://cmake.org/files/v3.4/cmake-3.4.0-Linux-x86_64.tar.gz | tar xz -C /t
|
||||
make
|
||||
make test
|
||||
|
||||
pip install --user cpp-coveralls
|
||||
pip install --user cpp-coveralls 'requests[security]'
|
||||
coveralls --exclude third-party --gcov-options '\-lp'; fi
|
||||
|
@ -4,7 +4,7 @@ pip install --user platformio
|
||||
|
||||
rm -r test
|
||||
|
||||
for EXAMPLE in JsonParserExample JsonGeneratorExample
|
||||
for EXAMPLE in $PWD/examples/*/*.ino;
|
||||
do
|
||||
platformio ci examples/$EXAMPLE/$EXAMPLE.ino -l '.' -b $BOARD
|
||||
platformio ci $EXAMPLE -l '.' -b $BOARD
|
||||
done
|
||||
|
7
scripts/travis/sanitize.sh
Executable file
7
scripts/travis/sanitize.sh
Executable file
@ -0,0 +1,7 @@
|
||||
#!/bin/sh -eux
|
||||
|
||||
curl https://cmake.org/files/v3.4/cmake-3.4.0-Linux-x86_64.tar.gz | tar xz -C /tmp --strip 1
|
||||
|
||||
/tmp/bin/cmake -DSANITIZE=true .
|
||||
cmake --build .
|
||||
ctest -VV .
|
@ -1,6 +1,6 @@
|
||||
# Copyright Benoit Blanchon 2014-2016
|
||||
# MIT License
|
||||
#
|
||||
#
|
||||
# Arduino JSON library
|
||||
# https://github.com/bblanchon/ArduinoJson
|
||||
# If you like this project, please add a star!
|
||||
@ -12,7 +12,6 @@ file(GLOB TESTS_FILES *.hpp *.cpp)
|
||||
if(CMAKE_CXX_COMPILER_ID MATCHES "(GNU|Clang)")
|
||||
add_compile_options(
|
||||
-fno-exceptions
|
||||
-fno-rtti
|
||||
-pedantic
|
||||
-Wall
|
||||
-Wcast-align
|
||||
@ -25,10 +24,7 @@ if(CMAKE_CXX_COMPILER_ID MATCHES "(GNU|Clang)")
|
||||
-Wformat=2
|
||||
-Winit-self
|
||||
-Wmissing-include-dirs
|
||||
-Wno-parentheses
|
||||
-Wno-sign-conversion
|
||||
-Wno-unused
|
||||
-Wno-variadic-macros
|
||||
-Wparentheses
|
||||
-Wnon-virtual-dtor
|
||||
-Wold-style-cast
|
||||
-Woverloaded-virtual
|
||||
@ -63,7 +59,10 @@ endif()
|
||||
|
||||
if(MSVC)
|
||||
add_definitions(-D_CRT_SECURE_NO_WARNINGS)
|
||||
add_compile_options(-W4)
|
||||
add_compile_options(
|
||||
/W4 # Set warning level
|
||||
/WX # Treats all compiler warnings as errors.
|
||||
)
|
||||
endif()
|
||||
|
||||
add_executable(ArduinoJsonTests ${TESTS_FILES})
|
||||
|
@ -5,8 +5,8 @@
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
class DynamicJsonBuffer_Basic_Tests : public testing::Test {
|
||||
protected:
|
||||
@ -30,11 +30,30 @@ TEST_F(DynamicJsonBuffer_Basic_Tests, ReturnDifferentPointer) {
|
||||
ASSERT_NE(p1, p2);
|
||||
}
|
||||
|
||||
TEST_F(DynamicJsonBuffer_Basic_Tests, Alignment) {
|
||||
size_t mask = sizeof(void*) - 1;
|
||||
|
||||
for (size_t size = 1; size <= sizeof(void*); size++) {
|
||||
size_t addr = reinterpret_cast<size_t>(buffer.alloc(1));
|
||||
ASSERT_EQ(0, addr & mask);
|
||||
}
|
||||
static bool isAligned(void* ptr) {
|
||||
const size_t mask = sizeof(void*) - 1;
|
||||
size_t addr = reinterpret_cast<size_t>(ptr);
|
||||
return (addr & mask) == 0;
|
||||
}
|
||||
|
||||
TEST_F(DynamicJsonBuffer_Basic_Tests, Alignment) {
|
||||
// make room for tow but not three
|
||||
buffer = DynamicJsonBuffer(2 * sizeof(void*) + 1);
|
||||
|
||||
ASSERT_TRUE(isAligned(buffer.alloc(1))); // this on is aligned by design
|
||||
ASSERT_TRUE(isAligned(buffer.alloc(1))); // this one fits in the first block
|
||||
ASSERT_TRUE(isAligned(buffer.alloc(1))); // this one requires a new block
|
||||
}
|
||||
|
||||
TEST_F(DynamicJsonBuffer_Basic_Tests, strdup) {
|
||||
char original[] = "hello";
|
||||
char* copy = buffer.strdup(original);
|
||||
strcpy(original, "world");
|
||||
ASSERT_STREQ("hello", copy);
|
||||
}
|
||||
|
||||
TEST_F(DynamicJsonBuffer_Basic_Tests, strdup_givenNull) {
|
||||
const char* original = NULL;
|
||||
char* copy = buffer.strdup(original);
|
||||
ASSERT_EQ(NULL, copy);
|
||||
}
|
||||
|
@ -5,18 +5,20 @@
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
class NoMemoryAllocator {
|
||||
public:
|
||||
void* allocate(size_t) { return NULL; }
|
||||
void* allocate(size_t) {
|
||||
return NULL;
|
||||
}
|
||||
void deallocate(void*) {}
|
||||
};
|
||||
|
||||
class DynamicJsonBuffer_NoMemory_Tests : public ::testing::Test {
|
||||
protected:
|
||||
Internals::BlockJsonBuffer<NoMemoryAllocator> _jsonBuffer;
|
||||
DynamicJsonBufferBase<NoMemoryAllocator> _jsonBuffer;
|
||||
};
|
||||
|
||||
TEST_F(DynamicJsonBuffer_NoMemory_Tests, FixCodeCoverage) {
|
||||
|
48
test/DynamicJsonBuffer_String_Tests.cpp
Normal file
48
test/DynamicJsonBuffer_String_Tests.cpp
Normal file
@ -0,0 +1,48 @@
|
||||
// Copyright Benoit Blanchon 2014-2016
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
TEST(DynamicJsonBuffer_String_Tests, WorksWhenBufferIsBigEnough) {
|
||||
DynamicJsonBuffer jsonBuffer(6);
|
||||
|
||||
DynamicJsonBuffer::String str = jsonBuffer.startString();
|
||||
str.append('h');
|
||||
str.append('e');
|
||||
str.append('l');
|
||||
str.append('l');
|
||||
str.append('o');
|
||||
|
||||
ASSERT_STREQ("hello", str.c_str());
|
||||
}
|
||||
|
||||
TEST(DynamicJsonBuffer_String_Tests, GrowsWhenBufferIsTooSmall) {
|
||||
DynamicJsonBuffer jsonBuffer(5);
|
||||
|
||||
DynamicJsonBuffer::String str = jsonBuffer.startString();
|
||||
str.append('h');
|
||||
str.append('e');
|
||||
str.append('l');
|
||||
str.append('l');
|
||||
str.append('o');
|
||||
|
||||
ASSERT_STREQ("hello", str.c_str());
|
||||
}
|
||||
|
||||
TEST(DynamicJsonBuffer_String_Tests, SizeIncreases) {
|
||||
DynamicJsonBuffer jsonBuffer(5);
|
||||
|
||||
DynamicJsonBuffer::String str = jsonBuffer.startString();
|
||||
ASSERT_EQ(0, jsonBuffer.size());
|
||||
|
||||
str.append('h');
|
||||
ASSERT_EQ(1, jsonBuffer.size());
|
||||
|
||||
str.c_str();
|
||||
ASSERT_EQ(2, jsonBuffer.size());
|
||||
}
|
@ -5,8 +5,8 @@
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
class JsonArray_PrettyPrintTo_Tests : public testing::Test {
|
||||
public:
|
||||
@ -19,7 +19,7 @@ class JsonArray_PrettyPrintTo_Tests : public testing::Test {
|
||||
void outputMustBe(const char* expected) {
|
||||
char actual[256];
|
||||
|
||||
size_t actualLen = array.prettyPrintTo(actual, sizeof(actual));
|
||||
size_t actualLen = array.prettyPrintTo(actual);
|
||||
size_t measuredLen = array.measurePrettyLength();
|
||||
|
||||
EXPECT_STREQ(expected, actual);
|
||||
@ -28,7 +28,9 @@ class JsonArray_PrettyPrintTo_Tests : public testing::Test {
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(JsonArray_PrettyPrintTo_Tests, Empty) { outputMustBe("[]"); }
|
||||
TEST_F(JsonArray_PrettyPrintTo_Tests, Empty) {
|
||||
outputMustBe("[]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_PrettyPrintTo_Tests, OneElement) {
|
||||
array.add(1);
|
||||
|
@ -5,8 +5,8 @@
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
class JsonArray_PrintTo_Tests : public testing::Test {
|
||||
public:
|
||||
@ -17,7 +17,7 @@ class JsonArray_PrintTo_Tests : public testing::Test {
|
||||
JsonArray &array;
|
||||
|
||||
void outputMustBe(const char *expected) {
|
||||
size_t actualLen = array.printTo(buffer, sizeof(buffer));
|
||||
size_t actualLen = array.printTo(buffer);
|
||||
size_t measuredLen = array.measureLength();
|
||||
|
||||
EXPECT_STREQ(expected, buffer);
|
||||
@ -29,7 +29,9 @@ class JsonArray_PrintTo_Tests : public testing::Test {
|
||||
char buffer[256];
|
||||
};
|
||||
|
||||
TEST_F(JsonArray_PrintTo_Tests, Empty) { outputMustBe("[]"); }
|
||||
TEST_F(JsonArray_PrintTo_Tests, Empty) {
|
||||
outputMustBe("[]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_PrintTo_Tests, Null) {
|
||||
array.add(static_cast<char *>(0));
|
||||
|
26
test/JsonObject_Get_Tests.cpp
Normal file
26
test/JsonObject_Get_Tests.cpp
Normal file
@ -0,0 +1,26 @@
|
||||
// Copyright Benoit Blanchon 2014-2016
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
class JsonObject_Get_Tests : public ::testing::Test {
|
||||
public:
|
||||
JsonObject_Get_Tests() : _object(_jsonBuffer.createObject()) {}
|
||||
|
||||
protected:
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonObject& _object;
|
||||
};
|
||||
|
||||
#define TEST_(name) TEST_F(JsonObject_Get_Tests, name)
|
||||
|
||||
TEST_(GetConstCharPointer_GivenStringLiteral) {
|
||||
_object.set("hello", "world");
|
||||
const char* value = _object.get<const char*>("hello");
|
||||
EXPECT_STREQ("world", value);
|
||||
}
|
@ -5,11 +5,11 @@
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
TEST(JsonObject_Invalid_Tests, SubscriptFails) {
|
||||
ASSERT_FALSE(JsonObject::invalid()[0].success());
|
||||
ASSERT_FALSE(JsonObject::invalid()["key"].success());
|
||||
}
|
||||
|
||||
TEST(JsonObject_Invalid_Tests, AddFails) {
|
||||
|
@ -5,8 +5,8 @@
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
class JsonObject_PrettyPrintTo_Tests : public testing::Test {
|
||||
public:
|
||||
@ -19,7 +19,7 @@ class JsonObject_PrettyPrintTo_Tests : public testing::Test {
|
||||
void outputMustBe(const char *expected) {
|
||||
char buffer[256];
|
||||
|
||||
size_t actualLen = _object.prettyPrintTo(buffer, sizeof(buffer));
|
||||
size_t actualLen = _object.prettyPrintTo(buffer);
|
||||
size_t measuredLen = _object.measurePrettyLength();
|
||||
|
||||
EXPECT_STREQ(expected, buffer);
|
||||
@ -28,7 +28,9 @@ class JsonObject_PrettyPrintTo_Tests : public testing::Test {
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(JsonObject_PrettyPrintTo_Tests, EmptyObject) { outputMustBe("{}"); }
|
||||
TEST_F(JsonObject_PrettyPrintTo_Tests, EmptyObject) {
|
||||
outputMustBe("{}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_PrettyPrintTo_Tests, OneMember) {
|
||||
_object["key"] = "value";
|
||||
|
@ -15,7 +15,7 @@ class JsonObject_PrintTo_Tests : public testing::Test {
|
||||
protected:
|
||||
void outputMustBe(const char *expected) {
|
||||
char actual[256];
|
||||
size_t actualLen = _object.printTo(actual, sizeof(actual));
|
||||
size_t actualLen = _object.printTo(actual);
|
||||
size_t measuredLen = _object.measureLength();
|
||||
|
||||
EXPECT_STREQ(expected, actual);
|
||||
@ -27,7 +27,9 @@ class JsonObject_PrintTo_Tests : public testing::Test {
|
||||
JsonObject &_object;
|
||||
};
|
||||
|
||||
TEST_F(JsonObject_PrintTo_Tests, EmptyObject) { outputMustBe("{}"); }
|
||||
TEST_F(JsonObject_PrintTo_Tests, EmptyObject) {
|
||||
outputMustBe("{}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_PrintTo_Tests, TwoStrings) {
|
||||
_object["key1"] = "value1";
|
||||
|
@ -5,8 +5,8 @@
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
class JsonObject_Set_Tests : public ::testing::Test {
|
||||
public:
|
||||
@ -107,3 +107,21 @@ TEST_(StoreObjectSubscript) {
|
||||
|
||||
EXPECT_EQ(42, _object["a"]);
|
||||
}
|
||||
|
||||
TEST_(ShouldReturnTrue_WhenAllocationSucceeds) {
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(1) + 15> jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
|
||||
bool result = obj.set(std::string("hello"), std::string("world"));
|
||||
|
||||
ASSERT_TRUE(result);
|
||||
}
|
||||
|
||||
TEST_(ShouldReturnFalse_WhenAllocationFails) {
|
||||
StaticJsonBuffer<JSON_OBJECT_SIZE(1) + 10> jsonBuffer;
|
||||
JsonObject& obj = jsonBuffer.createObject();
|
||||
|
||||
bool result = obj.set(std::string("hello"), std::string("world"));
|
||||
|
||||
ASSERT_FALSE(result);
|
||||
}
|
||||
|
@ -5,8 +5,8 @@
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
class JsonObject_Subscript_Tests : public ::testing::Test {
|
||||
public:
|
||||
|
@ -5,12 +5,14 @@
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
class JsonParser_Array_Tests : public testing::Test {
|
||||
protected:
|
||||
void whenInputIs(const char *json) { strcpy(_jsonString, json); }
|
||||
void whenInputIs(const char *json) {
|
||||
strcpy(_jsonString, json);
|
||||
}
|
||||
|
||||
void whenInputIs(const char *json, size_t len) {
|
||||
memcpy(_jsonString, json, len);
|
||||
@ -27,7 +29,9 @@ class JsonParser_Array_Tests : public testing::Test {
|
||||
EXPECT_EQ(0, _array->size());
|
||||
}
|
||||
|
||||
void sizeMustBe(int expected) { ASSERT_EQ(expected, _array->size()); }
|
||||
void sizeMustBe(int expected) {
|
||||
ASSERT_EQ(expected, _array->size());
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void firstElementMustBe(T expected) {
|
||||
@ -346,3 +350,18 @@ TEST_F(JsonParser_Array_Tests, UnfinishedCComment) {
|
||||
whenInputIs("[/*COMMENT]");
|
||||
parseMustFail();
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, EndsInCppComment) {
|
||||
whenInputIs("[//COMMENT");
|
||||
parseMustFail();
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, AfterClosingStar) {
|
||||
whenInputIs("[/*COMMENT*");
|
||||
parseMustFail();
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Array_Tests, DeeplyNested) {
|
||||
whenInputIs("[[[[[[[[[[[[[[[[[[[\"Not too deep\"]]]]]]]]]]]]]]]]]]]");
|
||||
parseMustSucceed();
|
||||
}
|
||||
|
@ -5,8 +5,8 @@
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
class JsonParser_Variant_Test : public testing::Test {
|
||||
protected:
|
||||
@ -24,13 +24,29 @@ class JsonParser_Variant_Test : public testing::Test {
|
||||
EXPECT_STREQ(expected, _result.as<char*>());
|
||||
}
|
||||
|
||||
void resultMustEqual(double expected) {
|
||||
EXPECT_DOUBLE_EQ(expected, _result.as<double>());
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void resultTypeMustBe() {
|
||||
EXPECT_TRUE(_result.is<T>());
|
||||
}
|
||||
|
||||
void resultMustBeInvalid() { EXPECT_FALSE(_result.success()); }
|
||||
void resultMustBeValid() { EXPECT_TRUE(_result.success()); }
|
||||
void resultMustBeInvalid() {
|
||||
EXPECT_FALSE(_result.success());
|
||||
}
|
||||
void resultMustBeValid() {
|
||||
EXPECT_TRUE(_result.success());
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void verify(const char* input, T expected) {
|
||||
whenInputIs(input);
|
||||
resultMustBeValid();
|
||||
resultTypeMustBe<T>();
|
||||
resultMustEqual(expected);
|
||||
}
|
||||
|
||||
private:
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
@ -51,41 +67,39 @@ TEST_F(JsonParser_Variant_Test, EmptyArray) {
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Variant_Test, Integer) {
|
||||
whenInputIs("42");
|
||||
resultMustBeValid();
|
||||
resultTypeMustBe<int>();
|
||||
resultMustEqual(42);
|
||||
verify("42", 42);
|
||||
verify("-42", -42);
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Variant_Test, Double) {
|
||||
whenInputIs("3.14");
|
||||
resultMustBeValid();
|
||||
resultTypeMustBe<double>();
|
||||
resultMustEqual(3.14);
|
||||
verify("3.14", 3.14);
|
||||
verify("3.14", 3.14);
|
||||
verify("1E+10", 1E+10);
|
||||
verify("-1E+10", -1E+10);
|
||||
verify("1.234E+10", 1.234E+10);
|
||||
verify("1.79769e+308", 1.79769e+308);
|
||||
verify("-1.79769e+308", -1.79769e+308);
|
||||
verify("1.7976931348623157e+308", 1.7976931348623157e+308);
|
||||
verify("0.017976931348623157e+310", 0.017976931348623157e+310);
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Variant_Test, String) {
|
||||
whenInputIs("\"hello world\"");
|
||||
resultMustBeValid();
|
||||
resultTypeMustBe<char*>();
|
||||
resultMustEqual("hello world");
|
||||
verify("\"hello world\"", "hello world");
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Variant_Test, True) {
|
||||
whenInputIs("true");
|
||||
resultMustBeValid();
|
||||
resultTypeMustBe<bool>();
|
||||
resultMustEqual(true);
|
||||
verify("true", true);
|
||||
verify("false", false);
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Variant_Test, False) {
|
||||
whenInputIs("false");
|
||||
resultMustBeValid();
|
||||
resultTypeMustBe<bool>();
|
||||
resultMustEqual(false);
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Variant_Test, Invalid) {
|
||||
TEST_F(JsonParser_Variant_Test, OpenBrace) {
|
||||
whenInputIs("{");
|
||||
resultMustBeInvalid();
|
||||
}
|
||||
|
||||
TEST_F(JsonParser_Variant_Test, IncompleteStrings) {
|
||||
verify("\"", "");
|
||||
verify("\"hello", "hello");
|
||||
verify("\'", "");
|
||||
verify("\'world", "world");
|
||||
}
|
||||
|
@ -24,7 +24,7 @@ TEST(JsonVariant_As_Tests, DoubleAsCstr) {
|
||||
|
||||
TEST(JsonVariant_As_Tests, DoubleAsString) {
|
||||
JsonVariant variant = 4.2;
|
||||
ASSERT_EQ(String("4.20"), variant.as<String>());
|
||||
ASSERT_EQ(std::string("4.20"), variant.as<std::string>());
|
||||
}
|
||||
|
||||
TEST(JsonVariant_As_Tests, DoubleAsLong) {
|
||||
@ -64,7 +64,7 @@ TEST(JsonVariant_As_Tests, FalseAsLong) {
|
||||
|
||||
TEST(JsonVariant_As_Tests, FalseAsString) {
|
||||
JsonVariant variant = false;
|
||||
ASSERT_EQ(String("false"), variant.as<String>());
|
||||
ASSERT_EQ(std::string("false"), variant.as<std::string>());
|
||||
}
|
||||
|
||||
TEST(JsonVariant_As_Tests, TrueAsBool) {
|
||||
@ -84,7 +84,7 @@ TEST(JsonVariant_As_Tests, TrueAsLong) {
|
||||
|
||||
TEST(JsonVariant_As_Tests, TrueAsString) {
|
||||
JsonVariant variant = true;
|
||||
ASSERT_EQ(String("true"), variant.as<String>());
|
||||
ASSERT_EQ(std::string("true"), variant.as<std::string>());
|
||||
}
|
||||
|
||||
TEST(JsonVariant_As_Tests, LongAsBool) {
|
||||
@ -109,7 +109,7 @@ TEST(JsonVariant_As_Tests, NegativeLongAsDouble) {
|
||||
|
||||
TEST(JsonVariant_As_Tests, LongAsString) {
|
||||
JsonVariant variant = 42L;
|
||||
ASSERT_EQ(String("42"), variant.as<String>());
|
||||
ASSERT_EQ(std::string("42"), variant.as<std::string>());
|
||||
}
|
||||
|
||||
TEST(JsonVariant_As_Tests, LongZeroAsDouble) {
|
||||
@ -134,7 +134,7 @@ TEST(JsonVariant_As_Tests, NullAsLong) {
|
||||
|
||||
TEST(JsonVariant_As_Tests, NullAsString) {
|
||||
JsonVariant variant = null;
|
||||
ASSERT_EQ(String("null"), variant.as<String>());
|
||||
ASSERT_EQ(std::string("null"), variant.as<std::string>());
|
||||
}
|
||||
|
||||
TEST(JsonVariant_As_Tests, NumberStringAsBool) {
|
||||
@ -181,7 +181,7 @@ TEST(JsonVariant_As_Tests, RandomStringAsCharPtr) {
|
||||
|
||||
TEST(JsonVariant_As_Tests, RandomStringAsString) {
|
||||
JsonVariant variant = "hello";
|
||||
ASSERT_EQ(String("hello"), variant.as<String>());
|
||||
ASSERT_EQ(std::string("hello"), variant.as<std::string>());
|
||||
}
|
||||
|
||||
TEST(JsonVariant_As_Tests, TrueStringAsBool) {
|
||||
@ -201,7 +201,7 @@ TEST(JsonVariant_As_Tests, ObjectAsString) {
|
||||
obj["key"] = "value";
|
||||
|
||||
JsonVariant variant = obj;
|
||||
ASSERT_EQ(String("{\"key\":\"value\"}"), variant.as<String>());
|
||||
ASSERT_EQ(std::string("{\"key\":\"value\"}"), variant.as<std::string>());
|
||||
}
|
||||
|
||||
TEST(JsonVariant_As_Tests, ArrayAsString) {
|
||||
@ -212,7 +212,7 @@ TEST(JsonVariant_As_Tests, ArrayAsString) {
|
||||
arr.add(2);
|
||||
|
||||
JsonVariant variant = arr;
|
||||
ASSERT_EQ(String("[4,2]"), variant.as<String>());
|
||||
ASSERT_EQ(std::string("[4,2]"), variant.as<std::string>());
|
||||
}
|
||||
|
||||
TEST(JsonVariant_As_Tests, ArrayAsJsonArray) {
|
||||
|
@ -5,8 +5,8 @@
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
class JsonVariant_Comparison_Tests : public ::testing::Test {
|
||||
protected:
|
||||
@ -92,3 +92,37 @@ TEST_F(JsonVariant_Comparison_Tests, ULong) {
|
||||
TEST_F(JsonVariant_Comparison_Tests, UShort) {
|
||||
testValue<unsigned short>(122, 123, 124);
|
||||
}
|
||||
|
||||
TEST_F(JsonVariant_Comparison_Tests, StringLiteral) {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonVariant variant = jsonBuffer.parse("\"hello\"");
|
||||
|
||||
ASSERT_TRUE(variant == "hello");
|
||||
ASSERT_FALSE(variant != "hello");
|
||||
|
||||
ASSERT_TRUE(variant != "world");
|
||||
ASSERT_FALSE(variant == "world");
|
||||
|
||||
ASSERT_TRUE("hello" == variant);
|
||||
ASSERT_FALSE("hello" != variant);
|
||||
|
||||
ASSERT_TRUE("world" != variant);
|
||||
ASSERT_FALSE("world" == variant);
|
||||
}
|
||||
|
||||
TEST_F(JsonVariant_Comparison_Tests, String) {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonVariant variant = jsonBuffer.parse("\"hello\"");
|
||||
|
||||
ASSERT_TRUE(variant == std::string("hello"));
|
||||
ASSERT_FALSE(variant != std::string("hello"));
|
||||
|
||||
ASSERT_TRUE(variant != std::string("world"));
|
||||
ASSERT_FALSE(variant == std::string("world"));
|
||||
|
||||
ASSERT_TRUE(std::string("hello") == variant);
|
||||
ASSERT_FALSE(std::string("hello") != variant);
|
||||
|
||||
ASSERT_TRUE(std::string("world") != variant);
|
||||
ASSERT_FALSE(std::string("world") == variant);
|
||||
}
|
||||
|
@ -5,8 +5,8 @@
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#define SUITE JsonVariant_Is_Tests
|
||||
|
||||
@ -34,75 +34,229 @@ void assertIs(JsonArray& value) {
|
||||
ASSERT_TRUE(variant.is<TTo>());
|
||||
}
|
||||
|
||||
TEST(SUITE, ArrayIsArry) { assertIs<JsonArray&>(JsonArray::invalid()); }
|
||||
TEST(SUITE, ArrayIsBool) { assertIsNot<bool>(JsonArray::invalid()); }
|
||||
TEST(SUITE, ArrayIsDouble) { assertIsNot<double>(JsonArray::invalid()); }
|
||||
TEST(SUITE, ArrayIsFloat) { assertIsNot<float>(JsonArray::invalid()); }
|
||||
TEST(SUITE, ArrayIsInt) { assertIsNot<int>(JsonArray::invalid()); }
|
||||
TEST(SUITE, ArrayIsLong) { assertIsNot<long>(JsonArray::invalid()); }
|
||||
TEST(SUITE, ArrayIsString) { assertIsNot<const char*>(JsonArray::invalid()); }
|
||||
TEST(SUITE, ArrayIsArray) {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
assertIs<JsonArray&>(jsonBuffer.createArray());
|
||||
}
|
||||
TEST(SUITE, ArrayIsNotBool) {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
assertIsNot<bool>(jsonBuffer.createArray());
|
||||
}
|
||||
TEST(SUITE, ArrayIsNotDouble) {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
assertIsNot<double>(jsonBuffer.createArray());
|
||||
}
|
||||
TEST(SUITE, ArrayIsNotFloat) {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
assertIsNot<float>(jsonBuffer.createArray());
|
||||
}
|
||||
TEST(SUITE, ArrayIsNotInt) {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
assertIsNot<int>(jsonBuffer.createArray());
|
||||
}
|
||||
TEST(SUITE, ArrayIsNotLong) {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
assertIsNot<long>(jsonBuffer.createArray());
|
||||
}
|
||||
TEST(SUITE, ArrayIsNotString) {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
assertIsNot<const char*>(jsonBuffer.createArray());
|
||||
}
|
||||
|
||||
TEST(SUITE, BoolIsArray) { assertIsNot<JsonArray&>(true); }
|
||||
TEST(SUITE, BoolIsBool) { assertIs<bool>(true); }
|
||||
TEST(SUITE, BoolIsDouble) { assertIsNot<double>(true); }
|
||||
TEST(SUITE, BoolIsFloat) { assertIsNot<float>(true); }
|
||||
TEST(SUITE, BoolIsInt) { assertIsNot<int>(true); }
|
||||
TEST(SUITE, BoolIsLong) { assertIsNot<long>(true); }
|
||||
TEST(SUITE, BoolIsString) { assertIsNot<const char*>(true); }
|
||||
TEST(SUITE, BoolIsArray) {
|
||||
assertIsNot<JsonArray&>(true);
|
||||
}
|
||||
TEST(SUITE, BoolIsBool) {
|
||||
assertIs<bool>(true);
|
||||
}
|
||||
TEST(SUITE, BoolIsDouble) {
|
||||
assertIsNot<double>(true);
|
||||
}
|
||||
TEST(SUITE, BoolIsFloat) {
|
||||
assertIsNot<float>(true);
|
||||
}
|
||||
TEST(SUITE, BoolIsInt) {
|
||||
assertIsNot<int>(true);
|
||||
}
|
||||
TEST(SUITE, BoolIsLong) {
|
||||
assertIsNot<long>(true);
|
||||
}
|
||||
TEST(SUITE, BoolIsString) {
|
||||
assertIsNot<const char*>(true);
|
||||
}
|
||||
|
||||
TEST(SUITE, DoubleIsArray) { assertIsNot<JsonArray&>(4.2); }
|
||||
TEST(SUITE, DoubleIsBool) { assertIsNot<bool>(4.2); }
|
||||
TEST(SUITE, DoubleIsDouble) { assertIs<double>(4.2); }
|
||||
TEST(SUITE, DoubleIsFloat) { assertIs<float>(4.2); }
|
||||
TEST(SUITE, DoubleIsInt) { assertIsNot<int>(4.2); }
|
||||
TEST(SUITE, DoubleIsLong) { assertIsNot<long>(4.2); }
|
||||
TEST(SUITE, DoubleIsString) { assertIsNot<const char*>(4.2); }
|
||||
TEST(SUITE, DoubleIsArray) {
|
||||
assertIsNot<JsonArray&>(4.2);
|
||||
}
|
||||
TEST(SUITE, DoubleIsBool) {
|
||||
assertIsNot<bool>(4.2);
|
||||
}
|
||||
TEST(SUITE, DoubleIsDouble) {
|
||||
assertIs<double>(4.2);
|
||||
}
|
||||
TEST(SUITE, DoubleIsFloat) {
|
||||
assertIs<float>(4.2);
|
||||
}
|
||||
TEST(SUITE, DoubleIsInt) {
|
||||
assertIsNot<int>(4.2);
|
||||
}
|
||||
TEST(SUITE, DoubleIsLong) {
|
||||
assertIsNot<long>(4.2);
|
||||
}
|
||||
TEST(SUITE, DoubleIsString) {
|
||||
assertIsNot<const char*>(4.2);
|
||||
}
|
||||
|
||||
TEST(SUITE, LongIsArray) { assertIsNot<JsonArray&>(42L); }
|
||||
TEST(SUITE, LongIsBool) { assertIsNot<bool>(42L); }
|
||||
TEST(SUITE, LongIsDouble) { assertIsNot<double>(42L); }
|
||||
TEST(SUITE, LongIsFloat) { assertIsNot<float>(42L); }
|
||||
TEST(SUITE, LongIsInt) { assertIs<int>(42L); }
|
||||
TEST(SUITE, LongIsLong) { assertIs<long>(42L); }
|
||||
TEST(SUITE, LongIsString) { assertIsNot<const char*>(42L); }
|
||||
TEST(SUITE, LongIsArray) {
|
||||
assertIsNot<JsonArray&>(42L);
|
||||
}
|
||||
TEST(SUITE, LongIsBool) {
|
||||
assertIsNot<bool>(42L);
|
||||
}
|
||||
TEST(SUITE, LongIsDouble) {
|
||||
assertIsNot<double>(42L);
|
||||
}
|
||||
TEST(SUITE, LongIsFloat) {
|
||||
assertIsNot<float>(42L);
|
||||
}
|
||||
TEST(SUITE, LongIsInt) {
|
||||
assertIs<int>(42L);
|
||||
}
|
||||
TEST(SUITE, LongIsLong) {
|
||||
assertIs<long>(42L);
|
||||
}
|
||||
TEST(SUITE, LongIsString) {
|
||||
assertIsNot<const char*>(42L);
|
||||
}
|
||||
|
||||
TEST(SUITE, StringIsArray) { assertIsNot<JsonArray&>("42"); }
|
||||
TEST(SUITE, StringIsBool) { assertIsNot<bool>("42"); }
|
||||
TEST(SUITE, StringIsDouble) { assertIsNot<double>("42"); }
|
||||
TEST(SUITE, StringIsFloat) { assertIsNot<float>("42"); }
|
||||
TEST(SUITE, StringIsInt) { assertIsNot<int>("42"); }
|
||||
TEST(SUITE, StringIsLong) { assertIsNot<long>("42"); }
|
||||
TEST(SUITE, StringIsString) { assertIs<const char*>("42"); }
|
||||
TEST(SUITE, StringIsArray) {
|
||||
assertIsNot<JsonArray&>("42");
|
||||
}
|
||||
TEST(SUITE, StringIsBool) {
|
||||
assertIsNot<bool>("42");
|
||||
}
|
||||
TEST(SUITE, StringIsDouble) {
|
||||
assertIsNot<double>("42");
|
||||
}
|
||||
TEST(SUITE, StringIsFloat) {
|
||||
assertIsNot<float>("42");
|
||||
}
|
||||
TEST(SUITE, StringIsInt) {
|
||||
assertIsNot<int>("42");
|
||||
}
|
||||
TEST(SUITE, StringIsLong) {
|
||||
assertIsNot<long>("42");
|
||||
}
|
||||
TEST(SUITE, StringIsString) {
|
||||
assertIs<const char*>("42");
|
||||
}
|
||||
|
||||
TEST(SUITE, UnparsedTrueIsArra) { assertIsNot<JsonArray&>(RawJson("true")); }
|
||||
TEST(SUITE, UnparsedTrueIsBool) { assertIs<bool>(RawJson("true")); }
|
||||
TEST(SUITE, UnparsedTrueIsDouble) { assertIsNot<double>(RawJson("true")); }
|
||||
TEST(SUITE, UnparsedTrueIsFloat) { assertIsNot<float>(RawJson("true")); }
|
||||
TEST(SUITE, UnparsedTrueIsInt) { assertIsNot<int>(RawJson("true")); }
|
||||
TEST(SUITE, UnparsedTrueIsLong) { assertIsNot<long>(RawJson("true")); }
|
||||
TEST(SUITE, UnparsedTrueIsString) { assertIsNot<const char*>(RawJson("true")); }
|
||||
TEST(SUITE, UnparsedTrueIsArray) {
|
||||
assertIsNot<JsonArray&>(RawJson("true"));
|
||||
}
|
||||
TEST(SUITE, UnparsedTrueIsBool) {
|
||||
assertIs<bool>(RawJson("true"));
|
||||
}
|
||||
TEST(SUITE, UnparsedTrueIsDouble) {
|
||||
assertIsNot<double>(RawJson("true"));
|
||||
}
|
||||
TEST(SUITE, UnparsedTrueIsFloat) {
|
||||
assertIsNot<float>(RawJson("true"));
|
||||
}
|
||||
TEST(SUITE, UnparsedTrueIsInt) {
|
||||
assertIsNot<int>(RawJson("true"));
|
||||
}
|
||||
TEST(SUITE, UnparsedTrueIsLong) {
|
||||
assertIsNot<long>(RawJson("true"));
|
||||
}
|
||||
TEST(SUITE, UnparsedTrueIsString) {
|
||||
assertIsNot<const char*>(RawJson("true"));
|
||||
}
|
||||
|
||||
TEST(SUITE, UnparsedFalseIsArra) { assertIsNot<JsonArray&>(RawJson("false")); }
|
||||
TEST(SUITE, UnparsedFalseIsBool) { assertIs<bool>(RawJson("false")); }
|
||||
TEST(SUITE, UnparsedFalseIsDouble) { assertIsNot<double>(RawJson("false")); }
|
||||
TEST(SUITE, UnparsedFalseIsFloat) { assertIsNot<float>(RawJson("false")); }
|
||||
TEST(SUITE, UnparsedFalseIsInt) { assertIsNot<int>(RawJson("false")); }
|
||||
TEST(SUITE, UnparsedFalseIsLong) { assertIsNot<long>(RawJson("false")); }
|
||||
TEST(SUITE, UnparsedFalseIsArray) {
|
||||
assertIsNot<JsonArray&>(RawJson("false"));
|
||||
}
|
||||
TEST(SUITE, UnparsedFalseIsBool) {
|
||||
assertIs<bool>(RawJson("false"));
|
||||
}
|
||||
TEST(SUITE, UnparsedFalseIsDouble) {
|
||||
assertIsNot<double>(RawJson("false"));
|
||||
}
|
||||
TEST(SUITE, UnparsedFalseIsFloat) {
|
||||
assertIsNot<float>(RawJson("false"));
|
||||
}
|
||||
TEST(SUITE, UnparsedFalseIsInt) {
|
||||
assertIsNot<int>(RawJson("false"));
|
||||
}
|
||||
TEST(SUITE, UnparsedFalseIsLong) {
|
||||
assertIsNot<long>(RawJson("false"));
|
||||
}
|
||||
TEST(SUITE, UnparsedFalseIsString) {
|
||||
assertIsNot<const char*>(RawJson("false"));
|
||||
}
|
||||
|
||||
TEST(SUITE, UnparsedIntIsArra) { assertIsNot<JsonArray&>(RawJson("42")); }
|
||||
TEST(SUITE, UnparsedIntIsBool) { assertIsNot<bool>(RawJson("42")); }
|
||||
TEST(SUITE, UnparsedIntIsDouble) { assertIsNot<double>(RawJson("42")); }
|
||||
TEST(SUITE, UnparsedIntIsFloat) { assertIsNot<float>(RawJson("42")); }
|
||||
TEST(SUITE, UnparsedIntIsInt) { assertIs<int>(RawJson("42")); }
|
||||
TEST(SUITE, UnparsedIntIsLong) { assertIs<long>(RawJson("42")); }
|
||||
TEST(SUITE, UnparsedIntIsString) { assertIsNot<const char*>(RawJson("42")); }
|
||||
TEST(SUITE, UnparsedIntIsArray) {
|
||||
assertIsNot<JsonArray&>(RawJson("42"));
|
||||
}
|
||||
TEST(SUITE, UnparsedIntIsBool) {
|
||||
assertIsNot<bool>(RawJson("42"));
|
||||
}
|
||||
TEST(SUITE, UnparsedIntIsDouble) {
|
||||
assertIsNot<double>(RawJson("42"));
|
||||
}
|
||||
TEST(SUITE, UnparsedIntIsFloat) {
|
||||
assertIsNot<float>(RawJson("42"));
|
||||
}
|
||||
TEST(SUITE, UnparsedIntIsInt) {
|
||||
assertIs<int>(RawJson("42"));
|
||||
}
|
||||
TEST(SUITE, UnparsedIntIsLong) {
|
||||
assertIs<long>(RawJson("42"));
|
||||
}
|
||||
TEST(SUITE, UnparsedIntIsString) {
|
||||
assertIsNot<const char*>(RawJson("42"));
|
||||
}
|
||||
|
||||
TEST(SUITE, UnparsedFloatIsBool) { assertIsNot<bool>(RawJson("4.2e-10")); }
|
||||
TEST(SUITE, UnparsedFloatIsDouble) { assertIs<double>(RawJson("4.2e-10")); }
|
||||
TEST(SUITE, UnparsedFloatIsFloat) { assertIs<float>(RawJson("4.2e-10")); }
|
||||
TEST(SUITE, UnparsedFloatIsInt) { assertIsNot<int>(RawJson("4.2e-10")); }
|
||||
TEST(SUITE, UnparsedFloatIsLong) { assertIsNot<long>(RawJson("4.2e-10")); }
|
||||
TEST(SUITE, UnparsedFloatIsStr) { assertIsNot<const char*>(RawJson("4.2")); }
|
||||
TEST(SUITE, UnparsedFloatIsBool) {
|
||||
assertIsNot<bool>(RawJson("4.2e-10"));
|
||||
}
|
||||
TEST(SUITE, UnparsedFloatIsDouble) {
|
||||
assertIs<double>(RawJson("4.2e-10"));
|
||||
}
|
||||
TEST(SUITE, UnparsedFloatIsFloat) {
|
||||
assertIs<float>(RawJson("4.2e-10"));
|
||||
}
|
||||
TEST(SUITE, UnparsedFloatIsInt) {
|
||||
assertIsNot<int>(RawJson("4.2e-10"));
|
||||
}
|
||||
TEST(SUITE, UnparsedFloatIsLong) {
|
||||
assertIsNot<long>(RawJson("4.2e-10"));
|
||||
}
|
||||
TEST(SUITE, UnparsedFloatIsStr) {
|
||||
assertIsNot<const char*>(RawJson("4.2"));
|
||||
}
|
||||
|
||||
TEST(SUITE, UnparsedNullIsArray) {
|
||||
assertIsNot<JsonArray&>(RawJson("null"));
|
||||
}
|
||||
TEST(SUITE, UnparsedNullIsBool) {
|
||||
assertIsNot<bool>(RawJson("null"));
|
||||
}
|
||||
TEST(SUITE, UnparsedNullIsDouble) {
|
||||
assertIsNot<double>(RawJson("null"));
|
||||
}
|
||||
TEST(SUITE, UnparsedNullIsFloat) {
|
||||
assertIsNot<float>(RawJson("null"));
|
||||
}
|
||||
TEST(SUITE, UnparsedNullIsInt) {
|
||||
assertIsNot<int>(RawJson("null"));
|
||||
}
|
||||
TEST(SUITE, UnparsedNullIsLong) {
|
||||
assertIsNot<long>(RawJson("null"));
|
||||
}
|
||||
TEST(SUITE, UnparsedNullIsConstCharPtr) {
|
||||
assertIs<const char*>(RawJson("null"));
|
||||
}
|
||||
TEST(SUITE, UnparsedNullIsCharPtr) {
|
||||
assertIs<char*>(RawJson("null"));
|
||||
}
|
||||
|
@ -5,8 +5,8 @@
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
class JsonVariant_Subscript_Tests : public ::testing::Test {
|
||||
protected:
|
||||
@ -24,6 +24,9 @@ TEST_F(JsonVariant_Subscript_Tests, Array) {
|
||||
EXPECT_EQ(2, _variant.size());
|
||||
EXPECT_STREQ("element at index 0", _variant[0].asString());
|
||||
EXPECT_STREQ("element at index 1", _variant[1].asString());
|
||||
EXPECT_STREQ(
|
||||
"element at index 0",
|
||||
_variant[static_cast<unsigned char>(0)].asString()); // issue #381
|
||||
EXPECT_FALSE(_variant[-1].success());
|
||||
EXPECT_FALSE(_variant[3].success());
|
||||
EXPECT_FALSE(_variant["0"].success());
|
||||
@ -56,3 +59,25 @@ TEST_F(JsonVariant_Subscript_Tests, String) {
|
||||
EXPECT_FALSE(_variant["0"].success());
|
||||
EXPECT_FALSE(_variant[0].success());
|
||||
}
|
||||
|
||||
TEST_F(JsonVariant_Subscript_Tests, ObjectSetValue) {
|
||||
_variant = _jsonBuffer.createObject();
|
||||
_variant["hello"] = "world";
|
||||
EXPECT_EQ(1, _variant.size());
|
||||
EXPECT_STREQ("world", _variant["hello"]);
|
||||
}
|
||||
|
||||
TEST_F(JsonVariant_Subscript_Tests, ArraySetValue) {
|
||||
_variant = _jsonBuffer.parseArray("[\"hello\"]");
|
||||
_variant[0] = "world";
|
||||
EXPECT_EQ(1, _variant.size());
|
||||
EXPECT_STREQ("world", _variant[0]);
|
||||
}
|
||||
|
||||
TEST_F(JsonVariant_Subscript_Tests, NestedObjectSetValue) {
|
||||
_variant = _jsonBuffer.parseArray("[{}]");
|
||||
_variant[0]["hello"] = "world";
|
||||
EXPECT_EQ(1, _variant.size());
|
||||
EXPECT_EQ(1, _variant[0].size());
|
||||
EXPECT_STREQ("world", _variant[0]["hello"]);
|
||||
}
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user