diff --git a/examples/FirebasePush_ESP8266/FirebasePush_ESP8266.ino b/examples/FirebasePush_ESP8266/FirebasePush_ESP8266.ino index 3584102e..37cbea2e 100644 --- a/examples/FirebasePush_ESP8266/FirebasePush_ESP8266.ino +++ b/examples/FirebasePush_ESP8266/FirebasePush_ESP8266.ino @@ -22,7 +22,6 @@ // create firebase client. Firebase fbase = Firebase("example.firebaseio.com") .auth("secret_or_token"); - void setup() { Serial.begin(9600); @@ -46,7 +45,7 @@ void setup() { } // print key. - Serial.println(push.name()); + Serial.println("Name: " + push.name()); // get all entries. FirebaseGet get = fbase.get("/logs"); @@ -55,8 +54,9 @@ void setup() { Serial.println(push.error().message()); return; } - // print json. - Serial.println(get.json()); + // Print written timestamp. + String data = get.json()[push.name()]; + Serial.println("Timestamp:" + data); } void loop() { diff --git a/library.properties b/library.properties new file mode 100644 index 00000000..a12130c2 --- /dev/null +++ b/library.properties @@ -0,0 +1,9 @@ +name=firebase-arduino +version=0.4 +author= +maintainer= +sentence=Fill in later +paragraph=Fill in later +category=Communication +url=http://example.com/ +architectures=esp8266 diff --git a/Firebase.cpp b/src/Firebase.cpp similarity index 95% rename from Firebase.cpp rename to src/Firebase.cpp index 154a72f3..701fa0dd 100644 --- a/Firebase.cpp +++ b/src/Firebase.cpp @@ -118,15 +118,18 @@ FirebaseCall::FirebaseCall(const String& host, const String& auth, } } +const JsonObject& FirebaseCall::json() { + //TODO(edcoyne): This is not efficient, we should do something smarter with + //the buffers. + buffer_ = DynamicJsonBuffer(); + return buffer_.parseObject(response()); +} + // FirebaseGet FirebaseGet::FirebaseGet(const String& host, const String& auth, const String& path, HTTPClient* http) : FirebaseCall(host, auth, "GET", path, "", http) { - if (!error()) { - // TODO: parse json - json_ = response(); - } } // FirebaseSet @@ -145,8 +148,7 @@ FirebasePush::FirebasePush(const String& host, const String& auth, HTTPClient* http) : FirebaseCall(host, auth, "POST", path, value, http) { if (!error()) { - // TODO: parse name - name_ = response(); + name_ = json()["name"].as(); } } diff --git a/Firebase.h b/src/Firebase.h similarity index 96% rename from Firebase.h rename to src/Firebase.h index 5691038d..92ddea86 100644 --- a/Firebase.h +++ b/src/Firebase.h @@ -24,6 +24,7 @@ #include #include #include +#include "third-party/arduino-json-5.1.1/include/ArduinoJson.h" class FirebaseGet; class FirebaseSet; @@ -81,13 +82,18 @@ class FirebaseCall { const FirebaseError& error() const { return error_; } + const String& response() { return response_; } + + const JsonObject& json(); + protected: HTTPClient* http_; FirebaseError error_; String response_; + DynamicJsonBuffer buffer_; }; class FirebaseGet : public FirebaseCall { @@ -95,10 +101,6 @@ class FirebaseGet : public FirebaseCall { FirebaseGet() {} FirebaseGet(const String& host, const String& auth, const String& path, HTTPClient* http = NULL); - - const String& json() const { - return json_; - } private: String json_; @@ -110,10 +112,6 @@ class FirebaseSet: public FirebaseCall { FirebaseSet(const String& host, const String& auth, const String& path, const String& value, HTTPClient* http = NULL); - const String& json() const { - return json_; - } - private: String json_; }; @@ -145,7 +143,7 @@ class FirebaseStream : public FirebaseCall { FirebaseStream() {} FirebaseStream(const String& host, const String& auth, const String& path, HTTPClient* http = NULL); - + // Return if there is any event available to read. bool available(); @@ -162,7 +160,7 @@ class FirebaseStream : public FirebaseCall { const FirebaseError& error() const { return _error; } - + private: FirebaseError _error; }; diff --git a/src/third-party/README.md b/src/third-party/README.md new file mode 100644 index 00000000..76816743 --- /dev/null +++ b/src/third-party/README.md @@ -0,0 +1,2 @@ +These are packages that we depend upon. They may need to be trimmed, all .cpp +files in them will be compiled by the arduino IDE. diff --git a/src/third-party/arduino-json-5.1.1/CHANGELOG.md b/src/third-party/arduino-json-5.1.1/CHANGELOG.md new file mode 100644 index 00000000..7ea8888c --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/CHANGELOG.md @@ -0,0 +1,244 @@ +ArduinoJson: change log +======================= + +v5.1.1 +------ + +* Removed `String` duplication when one replaces a value in a `JsonObject` (PR #232 by @ulion) + +v5.1.0 +------ + +* Added support of `long long` (issue #171) +* Moved all build settings to `ArduinoJson/Configuration.hpp` + +**BREAKING CHANGE**: +If you defined `ARDUINOJSON_ENABLE_STD_STREAM`, you now need to define it to `1`. + +v5.0.8 +------ + +* Made the library compatible with [PlatformIO](http://platformio.org/) (issue #181) +* Fixed `JsonVariant::is()` that was incorrectly returning false (issue #214) + +v5.0.7 +------ + +* Made library easier to use from a CMake project: simply `add_subdirectory(ArduinoJson/src)` +* Changed `String` to be a `typedef` of `std::string` (issues #142 and #161) + +**BREAKING CHANGES**: +- `JsonVariant(true).as()` now returns `"true"` instead of `"1"` +- `JsonVariant(false).as()` now returns `"false"` instead of `"0"` + +v5.0.6 +------ + +* Added parameter to `DynamicJsonBuffer` constructor to set initial size (issue #152) +* Fixed warning about library category in Arduino 1.6.6 (issue #147) +* Examples: Added a loop to wait for serial port to be ready (issue #156) + +v5.0.5 +------ + +* Added overload `JsonObjectSuscript::set(value, decimals)` (issue #143) +* Use `float` instead of `double` to reduce the size of `JsonVariant` (issue #134) + +v5.0.4 +------ + +* Fixed ambiguous overload with `JsonArraySubscript` and `JsonObjectSubscript` (issue #122) + +v5.0.3 +------ + +* Fixed `printTo(String)` which wrote numbers instead of strings (issue #120) +* Fixed return type of `JsonArray::is()` and some others (issue #121) + +v5.0.2 +------ + +* 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) +* Fixed memory alignment, which made ESP8266 crash (issue #104) +* Fixed compilation on Visual Studio 2010 and 2012 (issue #107) + +v5.0.1 +------ + +* Fixed compilation with Arduino 1.0.6 (issue #99) + +v5.0.0 +------ + +* Added support of `String` class (issues #55, #56, #70, #77) +* Added `JsonBuffer::strdup()` to make a copy of a string (issues #10, #57) +* Implicitly call `strdup()` for `String` but not for `char*` (issues #84, #87) +* Added support of non standard JSON input (issue #44) +* Added support of comments in JSON input (issue #88) +* Added implicit cast between numerical types (issues #64, #69, #93) +* Added ability to read number values as string (issue #90) +* Redesigned `JsonVariant` to leverage converting constructors instead of assignment operators (issue #66) +* Switched to new the library layout (requires Arduino 1.0.6 or above) + +**BREAKING CHANGES**: +- `JsonObject::add()` was renamed to `set()` +- `JsonArray::at()` and `JsonObject::at()` were renamed to `get()` +- Number of digits of floating point value are now set with `double_with_n_digits()` + +**Personal note about the `String` class**: +Support of the `String` class has been added to the library because many people use it in their programs. +However, you should not see this as an invitation to use the `String` class. +The `String` class is **bad** because it uses dynamic memory allocation. +Compared to static allocation, it compiles to a bigger, slower program, and is less predictable. +You certainly don't want that in an embedded environment! + +v4.6 +---- + +* Fixed segmentation fault in `DynamicJsonBuffer` when memory allocation fails (issue #92) + +v4.5 +---- + +* Fixed buffer overflow when input contains a backslash followed by a terminator (issue #81) + +**Upgrading is recommended** since previous versions contain a potential security risk. + +Special thanks to [Giancarlo Canales Barreto](https://github.com/gcanalesb) for finding this nasty bug. + +v4.4 +---- + +* Added `JsonArray::measureLength()` and `JsonObject::measureLength()` (issue #75) + +v4.3 +---- + +* Added `JsonArray::removeAt()` to remove an element of an array (issue #58) +* Fixed stack-overflow in `DynamicJsonBuffer` when parsing huge JSON files (issue #65) +* Fixed wrong return value of `parseArray()` and `parseObject()` when allocation fails (issue #68) + +v4.2 +---- + +* Switched back to old library layout (issues #39, #43 and #45) +* Removed global new operator overload (issue #40, #45 and #46) +* Added an example with EthernetServer + +v4.1 +---- + +* Added DynamicJsonBuffer (issue #19) + +v4.0 +---- + +* Unified parser and generator API (issue #23) +* Updated library layout, now requires Arduino 1.0.6 or newer + +**BREAKING CHANGE**: API changed significantly, see [Migrating code to the new API](https://github.com/bblanchon/ArduinoJson/wiki/Migrating-code-to-the-new-API). + + +v3.4 +---- + +* Fixed escaped char parsing (issue #16) + + +v3.3 +---- + +* Added indented output for the JSON generator (issue #11), see example bellow. +* Added `IndentedPrint`, a decorator for `Print` to allow indented output + +Example: + + JsonOject<2> json; + json["key"] = "value"; + json.prettyPrintTo(Serial); + +v3.2 +---- + +* Fixed a bug when adding nested object in `JsonArray` (bug introduced in v3.1). + +v3.1 +---- + +* Calling `Generator::JsonObject::add()` twice with the same `key` now replaces the `value` +* Added `Generator::JsonObject::operator[]`, see bellow the new API +* Added `Generator::JsonObject::remove()` (issue #9) + +Old generator API: + + JsonObject<3> root; + root.add("sensor", "gps"); + root.add("time", 1351824120); + root.add("data", array); + +New generator API: + + JsonObject<3> root; + root["sensor"] = "gps"; + root["time"] = 1351824120; + root["data"] = array; + +v3.0 +---- + +* New parser API, see bellow +* Renamed `JsonHashTable` into `JsonObject` +* Added iterators for `JsonArray` and `JsonObject` (issue #4) + +Old parser API: + + JsonHashTable root = parser.parseHashTable(json); + + char* sensor = root.getString("sensor"); + long time = root.getLong("time"); + double latitude = root.getArray("data").getDouble(0); + double longitude = root.getArray("data").getDouble(1); + +New parser API: + + JsonObject root = parser.parse(json); + + char* sensor = root["sensor"]; + long time = root["time"]; + double latitude = root["data"][0]; + double longitude = root["data"][1]; + +v2.1 +---- + +* Fixed case `#include "jsmn.cpp"` which caused an error in Linux (issue #6) +* Fixed a buffer overrun in JSON Parser (issue #5) + +v2.0 +---- + +* Added JSON encoding (issue #2) +* Renamed the library `ArduinoJsonParser` becomes `ArduinoJson` + +**Breaking change**: you need to add the following line at the top of your program. + + using namespace ArduinoJson::Parser; + +v1.2 +---- + +* Fixed error in JSON parser example (issue #1) + +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 +---- + +Initial release diff --git a/src/third-party/arduino-json-5.1.1/LICENSE.md b/src/third-party/arduino-json-5.1.1/LICENSE.md new file mode 100644 index 00000000..fea79d80 --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/LICENSE.md @@ -0,0 +1,10 @@ +The MIT License (MIT) +--------------------- + +Copyright © 2014-2016 Benoit BLANCHON + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/src/third-party/arduino-json-5.1.1/README.md b/src/third-party/arduino-json-5.1.1/README.md new file mode 100644 index 00000000..6a560077 --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/README.md @@ -0,0 +1,122 @@ +Arduino JSON library +==================== + +[![Build status](https://ci.appveyor.com/api/projects/status/m7s53wav1l0abssg/branch/master?svg=true)](https://ci.appveyor.com/project/bblanchon/arduinojson/branch/master) [![Build Status](https://travis-ci.org/bblanchon/ArduinoJson.svg?branch=master)](https://travis-ci.org/bblanchon/ArduinoJson) [![Coverage Status](https://img.shields.io/coveralls/bblanchon/ArduinoJson.svg)](https://coveralls.io/r/bblanchon/ArduinoJson?branch=master) [![Star this project](http://githubbadges.com/star.svg?user=bblanchon&repo=ArduinoJson&style=flat&color=fff&background=007ec6)](https://github.com/bblanchon/ArduinoJson) + +*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 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. + +Features +-------- + +* JSON decoding (comments are supported) +* JSON encoding (with optional indentation) +* Elegant API, very easy to use +* Efficient (no malloc, nor copy) +* Portable (written in C++98) +* Self-contained (no external dependency) +* Small footprint +* MIT License + +Works on +-------- + +* All Arduino boards (Uno, Due, Mini, Micro, Yun...) +* ESP8266 +* Teensy +* Intel Edison +* PlatformIO +* Energia +* RedBearLab boards (BLE Nano...) +* Computers (Windows, Linux, OSX...) + +See [FAQ: Compatibility issues](https://github.com/bblanchon/ArduinoJson/wiki/Compatibility-issues) + +Quick start +----------- + +#### Decoding / Parsing + +```c++ +char json[] = "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}"; + +StaticJsonBuffer<200> jsonBuffer; + +JsonObject& root = jsonBuffer.parseObject(json); + +const char* sensor = root["sensor"]; +long time = root["time"]; +double latitude = root["data"][0]; +double longitude = root["data"][1]; +``` + +#### Encoding / Generating + +```c++ +StaticJsonBuffer<200> jsonBuffer; + +JsonObject& root = jsonBuffer.createObject(); +root["sensor"] = "gps"; +root["time"] = 1351824120; + +JsonArray& data = root.createNestedArray("data"); +data.add(48.756080, 6); // 6 is the number of decimals to print +data.add(2.302038, 6); // if not specified, 2 digits are printed + +root.printTo(Serial); +// This prints: +// {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]} +``` + + +Documentation +------------- + +The documentation is available online in the [Arduino JSON wiki](https://github.com/bblanchon/ArduinoJson/wiki) + +Testimonials +------------ + +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. + +From GitHub user `zacsketches`: + +> Thanks for a great library!!! +> I've been watching you consistently develop this library over the past six months, and I used it today for a publish and subscribe architecture designed to help hobbyists move into more advanced robotics. Your library allowed me to implement remote subscription in order to facilitate multi-processor robots. +> ArduinoJson saved me a week's worth of time!! + +[From Reddit user `erm_what_`](https://www.reddit.com/r/arduino/comments/3jj6ep/announcing_arduinojson_50/cusjk8c): + +> This is a great library and I wouldn't be able to do the project I'm doing without it. I completely recommend it. + +[From Reddit user `makerhacks`](https://www.reddit.com/r/arduino/comments/3jj6ep/announcing_arduinojson_50/cusqg7b): + +> I am just starting an ESP8266 clock project and now I can output JSON from my server script and interpret it painlessly. + +Donators +-------- + +Special thanks to the following persons and companies who made generous donations to the library author: + +* Robert Murphy +* Surge Communications +* Alex Scott +* Firepick Services LLC +* A B Doodkorte +* Scott Smith +* Johann Stieger + +--- + +Found this library useful? Please star this project or [help me back with a donation!](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=donate%40benoitblanchon%2efr&lc=GB&item_name=Benoit%20Blanchon&item_number=Arduino%20JSON¤cy_code=EUR&bn=PP%2dDonationsBF%3abtn_donate_LG%2egif%3aNonHosted) :smile: diff --git a/src/third-party/arduino-json-5.1.1/examples/IndentedPrintExample/IndentedPrintExample.ino b/src/third-party/arduino-json-5.1.1/examples/IndentedPrintExample/IndentedPrintExample.ino new file mode 100644 index 00000000..9e86f0f5 --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/examples/IndentedPrintExample/IndentedPrintExample.ino @@ -0,0 +1,35 @@ +// 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 + +using namespace ArduinoJson::Internals; + +void setup() { + Serial.begin(9600); + while (!Serial) { + // wait serial port initialization + } + + IndentedPrint serial(Serial); + serial.setTabSize(4); + + serial.println("This is at indentation 0"); + serial.indent(); + serial.println("This is at indentation 1"); + serial.println("This is also at indentation 1"); + serial.indent(); + serial.println("This is at indentation 2"); + + serial.unindent(); + serial.unindent(); + serial.println("This is back at indentation 0"); +} + +void loop() { + // not used in this example +} diff --git a/src/third-party/arduino-json-5.1.1/examples/JsonGeneratorExample/JsonGeneratorExample.ino b/src/third-party/arduino-json-5.1.1/examples/JsonGeneratorExample/JsonGeneratorExample.ino new file mode 100644 index 00000000..717a2276 --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/examples/JsonGeneratorExample/JsonGeneratorExample.ino @@ -0,0 +1,70 @@ +// 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 + +void setup() { + Serial.begin(9600); + while (!Serial) { + // wait serial port initialization + } + + // Memory pool for JSON object tree. + // + // Inside the brackets, 200 is the size of the pool in bytes. + // If the JSON object is more complex, you need to increase that value. + StaticJsonBuffer<200> jsonBuffer; + + // StaticJsonBuffer allocates memory on the stack, it can be + // replaced by DynamicJsonBuffer which allocates in the heap. + // It's simpler but less efficient. + // + // DynamicJsonBuffer jsonBuffer; + + // Create the root of the object tree. + // + // It's a reference to the JsonObject, the actual bytes are inside the + // JsonBuffer with all the other nodes of the object tree. + // Memory is freed when jsonBuffer goes out of scope. + JsonObject& root = jsonBuffer.createObject(); + + // Add values in the object + // + // Most of the time, you can rely on the implicit casts. + // In other case, you can do root.set("time", 1351824120); + root["sensor"] = "gps"; + root["time"] = 1351824120; + + // Add a nested array. + // + // It's also possible to create the array separately and add it to the + // JsonObject but it's less efficient. + JsonArray& data = root.createNestedArray("data"); + data.add(double_with_n_digits(48.756080, 6)); + data.add(double_with_n_digits(2.302038, 6)); + + root.printTo(Serial); + // This prints: + // {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]} + + Serial.println(); + + root.prettyPrintTo(Serial); + // This prints: + // { + // "sensor": "gps", + // "time": 1351824120, + // "data": [ + // 48.756080, + // 2.302038 + // ] + // } +} + +void loop() { + // not used in this example +} diff --git a/src/third-party/arduino-json-5.1.1/examples/JsonParserExample/JsonParserExample.ino b/src/third-party/arduino-json-5.1.1/examples/JsonParserExample/JsonParserExample.ino new file mode 100644 index 00000000..13536019 --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/examples/JsonParserExample/JsonParserExample.ino @@ -0,0 +1,67 @@ +// 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 + +void setup() { + Serial.begin(9600); + while (!Serial) { + // wait serial port initialization + } + + // Memory pool for JSON object tree. + // + // Inside the brackets, 200 is the size of the pool in bytes, + // If the JSON object is more complex, you need to increase that value. + StaticJsonBuffer<200> jsonBuffer; + + // StaticJsonBuffer allocates memory on the stack, it can be + // replaced by DynamicJsonBuffer which allocates in the heap. + // It's simpler but less efficient. + // + // DynamicJsonBuffer jsonBuffer; + + // JSON input string. + // + // It's better to use a char[] as shown here. + // If you use a const char* or a String, ArduinoJson will + // have to make a copy of the input in the JsonBuffer. + char json[] = + "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}"; + + // Root of the object tree. + // + // It's a reference to the JsonObject, the actual bytes are inside the + // JsonBuffer with all the other nodes of the object tree. + // Memory is freed when jsonBuffer goes out of scope. + JsonObject& root = jsonBuffer.parseObject(json); + + // Test if parsing succeeds. + if (!root.success()) { + Serial.println("parseObject() failed"); + return; + } + + // Fetch values. + // + // Most of the time, you can rely on the implicit casts. + // In other case, you can do root["time"].as(); + const char* sensor = root["sensor"]; + long time = root["time"]; + double latitude = root["data"][0]; + double longitude = root["data"][1]; + + // Print values. + Serial.println(sensor); + Serial.println(time); + Serial.println(latitude, 6); + Serial.println(longitude, 6); +} + +void loop() { + // not used in this example +} diff --git a/src/third-party/arduino-json-5.1.1/examples/JsonServer/JsonServer.ino b/src/third-party/arduino-json-5.1.1/examples/JsonServer/JsonServer.ino new file mode 100644 index 00000000..895b6729 --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/examples/JsonServer/JsonServer.ino @@ -0,0 +1,74 @@ +// Sample Arduino Json Web Server +// Created by Benoit Blanchon. +// Heavily inspired by "Web Server" from David A. Mellis and Tom Igoe + +#include +#include +#include + +byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; +IPAddress ip(192, 168, 0, 177); +EthernetServer server(80); + +bool readRequest(EthernetClient& client) { + bool currentLineIsBlank = true; + while (client.connected()) { + if (client.available()) { + char c = client.read(); + if (c == '\n' && currentLineIsBlank) { + return true; + } else if (c == '\n') { + currentLineIsBlank = true; + } else if (c != '\r') { + currentLineIsBlank = false; + } + } + } + return false; +} + +JsonObject& prepareResponse(JsonBuffer& jsonBuffer) { + JsonObject& root = jsonBuffer.createObject(); + + JsonArray& analogValues = root.createNestedArray("analog"); + for (int pin = 0; pin < 6; pin++) { + int value = analogRead(pin); + analogValues.add(value); + } + + JsonArray& digitalValues = root.createNestedArray("digital"); + for (int pin = 0; pin < 14; pin++) { + int value = digitalRead(pin); + digitalValues.add(value); + } + + return root; +} + +void writeResponse(EthernetClient& client, JsonObject& json) { + client.println("HTTP/1.1 200 OK"); + client.println("Content-Type: application/json"); + client.println("Connection: close"); + client.println(); + + json.prettyPrintTo(client); +} + +void setup() { + Ethernet.begin(mac, ip); + server.begin(); +} + +void loop() { + EthernetClient client = server.available(); + if (client) { + bool success = readRequest(client); + if (success) { + StaticJsonBuffer<500> jsonBuffer; + JsonObject& json = prepareResponse(jsonBuffer); + writeResponse(client, json); + } + delay(1); + client.stop(); + } +} diff --git a/src/third-party/arduino-json-5.1.1/examples/JsonUdpBeacon/JsonUdpBeacon.ino b/src/third-party/arduino-json-5.1.1/examples/JsonUdpBeacon/JsonUdpBeacon.ino new file mode 100644 index 00000000..7d0fa38a --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/examples/JsonUdpBeacon/JsonUdpBeacon.ino @@ -0,0 +1,55 @@ +// Send a JSON object on UDP at regular interval +// +// You can easily test this program with netcat: +// $ nc -ulp 8888 +// +// by Benoit Blanchon, MIT License 2015-2016 + +#include +#include +#include + +byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; +IPAddress localIp(192, 168, 0, 177); +IPAddress remoteIp(192, 168, 0, 109); +unsigned int remotePort = 8888; +unsigned localPort = 8888; +EthernetUDP udp; + +JsonObject& buildJson(JsonBuffer& jsonBuffer) { + JsonObject& root = jsonBuffer.createObject(); + + JsonArray& analogValues = root.createNestedArray("analog"); + for (int pin = 0; pin < 6; pin++) { + int value = analogRead(pin); + analogValues.add(value); + } + + JsonArray& digitalValues = root.createNestedArray("digital"); + for (int pin = 0; pin < 14; pin++) { + int value = digitalRead(pin); + digitalValues.add(value); + } + + return root; +} + +void sendJson(JsonObject& json) { + udp.beginPacket(remoteIp, remotePort); + json.printTo(udp); + udp.println(); + udp.endPacket(); +} + +void setup() { + Ethernet.begin(mac, localIp); + udp.begin(localPort); +} + +void loop() { + delay(1000); + + StaticJsonBuffer<300> jsonBuffer; + JsonObject& json = buildJson(jsonBuffer); + sendJson(json); +} diff --git a/src/third-party/arduino-json-5.1.1/include/ArduinoJson.h b/src/third-party/arduino-json-5.1.1/include/ArduinoJson.h new file mode 100644 index 00000000..0a3f49a9 --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/include/ArduinoJson.h @@ -0,0 +1,13 @@ +// 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/DynamicJsonBuffer.hpp" +#include "ArduinoJson/JsonArray.hpp" +#include "ArduinoJson/JsonObject.hpp" +#include "ArduinoJson/StaticJsonBuffer.hpp" + +using namespace ArduinoJson; diff --git a/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Arduino/Print.hpp b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Arduino/Print.hpp new file mode 100644 index 00000000..ab1b0e8c --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Arduino/Print.hpp @@ -0,0 +1,89 @@ +// 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 + +#ifndef ARDUINO + +#include "../Internals/JsonFloat.hpp" +#include "../Internals/JsonInteger.hpp" + +#include +#include +#include + +#if defined(_MSC_VER) && _MSC_VER <= 1800 +// snprintf has been added in Visual Studio 2015 +#define ARDUINOJSON_SNPRINTF _snprintf +#else +#define ARDUINOJSON_SNPRINTF snprintf +#endif + +// This class reproduces Arduino's Print class +class Print { + public: + virtual ~Print() {} + + virtual size_t write(uint8_t) = 0; + + size_t print(const char* s) { + size_t n = 0; + while (*s) { + n += write(*s++); + } + return n; + } + + size_t print(ArduinoJson::Internals::JsonFloat value, int digits = 2) { + char tmp[32]; + + // https://github.com/arduino/Arduino/blob/db8cbf24c99dc930b9ccff1a43d018c81f178535/hardware/arduino/sam/cores/arduino/Print.cpp#L220 + bool isBigDouble = value > 4294967040.0 || value < -4294967040.0; + + if (isBigDouble) { + // Arduino's implementation prints "ovf" + // We prefer using the scientific notation, since we have sprintf + ARDUINOJSON_SNPRINTF(tmp, sizeof(tmp), "%g", value); + } else { + // Here we have the exact same output as Arduino's implementation + ARDUINOJSON_SNPRINTF(tmp, sizeof(tmp), "%.*f", digits, value); + } + + return print(tmp); + } + + size_t print(ArduinoJson::Internals::JsonInteger value) { + // see http://clc-wiki.net/wiki/K%26R2_solutions:Chapter_3:Exercise_4 + char buffer[22]; + + size_t n = 0; + if (value < 0) { + value = -value; + n += write('-'); + } + uint8_t i = 0; + do { + ArduinoJson::Internals::JsonInteger digit = value % 10; + value /= 10; + buffer[i++] = static_cast(digit >= 0 ? '0' + digit : '0' - digit); + } while (value); + + while (i > 0) { + n += write(buffer[--i]); + } + + return n; + } + + size_t println() { return write('\r') + write('\n'); } +}; + +#else + +#include + +#endif diff --git a/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Arduino/String.hpp b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Arduino/String.hpp new file mode 100644 index 00000000..a11a6511 --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Arduino/String.hpp @@ -0,0 +1,19 @@ +// 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 + +#ifndef ARDUINO + +#include +typedef std::string String; + +#else + +#include + +#endif diff --git a/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Configuration.hpp b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Configuration.hpp new file mode 100644 index 00000000..9b37548e --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Configuration.hpp @@ -0,0 +1,79 @@ +// 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 + +#ifdef ARDUINO // assume this is an embedded platform + +// store using float instead of double to reduce the memory usage (issue #134) +#ifndef ARDUINOJSON_USE_DOUBLE +#define ARDUINOJSON_USE_DOUBLE 0 +#endif + +// store using a long because it usually match the size of a float. +#ifndef ARDUINOJSON_USE_LONG_LONG +#define ARDUINOJSON_USE_LONG_LONG 0 +#endif +#ifndef ARDUINOJSON_USE_INT64 +#define ARDUINOJSON_USE_INT64 0 +#endif + +// arduino doesn't support STL stream +#ifndef ARDUINOJSON_ENABLE_STD_STREAM +#define ARDUINOJSON_ENABLE_STD_STREAM 0 +#endif + +#ifndef ARDUINOJSON_ENABLE_ALIGNMENT +#ifdef ARDUINO_ARCH_AVR +// alignment isn't needed for 8-bit AVR +#define ARDUINOJSON_ENABLE_ALIGNMENT 0 +#else +// but must processor needs pointer to be align on word size +#define ARDUINOJSON_ENABLE_ALIGNMENT 1 +#endif +#endif + +#else // assume this is a computer + +// on a computer we have plenty of memory so we can use doubles +#ifndef ARDUINOJSON_USE_DOUBLE +#define ARDUINOJSON_USE_DOUBLE 1 +#endif + +// use long long when available +#ifndef ARDUINOJSON_USE_LONG_LONG +#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800) +#define ARDUINOJSON_USE_LONG_LONG 1 +#else +#define ARDUINOJSON_USE_LONG_LONG 0 +#endif +#endif + +// use _int64 on old versions of Visual Studio +#ifndef ARDUINOJSON_USE_INT64 +#if defined(_MSC_VER) && _MSC_VER <= 1700 +#define ARDUINOJSON_USE_INT64 1 +#else +#define ARDUINOJSON_USE_INT64 0 +#endif +#endif + +// on a computer, we can assume that the STL is there +#ifndef ARDUINOJSON_ENABLE_STD_STREAM +#define ARDUINOJSON_ENABLE_STD_STREAM 1 +#endif + +#ifndef ARDUINOJSON_ENABLE_ALIGNMENT +// even if not required, most cpu's are faster with aligned pointers +#define ARDUINOJSON_ENABLE_ALIGNMENT 1 +#endif + +#endif + +#if ARDUINOJSON_USE_LONG_LONG && ARDUINOJSON_USE_INT64 +#error ARDUINOJSON_USE_LONG_LONG and ARDUINOJSON_USE_INT64 cannot be set together +#endif diff --git a/src/third-party/arduino-json-5.1.1/include/ArduinoJson/DynamicJsonBuffer.hpp b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/DynamicJsonBuffer.hpp new file mode 100644 index 00000000..2e02b097 --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/DynamicJsonBuffer.hpp @@ -0,0 +1,18 @@ +// 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/BlockJsonBuffer.hpp" + +namespace ArduinoJson { +// 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 + DynamicJsonBuffer; +} diff --git a/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/BlockJsonBuffer.hpp b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/BlockJsonBuffer.hpp new file mode 100644 index 00000000..4fa5a26f --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/BlockJsonBuffer.hpp @@ -0,0 +1,94 @@ +// 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 + +namespace ArduinoJson { +namespace Internals { +class DefaultAllocator { + public: + void* allocate(size_t size) { return malloc(size); } + void deallocate(void* pointer) { free(pointer); } +}; + +template +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; + } + + protected: + 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(_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; +}; +} +} diff --git a/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/Comments.hpp b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/Comments.hpp new file mode 100644 index 00000000..21763cc3 --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/Comments.hpp @@ -0,0 +1,14 @@ +// 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 { +const char *skipSpacesAndComments(const char *ptr); +} +} diff --git a/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/DummyPrint.hpp b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/DummyPrint.hpp new file mode 100644 index 00000000..7ab82b6b --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/DummyPrint.hpp @@ -0,0 +1,21 @@ +// 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 "../Arduino/Print.hpp" + +namespace ArduinoJson { +namespace Internals { + +// A dummy Print implementation used in JsonPrintable::measureLength() +class DummyPrint : public Print { + public: + virtual size_t write(uint8_t) { return 1; } +}; +} +} diff --git a/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/DynamicStringBuilder.hpp b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/DynamicStringBuilder.hpp new file mode 100644 index 00000000..6225ad94 --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/DynamicStringBuilder.hpp @@ -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 "../Arduino/Print.hpp" +#include "../Arduino/String.hpp" + +namespace ArduinoJson { +namespace Internals { + +// A Print implementation that allows to write in a String +class DynamicStringBuilder : public Print { + public: + DynamicStringBuilder(String &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(c); + return 1; + } + + private: + DynamicStringBuilder &operator=(const DynamicStringBuilder &); + + String &_str; +}; +} +} diff --git a/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/Encoding.hpp b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/Encoding.hpp new file mode 100644 index 00000000..b31f7e43 --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/Encoding.hpp @@ -0,0 +1,40 @@ +// 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 "../Arduino/Print.hpp" + +namespace ArduinoJson { +namespace Internals { + +class Encoding { + public: + // Optimized for code size on a 8-bit AVR + static char escapeChar(char c) { + const char *p = _escapeTable; + while (p[0] && p[1] != c) { + p += 2; + } + return p[0]; + } + + // Optimized for code size on a 8-bit AVR + static char unescapeChar(char c) { + const char *p = _escapeTable + 4; + for (;;) { + if (p[0] == '\0') return c; + if (p[0] == c) return p[1]; + p += 2; + } + } + + private: + static const char _escapeTable[]; +}; +} +} diff --git a/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/ForceInline.hpp b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/ForceInline.hpp new file mode 100644 index 00000000..37aad136 --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/ForceInline.hpp @@ -0,0 +1,14 @@ +// 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 + +#ifdef _MSC_VER +#define FORCE_INLINE __forceinline +#else +#define FORCE_INLINE __attribute__((always_inline)) +#endif diff --git a/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/IndentedPrint.hpp b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/IndentedPrint.hpp new file mode 100644 index 00000000..f0a9c1e8 --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/IndentedPrint.hpp @@ -0,0 +1,55 @@ +// 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 "../Arduino/Print.hpp" + +namespace ArduinoJson { +namespace Internals { + +// Decorator on top of Print to allow indented output. +// This class is used by JsonPrintable::prettyPrintTo() but can also be used +// for your own purpose, like logging. +class IndentedPrint : public Print { + public: + explicit IndentedPrint(Print &p) : sink(&p) { + level = 0; + tabSize = 2; + isNewLine = true; + } + + virtual size_t write(uint8_t); + + // Adds one level of indentation + void indent() { + if (level < MAX_LEVEL) level++; + } + + // Removes one level of indentation + void unindent() { + if (level > 0) level--; + } + + // Set the number of space printed for each level of indentation + void setTabSize(uint8_t n) { + if (n < MAX_TAB_SIZE) tabSize = n & MAX_TAB_SIZE; + } + + private: + Print *sink; + uint8_t level : 4; + uint8_t tabSize : 3; + bool isNewLine : 1; + + size_t writeTabs(); + + static const int MAX_LEVEL = 15; // because it's only 4 bits + static const int MAX_TAB_SIZE = 7; // because it's only 3 bits +}; +} +} diff --git a/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/JsonBufferAllocated.hpp b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/JsonBufferAllocated.hpp new file mode 100644 index 00000000..97328faf --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/JsonBufferAllocated.hpp @@ -0,0 +1,25 @@ +// 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" + +namespace ArduinoJson { +namespace Internals { + +class JsonBufferAllocated { + public: + void *operator new(size_t n, JsonBuffer *jsonBuffer) throw() { + if (!jsonBuffer) return NULL; + return jsonBuffer->alloc(n); + } + + void operator delete(void *, JsonBuffer *) throw() {} +}; +} +} diff --git a/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/JsonFloat.hpp b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/JsonFloat.hpp new file mode 100644 index 00000000..b6dc20f0 --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/JsonFloat.hpp @@ -0,0 +1,21 @@ +// 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 { + +#if ARDUINOJSON_USE_DOUBLE +typedef double JsonFloat; +#else +typedef float JsonFloat; +#endif +} +} diff --git a/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/JsonInteger.hpp b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/JsonInteger.hpp new file mode 100644 index 00000000..8db05992 --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/JsonInteger.hpp @@ -0,0 +1,23 @@ +// 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 { + +#if ARDUINOJSON_USE_LONG_LONG +typedef long long JsonInteger; +#elif ARDUINOJSON_USE_INT64 +typedef __int64 JsonInteger; +#else +typedef long JsonInteger; +#endif +} +} diff --git a/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/JsonParser.hpp b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/JsonParser.hpp new file mode 100644 index 00000000..001326dc --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/JsonParser.hpp @@ -0,0 +1,47 @@ +// 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(); + + 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); + + JsonBuffer *_buffer; + const char *_readPtr; + char *_writePtr; + uint8_t _nestingLimit; +}; +} +} diff --git a/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/JsonPrintable.hpp b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/JsonPrintable.hpp new file mode 100644 index 00000000..95e6201b --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/JsonPrintable.hpp @@ -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 "../Configuration.hpp" +#include "DummyPrint.hpp" +#include "IndentedPrint.hpp" +#include "JsonWriter.hpp" +#include "Prettyfier.hpp" +#include "StaticStringBuilder.hpp" +#include "DynamicStringBuilder.hpp" + +#if ARDUINOJSON_ENABLE_STD_STREAM +#include "StreamPrintAdapter.hpp" +#endif + +namespace ArduinoJson { +namespace Internals { + +// Implements all the overloads of printTo() and prettyPrintTo() +// Caution: this class use a template parameter to avoid virtual methods. +// This is a bit curious but allows to reduce the size of JsonVariant, JsonArray +// and JsonObject. +template +class JsonPrintable { + public: + size_t printTo(Print &print) const { + JsonWriter writer(print); + downcast().writeTo(writer); + return writer.bytesWritten(); + } + +#if ARDUINOJSON_ENABLE_STD_STREAM + std::ostream &printTo(std::ostream &os) const { + StreamPrintAdapter adapter(os); + printTo(adapter); + return os; + } +#endif + + size_t printTo(char *buffer, size_t bufferSize) const { + StaticStringBuilder sb(buffer, bufferSize); + return printTo(sb); + } + + size_t printTo(String &str) const { + DynamicStringBuilder sb(str); + return printTo(sb); + } + + size_t prettyPrintTo(IndentedPrint &print) const { + Prettyfier p(print); + return printTo(p); + } + + size_t prettyPrintTo(char *buffer, size_t bufferSize) const { + StaticStringBuilder sb(buffer, bufferSize); + return prettyPrintTo(sb); + } + + size_t prettyPrintTo(Print &print) const { + IndentedPrint indentedPrint = IndentedPrint(print); + return prettyPrintTo(indentedPrint); + } + + size_t prettyPrintTo(String &str) const { + DynamicStringBuilder sb(str); + return prettyPrintTo(sb); + } + + size_t measureLength() const { + DummyPrint dp; + return printTo(dp); + } + + size_t measurePrettyLength() const { + DummyPrint dp; + return prettyPrintTo(dp); + } + + private: + const T &downcast() const { return *static_cast(this); } +}; + +#if ARDUINOJSON_ENABLE_STD_STREAM +template +inline std::ostream &operator<<(std::ostream &os, const JsonPrintable &v) { + return v.printTo(os); +} +#endif +} +} diff --git a/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/JsonVariantContent.hpp b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/JsonVariantContent.hpp new file mode 100644 index 00000000..14d0eed9 --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/JsonVariantContent.hpp @@ -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 + +#include "JsonFloat.hpp" +#include "JsonInteger.hpp" + +namespace ArduinoJson { + +// Forward declarations +class JsonArray; +class JsonObject; + +namespace Internals { +// A union that defines the actual content of a JsonVariant. +// The enum JsonVariantType determines which member is in use. +union JsonVariantContent { + JsonFloat asFloat; // used for double and float + JsonInteger asInteger; // used for bool, char, short, int and longs + const char* asString; // asString can be null + JsonArray* asArray; // asArray cannot be null + JsonObject* asObject; // asObject cannot be null +}; +} +} diff --git a/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/JsonVariantType.hpp b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/JsonVariantType.hpp new file mode 100644 index 00000000..418b0e6b --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/JsonVariantType.hpp @@ -0,0 +1,37 @@ +// 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 { +class JsonArray; +class JsonObject; + +namespace Internals { + +// Enumerated type to know the current type of a JsonVariant. +// The value determines which member of JsonVariantContent is used. +enum JsonVariantType { + JSON_UNDEFINED, // the JsonVariant has not been initialized + JSON_UNPARSED, // the JsonVariant contains an unparsed string + JSON_STRING, // the JsonVariant stores a const char* + JSON_BOOLEAN, // the JsonVariant stores a bool + JSON_INTEGER, // the JsonVariant stores an integer + JSON_ARRAY, // the JsonVariant stores a pointer to a JsonArray + JSON_OBJECT, // the JsonVariant stores a pointer to a JsonObject + + // The following values are reserved for float values + // Multiple values are used for double, depending on the number of decimal + // digits that must be printed in the JSON output. + // This little trick allow to save one extra member in JsonVariant + JSON_FLOAT_0_DECIMALS + // JSON_FLOAT_1_DECIMAL + // JSON_FLOAT_2_DECIMALS + // ... +}; +} +} diff --git a/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/JsonWriter.hpp b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/JsonWriter.hpp new file mode 100644 index 00000000..8cdf848a --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/JsonWriter.hpp @@ -0,0 +1,85 @@ +// 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 "../Arduino/Print.hpp" +#include "Encoding.hpp" +#include "ForceInline.hpp" +#include "JsonFloat.hpp" +#include "JsonInteger.hpp" + +namespace ArduinoJson { +namespace Internals { + +// Writes the JSON tokens to a Print implementation +// This class is used by: +// - JsonArray::writeTo() +// - JsonObject::writeTo() +// - JsonVariant::writeTo() +// Its derived by PrettyJsonWriter that overrides some members to add +// indentation. +class JsonWriter { + public: + explicit JsonWriter(Print &sink) : _sink(sink), _length(0) {} + + // Returns the number of bytes sent to the Print implementation. + // This is very handy for implementations of printTo() that must return the + // number of bytes written. + size_t bytesWritten() const { return _length; } + + void beginArray() { write('['); } + void endArray() { write(']'); } + + void beginObject() { write('{'); } + void endObject() { write('}'); } + + void writeColon() { write(':'); } + void writeComma() { write(','); } + + void writeBoolean(bool value) { write(value ? "true" : "false"); } + + void writeString(const char *value) { + if (!value) { + write("null"); + } else { + write('\"'); + while (*value) writeChar(*value++); + write('\"'); + } + } + + void writeChar(char c) { + char specialChar = Encoding::escapeChar(c); + if (specialChar) { + write('\\'); + write(specialChar); + } else { + write(c); + } + } + + void writeInteger(JsonInteger value) { _length += _sink.print(value); } + + void writeFloat(JsonFloat value, uint8_t decimals) { + _length += _sink.print(value, decimals); + } + + void writeRaw(const char *s) { return write(s); } + + protected: + void write(char c) { _length += _sink.write(c); } + FORCE_INLINE void write(const char *s) { _length += _sink.print(s); } + + Print &_sink; + size_t _length; + + private: + JsonWriter &operator=(const JsonWriter &); // cannot be assigned +}; +} +} diff --git a/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/List.hpp b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/List.hpp new file mode 100644 index 00000000..7746f106 --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/List.hpp @@ -0,0 +1,59 @@ +// 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 "ListConstIterator.hpp" +#include "ListIterator.hpp" + +namespace ArduinoJson { +namespace Internals { + +// A singly linked list of T. +// The linked list is composed of ListNode. +// It is derived by JsonArray and JsonObject +template +class List { + public: + typedef T value_type; + typedef ListNode node_type; + typedef ListIterator iterator; + typedef ListConstIterator const_iterator; + + // Creates an empty List attached to a JsonBuffer. + // The JsonBuffer allows to allocate new nodes. + // When buffer is NULL, the List is not able to grow and success() returns + // false. This is used to identify bad memory allocations and parsing + // failures. + explicit List(JsonBuffer *buffer) : _buffer(buffer), _firstNode(NULL) {} + + // Returns true if the object is valid + // Would return false in the following situation: + // - the memory allocation failed (StaticJsonBuffer was too small) + // - the JSON parsing failed + bool success() const { return _buffer != NULL; } + + // Returns the numbers of elements in the list. + // For a JsonObject, it would return the number of key-value pairs + size_t size() const; + + iterator begin() { return iterator(_firstNode); } + iterator end() { return iterator(NULL); } + + const_iterator begin() const { return const_iterator(_firstNode); } + const_iterator end() const { return const_iterator(NULL); } + + protected: + node_type *addNewNode(); + void removeNode(node_type *nodeToRemove); + + JsonBuffer *_buffer; + node_type *_firstNode; +}; +} +} diff --git a/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/ListConstIterator.hpp b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/ListConstIterator.hpp new file mode 100644 index 00000000..f6b7ca53 --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/ListConstIterator.hpp @@ -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 "ListNode.hpp" + +namespace ArduinoJson { +namespace Internals { + +// A read-only forward itertor for List +template +class ListConstIterator { + public: + explicit ListConstIterator(const ListNode *node = NULL) : _node(node) {} + + const T &operator*() const { return _node->content; } + const T *operator->() { return &_node->content; } + + bool operator==(const ListConstIterator &other) const { + return _node == other._node; + } + + bool operator!=(const ListConstIterator &other) const { + return _node != other._node; + } + + ListConstIterator &operator++() { + if (_node) _node = _node->next; + return *this; + } + + private: + const ListNode *_node; +}; +} +} diff --git a/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/ListIterator.hpp b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/ListIterator.hpp new file mode 100644 index 00000000..17342971 --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/ListIterator.hpp @@ -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 + +#include "ListNode.hpp" +#include "ListConstIterator.hpp" + +namespace ArduinoJson { +namespace Internals { + +// A read-write forward iterator for List +template +class ListIterator { + public: + explicit ListIterator(ListNode *node = NULL) : _node(node) {} + + T &operator*() const { return _node->content; } + T *operator->() { return &_node->content; } + + bool operator==(const ListIterator &other) const { + return _node == other._node; + } + + bool operator!=(const ListIterator &other) const { + return _node != other._node; + } + + ListIterator &operator++() { + if (_node) _node = _node->next; + return *this; + } + + operator ListConstIterator() const { return ListConstIterator(_node); } + + private: + ListNode *_node; +}; +} +} diff --git a/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/ListNode.hpp b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/ListNode.hpp new file mode 100644 index 00000000..3c94662d --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/ListNode.hpp @@ -0,0 +1,27 @@ +// 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 // for NULL + +#include "JsonBufferAllocated.hpp" + +namespace ArduinoJson { +namespace Internals { + +// A node for a singly-linked list. +// Used by List and its iterators. +template +struct ListNode : public Internals::JsonBufferAllocated { + ListNode() : next(NULL) {} + + ListNode *next; + T content; +}; +} +} diff --git a/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/Parse.hpp b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/Parse.hpp new file mode 100644 index 00000000..adbd7a50 --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/Parse.hpp @@ -0,0 +1,51 @@ +// 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 + +namespace ArduinoJson { +namespace Internals { +template +TFloat parse(const char *); + +template <> +inline float parse(const char *s) { + return static_cast(strtod(s, NULL)); +} + +template <> +inline double parse(const char *s) { + return strtod(s, NULL); +} + +template <> +inline long parse(const char *s) { + return strtol(s, NULL, 10); +} + +template <> +inline int parse(const char *s) { + return atoi(s); +} + +#if ARDUINOJSON_USE_LONG_LONG +template <> +inline long long parse(const char *s) { + return strtoll(s, NULL, 10); +} +#endif + +#if ARDUINOJSON_USE_INT64 +template <> +inline __int64 parse<__int64>(const char *s) { + return _strtoi64(s, NULL, 10); +} +#endif +} +} diff --git a/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/Prettyfier.hpp b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/Prettyfier.hpp new file mode 100644 index 00000000..8f320d91 --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/Prettyfier.hpp @@ -0,0 +1,47 @@ +// 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 "IndentedPrint.hpp" + +namespace ArduinoJson { +namespace Internals { + +// Converts a compact JSON string into an indented one. +class Prettyfier : public Print { + public: + explicit Prettyfier(IndentedPrint& p) : _sink(p) { + _previousChar = 0; + _inString = false; + } + + virtual size_t write(uint8_t); + + private: + Prettyfier& operator=(const Prettyfier&); // cannot be assigned + + bool inEmptyBlock() { return _previousChar == '{' || _previousChar == '['; } + + size_t handleStringChar(uint8_t); + size_t handleMarkupChar(uint8_t); + + size_t handleBlockClose(uint8_t); + size_t handleBlockOpen(uint8_t); + size_t handleColon(); + size_t handleComma(); + size_t handleQuoteOpen(); + size_t handleNormalChar(uint8_t); + size_t indentIfNeeded(); + size_t unindentIfNeeded(); + + uint8_t _previousChar; + IndentedPrint& _sink; + bool _inString; +}; +} +} diff --git a/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/ReferenceType.hpp b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/ReferenceType.hpp new file mode 100644 index 00000000..a5cf507a --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/ReferenceType.hpp @@ -0,0 +1,35 @@ +// 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 { + +// A type that is meant to be used by reference only (JsonArray and JsonObject) +class ReferenceType { + public: + bool operator==(const ReferenceType& other) const { + // two JsonArray are equal if they are the same instance + // (we don't compare the content) + return this == &other; + } + + bool operator!=(const ReferenceType& other) const { return this != &other; } + + protected: + ReferenceType() {} + + private: + // copy constructor is private + ReferenceType(const ReferenceType&); + + // copy operator is private + ReferenceType& operator=(const ReferenceType&); +}; +} +} diff --git a/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/StaticStringBuilder.hpp b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/StaticStringBuilder.hpp new file mode 100644 index 00000000..df1ceecf --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/StaticStringBuilder.hpp @@ -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 "../Arduino/Print.hpp" + +namespace ArduinoJson { +namespace Internals { + +// A Print implementation that allows to write in a char[] +class StaticStringBuilder : public Print { + public: + StaticStringBuilder(char *buf, size_t size) + : buffer(buf), capacity(size - 1), length(0) { + buffer[0] = '\0'; + } + + virtual size_t write(uint8_t c); + + private: + char *buffer; + size_t capacity; + size_t length; +}; +} +} diff --git a/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/StreamPrintAdapter.hpp b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/StreamPrintAdapter.hpp new file mode 100644 index 00000000..ace1d9ed --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/StreamPrintAdapter.hpp @@ -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 "../Configuration.hpp" + +#if ARDUINOJSON_ENABLE_STD_STREAM + +#include "../Arduino/Print.hpp" + +#include + +namespace ArduinoJson { +namespace Internals { + +class StreamPrintAdapter : public Print { + public: + explicit StreamPrintAdapter(std::ostream& os) : _os(os) {} + + virtual size_t write(uint8_t c) { + _os << static_cast(c); + return 1; + } + + private: + // cannot be assigned + StreamPrintAdapter& operator=(const StreamPrintAdapter&); + + std::ostream& _os; +}; +} +} + +#endif // ARDUINOJSON_ENABLE_STD_STREAM diff --git a/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/Unparsed.hpp b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/Unparsed.hpp new file mode 100644 index 00000000..a01508b1 --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/Internals/Unparsed.hpp @@ -0,0 +1,21 @@ +// 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 Unparsed { + public: + explicit Unparsed(const char* str) : _str(str) {} + operator const char*() const { return _str; } + + private: + const char* _str; +}; +} +} diff --git a/src/third-party/arduino-json-5.1.1/include/ArduinoJson/JsonArray.hpp b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/JsonArray.hpp new file mode 100644 index 00000000..d478ab52 --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/JsonArray.hpp @@ -0,0 +1,181 @@ +// 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/JsonBufferAllocated.hpp" +#include "Internals/JsonPrintable.hpp" +#include "Internals/List.hpp" +#include "Internals/ReferenceType.hpp" +#include "JsonVariant.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. +// Can be very handy to determine the size of a StaticJsonBuffer. +#define JSON_ARRAY_SIZE(NUMBER_OF_ELEMENTS) \ + (sizeof(JsonArray) + (NUMBER_OF_ELEMENTS) * sizeof(JsonArray::node_type)) + +namespace ArduinoJson { + +// Forward declarations +class JsonObject; +class JsonBuffer; +class JsonArraySubscript; + +// An array of JsonVariant. +// +// The constructor is private, instances must be created via +// JsonBuffer::createArray() or JsonBuffer::parseArray(). +// A JsonArray can be serialized to a JSON string via JsonArray::printTo(). +// It can also be deserialized from a JSON string via JsonBuffer::parseArray(). +class JsonArray : public Internals::JsonPrintable, + public Internals::ReferenceType, + public Internals::List, + public Internals::JsonBufferAllocated { + public: + // A meta-function that returns true if type T can be used in + // JsonArray::set() + template + struct CanSet { + static const bool value = JsonVariant::IsConstructibleFrom::value || + TypeTraits::IsSame::value || + TypeTraits::IsSame::value; + }; + + // Create an empty JsonArray attached to the specified JsonBuffer. + // You should not call this constructor directly. + // Instead, use JsonBuffer::createArray() or JsonBuffer::parseArray(). + explicit JsonArray(JsonBuffer *buffer) + : Internals::List(buffer) {} + + // Gets the value at the specified index + FORCE_INLINE JsonVariant operator[](size_t index) const; + + // Gets or sets the value at specified index + FORCE_INLINE JsonArraySubscript operator[](size_t index); + + // Adds the specified value at the end of the array. + // + // bool add(bool); + // bool add(char); + // bool add(long); + // bool add(int); + // bool add(short); + // bool add(float value); + // bool add(double value); + // bool add(const char*); + template + FORCE_INLINE bool add( + T value, + typename TypeTraits::EnableIf< + CanSet::value && !TypeTraits::IsReference::value>::type * = 0) { + return addNode(value); + } + // bool add(const String&) + // bool add(const JsonVariant&); + // bool add(JsonArray&); + // bool add(JsonObject&); + template + FORCE_INLINE bool add( + const T &value, + typename TypeTraits::EnableIf::value>::type * = 0) { + return addNode(const_cast(value)); + } + // bool add(float value, uint8_t decimals); + // bool add(double value, uint8_t decimals); + template + FORCE_INLINE bool add( + T value, uint8_t decimals, + typename TypeTraits::EnableIf::value>::type + * = 0) { + return addNode(JsonVariant(value, decimals)); + } + + // Sets the value at specified index. + // + // bool set(size_t index, bool value); + // bool set(size_t index, long value); + // bool set(size_t index, int value); + // bool set(size_t index, short value); + template + FORCE_INLINE bool set( + size_t index, T value, + typename TypeTraits::EnableIf< + CanSet::value && !TypeTraits::IsReference::value>::type * = 0) { + return setNodeAt(index, value); + } + // 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 + FORCE_INLINE bool set( + size_t index, const T &value, + typename TypeTraits::EnableIf::value>::type * = 0) { + return setNodeAt(index, const_cast(value)); + } + // bool set(size_t index, float value, uint8_t decimals = 2); + // bool set(size_t index, double value, uint8_t decimals = 2); + template + FORCE_INLINE bool set( + size_t index, T value, uint8_t decimals, + typename TypeTraits::EnableIf::value>::type + * = 0) { + return setNodeAt(index, JsonVariant(value, decimals)); + } + + // Gets the value at the specified index. + FORCE_INLINE JsonVariant get(size_t index) const; + + // Gets the value at the specified index. + template + FORCE_INLINE T get(size_t index) const; + + // Check the type of the value at specified index. + template + FORCE_INLINE bool is(size_t index) const; + + // Creates a JsonArray and adds a reference at the end of the array. + // It's a shortcut for JsonBuffer::createArray() and JsonArray::add() + JsonArray &createNestedArray(); + + // Creates a JsonObject and adds a reference at the end of the array. + // It's a shortcut for JsonBuffer::createObject() and JsonArray::add() + JsonObject &createNestedObject(); + + // Removes element at specified index. + void removeAt(size_t index); + + // Returns a reference an invalid JsonArray. + // This object is meant to replace a NULL pointer. + // This is used when memory allocation or JSON parsing fail. + static JsonArray &invalid() { return _invalid; } + + // Serialize the array to the specified JsonWriter. + void writeTo(Internals::JsonWriter &writer) const; + + private: + node_type *getNodeAt(size_t index) const; + + template + bool setNodeAt(size_t index, TValue value); + + template + bool addNode(TValue); + + template + FORCE_INLINE bool setNodeValue(node_type *, T value); + + // The instance returned by JsonArray::invalid() + static JsonArray _invalid; +}; +} + +#include "JsonArray.ipp" diff --git a/src/third-party/arduino-json-5.1.1/include/ArduinoJson/JsonArray.ipp b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/JsonArray.ipp new file mode 100644 index 00000000..2881ed25 --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/JsonArray.ipp @@ -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 "JsonObject.hpp" +#include "JsonArraySubscript.hpp" + +namespace ArduinoJson { + +inline JsonArraySubscript JsonArray::operator[](size_t index) { + return JsonArraySubscript(*this, index); +} + +inline JsonVariant JsonArray::operator[](size_t index) const { + return get(index); +} + +template +inline bool JsonArray::addNode(TValue value) { + node_type *node = addNewNode(); + return node != NULL && setNodeValue(node, value); +} + +template +inline bool JsonArray::setNodeAt(size_t index, TValue value) { + node_type *node = getNodeAt(index); + return node != NULL && setNodeValue(node, value); +} + +template +inline bool JsonArray::setNodeValue(node_type *node, TValue value) { + node->content = value; + return true; +} + +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; +} + +inline JsonVariant JsonArray::get(size_t index) const { + node_type *node = getNodeAt(index); + return node ? node->content : JsonVariant(); +} + +template +inline T JsonArray::get(size_t index) const { + node_type *node = getNodeAt(index); + return node ? node->content.as() : JsonVariant::invalid(); +} + +template +inline bool JsonArray::is(size_t index) const { + node_type *node = getNodeAt(index); + return node ? node->content.is() : false; +} + +template +inline const JsonArraySubscript JsonVariantBase::operator[]( + int index) const { + return asArray()[index]; +} + +template <> +inline JsonArray &JsonVariant::invalid() { + return JsonArray::invalid(); +} + +template <> +inline JsonArray const &JsonVariant::invalid() { + 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(key, array); + return array; +} +} diff --git a/src/third-party/arduino-json-5.1.1/include/ArduinoJson/JsonArraySubscript.hpp b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/JsonArraySubscript.hpp new file mode 100644 index 00000000..57e55ebf --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/JsonArraySubscript.hpp @@ -0,0 +1,84 @@ +// 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" +#include "JsonVariantBase.hpp" + +#ifdef _MSC_VER +#pragma warning(push) +#pragma warning(disable : 4522) +#endif + +namespace ArduinoJson { +class JsonArraySubscript : public JsonVariantBase { + public: + FORCE_INLINE JsonArraySubscript(JsonArray& array, size_t index) + : _array(array), _index(index) {} + + JsonArraySubscript& operator=(const JsonArraySubscript& src) { + _array.set(_index, src); + return *this; + } + + template + typename TypeTraits::EnableIf::value, + JsonArraySubscript>::type& + operator=(const T& src) { + _array.set(_index, const_cast(src)); + return *this; + } + + template + typename TypeTraits::EnableIf::value, + JsonArraySubscript>::type& + operator=(T src) { + _array.set(_index, src); + return *this; + } + + FORCE_INLINE bool success() const { return _index < _array.size(); } + + FORCE_INLINE operator JsonVariant() const { return _array.get(_index); } + + template + FORCE_INLINE T as() const { + return _array.get(_index); + } + + template + FORCE_INLINE bool is() const { + return _array.is(_index); + } + + void writeTo(Internals::JsonWriter& writer) const { + _array.get(_index).writeTo(writer); + } + + template + void set(TValue value) { + _array.set(_index, value); + } + + private: + JsonArray& _array; + const size_t _index; +}; + +#if ARDUINOJSON_ENABLE_STD_STREAM +inline std::ostream& operator<<(std::ostream& os, + const JsonArraySubscript& source) { + return source.printTo(os); +} +#endif + +} // namespace ArduinoJson + +#ifdef _MSC_VER +#pragma warning(pop) +#endif diff --git a/src/third-party/arduino-json-5.1.1/include/ArduinoJson/JsonBuffer.hpp b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/JsonBuffer.hpp new file mode 100644 index 00000000..4ff05aa4 --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/JsonBuffer.hpp @@ -0,0 +1,136 @@ +// 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 // for size_t +#include // for uint8_t +#include + +#include "Arduino/String.hpp" +#include "JsonVariant.hpp" + +#if defined(__clang__) +#pragma clang diagnostic ignored "-Wnon-virtual-dtor" +#elif defined(__GNUC__) +#pragma GCC diagnostic ignored "-Wnon-virtual-dtor" +#endif + +namespace ArduinoJson { +class JsonArray; +class JsonObject; + +// Entry point for using the library. +// +// Handle the memory management (done in derived classes) and calls the parser. +// This abstract class is implemented by StaticJsonBuffer which implements a +// fixed memory allocation. +class JsonBuffer { + public: + // CAUTION: NO VIRTUAL DESTRUCTOR! + // If we add a virtual constructor the Arduino compiler will add malloc() and + // free() to the binary, adding 706 useless bytes. + // virtual ~JsonBuffer() {} + + // Allocates an empty JsonArray. + // + // Returns a reference to the new JsonArray or JsonArray::invalid() if the + // allocation fails. + JsonArray &createArray(); + + // Allocates an empty JsonObject. + // + // Returns a reference to the new JsonObject or JsonObject::invalid() if the + // 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); + } + + // Duplicate a string + char *strdup(const char *src) { + return src ? strdup(src, strlen(src)) : NULL; + } + 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 + static FORCE_INLINE size_t round_size_up(size_t bytes) { +#if ARDUINOJSON_ENABLE_ALIGNMENT + const size_t x = sizeof(void *) - 1; + return (bytes + x) & ~x; +#else + 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; +}; +} diff --git a/src/third-party/arduino-json-5.1.1/include/ArduinoJson/JsonObject.hpp b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/JsonObject.hpp new file mode 100644 index 00000000..9f4a061f --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/JsonObject.hpp @@ -0,0 +1,153 @@ +// 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 "Arduino/String.hpp" +#include "Internals/JsonBufferAllocated.hpp" +#include "Internals/JsonPrintable.hpp" +#include "Internals/List.hpp" +#include "Internals/ReferenceType.hpp" +#include "JsonPair.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. +// Can be very handy to determine the size of a StaticJsonBuffer. +#define JSON_OBJECT_SIZE(NUMBER_OF_ELEMENTS) \ + (sizeof(JsonObject) + (NUMBER_OF_ELEMENTS) * sizeof(JsonObject::node_type)) + +namespace ArduinoJson { + +// Forward declarations +class JsonArray; +class JsonBuffer; + +// A dictionary of JsonVariant indexed by string (char*) +// +// The constructor is private, instances must be created via +// JsonBuffer::createObject() or JsonBuffer::parseObject(). +// A JsonObject can be serialized to a JSON string via JsonObject::printTo(). +// It can also be deserialized from a JSON string via JsonBuffer::parseObject(). +class JsonObject : public Internals::JsonPrintable, + public Internals::ReferenceType, + public Internals::List, + public Internals::JsonBufferAllocated { + public: + // A meta-function that returns true if type T can be used in + // JsonObject::set() + template + struct CanSet { + static const bool value = JsonVariant::IsConstructibleFrom::value || + TypeTraits::IsSame::value || + TypeTraits::IsSame::value; + }; + + // Create an empty JsonArray attached to the specified JsonBuffer. + // You should not use this constructor directly. + // Instead, use JsonBuffer::createObject() or JsonBuffer.parseObject(). + FORCE_INLINE explicit JsonObject(JsonBuffer* buffer) + : Internals::List(buffer) {} + + // Gets or sets the value associated with the specified key. + FORCE_INLINE JsonObjectSubscript operator[](const char* key); + FORCE_INLINE JsonObjectSubscript operator[](const String& key); + + // Gets the value associated with the specified key. + FORCE_INLINE JsonVariant operator[](JsonObjectKey key) const; + + // Sets the specified key with the specified value. + // bool set(TKey key, bool value); + // bool set(TKey key, char value); + // bool set(TKey key, long value); + // bool set(TKey key, int value); + // bool set(TKey key, short value); + // bool set(TKey key, float value); + // bool set(TKey key, double value); + // bool set(TKey key, const char* value); + template + FORCE_INLINE bool set( + JsonObjectKey key, T value, + typename TypeTraits::EnableIf< + CanSet::value && !TypeTraits::IsReference::value>::type* = 0) { + return setNodeAt(key, value); + } + // bool set(Key, String&); + // bool set(Key, JsonArray&); + // bool set(Key, JsonObject&); + // bool set(Key, JsonVariant&); + template + FORCE_INLINE bool set( + JsonObjectKey key, const T& value, + typename TypeTraits::EnableIf::value>::type* = 0) { + return setNodeAt(key, const_cast(value)); + } + // bool set(Key, float value, uint8_t decimals); + // bool set(Key, double value, uint8_t decimals); + template + FORCE_INLINE bool set( + JsonObjectKey key, TValue value, uint8_t decimals, + typename TypeTraits::EnableIf< + TypeTraits::IsFloatingPoint::value>::type* = 0) { + return setNodeAt(key, JsonVariant(value, decimals)); + } + + // Gets the value associated with the specified key. + FORCE_INLINE JsonVariant get(JsonObjectKey) const; + + // Gets the value associated with the specified key. + template + FORCE_INLINE T get(JsonObjectKey) const; + + // Checks the type of the value associated with the specified key. + template + FORCE_INLINE bool is(JsonObjectKey) const; + + // Creates and adds a JsonArray. + // This is a shortcut for JsonBuffer::createArray() and JsonObject::add(). + FORCE_INLINE JsonArray& createNestedArray(JsonObjectKey key); + + // Creates and adds a JsonObject. + // This is a shortcut for JsonBuffer::createObject() and JsonObject::add(). + FORCE_INLINE JsonObject& createNestedObject(JsonObjectKey key); + + // Tells weither the specified key is present and associated with a value. + FORCE_INLINE bool containsKey(JsonObjectKey key) const; + + // Removes the specified key and the associated value. + void remove(JsonObjectKey key); + + // Returns a reference an invalid JsonObject. + // This object is meant to replace a NULL pointer. + // This is used when memory allocation or JSON parsing fail. + static JsonObject& invalid() { return _invalid; } + + // Serialize the object to the specified JsonWriter + void writeTo(Internals::JsonWriter& writer) const; + + private: + // Returns the list node that matches the specified key. + node_type* getNodeAt(const char* key) const; + + node_type* getOrCreateNodeAt(const char* key); + + template + FORCE_INLINE bool setNodeAt(JsonObjectKey key, T value); + + FORCE_INLINE bool setNodeKey(node_type*, JsonObjectKey key); + + template + FORCE_INLINE bool setNodeValue(node_type*, T value); + + // The instance returned by JsonObject::invalid() + static JsonObject _invalid; +}; +} + +#include "JsonObject.ipp" diff --git a/src/third-party/arduino-json-5.1.1/include/ArduinoJson/JsonObject.ipp b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/JsonObject.ipp new file mode 100644 index 00000000..b8fc87ff --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/JsonObject.ipp @@ -0,0 +1,134 @@ +// 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 { + +inline JsonVariant JsonObject::get(JsonObjectKey key) const { + node_type *node = getNodeAt(key.c_str()); + return node ? node->content.value : JsonVariant(); +} + +template +inline T JsonObject::get(JsonObjectKey key) const { + node_type *node = getNodeAt(key.c_str()); + return node ? node->content.value.as() : JsonVariant::invalid(); +} + +template +inline bool JsonObject::is(JsonObjectKey key) const { + node_type *node = getNodeAt(key.c_str()); + return node ? node->content.value.is() : false; +} + +inline JsonObjectSubscript JsonObject::operator[]( + const char *key) { + return JsonObjectSubscript(*this, key); +} + +inline JsonObjectSubscript JsonObject::operator[]( + const String &key) { + return JsonObjectSubscript(*this, key); +} + +inline JsonVariant JsonObject::operator[](JsonObjectKey key) const { + return get(key); +} + +inline bool JsonObject::containsKey(JsonObjectKey key) const { + return getNodeAt(key.c_str()) != NULL; +} + +inline void JsonObject::remove(JsonObjectKey key) { + removeNode(getNodeAt(key.c_str())); +} + +template +inline bool JsonObject::setNodeAt(JsonObjectKey key, T value) { + node_type *node = getNodeAt(key.c_str()); + if (!node) { + node = addNewNode(); + if (!node || !setNodeKey(node, key)) + return false; + } + return setNodeValue(node, value); +} + +inline bool JsonObject::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(); + } + return true; +} + +template +inline bool JsonObject::setNodeValue(node_type *node, TValue value) { + node->content.value = value; + return true; +} + +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 JsonObjectSubscript JsonVariantBase:: +operator[](const char *key) const { + return asObject()[key]; +} + +template +inline const JsonObjectSubscript JsonVariantBase:: +operator[](const String &key) const { + return asObject()[key]; +} + +template <> +inline JsonObject const &JsonVariant::invalid() { + return JsonObject::invalid(); +} + +template <> +inline JsonObject &JsonVariant::invalid() { + 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(key, array); + return array; +} + +inline JsonObject &JsonArray::createNestedObject() { + if (!_buffer) return JsonObject::invalid(); + JsonObject &object = _buffer->createObject(); + add(object); + return object; +} +} diff --git a/src/third-party/arduino-json-5.1.1/include/ArduinoJson/JsonObjectKey.hpp b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/JsonObjectKey.hpp new file mode 100644 index 00000000..5390b426 --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/JsonObjectKey.hpp @@ -0,0 +1,27 @@ +// 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 "Arduino/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; +}; +} diff --git a/src/third-party/arduino-json-5.1.1/include/ArduinoJson/JsonObjectSubscript.hpp b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/JsonObjectSubscript.hpp new file mode 100644 index 00000000..70b6a642 --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/JsonObjectSubscript.hpp @@ -0,0 +1,99 @@ +// 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" +#include "JsonVariantBase.hpp" +#include "TypeTraits/EnableIf.hpp" + +#ifdef _MSC_VER +#pragma warning(push) +#pragma warning(disable : 4522) +#endif + +namespace ArduinoJson { + +template +class JsonObjectSubscript : public JsonVariantBase > { + public: + FORCE_INLINE JsonObjectSubscript(JsonObject& object, TKey key) + : _object(object), _key(key) {} + + JsonObjectSubscript& operator=(const JsonObjectSubscript& src) { + _object.set(_key, src); + return *this; + } + + template + typename TypeTraits::EnableIf::value, + JsonObjectSubscript >::type& + operator=(const T& src) { + _object.set(_key, const_cast(src)); + return *this; + } + + template + typename TypeTraits::EnableIf::value, + JsonObjectSubscript >::type& + operator=(T src) { + _object.set(_key, src); + return *this; + } + + FORCE_INLINE bool success() const { return _object.containsKey(_key); } + + FORCE_INLINE operator JsonVariant() const { return _object.get(_key); } + + template + FORCE_INLINE TValue as() const { + return _object.get(_key); + } + + template + FORCE_INLINE bool is() const { + return _object.is(_key); + } + + template + FORCE_INLINE bool set(TValue value) { + return _object.set(_key, value); + } + + template + FORCE_INLINE bool set(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; +}; + +#if ARDUINOJSON_ENABLE_STD_STREAM +inline std::ostream& operator<<( + std::ostream& os, const JsonObjectSubscript& source) { + return source.printTo(os); +} + +inline std::ostream& operator<<( + std::ostream& os, const JsonObjectSubscript& source) { + return source.printTo(os); +} +#endif + +} // namespace ArduinoJson + +#ifdef _MSC_VER +#pragma warning(pop) +#endif diff --git a/src/third-party/arduino-json-5.1.1/include/ArduinoJson/JsonPair.hpp b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/JsonPair.hpp new file mode 100644 index 00000000..49ebed8d --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/JsonPair.hpp @@ -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 "JsonObjectKey.hpp" +#include "JsonVariant.hpp" + +namespace ArduinoJson { + +// A key value pair for JsonObject. +struct JsonPair { + const char* key; + JsonVariant value; +}; +} diff --git a/src/third-party/arduino-json-5.1.1/include/ArduinoJson/JsonVariant.hpp b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/JsonVariant.hpp new file mode 100644 index 00000000..f570c96f --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/JsonVariant.hpp @@ -0,0 +1,217 @@ +// 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 +#include // for uint8_t + +#include "Internals/JsonPrintable.hpp" +#include "Internals/JsonVariantContent.hpp" +#include "Internals/JsonVariantType.hpp" +#include "Internals/Unparsed.hpp" +#include "JsonVariantBase.hpp" +#include "TypeTraits/EnableIf.hpp" +#include "TypeTraits/IsFloatingPoint.hpp" +#include "TypeTraits/IsIntegral.hpp" +#include "TypeTraits/IsSame.hpp" +#include "TypeTraits/RemoveConst.hpp" +#include "TypeTraits/RemoveReference.hpp" + +namespace ArduinoJson { + +// Forward declarations. +class JsonArray; +class JsonObject; + +// A variant that can be a any value serializable to a JSON value. +// +// It can be set to: +// - a boolean +// - a char, short, int or a long (signed or unsigned) +// - a string (const char*) +// - a reference to a JsonArray or JsonObject +class JsonVariant : public JsonVariantBase { + public: + template + struct IsConstructibleFrom; + + // Creates an uninitialized JsonVariant + FORCE_INLINE JsonVariant() : _type(Internals::JSON_UNDEFINED) {} + + // Create a JsonVariant containing a boolean value. + // It will be serialized as "true" or "false" in JSON. + FORCE_INLINE JsonVariant(bool value); + + // Create a JsonVariant containing a floating point value. + // The second argument specifies the number of decimal digits to write in + // the JSON string. + // JsonVariant(double value, uint8_t decimals); + // JsonVariant(float value, uint8_t decimals); + template + FORCE_INLINE JsonVariant( + T value, uint8_t decimals = 2, + typename TypeTraits::EnableIf::value>::type + * = 0) { + using namespace Internals; + _type = static_cast(JSON_FLOAT_0_DECIMALS + decimals); + _content.asFloat = static_cast(value); + } + + // Create a JsonVariant containing an integer value. + // JsonVariant(short) + // JsonVariant(int) + // JsonVariant(long) + template + FORCE_INLINE JsonVariant( + T value, + typename TypeTraits::EnableIf::value>::type * = + 0) { + using namespace Internals; + _type = JSON_INTEGER; + _content.asInteger = static_cast(value); + } + + // Create a JsonVariant containing a string. + FORCE_INLINE JsonVariant(const char *value); + + // Create a JsonVariant containing an unparsed string + FORCE_INLINE JsonVariant(Internals::Unparsed value); + + // Create a JsonVariant containing a reference to an array. + FORCE_INLINE JsonVariant(JsonArray &array); + + // Create a JsonVariant containing a reference to an object. + FORCE_INLINE JsonVariant(JsonObject &object); + + // Get the variant as the specified type. + // short as() const; + // int as() const; + // long as() const; + template + const typename TypeTraits::EnableIf::value, T>::type + as() const { + return static_cast(asInteger()); + } + // double as() const; + // float as() const; + template + const typename TypeTraits::EnableIf::value, + T>::type + as() const { + return static_cast(asFloat()); + } + // const String as() const; + template + const typename TypeTraits::EnableIf::value, + T>::type + as() const { + return toString(); + } + // const char* as() const; + // const char* as() const; + template + typename TypeTraits::EnableIf::value, + const char *>::type + as() const { + return asString(); + } + // const bool as() const + template + const typename TypeTraits::EnableIf::value, + T>::type + as() const { + return asInteger() != 0; + } + // JsonArray& as const; + // JsonArray& as const; + // JsonArray& as const; + template + typename TypeTraits::EnableIf< + TypeTraits::IsSame< + typename TypeTraits::RemoveConst< + typename TypeTraits::RemoveReference::type>::type, + JsonArray>::value, + JsonArray &>::type + as() const { + return asArray(); + } + // JsonObject& as const; + // JsonObject& as const; + // JsonObject& as const; + template + typename TypeTraits::EnableIf< + TypeTraits::IsSame< + typename TypeTraits::RemoveConst< + typename TypeTraits::RemoveReference::type>::type, + JsonObject>::value, + JsonObject &>::type + as() const { + return asObject(); + } + + // Tells weither the variant has the specified type. + // Returns true if the variant has type type T, false otherwise. + template + bool is() const; + + // Serialize the variant to a JsonWriter + void writeTo(Internals::JsonWriter &writer) const; + + // TODO: rename + template + static T invalid(); + + const char *asString() const; + JsonArray &asArray() const; + JsonObject &asObject() const; + + private: + String toString() const; + Internals::JsonFloat asFloat() const; + Internals::JsonInteger asInteger() const; + + // The current type of the variant + Internals::JsonVariantType _type; + + // The various alternatives for the value of the variant. + Internals::JsonVariantContent _content; +}; + +inline JsonVariant float_with_n_digits(float value, uint8_t digits) { + return JsonVariant(value, digits); +} + +inline JsonVariant double_with_n_digits(double value, uint8_t digits) { + return JsonVariant(value, digits); +} + +template +struct JsonVariant::IsConstructibleFrom { + static const bool value = + TypeTraits::IsIntegral::value || + TypeTraits::IsFloatingPoint::value || + TypeTraits::IsSame::value || + TypeTraits::IsSame::value || + TypeTraits::IsSame::value || + TypeTraits::IsSame::value || + TypeTraits::IsSame::value || + TypeTraits::IsSame::value || + TypeTraits::IsSame::value || + TypeTraits::IsSame::value || + TypeTraits::IsSame::value || + TypeTraits::IsSame &>::value || + TypeTraits::IsSame &>::value || + TypeTraits::IsSame &>::value || + TypeTraits::IsSame &>::value || + TypeTraits::IsSame::value || + TypeTraits::IsSame::value; +}; +} + +// Include inline implementations +#include "JsonVariant.ipp" diff --git a/src/third-party/arduino-json-5.1.1/include/ArduinoJson/JsonVariant.ipp b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/JsonVariant.ipp new file mode 100644 index 00000000..c9fb3b71 --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/JsonVariant.ipp @@ -0,0 +1,150 @@ +// 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" +#include "JsonVariant.hpp" +#include "Internals/Parse.hpp" + +#include + +namespace ArduinoJson { + +inline JsonVariant::JsonVariant(bool value) { + using namespace Internals; + _type = JSON_BOOLEAN; + _content.asInteger = static_cast(value); +} + +inline JsonVariant::JsonVariant(const char *value) { + _type = Internals::JSON_STRING; + _content.asString = value; +} + +inline JsonVariant::JsonVariant(Internals::Unparsed value) { + _type = Internals::JSON_UNPARSED; + _content.asString = value; +} + +inline JsonVariant::JsonVariant(JsonArray &array) { + _type = Internals::JSON_ARRAY; + _content.asArray = &array; +} + +inline JsonVariant::JsonVariant(JsonObject &object) { + _type = Internals::JSON_OBJECT; + _content.asObject = &object; +} + +template +inline T JsonVariant::invalid() { + return T(); +} + +template +inline bool JsonVariant::is() const { + return false; +} + +template <> // in .cpp +bool JsonVariant::is() const; + +template <> // in .cpp +bool JsonVariant::is() const; + +template <> // int .cpp +bool JsonVariant::is() const; + +template <> +inline bool JsonVariant::is() const { + return _type == Internals::JSON_STRING; +} + +template <> +inline bool JsonVariant::is() const { + return is(); +} + +template <> +inline bool JsonVariant::is() const { + return _type == Internals::JSON_ARRAY; +} + +template <> +inline bool JsonVariant::is() const { + return _type == Internals::JSON_ARRAY; +} + +template <> +inline bool JsonVariant::is() const { + return _type == Internals::JSON_OBJECT; +} + +template <> +inline bool JsonVariant::is() const { + return _type == Internals::JSON_OBJECT; +} + +template <> +inline bool JsonVariant::is() const { + return is(); +} + +template <> +inline bool JsonVariant::is() const { + return is(); +} + +template <> +inline bool JsonVariant::is() const { + return is(); +} + +template <> +inline bool JsonVariant::is() const { + return is(); +} + +template <> +inline bool JsonVariant::is() const { + return is(); +} + +template <> +inline bool JsonVariant::is() const { + return is(); +} + +template <> +inline bool JsonVariant::is() const { + return is(); +} + +inline Internals::JsonInteger JsonVariant::asInteger() const { + if (_type == Internals::JSON_INTEGER || _type == Internals::JSON_BOOLEAN) + return _content.asInteger; + + if (_type >= Internals::JSON_FLOAT_0_DECIMALS) + return static_cast(_content.asFloat); + + if ((_type == Internals::JSON_STRING || _type == Internals::JSON_UNPARSED) && + _content.asString) { + if (!strcmp("true", _content.asString)) return 1; + return Internals::parse(_content.asString); + } + + return 0L; +} + +#if ARDUINOJSON_ENABLE_STD_STREAM +inline std::ostream &operator<<(std::ostream &os, const JsonVariant &source) { + return source.printTo(os); +} +#endif + +} // namespace ArduinoJson diff --git a/src/third-party/arduino-json-5.1.1/include/ArduinoJson/JsonVariantBase.hpp b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/JsonVariantBase.hpp new file mode 100644 index 00000000..7ca0919f --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/JsonVariantBase.hpp @@ -0,0 +1,133 @@ +// 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/ForceInline.hpp" +#include "JsonObjectKey.hpp" + +namespace ArduinoJson { + +// Forward declarations. +class JsonArraySubscript; +template +class JsonObjectSubscript; + +template +class JsonVariantBase : public Internals::JsonPrintable { + public: + FORCE_INLINE const char *asString() const { return as(); } + + // Gets the variant as an array. + // Returns a reference to the JsonArray or JsonArray::invalid() if the + // variant + // is not an array. + FORCE_INLINE operator JsonArray &() const { return as(); } + FORCE_INLINE JsonArray &asArray() const { return as(); } + + // Gets the variant as an object. + // Returns a reference to the JsonObject or JsonObject::invalid() if the + // variant is not an object. + FORCE_INLINE operator JsonObject &() const { return as(); } + FORCE_INLINE JsonObject &asObject() const { return as(); } + + template + FORCE_INLINE operator T() const { + return as(); + } + + template + FORCE_INLINE const T as() const { + return impl()->template as(); + } + + // Mimics an array or an object. + // Returns the size of the array or object if the variant has that type. + // Returns 0 if the variant is neither an array nor an object + size_t size() const { return asArray().size() + asObject().size(); } + + // Mimics an array. + // 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; + + // 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 operator[]( + const char *key) const; + FORCE_INLINE const JsonObjectSubscript operator[]( + const String &key) const; + + // Serialize the variant to a JsonWriter + void writeTo(Internals::JsonWriter &writer) const; + + private: + const TImpl *impl() const { return static_cast(this); } +}; + +template +inline bool operator==(const JsonVariantBase &left, TComparand right) { + return left.template as() == right; +} + +template +inline bool operator==(TComparand left, const JsonVariantBase &right) { + return left == right.template as(); +} + +template +inline bool operator!=(const JsonVariantBase &left, TComparand right) { + return left.template as() != right; +} + +template +inline bool operator!=(TComparand left, const JsonVariantBase &right) { + return left != right.template as(); +} + +template +inline bool operator<=(const JsonVariantBase &left, TComparand right) { + return left.template as() <= right; +} + +template +inline bool operator<=(TComparand left, const JsonVariantBase &right) { + return left <= right.template as(); +} + +template +inline bool operator>=(const JsonVariantBase &left, TComparand right) { + return left.template as() >= right; +} + +template +inline bool operator>=(TComparand left, const JsonVariantBase &right) { + return left >= right.template as(); +} + +template +inline bool operator<(const JsonVariantBase &left, TComparand right) { + return left.template as() < right; +} + +template +inline bool operator<(TComparand left, const JsonVariantBase &right) { + return left < right.template as(); +} + +template +inline bool operator>(const JsonVariantBase &left, TComparand right) { + return left.template as() > right; +} + +template +inline bool operator>(TComparand left, const JsonVariantBase &right) { + return left > right.template as(); +} +} diff --git a/src/third-party/arduino-json-5.1.1/include/ArduinoJson/StaticJsonBuffer.hpp b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/StaticJsonBuffer.hpp new file mode 100644 index 00000000..c1473e7f --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/StaticJsonBuffer.hpp @@ -0,0 +1,36 @@ +// 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" + +namespace ArduinoJson { + +// Implements a JsonBuffer with fixed memory allocation. +// The template paramenter CAPACITY specifies the capacity of the buffer in +// bytes. +template +class StaticJsonBuffer : public JsonBuffer { + public: + explicit StaticJsonBuffer() : _size(0) {} + + size_t capacity() const { 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; + } + + private: + uint8_t _buffer[CAPACITY]; + size_t _size; +}; +} diff --git a/src/third-party/arduino-json-5.1.1/include/ArduinoJson/TypeTraits/EnableIf.hpp b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/TypeTraits/EnableIf.hpp new file mode 100644 index 00000000..408e8bb0 --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/TypeTraits/EnableIf.hpp @@ -0,0 +1,22 @@ +// 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 if Condition is true. +template +struct EnableIf {}; + +template +struct EnableIf { + typedef T type; +}; +} +} diff --git a/src/third-party/arduino-json-5.1.1/include/ArduinoJson/TypeTraits/IsFloatingPoint.hpp b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/TypeTraits/IsFloatingPoint.hpp new file mode 100644 index 00000000..42a13ec6 --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/TypeTraits/IsFloatingPoint.hpp @@ -0,0 +1,21 @@ +// 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 "IsSame.hpp" + +namespace ArduinoJson { +namespace TypeTraits { + +// A meta-function that returns true if T is a floating point type +template +struct IsFloatingPoint { + static const bool value = IsSame::value || IsSame::value; +}; +} +} diff --git a/src/third-party/arduino-json-5.1.1/include/ArduinoJson/TypeTraits/IsIntegral.hpp b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/TypeTraits/IsIntegral.hpp new file mode 100644 index 00000000..b2e75ee6 --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/TypeTraits/IsIntegral.hpp @@ -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 "../Configuration.hpp" +#include "IsSame.hpp" + +#include + +namespace ArduinoJson { +namespace TypeTraits { + +// A meta-function that returns true if T is an integral type. +template +struct IsIntegral { + static const bool value = TypeTraits::IsSame::value || + TypeTraits::IsSame::value || + TypeTraits::IsSame::value || + TypeTraits::IsSame::value || + TypeTraits::IsSame::value || + TypeTraits::IsSame::value || + TypeTraits::IsSame::value || + TypeTraits::IsSame::value || +#if ARDUINOJSON_USE_LONG_LONG + TypeTraits::IsSame::value || + TypeTraits::IsSame::value || +#endif + +#if ARDUINOJSON_USE_INT64 + TypeTraits::IsSame::value || + TypeTraits::IsSame::value || +#endif + TypeTraits::IsSame::value; +}; +} +} diff --git a/src/third-party/arduino-json-5.1.1/include/ArduinoJson/TypeTraits/IsReference.hpp b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/TypeTraits/IsReference.hpp new file mode 100644 index 00000000..436a25db --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/TypeTraits/IsReference.hpp @@ -0,0 +1,24 @@ +// 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 T is a reference +template +struct IsReference { + static const bool value = false; +}; + +template +struct IsReference { + static const bool value = true; +}; +} +} diff --git a/src/third-party/arduino-json-5.1.1/include/ArduinoJson/TypeTraits/IsSame.hpp b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/TypeTraits/IsSame.hpp new file mode 100644 index 00000000..0b68d3c3 --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/TypeTraits/IsSame.hpp @@ -0,0 +1,24 @@ +// 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 types T and U are the same. +template +struct IsSame { + static const bool value = false; +}; + +template +struct IsSame { + static const bool value = true; +}; +} +} diff --git a/src/third-party/arduino-json-5.1.1/include/ArduinoJson/TypeTraits/RemoveConst.hpp b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/TypeTraits/RemoveConst.hpp new file mode 100644 index 00000000..cf7b3af2 --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/TypeTraits/RemoveConst.hpp @@ -0,0 +1,23 @@ +// 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 +struct RemoveConst { + typedef T type; +}; +template +struct RemoveConst { + typedef T type; +}; +} +} diff --git a/src/third-party/arduino-json-5.1.1/include/ArduinoJson/TypeTraits/RemoveReference.hpp b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/TypeTraits/RemoveReference.hpp new file mode 100644 index 00000000..d77eb85e --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/include/ArduinoJson/TypeTraits/RemoveReference.hpp @@ -0,0 +1,23 @@ +// 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 reference modifier. +template +struct RemoveReference { + typedef T type; +}; +template +struct RemoveReference { + typedef T type; +}; +} +} diff --git a/src/third-party/arduino-json-5.1.1/keywords.txt b/src/third-party/arduino-json-5.1.1/keywords.txt new file mode 100644 index 00000000..913b6aa6 --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/keywords.txt @@ -0,0 +1,14 @@ +JsonArray KEYWORD1 +JsonObject KEYWORD1 +JsonVariant KEYWORD1 +StaticJsonBuffer KEYWORD1 +add KEYWORD2 +createArray KEYWORD2 +createNestedArray KEYWORD2 +createNestedObject KEYWORD2 +createObject KEYWORD2 +parseArray KEYWORD2 +parseObject KEYWORD2 +prettyPrintTo KEYWORD2 +printTo KEYWORD2 +success KEYWORD2 diff --git a/src/third-party/arduino-json-5.1.1/library.properties b/src/third-party/arduino-json-5.1.1/library.properties new file mode 100644 index 00000000..5b0ae67b --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/library.properties @@ -0,0 +1,9 @@ +name=ArduinoJson +version=5.1.1 +author=Benoit Blanchon +maintainer=Benoit Blanchon +sentence=An efficient and elegant JSON library for Arduino. +paragraph=Like this project? Please star it on GitHub! +category=Data Processing +url=https://github.com/bblanchon/ArduinoJson +architectures=* diff --git a/src/third-party/arduino-json-5.1.1/src/ArduinoJson.h b/src/third-party/arduino-json-5.1.1/src/ArduinoJson.h new file mode 100644 index 00000000..d884cadc --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/src/ArduinoJson.h @@ -0,0 +1,14 @@ +// Copyright Benoit Blanchon 2014-2016 +// MIT License +// +// Arduino JSON library +// https://github.com/bblanchon/ArduinoJson +// If you like this project, please add a star! + +// About this file +// --------------- +// This file is here to please the Arduino IDE. It must be present in the src/ +// for the IDE to find it. Feel free to ignore this file if your working in +// another environment + +#include "../include/ArduinoJson.h" diff --git a/src/third-party/arduino-json-5.1.1/src/Internals/Comments.cpp b/src/third-party/arduino-json-5.1.1/src/Internals/Comments.cpp new file mode 100644 index 00000000..88e9ea80 --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/src/Internals/Comments.cpp @@ -0,0 +1,52 @@ +// 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 "../../include/ArduinoJson/Internals/Comments.hpp" + +inline static 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 static const char *skipCppStyleComment(const char *ptr) { + ptr += 2; + for (;;) { + if (ptr[0] == '\0' || ptr[0] == '\n') return ptr; + ptr++; + } +} + +const char *ArduinoJson::Internals::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; + } + } +} diff --git a/src/third-party/arduino-json-5.1.1/src/Internals/Encoding.cpp b/src/third-party/arduino-json-5.1.1/src/Internals/Encoding.cpp new file mode 100644 index 00000000..d9d070d6 --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/src/Internals/Encoding.cpp @@ -0,0 +1,14 @@ +// 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 "../../include/ArduinoJson/Internals/Encoding.hpp" + +// How to escape special chars: +// _escapeTable[2*i+1] => the special char +// _escapeTable[2*i] => the char to use instead +const char ArduinoJson::Internals::Encoding::_escapeTable[] = + "\"\"\\\\b\bf\fn\nr\rt\t"; diff --git a/src/third-party/arduino-json-5.1.1/src/Internals/IndentedPrint.cpp b/src/third-party/arduino-json-5.1.1/src/Internals/IndentedPrint.cpp new file mode 100644 index 00000000..f59fb5a2 --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/src/Internals/IndentedPrint.cpp @@ -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! + +#include "../../include/ArduinoJson/Internals/IndentedPrint.hpp" + +using namespace ArduinoJson::Internals; + +size_t IndentedPrint::write(uint8_t c) { + size_t n = 0; + + if (isNewLine) n += writeTabs(); + + n += sink->write(c); + + isNewLine = c == '\n'; + + return n; +} + +inline size_t IndentedPrint::writeTabs() { + size_t n = 0; + + for (int i = 0; i < level * tabSize; i++) n += sink->write(' '); + + return n; +} diff --git a/src/third-party/arduino-json-5.1.1/src/Internals/JsonParser.cpp b/src/third-party/arduino-json-5.1.1/src/Internals/JsonParser.cpp new file mode 100644 index 00000000..beb68092 --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/src/Internals/JsonParser.cpp @@ -0,0 +1,201 @@ +// 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 "../../include/ArduinoJson/Internals/JsonParser.hpp" + +#include "../../include/ArduinoJson/Internals/Comments.hpp" +#include "../../include/ArduinoJson/Internals/Encoding.hpp" +#include "../../include/ArduinoJson/JsonArray.hpp" +#include "../../include/ArduinoJson/JsonBuffer.hpp" +#include "../../include/ArduinoJson/JsonObject.hpp" + +using namespace ArduinoJson; +using namespace ArduinoJson::Internals; + +bool JsonParser::skip(char charToSkip) { + const char *ptr = skipSpacesAndComments(_readPtr); + if (*ptr != charToSkip) return false; + ptr++; + _readPtr = skipSpacesAndComments(ptr); + return true; +} + +bool JsonParser::parseAnythingTo(JsonVariant *destination) { + if (_nestingLimit == 0) return false; + _nestingLimit--; + bool success = parseAnythingToUnsafe(destination); + _nestingLimit++; + return success; +} + +inline bool JsonParser::parseAnythingToUnsafe(JsonVariant *destination) { + _readPtr = skipSpacesAndComments(_readPtr); + + switch (*_readPtr) { + case '[': + return parseArrayTo(destination); + + case '{': + return parseObjectTo(destination); + + default: + return parseStringTo(destination); + } +} + +JsonArray &JsonParser::parseArray() { + // Create an empty array + JsonArray &array = _buffer->createArray(); + + // Check opening braket + if (!skip('[')) goto ERROR_MISSING_BRACKET; + if (skip(']')) goto SUCCESS_EMPTY_ARRAY; + + // Read each value + for (;;) { + // 1 - Parse value + JsonVariant value; + if (!parseAnythingTo(&value)) goto ERROR_INVALID_VALUE; + if (!array.add(value)) goto ERROR_NO_MEMORY; + + // 2 - More values? + if (skip(']')) goto SUCCES_NON_EMPTY_ARRAY; + if (!skip(',')) goto ERROR_MISSING_COMMA; + } + +SUCCESS_EMPTY_ARRAY: +SUCCES_NON_EMPTY_ARRAY: + return array; + +ERROR_INVALID_VALUE: +ERROR_MISSING_BRACKET: +ERROR_MISSING_COMMA: +ERROR_NO_MEMORY: + return JsonArray::invalid(); +} + +bool JsonParser::parseArrayTo(JsonVariant *destination) { + JsonArray &array = parseArray(); + if (!array.success()) return false; + + *destination = array; + return true; +} + +JsonObject &JsonParser::parseObject() { + // Create an empty object + JsonObject &object = _buffer->createObject(); + + // Check opening brace + if (!skip('{')) goto ERROR_MISSING_BRACE; + if (skip('}')) 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; + + // 2 - Parse value + JsonVariant value; + if (!parseAnythingTo(&value)) goto ERROR_INVALID_VALUE; + 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; + } + +SUCCESS_EMPTY_OBJECT: +SUCCESS_NON_EMPTY_OBJECT: + return object; + +ERROR_INVALID_KEY: +ERROR_INVALID_VALUE: +ERROR_MISSING_BRACE: +ERROR_MISSING_COLON: +ERROR_MISSING_COMMA: +ERROR_NO_MEMORY: + return JsonObject::invalid(); +} + +bool JsonParser::parseObjectTo(JsonVariant *destination) { + JsonObject &object = parseObject(); + if (!object.success()) return false; + + *destination = object; + return true; +} + +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 == '\"'; } + +const char *JsonParser::parseString() { + const char *readPtr = _readPtr; + char *writePtr = _writePtr; + + char c = *readPtr; + + if (isQuote(c)) { // quotes + char stopChar = c; + for (;;) { + c = *++readPtr; + if (c == '\0') break; + + if (c == stopChar) { + readPtr++; + break; + } + + if (c == '\\') { + // replace char + c = Encoding::unescapeChar(*++readPtr); + if (c == '\0') break; + } + + *writePtr++ = c; + } + } else { // no quotes + for (;;) { + if (!isLetterOrNumber(c)) break; + *writePtr++ = c; + c = *++readPtr; + } + } + // end the string here + *writePtr++ = '\0'; + + const char *startPtr = _writePtr; + + // update end ptr + _readPtr = readPtr; + _writePtr = writePtr; + + // return pointer to unquoted string + return startPtr; +} + +bool JsonParser::parseStringTo(JsonVariant *destination) { + bool hasQuotes = isQuote(_readPtr[0]); + const char *value = parseString(); + if (value == NULL) return false; + if (hasQuotes) { + *destination = value; + } else { + *destination = Unparsed(value); + } + return true; +} diff --git a/src/third-party/arduino-json-5.1.1/src/Internals/List.cpp b/src/third-party/arduino-json-5.1.1/src/Internals/List.cpp new file mode 100644 index 00000000..c31a859f --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/src/Internals/List.cpp @@ -0,0 +1,50 @@ +// 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 "../../include/ArduinoJson/Internals/List.hpp" + +#include "../../include/ArduinoJson/JsonPair.hpp" +#include "../../include/ArduinoJson/JsonVariant.hpp" + +using namespace ArduinoJson; +using namespace ArduinoJson::Internals; + +template +size_t List::size() const { + size_t nodeCount = 0; + for (node_type *node = _firstNode; node; node = node->next) nodeCount++; + return nodeCount; +} + +template +typename List::node_type *List::addNewNode() { + node_type *newNode = new (_buffer) node_type(); + + if (_firstNode) { + node_type *lastNode = _firstNode; + while (lastNode->next) lastNode = lastNode->next; + lastNode->next = newNode; + } else { + _firstNode = newNode; + } + + return newNode; +} + +template +void List::removeNode(node_type *nodeToRemove) { + if (!nodeToRemove) return; + if (nodeToRemove == _firstNode) { + _firstNode = nodeToRemove->next; + } else { + for (node_type *node = _firstNode; node; node = node->next) + if (node->next == nodeToRemove) node->next = nodeToRemove->next; + } +} + +template class ArduinoJson::Internals::List; +template class ArduinoJson::Internals::List; diff --git a/src/third-party/arduino-json-5.1.1/src/Internals/Prettyfier.cpp b/src/third-party/arduino-json-5.1.1/src/Internals/Prettyfier.cpp new file mode 100644 index 00000000..79bfe763 --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/src/Internals/Prettyfier.cpp @@ -0,0 +1,87 @@ +// 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 "../../include/ArduinoJson/Internals/Prettyfier.hpp" + +using namespace ArduinoJson::Internals; + +size_t Prettyfier::write(uint8_t c) { + size_t n = _inString ? handleStringChar(c) : handleMarkupChar(c); + _previousChar = c; + return n; +} + +inline size_t Prettyfier::handleStringChar(uint8_t c) { + bool isQuote = c == '"' && _previousChar != '\\'; + + if (isQuote) _inString = false; + + return _sink.write(c); +} + +inline size_t Prettyfier::handleMarkupChar(uint8_t c) { + switch (c) { + case '{': + case '[': + return handleBlockOpen(c); + + case '}': + case ']': + return handleBlockClose(c); + + case ':': + return handleColon(); + + case ',': + return handleComma(); + + case '"': + return handleQuoteOpen(); + + default: + return handleNormalChar(c); + } +} + +inline size_t Prettyfier::handleBlockOpen(uint8_t c) { + return indentIfNeeded() + _sink.write(c); +} + +inline size_t Prettyfier::handleBlockClose(uint8_t c) { + return unindentIfNeeded() + _sink.write(c); +} + +inline size_t Prettyfier::handleColon() { + return _sink.write(':') + _sink.write(' '); +} + +inline size_t Prettyfier::handleComma() { + return _sink.write(',') + _sink.println(); +} + +inline size_t Prettyfier::handleQuoteOpen() { + _inString = true; + return indentIfNeeded() + _sink.write('"'); +} + +inline size_t Prettyfier::handleNormalChar(uint8_t c) { + return indentIfNeeded() + _sink.write(c); +} + +size_t Prettyfier::indentIfNeeded() { + if (!inEmptyBlock()) return 0; + + _sink.indent(); + return _sink.println(); +} + +size_t Prettyfier::unindentIfNeeded() { + if (inEmptyBlock()) return 0; + + _sink.unindent(); + return _sink.println(); +} diff --git a/src/third-party/arduino-json-5.1.1/src/Internals/StaticStringBuilder.cpp b/src/third-party/arduino-json-5.1.1/src/Internals/StaticStringBuilder.cpp new file mode 100644 index 00000000..4ac5d3f3 --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/src/Internals/StaticStringBuilder.cpp @@ -0,0 +1,18 @@ +// 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 "../../include/ArduinoJson/Internals/StaticStringBuilder.hpp" + +using namespace ArduinoJson::Internals; + +size_t StaticStringBuilder::write(uint8_t c) { + if (length >= capacity) return 0; + + buffer[length++] = c; + buffer[length] = '\0'; + return 1; +} diff --git a/src/third-party/arduino-json-5.1.1/src/JsonArray.cpp b/src/third-party/arduino-json-5.1.1/src/JsonArray.cpp new file mode 100644 index 00000000..7d39ced0 --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/src/JsonArray.cpp @@ -0,0 +1,40 @@ +// 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 "../include/ArduinoJson/JsonArray.hpp" + +#include "../include/ArduinoJson/JsonBuffer.hpp" +#include "../include/ArduinoJson/JsonObject.hpp" + +using namespace ArduinoJson; +using namespace ArduinoJson::Internals; + +JsonArray JsonArray::_invalid(NULL); + +JsonArray::node_type *JsonArray::getNodeAt(size_t index) const { + node_type *node = _firstNode; + while (node && index--) node = node->next; + return node; +} + +void JsonArray::removeAt(size_t index) { removeNode(getNodeAt(index)); } + +void JsonArray::writeTo(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(); +} diff --git a/src/third-party/arduino-json-5.1.1/src/JsonBuffer.cpp b/src/third-party/arduino-json-5.1.1/src/JsonBuffer.cpp new file mode 100644 index 00000000..f67ed2a4 --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/src/JsonBuffer.cpp @@ -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! + +#include "../include/ArduinoJson/JsonBuffer.hpp" + +#include "../include/ArduinoJson/Internals/JsonParser.hpp" +#include "../include/ArduinoJson/JsonArray.hpp" +#include "../include/ArduinoJson/JsonObject.hpp" + +using namespace ArduinoJson; +using namespace ArduinoJson::Internals; + +JsonArray &JsonBuffer::createArray() { + JsonArray *ptr = new (this) JsonArray(this); + return ptr ? *ptr : JsonArray::invalid(); +} + +JsonObject &JsonBuffer::createObject() { + JsonObject *ptr = new (this) JsonObject(this); + return ptr ? *ptr : JsonObject::invalid(); +} + +JsonArray &JsonBuffer::parseArray(char *json, uint8_t nestingLimit) { + JsonParser parser(this, json, nestingLimit); + return parser.parseArray(); +} + +JsonObject &JsonBuffer::parseObject(char *json, uint8_t nestingLimit) { + JsonParser parser(this, json, nestingLimit); + return parser.parseObject(); +} + +char *JsonBuffer::strdup(const char *source, size_t length) { + size_t size = length + 1; + char *dest = static_cast(alloc(size)); + if (dest != NULL) memcpy(dest, source, size); + return dest; +} diff --git a/src/third-party/arduino-json-5.1.1/src/JsonObject.cpp b/src/third-party/arduino-json-5.1.1/src/JsonObject.cpp new file mode 100644 index 00000000..a81772ca --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/src/JsonObject.cpp @@ -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! + +#include "../include/ArduinoJson/JsonObject.hpp" + +#include // for strcmp + +#include "../include/ArduinoJson/Internals/StaticStringBuilder.hpp" +#include "../include/ArduinoJson/JsonArray.hpp" +#include "../include/ArduinoJson/JsonBuffer.hpp" + +using namespace ArduinoJson; +using namespace ArduinoJson::Internals; + +JsonObject JsonObject::_invalid(NULL); + +JsonObject::node_type *JsonObject::getNodeAt(const char *key) const { + for (node_type *node = _firstNode; node; node = node->next) { + if (!strcmp(node->content.key, key)) return node; + } + return NULL; +} + +void JsonObject::writeTo(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(); +} diff --git a/src/third-party/arduino-json-5.1.1/src/JsonVariant.cpp b/src/third-party/arduino-json-5.1.1/src/JsonVariant.cpp new file mode 100644 index 00000000..d2bee252 --- /dev/null +++ b/src/third-party/arduino-json-5.1.1/src/JsonVariant.cpp @@ -0,0 +1,110 @@ +// 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 "../include/ArduinoJson/JsonVariant.hpp" + +#include "../include/ArduinoJson/JsonArray.hpp" +#include "../include/ArduinoJson/JsonObject.hpp" + +#include // for errno +#include // for strtol, strtod + +using namespace ArduinoJson::Internals; + +namespace ArduinoJson { + +const char *JsonVariant::asString() const { + if (_type == JSON_UNPARSED && _content.asString && + !strcmp("null", _content.asString)) + return NULL; + if (_type == JSON_STRING || _type == JSON_UNPARSED) return _content.asString; + return NULL; +} + +JsonFloat JsonVariant::asFloat() const { + if (_type >= JSON_FLOAT_0_DECIMALS) return _content.asFloat; + + if (_type == JSON_INTEGER || _type == JSON_BOOLEAN) + return static_cast(_content.asInteger); + + if ((_type == JSON_STRING || _type == JSON_UNPARSED) && _content.asString) + return parse(_content.asString); + + return 0.0; +} + +String JsonVariant::toString() const { + String s; + if ((_type == JSON_STRING || _type == JSON_UNPARSED) && + _content.asString != NULL) + s = _content.asString; + else + printTo(s); + return s; +} + +template <> +bool JsonVariant::is() const { + if (_type == JSON_BOOLEAN) return true; + + if (_type != JSON_UNPARSED || _content.asString == NULL) return false; + + return !strcmp(_content.asString, "true") || + !strcmp(_content.asString, "false"); +} + +template <> +bool JsonVariant::is() const { + if (_type == JSON_INTEGER) return true; + + if (_type != JSON_UNPARSED || _content.asString == NULL) return false; + + char *end; + errno = 0; + strtol(_content.asString, &end, 10); + + return *end == '\0' && errno == 0; +} + +template <> +bool JsonVariant::is() const { + if (_type >= JSON_FLOAT_0_DECIMALS) return true; + + if (_type != JSON_UNPARSED || _content.asString == NULL) return false; + + char *end; + errno = 0; + strtod(_content.asString, &end); + + return *end == '\0' && errno == 0 && !is(); +} + +void JsonVariant::writeTo(JsonWriter &writer) const { + if (_type == JSON_ARRAY) + _content.asArray->writeTo(writer); + + else if (_type == JSON_OBJECT) + _content.asObject->writeTo(writer); + + else if (_type == JSON_STRING) + writer.writeString(_content.asString); + + else if (_type == JSON_UNPARSED) + writer.writeRaw(_content.asString); + + else if (_type == JSON_INTEGER) + writer.writeInteger(_content.asInteger); + + else if (_type == JSON_BOOLEAN) + writer.writeBoolean(_content.asInteger != 0); + + else if (_type >= JSON_FLOAT_0_DECIMALS) { + uint8_t decimals = static_cast(_type - JSON_FLOAT_0_DECIMALS); + writer.writeFloat(_content.asFloat, decimals); + } +} +}