Compare commits

...

41 Commits

Author SHA1 Message Date
77b7124cf1 Set version to 5.0.8 2016-01-31 21:57:08 +01:00
6a608d4b49 Fixed JsonVariant::is<bool>() that was incorrectly returning false (issue #214) 2016-01-31 21:56:04 +01:00
01924618bd Merge pull request #215 from ivankravets/patch-1
Use tagged source code from repo
2016-01-31 21:00:46 +01:00
666e2fd0ce Use tagged source code from repo
http://docs.platformio.org/en/latest/librarymanager/config.html#version
2016-01-31 21:24:37 +02:00
0cfc25d751 Added Johann Stieger to the list of donators 2016-01-07 22:40:12 +01:00
b6d8e6c989 Updated copyright year from 2015 to 2016 2016-01-07 22:39:57 +01:00
ab2502f7b8 Fix misspelled PlatformIO 2015-12-23 14:42:22 +01:00
c57e6f3bd8 Made the library compatible with platform.io (issue #181) 2015-12-22 23:43:44 +01:00
b54f1ffc1d Travis: build with Arduino 1.5.8 and 1.6.7 2015-12-22 22:01:30 +01:00
bd0ea42277 Added a badge with the number of stars 2015-11-25 21:22:49 +01:00
7ae43bc4f8 Set version to 5.0.7 2015-11-25 21:15:11 +01:00
056682327b Changed String to be a typedef of std::string (issues #142 and #161) 2015-11-25 21:09:18 +01:00
8b66a25f66 Removed a useless line of code 2015-11-22 10:18:21 +01:00
96245dd3b4 Made library easier to use from a CMake project: simply add_subdirectory(ArduinoJson/src) 2015-11-19 21:53:27 +01:00
9cc49da68a Moved to the new Travis infrastructure 2015-11-19 21:31:23 +01:00
224918b463 Set version to 5.0.6 2015-11-09 23:10:14 +01:00
cbeefa2503 Examples: Added a loop to wait for serial port to be ready (issue #156) 2015-11-09 22:56:49 +01:00
6d68806633 Added donators list 2015-11-09 22:55:29 +01:00
ba3617c22f Added parameter to DynamicJsonBuffer constructor to set initial size (issue #152) 2015-11-09 21:37:40 +01:00
b7d9bb2765 Merge pull request #150 from marvinroger/patch-1
Add library category
2015-11-06 14:50:18 +01:00
74b42e2251 Add library category 2015-11-06 14:30:20 +01:00
c0cf9c3fcc Use float instead of double to reduce the size of JsonVariant (issue #134) 2015-10-30 23:03:16 +01:00
9f3ce18f06 Add overload JsonObjectSuscript::set(value, decimals) (issue #143) 2015-10-30 22:29:47 +01:00
b9e3255c9e Reached 100.00% code coverage :-) 2015-09-29 22:20:36 +02:00
e657396f65 Added -fno-rtti 2015-09-29 21:51:21 +02:00
929f608f2f Added list of supported platforms 2015-09-29 21:49:19 +02:00
c6a4bfa886 Fixed ambiguous overload with JsonArraySubscript and JsonObjectSubscript (issue #122) 2015-09-28 22:14:50 +02:00
d5e25b12b8 Added a comment to prevent issue #112 2015-09-27 14:18:14 +02:00
7cf6fe6d62 Fixed return type of JsonArray::is<T>() and some others (issue #121) 2015-09-19 16:25:18 +02:00
155dd653e7 Fixed printTo(String) which wrote numbers instead of strings (issue #120) 2015-09-19 16:23:09 +02:00
b5c8cd1766 Set version to 5.0.3 2015-09-19 16:20:06 +02:00
4967e389c5 Added a script to create the package with the old layout (issues #97, #114, #116) 2015-09-13 21:04:30 +02:00
ffbaebd198 Added testimonials from Reddit users erm_what_ and makerhacks 2015-09-06 22:11:53 +02:00
04b8781c8d Fixed GCC warning "declaration shadows a member" (issue #103) 2015-09-01 22:26:51 +02:00
5a4d993f7d Fixed memory alignment, which made ESP8266 crash (issue #104) 2015-09-01 22:21:50 +02:00
823a172681 Fixed segmentation fault in StaticJsonBuffer (issue #104) 2015-08-29 22:51:17 +02:00
a1943e21ed Fixed compilation on Visual Studio 2010 and 2012 (issue #107) 2015-08-27 21:47:49 +02:00
01c287bc89 Removed -Wdeprecated-register for GCC 2015-08-26 21:00:28 +02:00
0cf8249b14 Fixed Clang warning "register specifier is deprecated" (issue #102) 2015-08-26 20:49:24 +02:00
a8265a799d Merge pull request #100 from Eriatolc/master
Fix typo in Readme & clean file
2015-08-24 18:18:05 +02:00
18bb653f10 fix typo 2015-08-24 18:10:10 +02:00
128 changed files with 1610 additions and 653 deletions

2
.gitignore vendored
View File

@ -4,3 +4,5 @@
/bin /bin
/lib /lib
/sftp-config.json /sftp-config.json
.tags
.tags_sorted_by_file

View File

@ -1,20 +1,15 @@
language: c++ sudo: false
compiler: language: cpp
- gcc cache:
- clang directories:
before_install: - "~/.platformio"
- "/sbin/start-stop-daemon --start --quiet --pidfile /tmp/custom_xvfb_1.pid --make-pidfile --background --exec /usr/bin/Xvfb -- :1 -ac -screen 0 1280x1024x16" env:
- sleep 3 - COMPILER=gcc
- export DISPLAY=:1.0 - COMPILER=clang
- wget http://downloads.arduino.cc/arduino-1.6.5-linux64.tar.xz - COMPILER=arduino VERSION=1.5.8 BOARD=arduino:avr:uno
- tar xf arduino-1.6.5-linux64.tar.xz - COMPILER=arduino VERSION=1.6.7 BOARD=arduino:avr:uno
- sudo mv arduino-1.6.5 /usr/local/share/arduino - COMPILER=platformio BOARD=uno
- sudo ln -s /usr/local/share/arduino/arduino /usr/local/bin/arduino - COMPILER=platformio BOARD=due
- sudo ln -s $PWD /usr/local/share/arduino/libraries/ArduinoJson - COMPILER=platformio BOARD=esp01
- sudo pip install cpp-coveralls - COMPILER=platformio BOARD=teensy31
script: script: scripts/travis/$COMPILER.sh
- cmake -DCOVERAGE=true . && make && make test
- arduino --verify --board arduino:avr:uno $PWD/examples/JsonParserExample/JsonParserExample.ino
- arduino --verify --board arduino:avr:uno $PWD/examples/JsonGeneratorExample/JsonGeneratorExample.ino
after_success:
- if [ "$CC" = "gcc" ]; then coveralls --exclude third-party --gcov-options '\-lp'; fi

14
ArduinoJson.h Normal file
View File

@ -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 for [PlatformIO](http://platformio.org/).
// It must be present in the root for the tool to find it.
// Feel free to ignore this file if your working in another environment.
#include "include/ArduinoJson.h"

View File

@ -1,6 +1,56 @@
ArduinoJson: change log ArduinoJson: change log
======================= =======================
v5.0.8
------
* Made the library compatible with [PlatformIO](http://platformio.org/) (issue #181)
* Fixed `JsonVariant::is<bool>()` 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<String>()` now returns `"true"` instead of `"1"`
- `JsonVariant(false).as<String>()` 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<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 v5.0.1
------ ------

View File

@ -1,4 +1,11 @@
cmake_minimum_required(VERSION 2.8.4) # Copyright Benoit Blanchon 2014-2016
# MIT License
#
# Arduino JSON library
# https://github.com/bblanchon/ArduinoJson
# If you like this project, please add a star!
cmake_minimum_required(VERSION 2.8.12)
project(ArduinoJson) project(ArduinoJson)
enable_testing() enable_testing()
@ -7,10 +14,6 @@ set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/lib) set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
if(MSVC)
add_definitions(-D_CRT_SECURE_NO_WARNINGS -W4)
endif()
if(${COVERAGE}) if(${COVERAGE})
set(CMAKE_CXX_FLAGS "-g -O0 -fprofile-arcs -ftest-coverage") set(CMAKE_CXX_FLAGS "-g -O0 -fprofile-arcs -ftest-coverage")
endif() endif()

View File

@ -1,7 +1,7 @@
The MIT License (MIT) The MIT License (MIT)
--------------------- ---------------------
Copyright © 2014-2015 Benoit BLANCHON 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: 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:

View File

@ -1,11 +1,11 @@
Arduino JSON library Arduino JSON library
==================== ====================
[![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) [![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.* *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. 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,56 +14,73 @@ Features
* JSON decoding (comments are supported) * JSON decoding (comments are supported)
* JSON encoding (with optional indentation) * JSON encoding (with optional indentation)
* Elegant API, very easy to use * Elegant API, very easy to use
* Efficient (no malloc, nor copy) * Efficient (no malloc, nor copy)
* Portable (written in C++98) * Portable (written in C++98)
* Self-contained (no external dependency) * Self-contained (no external dependency)
* Small footprint * Small footprint
* MIT License * MIT License
Works on
--------
* All Arduino boards
* ESP8266
* Teensy
* Intel Edison
* PlatformIO
* Energia
* Computers (Windows, Linux, OSX...)
See [FAQ: Compatibility issues](https://github.com/bblanchon/ArduinoJson/wiki/Compatibility-issues)
Quick start Quick start
----------- -----------
#### Decoding / Parsing #### Decoding / Parsing
char json[] = "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
StaticJsonBuffer<200> jsonBuffer; ```c++
char json[] = "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
JsonObject& root = jsonBuffer.parseObject(json); StaticJsonBuffer<200> jsonBuffer;
const char* sensor = root["sensor"]; JsonObject& root = jsonBuffer.parseObject(json);
long time = root["time"];
double latitude = root["data"][0]; const char* sensor = root["sensor"];
double longitude = root["data"][1]; long time = root["time"];
double latitude = root["data"][0];
double longitude = root["data"][1];
```
#### Encoding / Generating #### Encoding / Generating
StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.createObject(); ```c++
root["sensor"] = "gps"; StaticJsonBuffer<200> jsonBuffer;
root["time"] = 1351824120;
JsonArray& data = root.createNestedArray("data"); JsonObject& root = jsonBuffer.createObject();
data.add(48.756080, 6); // 6 is the number of decimals to print root["sensor"] = "gps";
data.add(2.302038, 6); // if not specified, 2 digits are printed root["time"] = 1351824120;
root.printTo(Serial); JsonArray& data = root.createNestedArray("data");
// This prints: data.add(48.756080, 6); // 6 is the number of decimals to print
// {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]} 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 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 Testimonials
------------ ------------
From Arduino's Forum user `jflaplante`: 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. > I have no such problem so far with your library. It is working perfectly with my web services.
From Arduino's Forum user `gbathree`: From Arduino's Forum user `gbathree`:
@ -78,6 +95,27 @@ 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. > 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!! > 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&currency_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&currency_code=EUR&bn=PP%2dDonationsBF%3abtn_donate_LG%2egif%3aNonHosted) :smile:

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#include <ArduinoJson.h> #include <ArduinoJson.h>
@ -10,6 +11,9 @@ using namespace ArduinoJson::Internals;
void setup() { void setup() {
Serial.begin(9600); Serial.begin(9600);
while (!Serial) {
// wait serial port initialization
}
IndentedPrint serial(Serial); IndentedPrint serial(Serial);
serial.setTabSize(4); serial.setTabSize(4);

View File

@ -1,13 +1,17 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#include <ArduinoJson.h> #include <ArduinoJson.h>
void setup() { void setup() {
Serial.begin(9600); Serial.begin(9600);
while (!Serial) {
// wait serial port initialization
}
StaticJsonBuffer<200> jsonBuffer; StaticJsonBuffer<200> jsonBuffer;

View File

@ -1,13 +1,17 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#include <ArduinoJson.h> #include <ArduinoJson.h>
void setup() { void setup() {
Serial.begin(9600); Serial.begin(9600);
while (!Serial) {
// wait serial port initialization
}
StaticJsonBuffer<200> jsonBuffer; StaticJsonBuffer<200> jsonBuffer;

View File

@ -3,7 +3,7 @@
// You can easily test this program with netcat: // You can easily test this program with netcat:
// $ nc -ulp 8888 // $ nc -ulp 8888
// //
// by Benoit Blanchon, MIT License 2015 // by Benoit Blanchon, MIT License 2015-2016
#include <SPI.h> #include <SPI.h>
#include <Ethernet.h> #include <Ethernet.h>

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#include "ArduinoJson/DynamicJsonBuffer.hpp" #include "ArduinoJson/DynamicJsonBuffer.hpp"
#include "ArduinoJson/JsonArray.hpp" #include "ArduinoJson/JsonArray.hpp"

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#pragma once #pragma once
@ -20,6 +21,7 @@ class Print {
size_t print(const char[]); size_t print(const char[]);
size_t print(double, int = 2); size_t print(double, int = 2);
size_t print(int);
size_t print(long); size_t print(long);
size_t println(); size_t println();
}; };

View File

@ -1,23 +1,16 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#pragma once #pragma once
#ifndef ARDUINO #ifndef ARDUINO
#include <string> #include <string>
typedef std::string String;
// This class reproduces Arduino's String class
class String : public std::string {
public:
String(const char *cstr = "") : std::string(cstr) {}
String(const String &str) : std::string(str) {}
explicit String(long);
explicit String(double, unsigned char decimalPlaces = 2);
};
#else #else

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#pragma once #pragma once

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#pragma once #pragma once
@ -31,7 +32,8 @@ class BlockJsonBuffer : public JsonBuffer {
}; };
public: public:
BlockJsonBuffer() : _head(NULL) {} BlockJsonBuffer(size_t initialSize = 256)
: _head(NULL), _nextBlockSize(initialSize) {}
~BlockJsonBuffer() { ~BlockJsonBuffer() {
Block* currentBlock = _head; Block* currentBlock = _head;
@ -55,29 +57,27 @@ class BlockJsonBuffer : public JsonBuffer {
} }
private: private:
static const size_t FIRST_BLOCK_CAPACITY = 32;
bool canAllocInHead(size_t bytes) const { bool canAllocInHead(size_t bytes) const {
return _head != NULL && _head->size + bytes <= _head->capacity; return _head != NULL && _head->size + bytes <= _head->capacity;
} }
void* allocInHead(size_t bytes) { void* allocInHead(size_t bytes) {
void* p = _head->data + _head->size; void* p = _head->data + _head->size;
_head->size += bytes; _head->size += round_size_up(bytes);
return p; return p;
} }
void* allocInNewBlock(size_t bytes) { void* allocInNewBlock(size_t bytes) {
size_t capacity = FIRST_BLOCK_CAPACITY; size_t capacity = _nextBlockSize;
if (_head != NULL) capacity = _head->capacity * 2;
if (bytes > capacity) capacity = bytes; if (bytes > capacity) capacity = bytes;
if (!addNewBlock(capacity)) return NULL; if (!addNewBlock(capacity)) return NULL;
_nextBlockSize *= 2;
return allocInHead(bytes); return allocInHead(bytes);
} }
bool addNewBlock(size_t capacity) { bool addNewBlock(size_t capacity) {
size_t size = sizeof(EmptyBlock) + capacity; size_t bytes = sizeof(EmptyBlock) + capacity;
Block* block = static_cast<Block*>(_allocator.allocate(size)); Block* block = static_cast<Block*>(_allocator.allocate(bytes));
if (block == NULL) return false; if (block == NULL) return false;
block->capacity = capacity; block->capacity = capacity;
block->size = 0; block->size = 0;
@ -86,8 +86,9 @@ class BlockJsonBuffer : public JsonBuffer {
return true; return true;
} }
Block* _head;
TAllocator _allocator; TAllocator _allocator;
Block* _head;
size_t _nextBlockSize;
}; };
} }
} }

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#pragma once #pragma once

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#pragma once #pragma once

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#pragma once #pragma once
@ -18,7 +19,8 @@ class DynamicStringBuilder : public Print {
DynamicStringBuilder(String &str) : _str(str) {} DynamicStringBuilder(String &str) : _str(str) {}
virtual size_t write(uint8_t c) { 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; return 1;
} }

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#pragma once #pragma once
@ -32,8 +33,8 @@ class Encoding {
} }
} }
private: private:
static const char _escapeTable[]; static const char _escapeTable[];
}; };
} }
} }

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#pragma once #pragma once

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#pragma once #pragma once

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#pragma once #pragma once

View File

@ -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 {
#ifdef ARDUINO
// On embedded platform, we with use float instead of double to keep JsonVariant
// small (issue #134)
typedef float JsonFloat;
#else
typedef double JsonFloat;
#endif
}
}

View File

@ -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 {
typedef long JsonInteger;
}
}

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#pragma once #pragma once
@ -19,7 +20,7 @@ class JsonParser {
public: public:
JsonParser(JsonBuffer *buffer, char *json, uint8_t nestingLimit) JsonParser(JsonBuffer *buffer, char *json, uint8_t nestingLimit)
: _buffer(buffer), : _buffer(buffer),
_readPtr(json), _readPtr(json ? json : ""),
_writePtr(json), _writePtr(json),
_nestingLimit(nestingLimit) {} _nestingLimit(nestingLimit) {}

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#pragma once #pragma once

View File

@ -1,11 +1,15 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#pragma once #pragma once
#include "JsonFloat.hpp"
#include "JsonInteger.hpp"
namespace ArduinoJson { namespace ArduinoJson {
// Forward declarations // Forward declarations
@ -13,15 +17,14 @@ class JsonArray;
class JsonObject; class JsonObject;
namespace Internals { namespace Internals {
// A union that defines the actual content of a JsonVariant. // A union that defines the actual content of a JsonVariant.
// The enum JsonVariantType determines which member is in use. // The enum JsonVariantType determines which member is in use.
union JsonVariantContent { union JsonVariantContent {
double asDouble; // asDouble is also used for float JsonFloat asFloat; // used for double and float
long asLong; // asLong is also used for bool, char, short and int JsonInteger asInteger; // used for bool, char, short, int and longs
const char* asString; // asString can be null const char* asString; // asString can be null
JsonArray* asArray; // asArray cannot be null JsonArray* asArray; // asArray cannot be null
JsonObject* asObject; // asObject cannot be null JsonObject* asObject; // asObject cannot be null
}; };
} }
} }

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#pragma once #pragma once
@ -19,17 +20,17 @@ enum JsonVariantType {
JSON_UNPARSED, // the JsonVariant contains an unparsed string JSON_UNPARSED, // the JsonVariant contains an unparsed string
JSON_STRING, // the JsonVariant stores a const char* JSON_STRING, // the JsonVariant stores a const char*
JSON_BOOLEAN, // the JsonVariant stores a bool JSON_BOOLEAN, // the JsonVariant stores a bool
JSON_LONG, // the JsonVariant stores a long JSON_INTEGER, // the JsonVariant stores an integer
JSON_ARRAY, // the JsonVariant stores a pointer to a JsonArray JSON_ARRAY, // the JsonVariant stores a pointer to a JsonArray
JSON_OBJECT, // the JsonVariant stores a pointer to a JsonObject JSON_OBJECT, // the JsonVariant stores a pointer to a JsonObject
// The following values are reserved for double values // The following values are reserved for float values
// Multiple values are used for double, depending on the number of decimal // Multiple values are used for double, depending on the number of decimal
// digits that must be printed in the JSON output. // digits that must be printed in the JSON output.
// This little trick allow to save one extra member in JsonVariant // This little trick allow to save one extra member in JsonVariant
JSON_DOUBLE_0_DECIMALS JSON_FLOAT_0_DECIMALS
// JSON_DOUBLE_1_DECIMAL // JSON_FLOAT_1_DECIMAL
// JSON_DOUBLE_2_DECIMALS // JSON_FLOAT_2_DECIMALS
// ... // ...
}; };
} }

View File

@ -1,14 +1,17 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#pragma once #pragma once
#include "../Arduino/Print.hpp" #include "../Arduino/Print.hpp"
#include "Encoding.hpp" #include "Encoding.hpp"
#include "ForceInline.hpp" #include "ForceInline.hpp"
#include "JsonFloat.hpp"
#include "JsonInteger.hpp"
namespace ArduinoJson { namespace ArduinoJson {
namespace Internals { namespace Internals {
@ -60,9 +63,9 @@ class JsonWriter {
} }
} }
void writeLong(long value) { _length += _sink.print(value); } void writeInteger(JsonInteger value) { _length += _sink.print(value); }
void writeDouble(double value, uint8_t decimals) { void writeFloat(JsonFloat value, uint8_t decimals) {
_length += _sink.print(value, decimals); _length += _sink.print(value, decimals);
} }

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#pragma once #pragma once

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#pragma once #pragma once

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#pragma once #pragma once

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#pragma once #pragma once

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#pragma once #pragma once

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#pragma once #pragma once

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#pragma once #pragma once
@ -14,7 +15,7 @@ namespace Internals {
// A Print implementation that allows to write in a char[] // A Print implementation that allows to write in a char[]
class StaticStringBuilder : public Print { class StaticStringBuilder : public Print {
public: public:
StaticStringBuilder(char *buf, int size) StaticStringBuilder(char *buf, size_t size)
: buffer(buf), capacity(size - 1), length(0) { : buffer(buf), capacity(size - 1), length(0) {
buffer[0] = '\0'; buffer[0] = '\0';
} }
@ -23,8 +24,8 @@ class StaticStringBuilder : public Print {
private: private:
char *buffer; char *buffer;
int capacity; size_t capacity;
int length; size_t length;
}; };
} }
} }

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#pragma once #pragma once
@ -31,4 +32,4 @@ class StreamPrintAdapter : public Print {
} }
} }
#endif // ARDUINOJSON_ENABLE_STD_STREAM #endif // ARDUINOJSON_ENABLE_STD_STREAM

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#pragma once #pragma once

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#pragma once #pragma once
@ -63,7 +64,8 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
FORCE_INLINE bool add(const String &value); FORCE_INLINE bool add(const String &value);
FORCE_INLINE bool add(JsonArray &array); FORCE_INLINE bool add(JsonArray &array);
FORCE_INLINE bool add(JsonObject &object); 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. // Sets the value at specified index.
FORCE_INLINE void set(size_t index, bool value); FORCE_INLINE void set(size_t index, bool value);
@ -81,7 +83,8 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
FORCE_INLINE void set(size_t index, const String &value); 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, JsonArray &array);
FORCE_INLINE void set(size_t index, JsonObject &object); 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. // Gets the value at the specified index.
FORCE_INLINE JsonVariant get(size_t index) const; FORCE_INLINE JsonVariant get(size_t index) const;
@ -92,7 +95,7 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
// Check the type of the value at specified index. // Check the type of the value at specified index.
template <typename T> 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. // Creates a JsonArray and adds a reference at the end of the array.
// It's a shortcut for JsonBuffer::createArray() and JsonArray::add() // It's a shortcut for JsonBuffer::createArray() and JsonArray::add()

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#pragma once #pragma once
@ -77,8 +78,9 @@ inline bool JsonArray::add(JsonObject &object) {
return addNode<JsonObject &>(object); return addNode<JsonObject &>(object);
} }
inline bool JsonArray::add(const JsonVariant &object) { template <typename T>
return addNode<const JsonVariant &>(object); inline bool JsonArray::add(const T &variant) {
return addNode<const JsonVariant &>(variant);
} }
template <typename TValue> template <typename TValue>
@ -149,8 +151,9 @@ inline void JsonArray::set(size_t index, JsonObject &object) {
return setNodeAt<JsonObject &>(index, object); return setNodeAt<JsonObject &>(index, object);
} }
inline void JsonArray::set(size_t index, const JsonVariant &object) { template <typename T>
return setNodeAt<const JsonVariant &>(index, object); inline void JsonArray::set(size_t index, const T &variant) {
return setNodeAt<const JsonVariant &>(index, variant);
} }
template <typename TValue> template <typename TValue>
@ -182,7 +185,7 @@ inline T JsonArray::get(size_t index) const {
} }
template <typename T> template <typename T>
inline T JsonArray::is(size_t index) const { inline bool JsonArray::is(size_t index) const {
node_type *node = getNodeAt(index); node_type *node = getNodeAt(index);
return node ? node->content.is<T>() : false; return node ? node->content.is<T>() : false;
} }

View File

@ -1,13 +1,19 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#pragma once #pragma once
#include "JsonSubscriptBase.hpp" #include "JsonSubscriptBase.hpp"
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4522)
#endif
namespace ArduinoJson { namespace ArduinoJson {
class JsonArraySubscript : public JsonSubscriptBase<JsonArraySubscript> { class JsonArraySubscript : public JsonSubscriptBase<JsonArraySubscript> {
public: public:
@ -16,6 +22,15 @@ class JsonArraySubscript : public JsonSubscriptBase<JsonArraySubscript> {
using JsonSubscriptBase<JsonArraySubscript>::operator=; using JsonSubscriptBase<JsonArraySubscript>::operator=;
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(); } FORCE_INLINE bool success() const { return _index < _array.size(); }
FORCE_INLINE operator JsonVariant() const { return _array.get(_index); } FORCE_INLINE operator JsonVariant() const { return _array.get(_index); }
@ -26,7 +41,7 @@ class JsonArraySubscript : public JsonSubscriptBase<JsonArraySubscript> {
} }
template <typename T> template <typename T>
FORCE_INLINE T is() const { FORCE_INLINE bool is() const {
return _array.is<T>(_index); return _array.is<T>(_index);
} }
@ -52,3 +67,7 @@ inline std::ostream& operator<<(std::ostream& os,
#endif #endif
} // namespace ArduinoJson } // namespace ArduinoJson
#ifdef _MSC_VER
#pragma warning(pop)
#endif

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#pragma once #pragma once
@ -59,9 +60,15 @@ class JsonBuffer {
// allocation fails. // allocation fails.
JsonArray &parseArray(char *json, uint8_t nestingLimit = DEFAULT_LIMIT); 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 // Same as above with a String class
JsonArray &parseArray(const String &json, uint8_t nesting = DEFAULT_LIMIT) { 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. // Allocates and populate a JsonObject from a JSON string.
@ -76,30 +83,54 @@ class JsonBuffer {
// allocation fails. // allocation fails.
JsonObject &parseObject(char *json, uint8_t nestingLimit = DEFAULT_LIMIT); JsonObject &parseObject(char *json, uint8_t nestingLimit = DEFAULT_LIMIT);
// Same as above with a String class // Same with a const char*.
JsonObject &parseObject(const String &json, uint8_t nesting = DEFAULT_LIMIT) { // 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); 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 // 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 String &src) { return strdup(src.c_str(), src.length()); }
char *strdup(const char *, size_t);
// Allocates n bytes in the JsonBuffer. // Allocates n bytes in the JsonBuffer.
// Return a pointer to the allocated memory or NULL if allocation fails. // Return a pointer to the allocated memory or NULL if allocation fails.
virtual void *alloc(size_t size) = 0; 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(). // 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. // string.
// If set to 0, only a flat array or objects can be parsed. // 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 // If set to 1, the object can contain nested arrays or objects but only 1
// level deep. // level deep.
// And bigger values will allow more level of nesting. // 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. // a security risk.
static const uint8_t DEFAULT_LIMIT = 10; static const uint8_t DEFAULT_LIMIT = 10;
}; };

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#pragma once #pragma once
@ -65,6 +66,9 @@ class JsonObject : public Internals::JsonPrintable<JsonObject>,
FORCE_INLINE bool set(const char* key, JsonArray& array); 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, JsonObject& object);
FORCE_INLINE bool set(const char* key, const JsonVariant& value); 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, bool value);
FORCE_INLINE bool set(const String& key, float value, uint8_t decimals = 2); 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); FORCE_INLINE bool set(const String& key, double value, uint8_t decimals = 2);
@ -81,6 +85,8 @@ class JsonObject : public Internals::JsonPrintable<JsonObject>,
FORCE_INLINE bool set(const String& key, JsonArray& array); 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, JsonObject& object);
FORCE_INLINE bool set(const String& key, const JsonVariant& value); 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. // Gets the value associated with the specified key.
FORCE_INLINE JsonVariant get(JsonObjectKey) const; FORCE_INLINE JsonVariant get(JsonObjectKey) const;

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#pragma once #pragma once
@ -116,6 +117,10 @@ inline bool JsonObject::set(const char *key, JsonObject &object) {
inline bool JsonObject::set(const char *key, const JsonVariant &value) { inline bool JsonObject::set(const char *key, const JsonVariant &value) {
return setNodeAt<const char *, const JsonVariant &>(key, 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) { inline bool JsonObject::set(const String &key, bool value) {
return setNodeAt<const String &, bool>(key, value); return setNodeAt<const String &, bool>(key, value);
} }
@ -166,6 +171,10 @@ inline bool JsonObject::set(const String &key, JsonObject &object) {
inline bool JsonObject::set(const String &key, const JsonVariant &value) { inline bool JsonObject::set(const String &key, const JsonVariant &value) {
return setNodeAt<const String &, const JsonVariant &>(key, 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> template <typename TKey, typename TValue>
inline bool JsonObject::setNodeAt(TKey key, TValue value) { inline bool JsonObject::setNodeAt(TKey key, TValue value) {

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#pragma once #pragma once

View File

@ -1,13 +1,19 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#pragma once #pragma once
#include "JsonSubscriptBase.hpp" #include "JsonSubscriptBase.hpp"
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4522)
#endif
namespace ArduinoJson { namespace ArduinoJson {
template <typename TKey> template <typename TKey>
@ -19,6 +25,17 @@ class JsonObjectSubscript
using JsonSubscriptBase<JsonObjectSubscript<TKey> >::operator=; using JsonSubscriptBase<JsonObjectSubscript<TKey> >::operator=;
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); } FORCE_INLINE bool success() const { return _object.containsKey(_key); }
FORCE_INLINE operator JsonVariant() const { return _object.get(_key); } FORCE_INLINE operator JsonVariant() const { return _object.get(_key); }
@ -29,7 +46,7 @@ class JsonObjectSubscript
} }
template <typename TValue> template <typename TValue>
FORCE_INLINE TValue is() const { FORCE_INLINE bool is() const {
return _object.is<TValue>(_key); return _object.is<TValue>(_key);
} }
@ -38,6 +55,11 @@ class JsonObjectSubscript
return _object.set(_key, value); return _object.set(_key, value);
} }
template <typename TValue>
FORCE_INLINE bool set(TValue value, uint8_t decimals) {
return _object.set(_key, value, decimals);
}
FORCE_INLINE JsonVariant get() { return _object.get(_key); } FORCE_INLINE JsonVariant get() { return _object.get(_key); }
void writeTo(Internals::JsonWriter& writer) const { void writeTo(Internals::JsonWriter& writer) const {
@ -60,4 +82,8 @@ inline std::ostream& operator<<(
return source.printTo(os); return source.printTo(os);
} }
#endif #endif
} } // namespace ArduinoJson
#ifdef _MSC_VER
#pragma warning(pop)
#endif

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#pragma once #pragma once

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#pragma once #pragma once
@ -68,15 +69,15 @@ class JsonSubscriptBase : public JsonVariantBase<TImpl> {
} }
FORCE_INLINE TImpl& operator=(JsonVariant value) { FORCE_INLINE TImpl& operator=(JsonVariant value) {
return assign<JsonVariant>(value); return assign<const JsonVariant&>(value);
} }
private: protected:
template <typename TValue> template <typename TValue>
FORCE_INLINE TImpl& assign(TValue value) { FORCE_INLINE TImpl& assign(TValue value) {
TImpl* impl = static_cast<TImpl*>(this); TImpl* that = static_cast<TImpl*>(this);
impl->template set<TValue>(value); that->template set<TValue>(value);
return *impl; return *that;
} }
}; };
} }

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#pragma once #pragma once
@ -83,6 +84,9 @@ class JsonVariant : public JsonVariantBase<JsonVariant> {
static T invalid(); static T invalid();
private: private:
Internals::JsonFloat asFloat() const;
Internals::JsonInteger asInteger() const;
// The current type of the variant // The current type of the variant
Internals::JsonVariantType _type; Internals::JsonVariantType _type;

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#pragma once #pragma once
@ -11,8 +12,9 @@
namespace ArduinoJson { namespace ArduinoJson {
inline JsonVariant::JsonVariant(bool value) { inline JsonVariant::JsonVariant(bool value) {
_type = Internals::JSON_BOOLEAN; using namespace Internals;
_content.asLong = value; _type = JSON_BOOLEAN;
_content.asInteger = static_cast<JsonInteger>(value);
} }
inline JsonVariant::JsonVariant(const char *value) { inline JsonVariant::JsonVariant(const char *value) {
@ -26,15 +28,15 @@ inline JsonVariant::JsonVariant(Internals::Unparsed value) {
} }
inline JsonVariant::JsonVariant(double value, uint8_t decimals) { inline JsonVariant::JsonVariant(double value, uint8_t decimals) {
_type = static_cast<Internals::JsonVariantType>( using namespace Internals;
Internals::JSON_DOUBLE_0_DECIMALS + decimals); _type = static_cast<JsonVariantType>(JSON_FLOAT_0_DECIMALS + decimals);
_content.asDouble = value; _content.asFloat = static_cast<JsonFloat>(value);
} }
inline JsonVariant::JsonVariant(float value, uint8_t decimals) { inline JsonVariant::JsonVariant(float value, uint8_t decimals) {
_type = static_cast<Internals::JsonVariantType>( using namespace Internals;
Internals::JSON_DOUBLE_0_DECIMALS + decimals); _type = static_cast<JsonVariantType>(JSON_FLOAT_0_DECIMALS + decimals);
_content.asDouble = value; _content.asFloat = static_cast<JsonFloat>(value);
} }
inline JsonVariant::JsonVariant(JsonArray &array) { inline JsonVariant::JsonVariant(JsonArray &array) {
@ -48,51 +50,53 @@ inline JsonVariant::JsonVariant(JsonObject &object) {
} }
inline JsonVariant::JsonVariant(signed char value) { inline JsonVariant::JsonVariant(signed char value) {
_type = Internals::JSON_LONG; using namespace Internals;
_content.asLong = value; _type = JSON_INTEGER;
_content.asInteger = static_cast<JsonInteger>(value);
} }
inline JsonVariant::JsonVariant(signed int value) { inline JsonVariant::JsonVariant(signed int value) {
_type = Internals::JSON_LONG; using namespace Internals;
_content.asLong = value; _type = JSON_INTEGER;
_content.asInteger = static_cast<JsonInteger>(value);
} }
inline JsonVariant::JsonVariant(signed long value) { inline JsonVariant::JsonVariant(signed long value) {
_type = Internals::JSON_LONG; using namespace Internals;
_content.asLong = value; _type = JSON_INTEGER;
_content.asInteger = static_cast<JsonInteger>(value);
} }
inline JsonVariant::JsonVariant(signed short value) { inline JsonVariant::JsonVariant(signed short value) {
_type = Internals::JSON_LONG; using namespace Internals;
_content.asLong = value; _type = JSON_INTEGER;
_content.asInteger = static_cast<JsonInteger>(value);
} }
inline JsonVariant::JsonVariant(unsigned char value) { inline JsonVariant::JsonVariant(unsigned char value) {
_type = Internals::JSON_LONG; using namespace Internals;
_content.asLong = value; _type = JSON_INTEGER;
_content.asInteger = static_cast<JsonInteger>(value);
} }
inline JsonVariant::JsonVariant(unsigned int value) { inline JsonVariant::JsonVariant(unsigned int value) {
_type = Internals::JSON_LONG; using namespace Internals;
_content.asLong = value; _type = JSON_INTEGER;
_content.asInteger = static_cast<JsonInteger>(value);
} }
inline JsonVariant::JsonVariant(unsigned long value) { inline JsonVariant::JsonVariant(unsigned long value) {
_type = Internals::JSON_LONG; using namespace Internals;
_content.asLong = value; _type = JSON_INTEGER;
_content.asInteger = static_cast<JsonInteger>(value);
} }
inline JsonVariant::JsonVariant(unsigned short value) { inline JsonVariant::JsonVariant(unsigned short value) {
_type = Internals::JSON_LONG; using namespace Internals;
_content.asLong = value; _type = JSON_INTEGER;
_content.asInteger = static_cast<JsonInteger>(value);
} }
template <>
double JsonVariant::as<double>() const;
template <>
long JsonVariant::as<long>() const;
template <> template <>
String JsonVariant::as<String>() const; String JsonVariant::as<String>() const;
@ -101,47 +105,57 @@ const char *JsonVariant::as<const char *>() const;
template <> template <>
inline bool JsonVariant::as<bool>() const { inline bool JsonVariant::as<bool>() const {
return as<long>() != 0; return asInteger() != 0;
} }
template <> template <>
inline signed char JsonVariant::as<signed char>() const { inline signed char JsonVariant::as<signed char>() const {
return static_cast<signed char>(as<long>()); return static_cast<signed char>(asInteger());
} }
template <> template <>
inline unsigned char JsonVariant::as<unsigned char>() const { inline unsigned char JsonVariant::as<unsigned char>() const {
return static_cast<unsigned char>(as<long>()); return static_cast<unsigned char>(asInteger());
} }
template <> template <>
inline signed short JsonVariant::as<signed short>() const { inline signed short JsonVariant::as<signed short>() const {
return static_cast<signed short>(as<long>()); return static_cast<signed short>(asInteger());
} }
template <> template <>
inline unsigned short JsonVariant::as<unsigned short>() const { inline unsigned short JsonVariant::as<unsigned short>() const {
return static_cast<unsigned short>(as<long>()); return static_cast<unsigned short>(asInteger());
} }
template <> template <>
inline signed int JsonVariant::as<signed int>() const { inline signed int JsonVariant::as<signed int>() const {
return static_cast<signed int>(as<long>()); return static_cast<signed int>(asInteger());
} }
template <> template <>
inline unsigned int JsonVariant::as<unsigned int>() const { inline unsigned int JsonVariant::as<unsigned int>() const {
return static_cast<unsigned int>(as<long>()); return static_cast<unsigned int>(asInteger());
} }
template <> template <>
inline unsigned long JsonVariant::as<unsigned long>() const { inline unsigned long JsonVariant::as<unsigned long>() const {
return static_cast<unsigned long>(as<long>()); return static_cast<unsigned long>(asInteger());
}
template <>
inline signed long JsonVariant::as<signed long>() const {
return static_cast<unsigned long>(asInteger());
}
template <>
inline double JsonVariant::as<double>() const {
return static_cast<double>(asFloat());
} }
template <> template <>
inline float JsonVariant::as<float>() const { inline float JsonVariant::as<float>() const {
return static_cast<float>(as<double>()); return static_cast<float>(asFloat());
} }
template <typename T> template <typename T>
@ -160,10 +174,8 @@ bool JsonVariant::is<signed long>() const;
template <> // in .cpp template <> // in .cpp
bool JsonVariant::is<double>() const; bool JsonVariant::is<double>() const;
template <> template <> // int .cpp
inline bool JsonVariant::is<bool>() const { bool JsonVariant::is<bool>() const;
return _type == Internals::JSON_BOOLEAN;
}
template <> template <>
inline bool JsonVariant::is<char const *>() const { inline bool JsonVariant::is<char const *>() const {

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#pragma once #pragma once

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#pragma once #pragma once
@ -21,11 +22,10 @@ class StaticJsonBuffer : public JsonBuffer {
size_t capacity() const { return CAPACITY; } size_t capacity() const { return CAPACITY; }
size_t size() const { return _size; } size_t size() const { return _size; }
protected:
virtual void* alloc(size_t bytes) { virtual void* alloc(size_t bytes) {
if (_size + bytes > CAPACITY) return NULL; if (_size + bytes > CAPACITY) return NULL;
void* p = &_buffer[_size]; void* p = &_buffer[_size];
_size += bytes; _size += round_size_up(bytes);
return p; return p;
} }

View File

@ -2,16 +2,21 @@
"name": "Json", "name": "Json",
"keywords": "json, rest, http, web", "keywords": "json, rest, http, web",
"description": "An elegant and efficient JSON library for embedded systems", "description": "An elegant and efficient JSON library for embedded systems",
"repository": "repository": {
{
"type": "git", "type": "git",
"url": "https://github.com/bblanchon/ArduinoJson.git" "url": "https://github.com/bblanchon/ArduinoJson.git"
}, },
"authors": "version": "5.0.8",
{ "authors": {
"name": "Benoit Blanchon", "name": "Benoit Blanchon",
"url": "http://blog.benoitblanchon.fr" "url": "http://blog.benoitblanchon.fr"
}, },
"exclude": [
"scripts",
"src/ArduinoJson.h",
"test",
"third-party"
],
"frameworks": "arduino", "frameworks": "arduino",
"platforms": "atmelavr" "platforms": "*"
} }

View File

@ -1,8 +1,9 @@
name=ArduinoJson name=ArduinoJson
version=5.0.1 version=5.0.8
author=Benoit Blanchon <blog.benoitblanchon.fr> author=Benoit Blanchon <blog.benoitblanchon.fr>
maintainer=Benoit Blanchon <blog.benoitblanchon.fr> maintainer=Benoit Blanchon <blog.benoitblanchon.fr>
sentence=An efficient and elegant JSON library for Arduino. sentence=An efficient and elegant JSON library for Arduino.
paragraph=Like this project? Please star it on GitHub! paragraph=Like this project? Please star it on GitHub!
category=Data Processing
url=https://github.com/bblanchon/ArduinoJson url=https://github.com/bblanchon/ArduinoJson
architectures=* architectures=*

View 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

15
scripts/travis/arduino.sh Executable file
View File

@ -0,0 +1,15 @@
#!/bin/sh -eux
/sbin/start-stop-daemon --start --quiet --pidfile /tmp/custom_xvfb_1.pid --make-pidfile --background --exec /usr/bin/Xvfb -- :1 -ac -screen 0 1280x1024x16
sleep 3
export DISPLAY=:1.0
mkdir -p /tmp/arduino
curl -sS http://downloads.arduino.cc/arduino-$VERSION-linux64.tar.xz | tar xJ -C /tmp/arduino --strip 1 ||
curl -sS http://downloads.arduino.cc/arduino-$VERSION-linux64.tgz | tar xz -C /tmp/arduino --strip 1
export PATH=$PATH:/tmp/arduino/
ln -s $PWD /tmp/arduino/libraries/ArduinoJson
arduino --verify --board $BOARD $PWD/examples/JsonParserExample/JsonParserExample.ino
arduino --verify --board $BOARD $PWD/examples/JsonGeneratorExample/JsonGeneratorExample.ino

10
scripts/travis/clang.sh Executable file
View File

@ -0,0 +1,10 @@
#!/bin/sh -eux
export CC=clang
export CXX=clang++
curl -sS https://cmake.org/files/v3.4/cmake-3.4.0-Linux-x86_64.tar.gz | tar xz -C /tmp --strip 1
/tmp/bin/cmake .
make
make test

13
scripts/travis/gcc.sh Executable file
View File

@ -0,0 +1,13 @@
#!/bin/sh -eux
export CC=gcc
export CXX=g++
curl https://cmake.org/files/v3.4/cmake-3.4.0-Linux-x86_64.tar.gz | tar xz -C /tmp --strip 1
/tmp/bin/cmake -DCOVERAGE=true .
make
make test
pip install --user cpp-coveralls
coveralls --exclude third-party --gcov-options '\-lp'; fi

10
scripts/travis/platformio.sh Executable file
View File

@ -0,0 +1,10 @@
#!/bin/sh -eux
pip install --user platformio
rm -r test
for EXAMPLE in JsonParserExample JsonGeneratorExample
do
platformio ci examples/$EXAMPLE/$EXAMPLE.ino -l '.' -b $BOARD
done

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#ifndef ARDUINO #ifndef ARDUINO
@ -17,6 +18,13 @@
#pragma GCC diagnostic ignored "-Wfloat-conversion" #pragma GCC diagnostic ignored "-Wfloat-conversion"
#endif #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 Print::print(const char s[]) {
size_t n = 0; size_t n = 0;
while (*s) { while (*s) {
@ -53,6 +61,12 @@ size_t Print::print(long value) {
return print(tmp); return print(tmp);
} }
size_t Print::print(int value) {
char tmp[32];
sprintf(tmp, "%d", value);
return print(tmp);
}
size_t Print::println() { return write('\r') + write('\n'); } size_t Print::println() { return write('\r') + write('\n'); }
#endif #endif

View File

@ -1,25 +0,0 @@
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
#ifndef ARDUINO
#include "../../include/ArduinoJson/Arduino/String.hpp"
#include <stdio.h> // for sprintf()
String::String(double value, unsigned char digits) {
char tmp[32];
sprintf(tmp, "%.*f", digits, value);
*this = tmp;
}
String::String(long value) {
char tmp[32];
sprintf(tmp, "%ld", value);
*this = tmp;
}
#endif

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
// About this file // About this file
// --------------- // ---------------

View File

@ -1,3 +1,10 @@
# Copyright Benoit Blanchon 2014-2016
# MIT License
#
# Arduino JSON library
# https://github.com/bblanchon/ArduinoJson
# If you like this project, please add a star!
file(GLOB_RECURSE HPP_FILES ../include/*.hpp) file(GLOB_RECURSE HPP_FILES ../include/*.hpp)
file(GLOB_RECURSE IPP_FILES ../include/*.ipp) file(GLOB_RECURSE IPP_FILES ../include/*.ipp)
file(GLOB_RECURSE CPP_FILES *.cpp) file(GLOB_RECURSE CPP_FILES *.cpp)
@ -5,6 +12,7 @@ file(GLOB_RECURSE CPP_FILES *.cpp)
if(CMAKE_CXX_COMPILER_ID MATCHES "(GNU|Clang)") if(CMAKE_CXX_COMPILER_ID MATCHES "(GNU|Clang)")
add_definitions( add_definitions(
-fno-exceptions -fno-exceptions
-fno-rtti
-pedantic -pedantic
-Wall -Wall
-Wcast-align -Wcast-align
@ -43,7 +51,16 @@ endif()
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang") if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_definitions( add_definitions(
-Wc++11-compat -Wc++11-compat
-Wdeprecated-register
) )
endif() endif()
if(MSVC)
add_definitions(
-D_CRT_SECURE_NO_WARNINGS
-W4)
endif()
add_library(ArduinoJson ${CPP_FILES} ${HPP_FILES} ${IPP_FILES}) add_library(ArduinoJson ${CPP_FILES} ${HPP_FILES} ${IPP_FILES})
target_include_directories(ArduinoJson INTERFACE ${CMAKE_CURRENT_LIST_DIR}/../include)

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#include "../../include/ArduinoJson/Internals/Comments.hpp" #include "../../include/ArduinoJson/Internals/Comments.hpp"

View File

@ -1,12 +1,14 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#include "../../include/ArduinoJson/Internals/Encoding.hpp" #include "../../include/ArduinoJson/Internals/Encoding.hpp"
// How to escape special chars: // How to escape special chars:
// _escapeTable[2*i+1] => the special char // _escapeTable[2*i+1] => the special char
// _escapeTable[2*i] => the char to use instead // _escapeTable[2*i] => the char to use instead
const char ArduinoJson::Internals::Encoding::_escapeTable[] = "\"\"\\\\b\bf\fn\nr\rt\t"; const char ArduinoJson::Internals::Encoding::_escapeTable[] =
"\"\"\\\\b\bf\fn\nr\rt\t";

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#include "../../include/ArduinoJson/Internals/IndentedPrint.hpp" #include "../../include/ArduinoJson/Internals/IndentedPrint.hpp"

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#include "../../include/ArduinoJson/Internals/JsonParser.hpp" #include "../../include/ArduinoJson/Internals/JsonParser.hpp"
@ -16,7 +17,7 @@ using namespace ArduinoJson;
using namespace ArduinoJson::Internals; using namespace ArduinoJson::Internals;
bool JsonParser::skip(char charToSkip) { bool JsonParser::skip(char charToSkip) {
register const char *ptr = skipSpacesAndComments(_readPtr); const char *ptr = skipSpacesAndComments(_readPtr);
if (*ptr != charToSkip) return false; if (*ptr != charToSkip) return false;
ptr++; ptr++;
_readPtr = skipSpacesAndComments(ptr); _readPtr = skipSpacesAndComments(ptr);

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#include "../../include/ArduinoJson/Internals/List.hpp" #include "../../include/ArduinoJson/Internals/List.hpp"

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#include "../../include/ArduinoJson/Internals/Prettyfier.hpp" #include "../../include/ArduinoJson/Internals/Prettyfier.hpp"

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#include "../../include/ArduinoJson/Internals/StaticStringBuilder.hpp" #include "../../include/ArduinoJson/Internals/StaticStringBuilder.hpp"

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#include "../include/ArduinoJson/JsonArray.hpp" #include "../include/ArduinoJson/JsonArray.hpp"

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#include "../include/ArduinoJson/JsonBuffer.hpp" #include "../include/ArduinoJson/JsonBuffer.hpp"

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#include "../include/ArduinoJson/JsonObject.hpp" #include "../include/ArduinoJson/JsonObject.hpp"

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#include "../include/ArduinoJson/JsonVariant.hpp" #include "../include/ArduinoJson/JsonVariant.hpp"
@ -16,6 +17,29 @@ using namespace ArduinoJson::Internals;
namespace ArduinoJson { namespace ArduinoJson {
template <typename TFloat>
static TFloat parse(const char *);
template <>
float parse<float>(const char *s) {
return static_cast<float>(strtod(s, NULL));
}
template <>
double parse<double>(const char *s) {
return strtod(s, NULL);
}
template <>
long parse<long>(const char *s) {
return strtol(s, NULL, 10);
}
template <>
int parse<int>(const char *s) {
return atoi(s);
}
template <> template <>
const char *JsonVariant::as<const char *>() const { const char *JsonVariant::as<const char *>() const {
if (_type == JSON_UNPARSED && _content.asString && if (_type == JSON_UNPARSED && _content.asString &&
@ -25,29 +49,27 @@ const char *JsonVariant::as<const char *>() const {
return NULL; return NULL;
} }
template <> JsonFloat JsonVariant::asFloat() const {
double JsonVariant::as<double>() const { if (_type >= JSON_FLOAT_0_DECIMALS) return _content.asFloat;
if (_type >= JSON_DOUBLE_0_DECIMALS) return _content.asDouble;
if (_type == JSON_LONG || _type == JSON_BOOLEAN) if (_type == JSON_INTEGER || _type == JSON_BOOLEAN)
return static_cast<double>(_content.asLong); return static_cast<JsonFloat>(_content.asInteger);
if ((_type == JSON_STRING || _type == JSON_UNPARSED) && _content.asString) if ((_type == JSON_STRING || _type == JSON_UNPARSED) && _content.asString)
return strtod(_content.asString, NULL); return parse<JsonFloat>(_content.asString);
return 0.0; return 0.0;
} }
template <> JsonInteger JsonVariant::asInteger() const {
long JsonVariant::as<long>() const { if (_type == JSON_INTEGER || _type == JSON_BOOLEAN) return _content.asInteger;
if (_type == JSON_LONG || _type == JSON_BOOLEAN) return _content.asLong;
if (_type >= JSON_DOUBLE_0_DECIMALS) if (_type >= JSON_FLOAT_0_DECIMALS)
return static_cast<long>(_content.asDouble); return static_cast<JsonInteger>(_content.asFloat);
if ((_type == JSON_STRING || _type == JSON_UNPARSED) && _content.asString) { if ((_type == JSON_STRING || _type == JSON_UNPARSED) && _content.asString) {
if (!strcmp("true", _content.asString)) return 1; if (!strcmp("true", _content.asString)) return 1;
return strtol(_content.asString, NULL, 10); return parse<JsonInteger>(_content.asString);
} }
return 0L; return 0L;
@ -55,26 +77,28 @@ long JsonVariant::as<long>() const {
template <> template <>
String JsonVariant::as<String>() const { String JsonVariant::as<String>() const {
String s;
if ((_type == JSON_STRING || _type == JSON_UNPARSED) && if ((_type == JSON_STRING || _type == JSON_UNPARSED) &&
_content.asString != NULL) _content.asString != NULL)
return String(_content.asString); s = _content.asString;
else
if (_type == JSON_LONG || _type == JSON_BOOLEAN) printTo(s);
return String(_content.asLong);
if (_type >= JSON_DOUBLE_0_DECIMALS) {
uint8_t decimals = static_cast<uint8_t>(_type - JSON_DOUBLE_0_DECIMALS);
return String(_content.asDouble, decimals);
}
String s;
printTo(s);
return s; return s;
} }
template <>
bool JsonVariant::is<bool>() 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 <> template <>
bool JsonVariant::is<signed long>() const { bool JsonVariant::is<signed long>() const {
if (_type == JSON_LONG) return true; if (_type == JSON_INTEGER) return true;
if (_type != JSON_UNPARSED || _content.asString == NULL) return false; if (_type != JSON_UNPARSED || _content.asString == NULL) return false;
@ -87,7 +111,7 @@ bool JsonVariant::is<signed long>() const {
template <> template <>
bool JsonVariant::is<double>() const { bool JsonVariant::is<double>() const {
if (_type >= JSON_DOUBLE_0_DECIMALS) return true; if (_type >= JSON_FLOAT_0_DECIMALS) return true;
if (_type != JSON_UNPARSED || _content.asString == NULL) return false; if (_type != JSON_UNPARSED || _content.asString == NULL) return false;
@ -111,15 +135,15 @@ void JsonVariant::writeTo(JsonWriter &writer) const {
else if (_type == JSON_UNPARSED) else if (_type == JSON_UNPARSED)
writer.writeRaw(_content.asString); writer.writeRaw(_content.asString);
else if (_type == JSON_LONG) else if (_type == JSON_INTEGER)
writer.writeLong(_content.asLong); writer.writeInteger(_content.asInteger);
else if (_type == JSON_BOOLEAN) else if (_type == JSON_BOOLEAN)
writer.writeBoolean(_content.asLong != 0); writer.writeBoolean(_content.asInteger != 0);
else if (_type >= JSON_DOUBLE_0_DECIMALS) { else if (_type >= JSON_FLOAT_0_DECIMALS) {
uint8_t decimals = static_cast<uint8_t>(_type - JSON_DOUBLE_0_DECIMALS); uint8_t decimals = static_cast<uint8_t>(_type - JSON_FLOAT_0_DECIMALS);
writer.writeDouble(_content.asDouble, decimals); writer.writeFloat(_content.asFloat, decimals);
} }
} }
} }

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>
@ -71,6 +72,26 @@ TEST_F(ArduinoStringTests, JsonObject_SetKeyValue) {
ASSERT_STREQ("world", object["hello"]); 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) { TEST_F(ArduinoStringTests, JsonObject_Get) {
char json[] = "{\"key\":\"value\"}"; char json[] = "{\"key\":\"value\"}";
const JsonObject &object = _jsonBuffer.parseObject(json); const JsonObject &object = _jsonBuffer.parseObject(json);

View File

@ -1,18 +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!
set(GTEST_DIR ../third-party/gtest-1.7.0) set(GTEST_DIR ../third-party/gtest-1.7.0)
file(GLOB_RECURSE INC_FILES ../include/*.h)
file(GLOB TESTS_FILES *.hpp *.cpp) file(GLOB TESTS_FILES *.hpp *.cpp)
include_directories( include_directories(
../include
${GTEST_DIR} ${GTEST_DIR}
${GTEST_DIR}/include) ${GTEST_DIR}/include)
add_definitions(-DGTEST_HAS_PTHREAD=0) 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 add_executable(ArduinoJsonTests
${TESTS_FILES} ${TESTS_FILES}
${INC_FILES}
${GTEST_DIR}/src/gtest-all.cc ${GTEST_DIR}/src/gtest-all.cc
${GTEST_DIR}/src/gtest_main.cc) ${GTEST_DIR}/src/gtest_main.cc)

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#include <gtest/gtest.h> #include <gtest/gtest.h>
@ -22,9 +23,9 @@ TEST_F(DynamicJsonBuffer_Basic_Tests, InitialSizeIsZero) {
TEST_F(DynamicJsonBuffer_Basic_Tests, SizeIncreasesAfterAlloc) { TEST_F(DynamicJsonBuffer_Basic_Tests, SizeIncreasesAfterAlloc) {
buffer.alloc(1); buffer.alloc(1);
ASSERT_EQ(1, buffer.size()); ASSERT_LE(1U, buffer.size());
buffer.alloc(1); buffer.alloc(1);
ASSERT_EQ(2, buffer.size()); ASSERT_LE(2U, buffer.size());
} }
TEST_F(DynamicJsonBuffer_Basic_Tests, ReturnDifferentPointer) { TEST_F(DynamicJsonBuffer_Basic_Tests, ReturnDifferentPointer) {
@ -32,3 +33,12 @@ TEST_F(DynamicJsonBuffer_Basic_Tests, ReturnDifferentPointer) {
void* p2 = buffer.alloc(2); void* p2 = buffer.alloc(2);
ASSERT_NE(p1, p2); 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);
}
}

View File

@ -1,23 +1,29 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>
class DynamicJsonBuffer_NoMemory_Tests : public ::testing::Test { class NoMemoryAllocator {
class NoMemoryAllocator { public:
public: void* allocate(size_t) { return NULL; }
void* allocate(size_t) { return NULL; } void deallocate(void*) {}
void deallocate(void*) {} };
};
class DynamicJsonBuffer_NoMemory_Tests : public ::testing::Test {
protected: protected:
Internals::BlockJsonBuffer<NoMemoryAllocator> _jsonBuffer; 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) { TEST_F(DynamicJsonBuffer_NoMemory_Tests, CreateArray) {
ASSERT_FALSE(_jsonBuffer.createArray().success()); ASSERT_FALSE(_jsonBuffer.createArray().success());
} }

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#include <gtest/gtest.h> #include <gtest/gtest.h>
#define ARDUINOJSON_ENABLE_STD_STREAM #define ARDUINOJSON_ENABLE_STD_STREAM

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>

16
test/Issue214.cpp Normal file
View File

@ -0,0 +1,16 @@
// Copyright Benoit Blanchon 2014-2016
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#include <ArduinoJson.h>
#include <gtest/gtest.h>
TEST(Issue214, IsBool) {
char json[] = "{\"ota\": {\"enabled\": true}}";
StaticJsonBuffer<JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(1)> jsonBuffer;
JsonObject& parsedJson = jsonBuffer.parseObject(json);
ASSERT_TRUE(parsedJson["ota"]["enabled"].is<bool>());
}

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <limits.h> // for LONG_MAX #include <limits.h> // for LONG_MAX

View File

@ -0,0 +1,91 @@
// 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 <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]);
}

View File

@ -0,0 +1,42 @@
// Copyright Benoit Blanchon 2014-2016
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#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&>());
}

View File

@ -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");
}

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>

View File

@ -1,8 +1,9 @@
// Copyright Benoit Blanchon 2014-2015 // Copyright Benoit Blanchon 2014-2016
// MIT License // MIT License
// //
// Arduino JSON library // Arduino JSON library
// https://github.com/bblanchon/ArduinoJson // https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>

View File

@ -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!
#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]);
}

View File

@ -0,0 +1,91 @@
// 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 <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]);
}

View File

@ -0,0 +1,93 @@
// 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 <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]);
}

View File

@ -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!
#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());
}

View File

@ -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"));
}

Some files were not shown because too many files have changed in this diff Show More