forked from bblanchon/ArduinoJson
Compare commits
22 Commits
v5.0-beta-
...
v5.0.4
Author | SHA1 | Date | |
---|---|---|---|
b9e3255c9e | |||
e657396f65 | |||
929f608f2f | |||
c6a4bfa886 | |||
d5e25b12b8 | |||
7cf6fe6d62 | |||
155dd653e7 | |||
b5c8cd1766 | |||
4967e389c5 | |||
ffbaebd198 | |||
04b8781c8d | |||
5a4d993f7d | |||
823a172681 | |||
a1943e21ed | |||
01c287bc89 | |||
0cf8249b14 | |||
a8265a799d | |||
18bb653f10 | |||
a003a31952 | |||
0a1c27f873 | |||
75f8e25aed | |||
39c506b419 |
2
.gitignore
vendored
2
.gitignore
vendored
@ -4,3 +4,5 @@
|
||||
/bin
|
||||
/lib
|
||||
/sftp-config.json
|
||||
.tags
|
||||
.tags_sorted_by_file
|
||||
|
30
CHANGELOG.md
30
CHANGELOG.md
@ -1,8 +1,34 @@
|
||||
ArduinoJson: change log
|
||||
=======================
|
||||
|
||||
v5.0 (currently in beta)
|
||||
----
|
||||
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<T>()` 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)
|
||||
|
@ -1,10 +1,10 @@
|
||||
The MIT License (MIT)
|
||||
---------------------
|
||||
|
||||
Copyright © 2014 Benoit BLANCHON
|
||||
Copyright © 2014-2015 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.
|
||||
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.
|
||||
|
32
README.md
32
README.md
@ -1,11 +1,11 @@
|
||||
Arduino JSON library
|
||||
====================
|
||||
|
||||
[](https://travis-ci.org/bblanchon/ArduinoJson) [](https://coveralls.io/r/bblanchon/ArduinoJson?branch=master)
|
||||
[](https://ci.appveyor.com/project/bblanchon/arduinojson/branch/master) [](https://travis-ci.org/bblanchon/ArduinoJson) [](https://coveralls.io/r/bblanchon/ArduinoJson?branch=master)
|
||||
|
||||
*An elegant and efficient JSON library for embedded systems.*
|
||||
|
||||
It's design to have the most intuitive API, the smallest footprint and works without any allocation on the heap (no malloc).
|
||||
It's designed to have the most intuitive API, the smallest footprint and 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.
|
||||
|
||||
@ -14,18 +14,26 @@ Features
|
||||
|
||||
* JSON decoding (comments are supported)
|
||||
* JSON encoding (with optional indentation)
|
||||
* Elegant API, very easy to use
|
||||
* 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
|
||||
* ESP8266
|
||||
* Teensy
|
||||
* Computers (Windows, Linux, OSX...)
|
||||
|
||||
Quick start
|
||||
-----------
|
||||
|
||||
#### Decoding / Parsing
|
||||
|
||||
|
||||
char json[] = "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
|
||||
|
||||
StaticJsonBuffer<200> jsonBuffer;
|
||||
@ -38,7 +46,7 @@ Quick start
|
||||
double longitude = root["data"][1];
|
||||
|
||||
#### Encoding / Generating
|
||||
|
||||
|
||||
StaticJsonBuffer<200> jsonBuffer;
|
||||
|
||||
JsonObject& root = jsonBuffer.createObject();
|
||||
@ -57,13 +65,13 @@ Quick start
|
||||
Documentation
|
||||
-------------
|
||||
|
||||
The documentation is available online in the [Arduino JSON wiki](https://github.com/bblanchon/ArduinoJson/wiki)
|
||||
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 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`:
|
||||
@ -78,6 +86,14 @@ From GitHub user `zacsketches`:
|
||||
> 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.
|
||||
|
||||
---
|
||||
|
||||
Found this library useful? [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:
|
||||
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:
|
||||
|
@ -1,7 +1,8 @@
|
||||
/*
|
||||
* Arduino JSON library - IndentedPrint example
|
||||
* Benoit Blanchon 2014 - MIT License
|
||||
*/
|
||||
// Copyright Benoit Blanchon 2014-2015
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
@ -9,6 +10,7 @@ using namespace ArduinoJson::Internals;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
// delay(1000); <--needed for some boards (like Teensy)
|
||||
|
||||
IndentedPrint serial(Serial);
|
||||
serial.setTabSize(4);
|
||||
@ -27,4 +29,4 @@ void setup() {
|
||||
|
||||
void loop() {
|
||||
// not used in this example
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright Benoit Blanchon 2014
|
||||
// Copyright Benoit Blanchon 2014-2015
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
@ -8,6 +8,7 @@
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
// delay(1000); <--needed for some boards (like Teensy)
|
||||
|
||||
StaticJsonBuffer<200> jsonBuffer;
|
||||
|
||||
@ -39,4 +40,4 @@ void setup() {
|
||||
|
||||
void loop() {
|
||||
// not used in this example
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
// Copyright Benoit Blanchon 2014
|
||||
// Copyright Benoit Blanchon 2014-2015
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
@ -8,6 +8,7 @@
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
// delay(1000); <--needed for some boards (like Teensy)
|
||||
|
||||
StaticJsonBuffer<200> jsonBuffer;
|
||||
|
||||
@ -34,4 +35,4 @@ void setup() {
|
||||
|
||||
void loop() {
|
||||
// not used in this example
|
||||
}
|
||||
}
|
||||
|
@ -15,13 +15,7 @@ class String : public std::string {
|
||||
public:
|
||||
String(const char *cstr = "") : std::string(cstr) {}
|
||||
String(const String &str) : std::string(str) {}
|
||||
explicit String(char c);
|
||||
explicit String(unsigned char);
|
||||
explicit String(int);
|
||||
explicit String(unsigned int);
|
||||
explicit String(long);
|
||||
explicit String(unsigned long);
|
||||
explicit String(float, unsigned char decimalPlaces = 2);
|
||||
explicit String(double, unsigned char decimalPlaces = 2);
|
||||
};
|
||||
|
||||
|
@ -63,7 +63,7 @@ class BlockJsonBuffer : public JsonBuffer {
|
||||
|
||||
void* allocInHead(size_t bytes) {
|
||||
void* p = _head->data + _head->size;
|
||||
_head->size += bytes;
|
||||
_head->size += round_size_up(bytes);
|
||||
return p;
|
||||
}
|
||||
|
||||
@ -76,8 +76,8 @@ class BlockJsonBuffer : public JsonBuffer {
|
||||
}
|
||||
|
||||
bool addNewBlock(size_t capacity) {
|
||||
size_t size = sizeof(EmptyBlock) + capacity;
|
||||
Block* block = static_cast<Block*>(_allocator.allocate(size));
|
||||
size_t bytes = sizeof(EmptyBlock) + capacity;
|
||||
Block* block = static_cast<Block*>(_allocator.allocate(bytes));
|
||||
if (block == NULL) return false;
|
||||
block->capacity = capacity;
|
||||
block->size = 0;
|
||||
|
@ -18,11 +18,14 @@ class DynamicStringBuilder : public Print {
|
||||
DynamicStringBuilder(String &str) : _str(str) {}
|
||||
|
||||
virtual size_t write(uint8_t c) {
|
||||
_str += c;
|
||||
// Need to cast to char, otherwise String will print a number (issue #120)
|
||||
_str += static_cast<char>(c);
|
||||
return 1;
|
||||
}
|
||||
|
||||
private:
|
||||
DynamicStringBuilder &operator=(const DynamicStringBuilder &);
|
||||
|
||||
String &_str;
|
||||
};
|
||||
}
|
||||
|
@ -19,7 +19,7 @@ class JsonParser {
|
||||
public:
|
||||
JsonParser(JsonBuffer *buffer, char *json, uint8_t nestingLimit)
|
||||
: _buffer(buffer),
|
||||
_readPtr(json),
|
||||
_readPtr(json ? json : ""),
|
||||
_writePtr(json),
|
||||
_nestingLimit(nestingLimit) {}
|
||||
|
||||
|
@ -8,6 +8,7 @@
|
||||
|
||||
#include "../Arduino/Print.hpp"
|
||||
#include "Encoding.hpp"
|
||||
#include "ForceInline.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
namespace Internals {
|
||||
@ -69,7 +70,7 @@ class JsonWriter {
|
||||
|
||||
protected:
|
||||
void write(char c) { _length += _sink.write(c); }
|
||||
void write(const char *s) { _length += _sink.print(s); }
|
||||
FORCE_INLINE void write(const char *s) { _length += _sink.print(s); }
|
||||
|
||||
Print &_sink;
|
||||
size_t _length;
|
||||
|
@ -63,7 +63,8 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
|
||||
FORCE_INLINE bool add(const String &value);
|
||||
FORCE_INLINE bool add(JsonArray &array);
|
||||
FORCE_INLINE bool add(JsonObject &object);
|
||||
FORCE_INLINE bool add(const JsonVariant &object);
|
||||
template <typename T>
|
||||
FORCE_INLINE bool add(const T &value);
|
||||
|
||||
// Sets the value at specified index.
|
||||
FORCE_INLINE void set(size_t index, bool value);
|
||||
@ -81,7 +82,8 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
|
||||
FORCE_INLINE void set(size_t index, const String &value);
|
||||
FORCE_INLINE void set(size_t index, JsonArray &array);
|
||||
FORCE_INLINE void set(size_t index, JsonObject &object);
|
||||
FORCE_INLINE void set(size_t index, const JsonVariant &object);
|
||||
template <typename T>
|
||||
FORCE_INLINE void set(size_t index, const T &value);
|
||||
|
||||
// Gets the value at the specified index.
|
||||
FORCE_INLINE JsonVariant get(size_t index) const;
|
||||
@ -92,7 +94,7 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
|
||||
|
||||
// Check the type of the value at specified index.
|
||||
template <typename T>
|
||||
FORCE_INLINE T is(size_t index) const;
|
||||
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()
|
||||
|
@ -77,8 +77,9 @@ inline bool JsonArray::add(JsonObject &object) {
|
||||
return addNode<JsonObject &>(object);
|
||||
}
|
||||
|
||||
inline bool JsonArray::add(const JsonVariant &object) {
|
||||
return addNode<const JsonVariant &>(object);
|
||||
template <typename T>
|
||||
inline bool JsonArray::add(const T &variant) {
|
||||
return addNode<const JsonVariant &>(variant);
|
||||
}
|
||||
|
||||
template <typename TValue>
|
||||
@ -149,8 +150,9 @@ inline void JsonArray::set(size_t index, JsonObject &object) {
|
||||
return setNodeAt<JsonObject &>(index, object);
|
||||
}
|
||||
|
||||
inline void JsonArray::set(size_t index, const JsonVariant &object) {
|
||||
return setNodeAt<const JsonVariant &>(index, object);
|
||||
template <typename T>
|
||||
inline void JsonArray::set(size_t index, const T &variant) {
|
||||
return setNodeAt<const JsonVariant &>(index, variant);
|
||||
}
|
||||
|
||||
template <typename TValue>
|
||||
@ -182,7 +184,7 @@ inline T JsonArray::get(size_t index) const {
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline T JsonArray::is(size_t index) const {
|
||||
inline bool JsonArray::is(size_t index) const {
|
||||
node_type *node = getNodeAt(index);
|
||||
return node ? node->content.is<T>() : false;
|
||||
}
|
||||
|
@ -8,19 +8,26 @@
|
||||
|
||||
#include "JsonSubscriptBase.hpp"
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(push)
|
||||
#pragma warning(disable : 4522)
|
||||
#endif
|
||||
|
||||
namespace ArduinoJson {
|
||||
class JsonArraySubscript : public JsonSubscriptBase<JsonArraySubscript> {
|
||||
public:
|
||||
FORCE_INLINE JsonArraySubscript(JsonArray& array, size_t index)
|
||||
: _array(array), _index(index) {}
|
||||
|
||||
using JsonSubscriptBase::operator=;
|
||||
using JsonSubscriptBase<JsonArraySubscript>::operator=;
|
||||
|
||||
FORCE_INLINE JsonArraySubscript& operator=(const JsonArraySubscript& other) {
|
||||
// to prevent Visual Studio warning C4512: assignment operator could not be
|
||||
// generated
|
||||
_array.set(_index, other._array.get(other._index));
|
||||
return *this;
|
||||
JsonArraySubscript& operator=(const JsonArraySubscript& src) {
|
||||
return assign<const JsonVariant&>(src);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
JsonArraySubscript& operator=(const T& src) {
|
||||
return assign<const JsonVariant&>(src);
|
||||
}
|
||||
|
||||
FORCE_INLINE bool success() const { return _index < _array.size(); }
|
||||
@ -33,7 +40,7 @@ class JsonArraySubscript : public JsonSubscriptBase<JsonArraySubscript> {
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
FORCE_INLINE T is() const {
|
||||
FORCE_INLINE bool is() const {
|
||||
return _array.is<T>(_index);
|
||||
}
|
||||
|
||||
@ -59,3 +66,7 @@ inline std::ostream& operator<<(std::ostream& os,
|
||||
#endif
|
||||
|
||||
} // namespace ArduinoJson
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(pop)
|
||||
#endif
|
||||
|
@ -59,9 +59,15 @@ class JsonBuffer {
|
||||
// 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(strdup(json), nesting);
|
||||
return parseArray(json.c_str(), nesting);
|
||||
}
|
||||
|
||||
// Allocates and populate a JsonObject from a JSON string.
|
||||
@ -76,30 +82,54 @@ class JsonBuffer {
|
||||
// allocation fails.
|
||||
JsonObject &parseObject(char *json, uint8_t nestingLimit = DEFAULT_LIMIT);
|
||||
|
||||
// Same as above with a String class
|
||||
JsonObject &parseObject(const String &json, uint8_t nesting = 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 strdup(src, strlen(src)); }
|
||||
char *strdup(const char *src) {
|
||||
return src ? strdup(src, strlen(src)) : NULL;
|
||||
}
|
||||
char *strdup(const String &src) { return strdup(src.c_str(), src.length()); }
|
||||
char *strdup(const char *, size_t);
|
||||
|
||||
// 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 defined ARDUINO_ARCH_AVR
|
||||
// alignment isn't needed for 8-bit AVR
|
||||
return bytes;
|
||||
#else
|
||||
const size_t x = sizeof(void *) - 1;
|
||||
return (bytes + x) & ~x;
|
||||
#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
|
||||
// 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
|
||||
// The purpose of this feature is to prevent stack overflow that could
|
||||
// lead to
|
||||
// a security risk.
|
||||
static const uint8_t DEFAULT_LIMIT = 10;
|
||||
};
|
||||
|
@ -65,6 +65,9 @@ class JsonObject : public Internals::JsonPrintable<JsonObject>,
|
||||
FORCE_INLINE bool set(const char* key, JsonArray& array);
|
||||
FORCE_INLINE bool set(const char* key, JsonObject& object);
|
||||
FORCE_INLINE bool set(const char* key, const JsonVariant& value);
|
||||
template <typename T>
|
||||
FORCE_INLINE bool set(const char* key, const T& value);
|
||||
|
||||
FORCE_INLINE bool set(const String& key, bool value);
|
||||
FORCE_INLINE bool set(const String& key, float value, uint8_t decimals = 2);
|
||||
FORCE_INLINE bool set(const String& key, double value, uint8_t decimals = 2);
|
||||
@ -81,6 +84,8 @@ class JsonObject : public Internals::JsonPrintable<JsonObject>,
|
||||
FORCE_INLINE bool set(const String& key, JsonArray& array);
|
||||
FORCE_INLINE bool set(const String& key, JsonObject& object);
|
||||
FORCE_INLINE bool set(const String& key, const JsonVariant& value);
|
||||
template <typename T>
|
||||
FORCE_INLINE bool set(const String& key, const T& value);
|
||||
|
||||
// Gets the value associated with the specified key.
|
||||
FORCE_INLINE JsonVariant get(JsonObjectKey) const;
|
||||
|
@ -116,6 +116,10 @@ inline bool JsonObject::set(const char *key, JsonObject &object) {
|
||||
inline bool JsonObject::set(const char *key, const JsonVariant &value) {
|
||||
return setNodeAt<const char *, const JsonVariant &>(key, value);
|
||||
}
|
||||
template <typename T>
|
||||
inline bool JsonObject::set(const char *key, const T &value) {
|
||||
return setNodeAt<const char *, JsonVariant>(key, value);
|
||||
}
|
||||
inline bool JsonObject::set(const String &key, bool value) {
|
||||
return setNodeAt<const String &, bool>(key, value);
|
||||
}
|
||||
@ -166,6 +170,10 @@ inline bool JsonObject::set(const String &key, JsonObject &object) {
|
||||
inline bool JsonObject::set(const String &key, const JsonVariant &value) {
|
||||
return setNodeAt<const String &, const JsonVariant &>(key, value);
|
||||
}
|
||||
template <typename T>
|
||||
inline bool JsonObject::set(const String &key, const T &value) {
|
||||
return setNodeAt<const String &, JsonVariant>(key, value);
|
||||
}
|
||||
|
||||
template <typename TKey, typename TValue>
|
||||
inline bool JsonObject::setNodeAt(TKey key, TValue value) {
|
||||
|
@ -8,6 +8,11 @@
|
||||
|
||||
#include "JsonSubscriptBase.hpp"
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(push)
|
||||
#pragma warning(disable : 4522)
|
||||
#endif
|
||||
|
||||
namespace ArduinoJson {
|
||||
|
||||
template <typename TKey>
|
||||
@ -19,11 +24,15 @@ class JsonObjectSubscript
|
||||
|
||||
using JsonSubscriptBase<JsonObjectSubscript<TKey> >::operator=;
|
||||
|
||||
FORCE_INLINE JsonObjectSubscript<TKey>& operator=(
|
||||
const JsonObjectSubscript<TKey>& other) {
|
||||
// to prevent Visual Studio warning C4512: assignment operator could not be
|
||||
// generated
|
||||
return set(other.get());
|
||||
JsonObjectSubscript<TKey>& operator=(const JsonObjectSubscript<TKey>& src) {
|
||||
return JsonSubscriptBase<JsonObjectSubscript<TKey> >::template assign<
|
||||
JsonVariant>(src);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
JsonObjectSubscript<TKey>& operator=(const T& src) {
|
||||
return JsonSubscriptBase<JsonObjectSubscript<TKey> >::template assign<
|
||||
JsonVariant>(src);
|
||||
}
|
||||
|
||||
FORCE_INLINE bool success() const { return _object.containsKey(_key); }
|
||||
@ -36,7 +45,7 @@ class JsonObjectSubscript
|
||||
}
|
||||
|
||||
template <typename TValue>
|
||||
FORCE_INLINE TValue is() const {
|
||||
FORCE_INLINE bool is() const {
|
||||
return _object.is<TValue>(_key);
|
||||
}
|
||||
|
||||
@ -67,4 +76,8 @@ inline std::ostream& operator<<(
|
||||
return source.printTo(os);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
} // namespace ArduinoJson
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(pop)
|
||||
#endif
|
||||
|
@ -68,15 +68,15 @@ class JsonSubscriptBase : public JsonVariantBase<TImpl> {
|
||||
}
|
||||
|
||||
FORCE_INLINE TImpl& operator=(JsonVariant value) {
|
||||
return assign<JsonVariant>(value);
|
||||
return assign<const JsonVariant&>(value);
|
||||
}
|
||||
|
||||
private:
|
||||
protected:
|
||||
template <typename TValue>
|
||||
FORCE_INLINE TImpl& assign(TValue value) {
|
||||
TImpl* impl = static_cast<TImpl*>(this);
|
||||
impl->template set<TValue>(value);
|
||||
return *impl;
|
||||
TImpl* that = static_cast<TImpl*>(this);
|
||||
that->template set<TValue>(value);
|
||||
return *that;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
@ -101,7 +101,7 @@ const char *JsonVariant::as<const char *>() const;
|
||||
|
||||
template <>
|
||||
inline bool JsonVariant::as<bool>() const {
|
||||
return as<long>();
|
||||
return as<long>() != 0;
|
||||
}
|
||||
|
||||
template <>
|
||||
|
@ -21,11 +21,10 @@ class StaticJsonBuffer : public JsonBuffer {
|
||||
size_t capacity() const { return CAPACITY; }
|
||||
size_t size() const { return _size; }
|
||||
|
||||
protected:
|
||||
virtual void* alloc(size_t bytes) {
|
||||
if (_size + bytes > CAPACITY) return NULL;
|
||||
void* p = &_buffer[_size];
|
||||
_size += bytes;
|
||||
_size += round_size_up(bytes);
|
||||
return p;
|
||||
}
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
name=ArduinoJson
|
||||
version=5.0.0
|
||||
version=5.0.4
|
||||
author=Benoit Blanchon <blog.benoitblanchon.fr>
|
||||
maintainer=Benoit Blanchon <blog.benoitblanchon.fr>
|
||||
sentence=An efficient and elegant JSON library for Arduino.
|
||||
|
52
scripts/build-old-arduino-package.sh
Normal file
52
scripts/build-old-arduino-package.sh
Normal file
@ -0,0 +1,52 @@
|
||||
#!/bin/bash
|
||||
|
||||
TAG=$(git describe)
|
||||
OUTPUT="ArduinoJson-$TAG-old-layout.zip"
|
||||
|
||||
cd $(dirname $0)/..
|
||||
|
||||
cat > ArduinoJson.h <<END
|
||||
// WARNING:
|
||||
// --------
|
||||
// This file is a workaround for old version of the Arduino IDE.
|
||||
// If you are using Arduino IDE 1.0.6 or above, then you installed the wrong
|
||||
// package of ArduinoJson.
|
||||
// In that case, just delete the current installation and install the package.
|
||||
|
||||
END
|
||||
cp ArduinoJson.h ArduinoJson.cpp
|
||||
|
||||
cat "include/ArduinoJson.h" | sed 's!include "!include "include/!g' >> ArduinoJson.h
|
||||
|
||||
find src -name "*.cpp" |
|
||||
while read FILE; do
|
||||
echo >> ArduinoJson.cpp
|
||||
echo "// $FILE" >> ArduinoJson.cpp
|
||||
echo "//" >> ArduinoJson.cpp
|
||||
cat "$FILE" | sed 's!\.\./!!g' >> ArduinoJson.cpp
|
||||
done
|
||||
|
||||
unix2dos ArduinoJson.cpp
|
||||
unix2dos ArduinoJson.h
|
||||
|
||||
pushd ..
|
||||
|
||||
# remove existing file
|
||||
rm -f $OUTPUT
|
||||
|
||||
# create zipman dos2
|
||||
7z a $OUTPUT \
|
||||
ArduinoJson/CHANGELOG.md \
|
||||
ArduinoJson/examples \
|
||||
ArduinoJson/include/ArduinoJson \
|
||||
ArduinoJson/keywords.txt \
|
||||
ArduinoJson/LICENSE.md \
|
||||
ArduinoJson/README.md \
|
||||
ArduinoJson/ArduinoJson.h \
|
||||
ArduinoJson/ArduinoJson.cpp \
|
||||
-x!ArduinoJson/src/CMakeLists.txt
|
||||
|
||||
popd
|
||||
|
||||
rm ArduinoJson.h
|
||||
rm ArduinoJson.cpp
|
42
scripts/create-size-graph.sh
Executable file
42
scripts/create-size-graph.sh
Executable file
@ -0,0 +1,42 @@
|
||||
#!/bin/bash
|
||||
|
||||
set -eu
|
||||
|
||||
OUTPUT="$(pwd)/sizes.csv"
|
||||
|
||||
echo "Tag;Date;Parser;Generator" > $OUTPUT
|
||||
|
||||
cd $(dirname $(dirname $0))
|
||||
|
||||
git tag | while read TAG
|
||||
do
|
||||
|
||||
git checkout -q tags/$TAG
|
||||
|
||||
DATE=$(git log -1 --date=short --pretty=format:%cd)
|
||||
PARSER_SIZE=$(arduino --verify examples/JsonParserExample/JsonParserExample.ino 2>/dev/null | grep -e 'Sketch uses' | sed 's/.*uses \([0-9]*\).\([0-9]\+\).*/\1\2/')
|
||||
|
||||
if [ -e 'examples/JsonGeneratorExample/JsonGeneratorExample.ino' ]; then
|
||||
GENERATOR_SIZE=$(arduino --verify examples/JsonGeneratorExample/JsonGeneratorExample.ino 2>/dev/null | grep -e 'Sketch uses' | sed 's/.*uses \([0-9]*\).\([0-9]\+\).*/\1\2/')
|
||||
else
|
||||
GENERATOR_SIZE=""
|
||||
fi
|
||||
|
||||
echo $TAG
|
||||
if [ ! -z "$PARSER_SIZE" ]
|
||||
then
|
||||
echo "JsonParserExample = $PARSER_SIZE bytes"
|
||||
else
|
||||
echo "JsonParserExample compilation failed."
|
||||
fi
|
||||
|
||||
if [ ! -z "$GENERATOR_SIZE" ]
|
||||
then
|
||||
echo "JsonGeneratorExample = $GENERATOR_SIZE bytes"
|
||||
else
|
||||
echo "JsonGeneratorExample compilation failed."
|
||||
fi
|
||||
|
||||
echo "$TAG;$DATE;$PARSER_SIZE;$GENERATOR_SIZE" >> $OUTPUT
|
||||
|
||||
done
|
@ -17,6 +17,13 @@
|
||||
#pragma GCC diagnostic ignored "-Wfloat-conversion"
|
||||
#endif
|
||||
|
||||
// Visual Studo 2012 didn't have isnan, nor isinf
|
||||
#if defined(_MSC_VER) && _MSC_VER <= 1700
|
||||
#include <float.h>
|
||||
#define isnan(x) _isnan(x)
|
||||
#define isinf(x) (!_finite(x))
|
||||
#endif
|
||||
|
||||
size_t Print::print(const char s[]) {
|
||||
size_t n = 0;
|
||||
while (*s) {
|
||||
|
@ -16,12 +16,6 @@ String::String(double value, unsigned char digits) {
|
||||
*this = tmp;
|
||||
}
|
||||
|
||||
String::String(int value) {
|
||||
char tmp[32];
|
||||
sprintf(tmp, "%d", value);
|
||||
*this = tmp;
|
||||
}
|
||||
|
||||
String::String(long value) {
|
||||
char tmp[32];
|
||||
sprintf(tmp, "%ld", value);
|
||||
|
@ -5,6 +5,7 @@ file(GLOB_RECURSE CPP_FILES *.cpp)
|
||||
if(CMAKE_CXX_COMPILER_ID MATCHES "(GNU|Clang)")
|
||||
add_definitions(
|
||||
-fno-exceptions
|
||||
-fno-rtti
|
||||
-pedantic
|
||||
-Wall
|
||||
-Wcast-align
|
||||
@ -43,6 +44,7 @@ endif()
|
||||
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
|
||||
add_definitions(
|
||||
-Wc++11-compat
|
||||
-Wdeprecated-register
|
||||
)
|
||||
endif()
|
||||
|
||||
|
@ -16,7 +16,7 @@ using namespace ArduinoJson;
|
||||
using namespace ArduinoJson::Internals;
|
||||
|
||||
bool JsonParser::skip(char charToSkip) {
|
||||
register const char *ptr = skipSpacesAndComments(_readPtr);
|
||||
const char *ptr = skipSpacesAndComments(_readPtr);
|
||||
if (*ptr != charToSkip) return false;
|
||||
ptr++;
|
||||
_readPtr = skipSpacesAndComments(ptr);
|
||||
|
@ -99,19 +99,25 @@ bool JsonVariant::is<double>() const {
|
||||
}
|
||||
|
||||
void JsonVariant::writeTo(JsonWriter &writer) const {
|
||||
if (_type == JSON_ARRAY) _content.asArray->writeTo(writer);
|
||||
if (_type == JSON_ARRAY)
|
||||
_content.asArray->writeTo(writer);
|
||||
|
||||
if (_type == JSON_OBJECT) _content.asObject->writeTo(writer);
|
||||
else if (_type == JSON_OBJECT)
|
||||
_content.asObject->writeTo(writer);
|
||||
|
||||
if (_type == JSON_STRING) writer.writeString(_content.asString);
|
||||
else if (_type == JSON_STRING)
|
||||
writer.writeString(_content.asString);
|
||||
|
||||
if (_type == JSON_UNPARSED) writer.writeRaw(_content.asString);
|
||||
else if (_type == JSON_UNPARSED)
|
||||
writer.writeRaw(_content.asString);
|
||||
|
||||
if (_type == JSON_LONG) writer.writeLong(_content.asLong);
|
||||
else if (_type == JSON_LONG)
|
||||
writer.writeLong(_content.asLong);
|
||||
|
||||
if (_type == JSON_BOOLEAN) writer.writeBoolean(_content.asLong);
|
||||
else if (_type == JSON_BOOLEAN)
|
||||
writer.writeBoolean(_content.asLong != 0);
|
||||
|
||||
if (_type >= JSON_DOUBLE_0_DECIMALS) {
|
||||
else if (_type >= JSON_DOUBLE_0_DECIMALS) {
|
||||
uint8_t decimals = static_cast<uint8_t>(_type - JSON_DOUBLE_0_DECIMALS);
|
||||
writer.writeDouble(_content.asDouble, decimals);
|
||||
}
|
||||
|
@ -71,6 +71,26 @@ TEST_F(ArduinoStringTests, JsonObject_SetKeyValue) {
|
||||
ASSERT_STREQ("world", object["hello"]);
|
||||
}
|
||||
|
||||
TEST_F(ArduinoStringTests, JsonObject_SetToArraySubscript) {
|
||||
JsonArray &arr = _jsonBuffer.createArray();
|
||||
arr.add("world");
|
||||
|
||||
JsonObject &object = _jsonBuffer.createObject();
|
||||
object.set(String("hello"), arr[0]);
|
||||
|
||||
ASSERT_STREQ("world", object["hello"]);
|
||||
}
|
||||
|
||||
TEST_F(ArduinoStringTests, JsonObject_SetToObjectSubscript) {
|
||||
JsonObject &arr = _jsonBuffer.createObject();
|
||||
arr.set("x", "world");
|
||||
|
||||
JsonObject &object = _jsonBuffer.createObject();
|
||||
object.set(String("hello"), arr["x"]);
|
||||
|
||||
ASSERT_STREQ("world", object["hello"]);
|
||||
}
|
||||
|
||||
TEST_F(ArduinoStringTests, JsonObject_Get) {
|
||||
char json[] = "{\"key\":\"value\"}";
|
||||
const JsonObject &object = _jsonBuffer.parseObject(json);
|
||||
|
@ -10,6 +10,11 @@ include_directories(
|
||||
|
||||
add_definitions(-DGTEST_HAS_PTHREAD=0)
|
||||
|
||||
# Workaround for Visual Studio 2012
|
||||
if (MSVC AND MSVC_VERSION EQUAL 1700)
|
||||
add_definitions(-D_VARIADIC_MAX=10)
|
||||
endif()
|
||||
|
||||
add_executable(ArduinoJsonTests
|
||||
${TESTS_FILES}
|
||||
${INC_FILES}
|
||||
|
@ -22,9 +22,9 @@ TEST_F(DynamicJsonBuffer_Basic_Tests, InitialSizeIsZero) {
|
||||
|
||||
TEST_F(DynamicJsonBuffer_Basic_Tests, SizeIncreasesAfterAlloc) {
|
||||
buffer.alloc(1);
|
||||
ASSERT_EQ(1, buffer.size());
|
||||
ASSERT_LE(1U, buffer.size());
|
||||
buffer.alloc(1);
|
||||
ASSERT_EQ(2, buffer.size());
|
||||
ASSERT_LE(2U, buffer.size());
|
||||
}
|
||||
|
||||
TEST_F(DynamicJsonBuffer_Basic_Tests, ReturnDifferentPointer) {
|
||||
@ -32,3 +32,12 @@ TEST_F(DynamicJsonBuffer_Basic_Tests, ReturnDifferentPointer) {
|
||||
void* p2 = buffer.alloc(2);
|
||||
ASSERT_NE(p1, p2);
|
||||
}
|
||||
|
||||
TEST_F(DynamicJsonBuffer_Basic_Tests, Alignment) {
|
||||
size_t mask = sizeof(void*) - 1;
|
||||
|
||||
for (size_t size = 1; size <= sizeof(void*); size++) {
|
||||
size_t addr = reinterpret_cast<size_t>(buffer.alloc(1));
|
||||
ASSERT_EQ(0, addr & mask);
|
||||
}
|
||||
}
|
||||
|
@ -7,17 +7,22 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
class DynamicJsonBuffer_NoMemory_Tests : public ::testing::Test {
|
||||
class NoMemoryAllocator {
|
||||
public:
|
||||
void* allocate(size_t) { return NULL; }
|
||||
void deallocate(void*) {}
|
||||
};
|
||||
class NoMemoryAllocator {
|
||||
public:
|
||||
void* allocate(size_t) { return NULL; }
|
||||
void deallocate(void*) {}
|
||||
};
|
||||
|
||||
class DynamicJsonBuffer_NoMemory_Tests : public ::testing::Test {
|
||||
protected:
|
||||
Internals::BlockJsonBuffer<NoMemoryAllocator> _jsonBuffer;
|
||||
};
|
||||
|
||||
TEST_F(DynamicJsonBuffer_NoMemory_Tests, FixCodeCoverage) {
|
||||
// call this function to fix code coverage
|
||||
NoMemoryAllocator().deallocate(NULL);
|
||||
}
|
||||
|
||||
TEST_F(DynamicJsonBuffer_NoMemory_Tests, CreateArray) {
|
||||
ASSERT_FALSE(_jsonBuffer.createArray().success());
|
||||
}
|
||||
|
90
test/JsonArray_Add_Tests.cpp
Normal file
90
test/JsonArray_Add_Tests.cpp
Normal file
@ -0,0 +1,90 @@
|
||||
// Copyright Benoit Blanchon 2014-2015
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#define ARDUINOJSON_ENABLE_STD_STREAM
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
class JsonArray_Add_Tests : public ::testing::Test {
|
||||
protected:
|
||||
JsonArray_Add_Tests() : _array(_jsonBuffer.createArray()) {}
|
||||
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonArray& _array;
|
||||
};
|
||||
|
||||
#define TEST_(name) TEST_F(JsonArray_Add_Tests, name)
|
||||
|
||||
TEST_(SizeIncreased_WhenValuesAreAdded) {
|
||||
_array.add("hello");
|
||||
EXPECT_EQ(1U, _array.size());
|
||||
}
|
||||
|
||||
TEST_(StoreInteger) {
|
||||
_array.add(123);
|
||||
EXPECT_EQ(123, _array[0].as<int>());
|
||||
EXPECT_TRUE(_array[0].is<int>());
|
||||
EXPECT_FALSE(_array[0].is<double>());
|
||||
}
|
||||
|
||||
TEST_(StoreDouble) {
|
||||
_array.add(123.45);
|
||||
EXPECT_EQ(123.45, _array[0].as<double>());
|
||||
EXPECT_TRUE(_array[0].is<double>());
|
||||
EXPECT_FALSE(_array[0].is<int>());
|
||||
}
|
||||
|
||||
TEST_(StoreBoolean) {
|
||||
_array.add(true);
|
||||
EXPECT_EQ(true, _array[0].as<bool>());
|
||||
EXPECT_TRUE(_array[0].is<bool>());
|
||||
EXPECT_FALSE(_array[0].is<int>());
|
||||
}
|
||||
|
||||
TEST_(StoreString) {
|
||||
_array.add("hello");
|
||||
EXPECT_STREQ("hello", _array[0].as<const char*>());
|
||||
EXPECT_TRUE(_array[0].is<const char*>());
|
||||
EXPECT_FALSE(_array[0].is<int>());
|
||||
}
|
||||
|
||||
TEST_(StoreNestedArray) {
|
||||
JsonArray& arr = _jsonBuffer.createArray();
|
||||
|
||||
_array.add(arr);
|
||||
|
||||
EXPECT_EQ(&arr, &_array[0].as<JsonArray&>());
|
||||
EXPECT_TRUE(_array[0].is<JsonArray&>());
|
||||
EXPECT_FALSE(_array[0].is<int>());
|
||||
}
|
||||
|
||||
TEST_(StoreNestedObject) {
|
||||
JsonObject& obj = _jsonBuffer.createObject();
|
||||
|
||||
_array.add(obj);
|
||||
|
||||
EXPECT_EQ(&obj, &_array[0].as<JsonObject&>());
|
||||
EXPECT_TRUE(_array[0].is<JsonObject&>());
|
||||
EXPECT_FALSE(_array[0].is<int>());
|
||||
}
|
||||
|
||||
TEST_(StoreArraySubscript) {
|
||||
JsonArray& arr = _jsonBuffer.createArray();
|
||||
arr.add("hello");
|
||||
|
||||
_array.add(arr[0]);
|
||||
|
||||
EXPECT_STREQ("hello", _array[0]);
|
||||
}
|
||||
|
||||
TEST_(StoreObjectSubscript) {
|
||||
JsonObject& obj = _jsonBuffer.createObject();
|
||||
obj["x"] = "hello";
|
||||
|
||||
_array.add(obj["x"]);
|
||||
|
||||
EXPECT_STREQ("hello", _array[0]);
|
||||
}
|
41
test/JsonArray_Basic_Tests.cpp
Normal file
41
test/JsonArray_Basic_Tests.cpp
Normal file
@ -0,0 +1,41 @@
|
||||
// Copyright Benoit Blanchon 2014-2015
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#define ARDUINOJSON_ENABLE_STD_STREAM
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
#define TEST_(name) TEST(JsonArray_Basic_Tests, name)
|
||||
|
||||
TEST_(SuccessIsTrue) {
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonArray& array = _jsonBuffer.createArray();
|
||||
|
||||
EXPECT_TRUE(array.success());
|
||||
}
|
||||
|
||||
TEST_(InitialSizeIsZero) {
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonArray& array = _jsonBuffer.createArray();
|
||||
|
||||
EXPECT_EQ(0U, array.size());
|
||||
}
|
||||
|
||||
TEST_(CreateNestedArray) {
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonArray& array = _jsonBuffer.createArray();
|
||||
|
||||
JsonArray& arr = array.createNestedArray();
|
||||
EXPECT_EQ(&arr, &array[0].as<JsonArray&>());
|
||||
}
|
||||
|
||||
TEST_(CreateNestedObject) {
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonArray& array = _jsonBuffer.createArray();
|
||||
|
||||
JsonObject& obj = array.createNestedObject();
|
||||
EXPECT_EQ(&obj, &array[0].as<JsonObject&>());
|
||||
}
|
@ -1,180 +0,0 @@
|
||||
// Copyright Benoit Blanchon 2014-2015
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
class JsonArray_Container_Tests : public ::testing::Test {
|
||||
protected:
|
||||
JsonArray_Container_Tests() : _array(_jsonBuffer.createArray()) {}
|
||||
|
||||
template <typename T>
|
||||
void firstMustEqual(T expected) {
|
||||
itemMustEqual(0, expected);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void secondMustEqual(T expected) {
|
||||
itemMustEqual(1, expected);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void firstMustReference(const T& expected) {
|
||||
itemMustReference(0, expected);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void secondMustReference(const T& expected) {
|
||||
itemMustReference(1, expected);
|
||||
}
|
||||
|
||||
void sizeMustBe(int expected) { EXPECT_EQ(expected, _array.size()); }
|
||||
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonArray& _array;
|
||||
|
||||
private:
|
||||
template <typename T>
|
||||
void itemMustEqual(int index, T expected) {
|
||||
EXPECT_EQ(expected, _array[index].as<T>());
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void itemMustReference(int index, const T& expected) {
|
||||
EXPECT_EQ(&expected, &_array[index].as<T&>());
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
void JsonArray_Container_Tests::itemMustEqual(int index, const char* expected) {
|
||||
EXPECT_STREQ(expected, _array[index].asString());
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Container_Tests, SuccessIsTrue) {
|
||||
EXPECT_TRUE(_array.success());
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Container_Tests, InitialSizeIsZero) { sizeMustBe(0); }
|
||||
|
||||
TEST_F(JsonArray_Container_Tests, Grow_WhenValuesAreAdded) {
|
||||
_array.add("hello");
|
||||
sizeMustBe(1);
|
||||
|
||||
_array.add("world");
|
||||
sizeMustBe(2);
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Container_Tests, DontGrow_WhenValuesAreReplaced) {
|
||||
_array.add("hello");
|
||||
_array[0] = "world";
|
||||
sizeMustBe(1);
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Container_Tests, CanStoreIntegers) {
|
||||
_array.add(123);
|
||||
_array.add(456);
|
||||
|
||||
firstMustEqual(123);
|
||||
secondMustEqual(456);
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Container_Tests, CanStoreDoubles) {
|
||||
_array.add(123.45);
|
||||
_array.add(456.78);
|
||||
|
||||
firstMustEqual(123.45);
|
||||
secondMustEqual(456.78);
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Container_Tests, CanStoreBooleans) {
|
||||
_array.add(true);
|
||||
_array.add(false);
|
||||
|
||||
firstMustEqual(true);
|
||||
secondMustEqual(false);
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Container_Tests, CanStoreStrings) {
|
||||
_array.add("hello");
|
||||
_array.add("world");
|
||||
|
||||
firstMustEqual("hello");
|
||||
secondMustEqual("world");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Container_Tests, CanStoreNestedArrays) {
|
||||
JsonArray& inner_array1 = _jsonBuffer.createArray();
|
||||
JsonArray& inner_array2 = _jsonBuffer.createArray();
|
||||
|
||||
_array.add(inner_array1);
|
||||
_array.add(inner_array2);
|
||||
|
||||
firstMustReference(inner_array1);
|
||||
secondMustReference(inner_array2);
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Container_Tests, CanStoreNestedObjects) {
|
||||
JsonObject& innerObject1 = _jsonBuffer.createObject();
|
||||
JsonObject& innerObject2 = _jsonBuffer.createObject();
|
||||
|
||||
_array.add(innerObject1);
|
||||
_array.add(innerObject2);
|
||||
|
||||
firstMustReference(innerObject1);
|
||||
secondMustReference(innerObject2);
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Container_Tests, CanCreateNestedArrays) {
|
||||
JsonArray& inner_array1 = _array.createNestedArray();
|
||||
JsonArray& inner_array2 = _array.createNestedArray();
|
||||
|
||||
firstMustReference(inner_array1);
|
||||
secondMustReference(inner_array2);
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Container_Tests, CanCreateNestedObjects) {
|
||||
JsonObject& innerObject1 = _array.createNestedObject();
|
||||
JsonObject& innerObject2 = _array.createNestedObject();
|
||||
|
||||
firstMustReference(innerObject1);
|
||||
secondMustReference(innerObject2);
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Container_Tests, RemoveFirstElement) {
|
||||
_array.add("one");
|
||||
_array.add("two");
|
||||
_array.add("three");
|
||||
|
||||
_array.removeAt(0);
|
||||
|
||||
sizeMustBe(2);
|
||||
firstMustEqual("two");
|
||||
secondMustEqual("three");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Container_Tests, RemoveMiddleElement) {
|
||||
_array.add("one");
|
||||
_array.add("two");
|
||||
_array.add("three");
|
||||
|
||||
_array.removeAt(1);
|
||||
|
||||
sizeMustBe(2);
|
||||
firstMustEqual("one");
|
||||
secondMustEqual("three");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_Container_Tests, RemoveLastElement) {
|
||||
_array.add("one");
|
||||
_array.add("two");
|
||||
_array.add("three");
|
||||
|
||||
_array.removeAt(2);
|
||||
|
||||
sizeMustBe(2);
|
||||
firstMustEqual("one");
|
||||
secondMustEqual("two");
|
||||
}
|
46
test/JsonArray_Remove_Tests.cpp
Normal file
46
test/JsonArray_Remove_Tests.cpp
Normal file
@ -0,0 +1,46 @@
|
||||
// Copyright Benoit Blanchon 2014-2015
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
class JsonArray_Remove_Tests : public ::testing::Test {
|
||||
protected:
|
||||
JsonArray_Remove_Tests() : _array(_jsonBuffer.createArray()) {
|
||||
_array.add("one");
|
||||
_array.add("two");
|
||||
_array.add("three");
|
||||
}
|
||||
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonArray& _array;
|
||||
};
|
||||
|
||||
#define TEST_(name) TEST_F(JsonArray_Remove_Tests, name)
|
||||
|
||||
TEST_(RemoveFirstElement) {
|
||||
_array.removeAt(0);
|
||||
|
||||
EXPECT_EQ(2, _array.size());
|
||||
EXPECT_STREQ("two", _array[0]);
|
||||
EXPECT_STREQ("three", _array[1]);
|
||||
}
|
||||
|
||||
TEST_(RemoveMiddleElement) {
|
||||
_array.removeAt(1);
|
||||
|
||||
EXPECT_EQ(2, _array.size());
|
||||
EXPECT_STREQ("one", _array[0]);
|
||||
EXPECT_STREQ("three", _array[1]);
|
||||
}
|
||||
|
||||
TEST_(RemoveLastElement) {
|
||||
_array.removeAt(2);
|
||||
|
||||
EXPECT_EQ(2, _array.size());
|
||||
EXPECT_STREQ("one", _array[0]);
|
||||
EXPECT_STREQ("two", _array[1]);
|
||||
}
|
90
test/JsonArray_Set_Tests.cpp
Normal file
90
test/JsonArray_Set_Tests.cpp
Normal file
@ -0,0 +1,90 @@
|
||||
// Copyright Benoit Blanchon 2014-2015
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#define ARDUINOJSON_ENABLE_STD_STREAM
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
class JsonArray_Set_Tests : public ::testing::Test {
|
||||
protected:
|
||||
JsonArray_Set_Tests() : _array(_jsonBuffer.createArray()) { _array.add(0); }
|
||||
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonArray& _array;
|
||||
};
|
||||
|
||||
#define TEST_(name) TEST_F(JsonArray_Set_Tests, name)
|
||||
|
||||
TEST_(SizeIsUnchanged) {
|
||||
_array.set(0, "hello");
|
||||
EXPECT_EQ(1U, _array.size());
|
||||
}
|
||||
|
||||
TEST_(StoreInteger) {
|
||||
_array.set(0, 123);
|
||||
EXPECT_EQ(123, _array[0].as<int>());
|
||||
EXPECT_TRUE(_array[0].is<int>());
|
||||
EXPECT_FALSE(_array[0].is<double>());
|
||||
}
|
||||
|
||||
TEST_(StoreDouble) {
|
||||
_array.set(0, 123.45);
|
||||
EXPECT_EQ(123.45, _array[0].as<double>());
|
||||
EXPECT_TRUE(_array[0].is<double>());
|
||||
EXPECT_FALSE(_array[0].is<int>());
|
||||
}
|
||||
|
||||
TEST_(StoreBoolean) {
|
||||
_array.set(0, true);
|
||||
EXPECT_EQ(true, _array[0].as<bool>());
|
||||
EXPECT_TRUE(_array[0].is<bool>());
|
||||
EXPECT_FALSE(_array[0].is<int>());
|
||||
}
|
||||
|
||||
TEST_(StoreString) {
|
||||
_array.set(0, "hello");
|
||||
EXPECT_STREQ("hello", _array[0].as<const char*>());
|
||||
EXPECT_TRUE(_array[0].is<const char*>());
|
||||
EXPECT_FALSE(_array[0].is<int>());
|
||||
}
|
||||
|
||||
TEST_(StoreNestedArray) {
|
||||
JsonArray& arr = _jsonBuffer.createArray();
|
||||
|
||||
_array.set(0, arr);
|
||||
|
||||
EXPECT_EQ(&arr, &_array[0].as<JsonArray&>());
|
||||
EXPECT_TRUE(_array[0].is<JsonArray&>());
|
||||
EXPECT_FALSE(_array[0].is<int>());
|
||||
}
|
||||
|
||||
TEST_(StoreNestedObject) {
|
||||
JsonObject& obj = _jsonBuffer.createObject();
|
||||
|
||||
_array.set(0, obj);
|
||||
|
||||
EXPECT_EQ(&obj, &_array[0].as<JsonObject&>());
|
||||
EXPECT_TRUE(_array[0].is<JsonObject&>());
|
||||
EXPECT_FALSE(_array[0].is<int>());
|
||||
}
|
||||
|
||||
TEST_(StoreArraySubscript) {
|
||||
JsonArray& arr = _jsonBuffer.createArray();
|
||||
arr.add("hello");
|
||||
|
||||
_array.set(0, arr[0]);
|
||||
|
||||
EXPECT_STREQ("hello", _array[0]);
|
||||
}
|
||||
|
||||
TEST_(StoreObjectSubscript) {
|
||||
JsonObject& obj = _jsonBuffer.createObject();
|
||||
obj["x"] = "hello";
|
||||
|
||||
_array.set(0, obj["x"]);
|
||||
|
||||
EXPECT_STREQ("hello", _array[0]);
|
||||
}
|
92
test/JsonArray_Subscript_Tests.cpp
Normal file
92
test/JsonArray_Subscript_Tests.cpp
Normal file
@ -0,0 +1,92 @@
|
||||
// Copyright Benoit Blanchon 2014-2015
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#define ARDUINOJSON_ENABLE_STD_STREAM
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
class JsonArray_Subscript_Tests : public ::testing::Test {
|
||||
protected:
|
||||
JsonArray_Subscript_Tests() : _array(_jsonBuffer.createArray()) {
|
||||
_array.add(0);
|
||||
}
|
||||
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonArray& _array;
|
||||
};
|
||||
|
||||
#define TEST_(name) TEST_F(JsonArray_Subscript_Tests, name)
|
||||
|
||||
TEST_(SizeIsUnchanged) {
|
||||
_array[0] = "hello";
|
||||
EXPECT_EQ(1U, _array.size());
|
||||
}
|
||||
|
||||
TEST_(StoreInteger) {
|
||||
_array[0] = 123;
|
||||
EXPECT_EQ(123, _array[0].as<int>());
|
||||
EXPECT_TRUE(_array[0].is<int>());
|
||||
EXPECT_FALSE(_array[0].is<double>());
|
||||
}
|
||||
|
||||
TEST_(StoreDouble) {
|
||||
_array[0] = 123.45;
|
||||
EXPECT_EQ(123.45, _array[0].as<double>());
|
||||
EXPECT_TRUE(_array[0].is<double>());
|
||||
EXPECT_FALSE(_array[0].is<int>());
|
||||
}
|
||||
|
||||
TEST_(StoreBoolean) {
|
||||
_array[0] = true;
|
||||
EXPECT_EQ(true, _array[0].as<bool>());
|
||||
EXPECT_TRUE(_array[0].is<bool>());
|
||||
EXPECT_FALSE(_array[0].is<int>());
|
||||
}
|
||||
|
||||
TEST_(StoreString) {
|
||||
_array[0] = "hello";
|
||||
EXPECT_STREQ("hello", _array[0].as<const char*>());
|
||||
EXPECT_TRUE(_array[0].is<const char*>());
|
||||
EXPECT_FALSE(_array[0].is<int>());
|
||||
}
|
||||
|
||||
TEST_(StoreNestedArray) {
|
||||
JsonArray& arr = _jsonBuffer.createArray();
|
||||
|
||||
_array[0] = arr;
|
||||
|
||||
EXPECT_EQ(&arr, &_array[0].as<JsonArray&>());
|
||||
EXPECT_TRUE(_array[0].is<JsonArray&>());
|
||||
EXPECT_FALSE(_array[0].is<int>());
|
||||
}
|
||||
|
||||
TEST_(StoreNestedObject) {
|
||||
JsonObject& obj = _jsonBuffer.createObject();
|
||||
|
||||
_array[0] = obj;
|
||||
|
||||
EXPECT_EQ(&obj, &_array[0].as<JsonObject&>());
|
||||
EXPECT_TRUE(_array[0].is<JsonObject&>());
|
||||
EXPECT_FALSE(_array[0].is<int>());
|
||||
}
|
||||
|
||||
TEST_(StoreArraySubscript) {
|
||||
JsonArray& arr = _jsonBuffer.createArray();
|
||||
arr.add("hello");
|
||||
|
||||
_array[0] = arr[0];
|
||||
|
||||
EXPECT_STREQ("hello", _array[0]);
|
||||
}
|
||||
|
||||
TEST_(StoreObjectSubscript) {
|
||||
JsonObject& obj = _jsonBuffer.createObject();
|
||||
obj["x"] = "hello";
|
||||
|
||||
_array[0] = obj["x"];
|
||||
|
||||
EXPECT_STREQ("hello", _array[0]);
|
||||
}
|
24
test/JsonObject_Basic_Tests.cpp
Normal file
24
test/JsonObject_Basic_Tests.cpp
Normal file
@ -0,0 +1,24 @@
|
||||
// Copyright Benoit Blanchon 2014-2015
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
#define TEST_(name) TEST(JsonObject_Basic_Tests, name)
|
||||
|
||||
TEST_(InitialSizeIsZero) {
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonObject& _object = _jsonBuffer.createObject();
|
||||
|
||||
EXPECT_EQ(0, _object.size());
|
||||
}
|
||||
|
||||
TEST_(SuccessIsTrue) {
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonObject& _object = _jsonBuffer.createObject();
|
||||
|
||||
EXPECT_TRUE(_object.success());
|
||||
}
|
@ -1,121 +0,0 @@
|
||||
// Copyright Benoit Blanchon 2014-2015
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
class JsonObject_Container_Tests : public ::testing::Test {
|
||||
public:
|
||||
JsonObject_Container_Tests() : _object(_jsonBuffer.createObject()) {}
|
||||
|
||||
protected:
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonObject& _object;
|
||||
};
|
||||
|
||||
TEST_F(JsonObject_Container_Tests, InitialSizeIsZero) {
|
||||
EXPECT_EQ(0, _object.size());
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_Container_Tests, Grow_WhenValuesAreAdded) {
|
||||
_object["hello"] = 1;
|
||||
EXPECT_EQ(1, _object.size());
|
||||
|
||||
_object.set("world", 2);
|
||||
EXPECT_EQ(2, _object.size());
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_Container_Tests, DoNotGrow_WhenSameValueIsAdded) {
|
||||
_object["hello"] = 1;
|
||||
EXPECT_EQ(1, _object.size());
|
||||
|
||||
_object["hello"] = 2;
|
||||
EXPECT_EQ(1, _object.size());
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_Container_Tests, Shrink_WhenValuesAreRemoved) {
|
||||
_object["hello"] = 1;
|
||||
_object["world"] = 2;
|
||||
|
||||
_object.remove("hello");
|
||||
EXPECT_EQ(1, _object.size());
|
||||
|
||||
_object.remove("world");
|
||||
EXPECT_EQ(0, _object.size());
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_Container_Tests,
|
||||
DoNotShrink_WhenRemoveIsCalledWithAWrongKey) {
|
||||
_object["hello"] = 1;
|
||||
_object["world"] = 2;
|
||||
|
||||
_object.remove(":-P");
|
||||
|
||||
EXPECT_EQ(2, _object.size());
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_Container_Tests, CanStoreIntegers) {
|
||||
_object["hello"] = 123;
|
||||
_object.set("world", 456);
|
||||
|
||||
EXPECT_EQ(123, _object["hello"].as<int>());
|
||||
EXPECT_EQ(456, _object["world"].as<int>());
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_Container_Tests, CanStoreDoubles) {
|
||||
_object["hello"] = 123.45;
|
||||
_object.set("world", 456.78);
|
||||
|
||||
EXPECT_EQ(123.45, _object["hello"].as<double>());
|
||||
EXPECT_EQ(456.78, _object["world"].as<double>());
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_Container_Tests, CanStoreBooleans) {
|
||||
_object["hello"] = true;
|
||||
_object.set("world", false);
|
||||
|
||||
EXPECT_TRUE(_object["hello"].as<bool>());
|
||||
EXPECT_FALSE(_object["world"].as<bool>());
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_Container_Tests, CanStoreStrings) {
|
||||
_object["hello"] = "h3110";
|
||||
_object.set("world", "w0r1d");
|
||||
|
||||
EXPECT_STREQ("h3110", _object["hello"].as<const char*>());
|
||||
EXPECT_STREQ("w0r1d", _object["world"].as<const char*>());
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_Container_Tests, CanStoreArrays) {
|
||||
JsonArray& array1 = _jsonBuffer.createArray();
|
||||
JsonArray& array2 = _jsonBuffer.createArray();
|
||||
|
||||
_object["hello"] = array1;
|
||||
_object.set("world", array2);
|
||||
|
||||
EXPECT_EQ(&array1, &_object["hello"].asArray());
|
||||
EXPECT_EQ(&array2, &_object["world"].asArray());
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_Container_Tests, CanStoreObjects) {
|
||||
JsonObject& object1 = _jsonBuffer.createObject();
|
||||
JsonObject& object2 = _jsonBuffer.createObject();
|
||||
|
||||
_object["hello"] = object1;
|
||||
_object.set("world", object2);
|
||||
|
||||
EXPECT_EQ(&object1, &_object["hello"].asObject());
|
||||
EXPECT_EQ(&object2, &_object["world"].asObject());
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_Container_Tests, ContainsKeyReturnsFalseForNonExistingKey) {
|
||||
EXPECT_FALSE(_object.containsKey("hello"));
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_Container_Tests, ContainsKeyReturnsTrueForDefinedValue) {
|
||||
_object.set("hello", 42);
|
||||
EXPECT_TRUE(_object.containsKey("hello"));
|
||||
}
|
38
test/JsonObject_ContainsKey_Tests.cpp
Normal file
38
test/JsonObject_ContainsKey_Tests.cpp
Normal file
@ -0,0 +1,38 @@
|
||||
// Copyright Benoit Blanchon 2014-2015
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
#define TEST_(name) TEST(JsonObject_Basic_Tests, name)
|
||||
|
||||
TEST_(ContainsKeyReturnsFalseForNonExistingKey) {
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonObject& _object = _jsonBuffer.createObject();
|
||||
|
||||
_object.set("hello", 42);
|
||||
|
||||
EXPECT_FALSE(_object.containsKey("world"));
|
||||
}
|
||||
|
||||
TEST_(ContainsKeyReturnsTrueForDefinedValue) {
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonObject& _object = _jsonBuffer.createObject();
|
||||
|
||||
_object.set("hello", 42);
|
||||
|
||||
EXPECT_TRUE(_object.containsKey("hello"));
|
||||
}
|
||||
|
||||
TEST_(ContainsKeyReturnsFalseAfterRemove) {
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonObject& _object = _jsonBuffer.createObject();
|
||||
|
||||
_object.set("hello", 42);
|
||||
_object.remove("hello");
|
||||
|
||||
EXPECT_FALSE(_object.containsKey("hello"));
|
||||
}
|
30
test/JsonObject_Remove_Tests.cpp
Normal file
30
test/JsonObject_Remove_Tests.cpp
Normal file
@ -0,0 +1,30 @@
|
||||
// Copyright Benoit Blanchon 2014-2015
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
#define TEST_(name) TEST(JsonObject_Remove_Tests, name)
|
||||
|
||||
TEST_(SizeDecreased_WhenValuesAreRemoved) {
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonObject& _object = _jsonBuffer.createObject();
|
||||
_object["hello"] = 1;
|
||||
|
||||
_object.remove("hello");
|
||||
|
||||
EXPECT_EQ(0, _object.size());
|
||||
}
|
||||
|
||||
TEST_(SizeUntouched_WhenRemoveIsCalledWithAWrongKey) {
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonObject& _object = _jsonBuffer.createObject();
|
||||
_object["hello"] = 1;
|
||||
|
||||
_object.remove("world");
|
||||
|
||||
EXPECT_EQ(1, _object.size());
|
||||
}
|
101
test/JsonObject_Set_Tests.cpp
Normal file
101
test/JsonObject_Set_Tests.cpp
Normal file
@ -0,0 +1,101 @@
|
||||
// Copyright Benoit Blanchon 2014-2015
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#define ARDUINOJSON_ENABLE_STD_STREAM
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
class JsonObject_Set_Tests : public ::testing::Test {
|
||||
public:
|
||||
JsonObject_Set_Tests() : _object(_jsonBuffer.createObject()) {}
|
||||
|
||||
protected:
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonObject& _object;
|
||||
};
|
||||
|
||||
#define TEST_(name) TEST_F(JsonObject_Set_Tests, name)
|
||||
|
||||
TEST_(SizeIncreased_WhenValuesAreAdded) {
|
||||
_object.set("hello", 42);
|
||||
EXPECT_EQ(1, _object.size());
|
||||
}
|
||||
|
||||
TEST_(SizeUntouched_WhenSameValueIsAdded) {
|
||||
_object["hello"] = 1;
|
||||
_object["hello"] = 2;
|
||||
EXPECT_EQ(1, _object.size());
|
||||
}
|
||||
|
||||
TEST_(StoreInteger) {
|
||||
_object.set("hello", 123);
|
||||
|
||||
EXPECT_EQ(123, _object["hello"].as<int>());
|
||||
EXPECT_TRUE(_object["hello"].is<int>());
|
||||
EXPECT_FALSE(_object["hello"].is<double>());
|
||||
}
|
||||
|
||||
TEST_(StoreDouble) {
|
||||
_object.set("hello", 123.45);
|
||||
|
||||
EXPECT_EQ(123.45, _object["hello"].as<double>());
|
||||
EXPECT_TRUE(_object["hello"].is<double>());
|
||||
EXPECT_FALSE(_object["hello"].is<long>());
|
||||
}
|
||||
|
||||
TEST_(StoreBoolean) {
|
||||
_object.set("hello", true);
|
||||
|
||||
EXPECT_TRUE(_object["hello"].as<bool>());
|
||||
EXPECT_TRUE(_object["hello"].is<bool>());
|
||||
EXPECT_FALSE(_object["hello"].is<long>());
|
||||
}
|
||||
|
||||
TEST_(StoreString) {
|
||||
_object.set("hello", "h3110");
|
||||
|
||||
EXPECT_STREQ("h3110", _object["hello"].as<const char*>());
|
||||
EXPECT_TRUE(_object["hello"].is<const char*>());
|
||||
EXPECT_FALSE(_object["hello"].is<long>());
|
||||
}
|
||||
|
||||
TEST_(StoreArray) {
|
||||
JsonArray& arr = _jsonBuffer.createArray();
|
||||
|
||||
_object.set("hello", arr);
|
||||
|
||||
EXPECT_EQ(&arr, &_object["hello"].asArray());
|
||||
EXPECT_TRUE(_object["hello"].is<JsonArray&>());
|
||||
EXPECT_FALSE(_object["hello"].is<JsonObject&>());
|
||||
}
|
||||
|
||||
TEST_(StoreObject) {
|
||||
JsonObject& obj = _jsonBuffer.createObject();
|
||||
|
||||
_object.set("hello", obj);
|
||||
|
||||
EXPECT_EQ(&obj, &_object["hello"].asObject());
|
||||
EXPECT_TRUE(_object["hello"].is<JsonObject&>());
|
||||
EXPECT_FALSE(_object["hello"].is<JsonArray&>());
|
||||
}
|
||||
|
||||
TEST_(StoreArraySubscript) {
|
||||
JsonArray& arr = _jsonBuffer.createArray();
|
||||
arr.add(42);
|
||||
|
||||
_object.set("a", arr[0]);
|
||||
|
||||
EXPECT_EQ(42, _object["a"]);
|
||||
}
|
||||
|
||||
TEST_(StoreObjectSubscript) {
|
||||
JsonObject& obj = _jsonBuffer.createObject();
|
||||
obj.set("x", 42);
|
||||
|
||||
_object.set("a", obj["x"]);
|
||||
|
||||
EXPECT_EQ(42, _object["a"]);
|
||||
}
|
101
test/JsonObject_Subscript_Tests.cpp
Normal file
101
test/JsonObject_Subscript_Tests.cpp
Normal file
@ -0,0 +1,101 @@
|
||||
// Copyright Benoit Blanchon 2014-2015
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#define ARDUINOJSON_ENABLE_STD_STREAM
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
class JsonObject_Subscript_Tests : public ::testing::Test {
|
||||
public:
|
||||
JsonObject_Subscript_Tests() : _object(_jsonBuffer.createObject()) {}
|
||||
|
||||
protected:
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
JsonObject& _object;
|
||||
};
|
||||
|
||||
#define TEST_(name) TEST_F(JsonObject_Subscript_Tests, name)
|
||||
|
||||
TEST_(SizeIncreased_WhenValuesAreAdded) {
|
||||
_object["hello"] = 1;
|
||||
EXPECT_EQ(1, _object.size());
|
||||
}
|
||||
|
||||
TEST_(SizeUntouched_WhenSameValueIsAdded) {
|
||||
_object["hello"] = 1;
|
||||
_object["hello"] = 2;
|
||||
EXPECT_EQ(1, _object.size());
|
||||
}
|
||||
|
||||
TEST_(StoreInteger) {
|
||||
_object["hello"] = 123;
|
||||
|
||||
EXPECT_EQ(123, _object["hello"].as<int>());
|
||||
EXPECT_TRUE(_object["hello"].is<int>());
|
||||
EXPECT_FALSE(_object["hello"].is<double>());
|
||||
}
|
||||
|
||||
TEST_(StoreDouble) {
|
||||
_object["hello"] = 123.45;
|
||||
|
||||
EXPECT_TRUE(_object["hello"].is<double>());
|
||||
EXPECT_FALSE(_object["hello"].is<long>());
|
||||
EXPECT_EQ(123.45, _object["hello"].as<double>());
|
||||
}
|
||||
|
||||
TEST_(StoreBoolean) {
|
||||
_object["hello"] = true;
|
||||
|
||||
EXPECT_TRUE(_object["hello"].is<bool>());
|
||||
EXPECT_FALSE(_object["hello"].is<long>());
|
||||
EXPECT_TRUE(_object["hello"].as<bool>());
|
||||
}
|
||||
|
||||
TEST_(StoreString) {
|
||||
_object["hello"] = "h3110";
|
||||
|
||||
EXPECT_TRUE(_object["hello"].is<const char*>());
|
||||
EXPECT_FALSE(_object["hello"].is<long>());
|
||||
EXPECT_STREQ("h3110", _object["hello"].as<const char*>());
|
||||
}
|
||||
|
||||
TEST_(StoreArray) {
|
||||
JsonArray& arr = _jsonBuffer.createArray();
|
||||
|
||||
_object["hello"] = arr;
|
||||
|
||||
EXPECT_EQ(&arr, &_object["hello"].asArray());
|
||||
EXPECT_TRUE(_object["hello"].is<JsonArray&>());
|
||||
EXPECT_FALSE(_object["hello"].is<JsonObject&>());
|
||||
}
|
||||
|
||||
TEST_(StoreObject) {
|
||||
JsonObject& obj = _jsonBuffer.createObject();
|
||||
|
||||
_object["hello"] = obj;
|
||||
|
||||
EXPECT_EQ(&obj, &_object["hello"].asObject());
|
||||
EXPECT_TRUE(_object["hello"].is<JsonObject&>());
|
||||
EXPECT_FALSE(_object["hello"].is<JsonArray&>());
|
||||
}
|
||||
|
||||
TEST_(StoreArraySubscript) {
|
||||
JsonArray& arr = _jsonBuffer.createArray();
|
||||
arr.add(42);
|
||||
|
||||
_object["a"] = arr[0];
|
||||
|
||||
EXPECT_EQ(42, _object["a"]);
|
||||
}
|
||||
|
||||
TEST_(StoreObjectSubscript) {
|
||||
JsonObject& obj = _jsonBuffer.createObject();
|
||||
obj.set("x", 42);
|
||||
|
||||
_object["a"] = obj["x"];
|
||||
|
||||
EXPECT_EQ(42, _object["a"]);
|
||||
}
|
@ -13,21 +13,26 @@ TEST(JsonParser_Nested_Tests, ArrayNestedInObject) {
|
||||
|
||||
JsonObject &object = jsonBuffer.parseObject(jsonString);
|
||||
JsonArray &array1 = object["ab"];
|
||||
JsonArray &array2 = object["cd"];
|
||||
const JsonArray &array2 = object["cd"];
|
||||
JsonArray &array3 = object["ef"];
|
||||
|
||||
ASSERT_TRUE(object.success());
|
||||
|
||||
ASSERT_TRUE(array1.success());
|
||||
ASSERT_TRUE(array2.success());
|
||||
ASSERT_FALSE(array3.success());
|
||||
|
||||
ASSERT_EQ(2, array1.size());
|
||||
ASSERT_EQ(2, array2.size());
|
||||
ASSERT_EQ(0, array3.size());
|
||||
|
||||
EXPECT_EQ(1, array1[0].as<int>());
|
||||
EXPECT_EQ(2, array1[1].as<int>());
|
||||
|
||||
EXPECT_EQ(3, array2[0].as<int>());
|
||||
EXPECT_EQ(4, array2[1].as<int>());
|
||||
|
||||
EXPECT_EQ(0, array3[0].as<int>());
|
||||
}
|
||||
|
||||
TEST(JsonParser_Nested_Tests, ObjectNestedInArray) {
|
||||
@ -37,18 +42,22 @@ TEST(JsonParser_Nested_Tests, ObjectNestedInArray) {
|
||||
|
||||
JsonArray &array = jsonBuffer.parseArray(jsonString);
|
||||
JsonObject &object1 = array[0];
|
||||
JsonObject &object2 = array[1];
|
||||
const JsonObject &object2 = array[1];
|
||||
JsonObject &object3 = array[2];
|
||||
|
||||
ASSERT_TRUE(array.success());
|
||||
|
||||
ASSERT_TRUE(object1.success());
|
||||
ASSERT_TRUE(object2.success());
|
||||
ASSERT_FALSE(object3.success());
|
||||
|
||||
ASSERT_EQ(2, object1.size());
|
||||
ASSERT_EQ(2, object2.size());
|
||||
ASSERT_EQ(0, object3.size());
|
||||
|
||||
EXPECT_EQ(1, object1["a"].as<int>());
|
||||
EXPECT_EQ(2, object1["b"].as<int>());
|
||||
EXPECT_EQ(3, object2["c"].as<int>());
|
||||
EXPECT_EQ(4, object2["d"].as<int>());
|
||||
EXPECT_EQ(0, object3["e"].as<int>());
|
||||
}
|
||||
|
@ -145,6 +145,11 @@ TEST(JsonVariant_As_Tests, RandomStringAsLong) {
|
||||
ASSERT_EQ(0L, variant.as<long>());
|
||||
}
|
||||
|
||||
TEST(JsonVariant_As_Tests, RandomStringAsString) {
|
||||
JsonVariant variant = "hello";
|
||||
ASSERT_EQ(String("hello"), variant.as<String>());
|
||||
}
|
||||
|
||||
TEST(JsonVariant_As_Tests, TrueStringAsBool) {
|
||||
JsonVariant variant = "true";
|
||||
ASSERT_TRUE(variant.as<bool>());
|
||||
|
@ -13,11 +13,11 @@ using namespace ArduinoJson;
|
||||
|
||||
class StaticJsonBuffer_Basic_Tests : public testing::Test {
|
||||
protected:
|
||||
StaticJsonBuffer<42> buffer;
|
||||
StaticJsonBuffer<64> buffer;
|
||||
};
|
||||
|
||||
TEST_F(StaticJsonBuffer_Basic_Tests, CapacityMatchTemplateParameter) {
|
||||
ASSERT_EQ(42, buffer.capacity());
|
||||
ASSERT_EQ(64, buffer.capacity());
|
||||
}
|
||||
|
||||
TEST_F(StaticJsonBuffer_Basic_Tests, InitialSizeIsZero) {
|
||||
@ -26,34 +26,43 @@ TEST_F(StaticJsonBuffer_Basic_Tests, InitialSizeIsZero) {
|
||||
|
||||
TEST_F(StaticJsonBuffer_Basic_Tests, GrowsAfterAlloc) {
|
||||
buffer.alloc(1);
|
||||
ASSERT_EQ(1, buffer.size());
|
||||
ASSERT_LE(1U, buffer.size());
|
||||
buffer.alloc(1);
|
||||
ASSERT_EQ(2, buffer.size());
|
||||
ASSERT_LE(2U, buffer.size());
|
||||
}
|
||||
|
||||
TEST_F(StaticJsonBuffer_Basic_Tests, DoesntGrowWhenFull) {
|
||||
buffer.alloc(42);
|
||||
buffer.alloc(64);
|
||||
buffer.alloc(1);
|
||||
ASSERT_EQ(42, buffer.size());
|
||||
ASSERT_EQ(64, buffer.size());
|
||||
}
|
||||
|
||||
TEST_F(StaticJsonBuffer_Basic_Tests, DoesntGrowWhenTooSmall) {
|
||||
buffer.alloc(43);
|
||||
buffer.alloc(65);
|
||||
ASSERT_EQ(0, buffer.size());
|
||||
}
|
||||
|
||||
TEST_F(StaticJsonBuffer_Basic_Tests, ReturnsNonNull) {
|
||||
void *p = buffer.alloc(42);
|
||||
void *p = buffer.alloc(64);
|
||||
ASSERT_NE(static_cast<void *>(0), p);
|
||||
}
|
||||
|
||||
TEST_F(StaticJsonBuffer_Basic_Tests, ReturnsNullWhenFull) {
|
||||
buffer.alloc(42);
|
||||
buffer.alloc(64);
|
||||
void *p = buffer.alloc(1);
|
||||
ASSERT_EQ(NULL, p);
|
||||
}
|
||||
|
||||
TEST_F(StaticJsonBuffer_Basic_Tests, ReturnsNullWhenTooSmall) {
|
||||
void *p = buffer.alloc(43);
|
||||
void *p = buffer.alloc(65);
|
||||
ASSERT_EQ(NULL, p);
|
||||
}
|
||||
|
||||
TEST_F(StaticJsonBuffer_Basic_Tests, Alignment) {
|
||||
size_t mask = sizeof(void *) - 1;
|
||||
|
||||
for (size_t size = 1; size <= sizeof(void *); size++) {
|
||||
size_t addr = reinterpret_cast<size_t>(buffer.alloc(1));
|
||||
ASSERT_EQ(0, addr & mask);
|
||||
}
|
||||
}
|
||||
|
@ -69,4 +69,12 @@ TEST_F(StaticJsonBuffer_ParseArray_Tests,
|
||||
with(bufferOfRightSize);
|
||||
whenInputIs("[{}]");
|
||||
parseMustSucceed();
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(StaticJsonBuffer_ParseArray_Tests, CharPtrNull) {
|
||||
ASSERT_FALSE(StaticJsonBuffer<100>().parseArray((char*)0).success());
|
||||
}
|
||||
|
||||
TEST_F(StaticJsonBuffer_ParseArray_Tests, ConstCharPtrNull) {
|
||||
ASSERT_FALSE(StaticJsonBuffer<100>().parseArray((const char*)0).success());
|
||||
}
|
||||
|
@ -70,4 +70,12 @@ TEST_F(StaticJsonBuffer_ParseObject_Tests,
|
||||
with(bufferOfRightSize);
|
||||
whenInputIs("{\"a\":[]}");
|
||||
parseMustSucceed();
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(StaticJsonBuffer_ParseObject_Tests, CharPtrNull) {
|
||||
ASSERT_FALSE(StaticJsonBuffer<100>().parseObject((char*)0).success());
|
||||
}
|
||||
|
||||
TEST_F(StaticJsonBuffer_ParseObject_Tests, ConstCharPtrNull) {
|
||||
ASSERT_FALSE(StaticJsonBuffer<100>().parseObject((const char*)0).success());
|
||||
}
|
||||
|
Reference in New Issue
Block a user