forked from bblanchon/ArduinoJson
Compare commits
27 Commits
Author | SHA1 | Date | |
---|---|---|---|
afd033e9c9 | |||
6ec5ba521b | |||
c8e49a7e4e | |||
dee8c8e242 | |||
576543c4b4 | |||
746f2882f7 | |||
c4cbf9d0bb | |||
49bd51b5f9 | |||
e53ae0f9bb | |||
afdd913a2f | |||
3df4efd512 | |||
91dd45c387 | |||
136ee0d576 | |||
1ea8d92cc3 | |||
20fcb99830 | |||
f3265d2111 | |||
d6e7709866 | |||
d11019d9e1 | |||
cfd924622e | |||
27c08b785d | |||
77f38e4449 | |||
16ddfbc4e0 | |||
8dea900869 | |||
f342dee2b4 | |||
1d5721f3bd | |||
3170558d6d | |||
3530aa88d6 |
105
.travis.yml
105
.travis.yml
@ -2,122 +2,109 @@ sudo: false
|
||||
language: cpp
|
||||
matrix:
|
||||
include:
|
||||
- compiler: gcc
|
||||
addons:
|
||||
- addons:
|
||||
apt:
|
||||
sources: ['ubuntu-toolchain-r-test']
|
||||
packages: ['g++-4.4']
|
||||
env: SCRIPT=test _CC=gcc-4.4 _CXX=g++-4.4
|
||||
- addons:
|
||||
apt:
|
||||
sources: ['ubuntu-toolchain-r-test']
|
||||
packages: ['g++-4.6']
|
||||
env: SCRIPT=cmake GCC=4.6
|
||||
- compiler: gcc
|
||||
addons:
|
||||
env: SCRIPT=test _CC=gcc-4.6 _CXX=g++-4.6
|
||||
- addons:
|
||||
apt:
|
||||
sources: ['ubuntu-toolchain-r-test']
|
||||
packages: ['g++-4.7']
|
||||
env: SCRIPT=cmake GCC=4.7
|
||||
- compiler: gcc
|
||||
addons:
|
||||
env: SCRIPT=test _CC=gcc-4.7 _CXX=g++-4.7
|
||||
- addons:
|
||||
apt:
|
||||
sources: ['ubuntu-toolchain-r-test']
|
||||
packages: ['g++-4.8']
|
||||
env: SCRIPT=cmake GCC=4.8 SANITIZE=address
|
||||
- compiler: gcc
|
||||
addons:
|
||||
env: SCRIPT=test _CC=gcc-4.8 _CXX=g++-4.8 SANITIZE=address
|
||||
- addons:
|
||||
apt:
|
||||
sources: ['ubuntu-toolchain-r-test']
|
||||
packages: ['g++-4.9']
|
||||
env: SCRIPT=cmake GCC=4.9 SANITIZE=leak
|
||||
- compiler: gcc
|
||||
addons:
|
||||
env: SCRIPT=test _CC=gcc-4.9 _CXX=g++-4.9 SANITIZE=leak
|
||||
- addons:
|
||||
apt:
|
||||
sources: ['ubuntu-toolchain-r-test']
|
||||
packages: ['g++-5']
|
||||
env: SCRIPT=cmake GCC=5 # SANITIZE=undefined
|
||||
- compiler: gcc
|
||||
addons:
|
||||
env: SCRIPT=test _CC=gcc-5 _CXX=g++-5 # SANITIZE=undefined
|
||||
- addons:
|
||||
apt:
|
||||
sources: ['ubuntu-toolchain-r-test']
|
||||
packages: ['g++-6']
|
||||
env: SCRIPT=cmake GCC=6
|
||||
- compiler: gcc
|
||||
addons:
|
||||
env: SCRIPT=test _CC=gcc-6 _CXX=g++-6
|
||||
- addons:
|
||||
apt:
|
||||
sources: ['ubuntu-toolchain-r-test']
|
||||
packages: ['g++-7']
|
||||
env: SCRIPT=cmake GCC=7
|
||||
- compiler: clang
|
||||
env: SCRIPT=cmake
|
||||
- compiler: clang
|
||||
addons:
|
||||
env: SCRIPT=test _CC=gcc-7 _CXX=g++-7
|
||||
- addons:
|
||||
apt:
|
||||
packages: ['g++-arm-linux-gnueabihf']
|
||||
env: SCRIPT=build _CC=arm-linux-gnueabihf-gcc _CXX=arm-linux-gnueabihf-g++
|
||||
- env: SCRIPT=test _CC=clang _CXX=clang++
|
||||
- addons:
|
||||
apt:
|
||||
sources: ['ubuntu-toolchain-r-test','llvm-toolchain-precise-3.5']
|
||||
packages: ['clang-3.5']
|
||||
env: SCRIPT=cmake CLANG=3.5 SANITIZE=address
|
||||
- compiler: clang
|
||||
addons:
|
||||
env: SCRIPT=test _CC=clang-3.5 _CXX=clang++-3.5 SANITIZE=address
|
||||
- addons:
|
||||
apt:
|
||||
sources: ['ubuntu-toolchain-r-test','llvm-toolchain-precise-3.6']
|
||||
packages: ['clang-3.6']
|
||||
env: SCRIPT=cmake CLANG=3.6 SANITIZE=leak
|
||||
- compiler: clang
|
||||
addons:
|
||||
env: SCRIPT=test _CC=clang-3.6 _CXX=clang++-3.6 SANITIZE=leak
|
||||
- addons:
|
||||
apt:
|
||||
sources: ['ubuntu-toolchain-r-test','llvm-toolchain-precise-3.7']
|
||||
packages: ['clang-3.7']
|
||||
env: SCRIPT=cmake CLANG=3.7
|
||||
- compiler: clang
|
||||
addons:
|
||||
env: SCRIPT=test _CC=clang-3.7 _CXX=clang++-3.7
|
||||
- addons:
|
||||
apt:
|
||||
sources: ['ubuntu-toolchain-r-test','llvm-toolchain-precise-3.8']
|
||||
packages: ['clang-3.8']
|
||||
env: SCRIPT=cmake CLANG=3.8 SANITIZE=undefined
|
||||
- compiler: clang
|
||||
addons:
|
||||
env: SCRIPT=test _CC=clang-3.8 _CXX=clang++-3.8 SANITIZE=undefined
|
||||
- addons:
|
||||
apt:
|
||||
sources: ['ubuntu-toolchain-r-test','llvm-toolchain-trusty-3.9']
|
||||
packages: ['clang-3.9']
|
||||
env: SCRIPT=cmake CLANG=3.9
|
||||
- compiler: clang
|
||||
addons:
|
||||
env: SCRIPT=test _CC=clang-3.9 _CXX=clang++-3.9
|
||||
- addons:
|
||||
apt:
|
||||
sources: ['ubuntu-toolchain-r-test','llvm-toolchain-trusty-4.0']
|
||||
packages: ['clang-4.0']
|
||||
env: SCRIPT=cmake CLANG=4.0
|
||||
- compiler: clang
|
||||
addons:
|
||||
env: SCRIPT=test _CC=clang-4.0 _CXX=clang++-4.0
|
||||
- addons:
|
||||
apt:
|
||||
sources: ['ubuntu-toolchain-r-test','llvm-toolchain-trusty-5.0']
|
||||
packages: ['clang-5.0']
|
||||
env: SCRIPT=cmake CLANG=5.0
|
||||
- compiler: clang
|
||||
addons:
|
||||
env: SCRIPT=test _CC=clang-5.0 _CXX=clang++-5.0
|
||||
- addons:
|
||||
apt:
|
||||
sources: ['ubuntu-toolchain-r-test','llvm-toolchain-trusty-6.0']
|
||||
packages: ['clang-6.0']
|
||||
env: SCRIPT=cmake CLANG=6.0
|
||||
- compiler: gcc
|
||||
env: SCRIPT=coverage
|
||||
env: SCRIPT=test _CC=clang-6.0 _CXX=clang++-6.0
|
||||
- env: SCRIPT=coverage
|
||||
- os: osx
|
||||
osx_image: xcode7.3
|
||||
compiler: clang
|
||||
env: SCRIPT=cmake
|
||||
env: SCRIPT=test
|
||||
- os: osx
|
||||
osx_image: xcode8.3
|
||||
compiler: clang
|
||||
env: SCRIPT=cmake
|
||||
env: SCRIPT=test
|
||||
- os: osx
|
||||
osx_image: xcode9.4
|
||||
compiler: clang
|
||||
env: SCRIPT=cmake
|
||||
env: SCRIPT=test
|
||||
- os: osx
|
||||
osx_image: xcode10
|
||||
compiler: clang
|
||||
env: SCRIPT=cmake SANITIZE=address
|
||||
env: SCRIPT=test SANITIZE=address
|
||||
- env: SCRIPT=arduino VERSION=1.6.7 BOARD=arduino:avr:uno
|
||||
- env: SCRIPT=arduino VERSION=1.8.2 BOARD=arduino:samd:mkr1000
|
||||
- env: SCRIPT=platformio BOARD=uno
|
||||
- env: SCRIPT=platformio BOARD=esp01
|
||||
- compiler: clang
|
||||
addons:
|
||||
- addons:
|
||||
apt:
|
||||
sources: ['ubuntu-toolchain-r-test','llvm-toolchain-trusty-6.0']
|
||||
packages: ['clang-6.0','llvm-6.0']
|
||||
|
22
CHANGELOG.md
22
CHANGELOG.md
@ -1,6 +1,28 @@
|
||||
ArduinoJson: change log
|
||||
=======================
|
||||
|
||||
v6.10.0 (2019-03-22)
|
||||
-------
|
||||
|
||||
* Fixed an integer overflow in the JSON deserializer
|
||||
* Added overflow handling in `JsonVariant::as<T>()` and `JsonVariant::is<T>()`.
|
||||
- `as<T>()` returns `0` if the integer `T` overflows
|
||||
- `is<T>()` returns `false` if the integer `T` overflows
|
||||
* Added `BasicJsonDocument` to support custom allocator (issue #876)
|
||||
* Added `JsonDocument::containsKey()` (issue #938)
|
||||
* Added `JsonVariant::containsKey()`
|
||||
|
||||
v6.9.1 (2019-03-01)
|
||||
------
|
||||
|
||||
* Fixed warning "unused variable" with GCC 4.4 (issue #912)
|
||||
* Fixed warning "cast increases required alignment" (issue #914)
|
||||
* Fixed warning "conversion may alter value" (issue #914)
|
||||
* Fixed naming conflict with "CAPACITY" (issue #839)
|
||||
* Muted warning "will change in GCC 7.1" (issue #914)
|
||||
* Added a clear error message for `StaticJsonBuffer` and `DynamicJsonBuffer`
|
||||
* Marked ArduinoJson.h as a "system header"
|
||||
|
||||
v6.9.0 (2019-02-26)
|
||||
------
|
||||
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
---
|
||||
|
||||
[](https://www.ardu-badge.com/ArduinoJson/6.9.0)
|
||||
[](https://www.ardu-badge.com/ArduinoJson/6.10.0)
|
||||
[](https://ci.appveyor.com/project/bblanchon/arduinojson/branch/6.x)
|
||||
[](https://travis-ci.org/bblanchon/ArduinoJson)
|
||||
[](https://coveralls.io/github/bblanchon/ArduinoJson?branch=6.x)
|
||||
|
@ -1,4 +1,4 @@
|
||||
version: 6.4.0.{build}
|
||||
version: 6.9.1.{build}
|
||||
environment:
|
||||
matrix:
|
||||
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017
|
||||
|
@ -10,6 +10,8 @@
|
||||
// "hostname": "examples.com",
|
||||
// "port": 2731
|
||||
// }
|
||||
//
|
||||
// https://arduinojson.org/v6/example/config/
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <SD.h>
|
||||
@ -130,4 +132,15 @@ void loop() {
|
||||
// not used in this example
|
||||
}
|
||||
|
||||
// Visit https://arduinojson.org/v6/example/config/ for more.
|
||||
// See also
|
||||
// --------
|
||||
//
|
||||
// https://arduinojson.org/ contains the documentation for all the functions
|
||||
// used above. It also includes an FAQ that will help you solve any
|
||||
// serialization or deserialization problem.
|
||||
//
|
||||
// The book "Mastering ArduinoJson" contains a case study of a project that has
|
||||
// a complex configuration with nested members.
|
||||
// Contrary to this example, the project in the book uses the SPIFFS filesystem.
|
||||
// Learn more at https://arduinojson.org/book/
|
||||
// Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤
|
||||
|
@ -3,6 +3,8 @@
|
||||
// MIT License
|
||||
//
|
||||
// This example shows how to generate a JSON document with ArduinoJson.
|
||||
//
|
||||
// https://arduinojson.org/v6/example/generator/
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
@ -61,4 +63,15 @@ void loop() {
|
||||
// not used in this example
|
||||
}
|
||||
|
||||
// Visit https://arduinojson.org/v6/example/generator/ for more.
|
||||
// See also
|
||||
// --------
|
||||
//
|
||||
// https://arduinojson.org/ contains the documentation for all the functions
|
||||
// used above. It also includes an FAQ that will help you solve any
|
||||
// serialization problem.
|
||||
//
|
||||
// The book "Mastering ArduinoJson" contains a tutorial on serialization.
|
||||
// It begins with a simple example, like the one above, and then adds more
|
||||
// features like serializing directly to a file or an HTTP request.
|
||||
// Learn more at https://arduinojson.org/book/
|
||||
// Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤
|
||||
|
@ -15,6 +15,8 @@
|
||||
// 2.302038
|
||||
// ]
|
||||
// }
|
||||
//
|
||||
// https://arduinojson.org/v6/example/http-client/
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <Ethernet.h>
|
||||
@ -98,4 +100,16 @@ void loop() {
|
||||
// not used in this example
|
||||
}
|
||||
|
||||
// Visit https://arduinojson.org/v6/example/http-client/ for more.
|
||||
// See also
|
||||
// --------
|
||||
//
|
||||
// https://arduinojson.org/ contains the documentation for all the functions
|
||||
// used above. It also includes an FAQ that will help you solve any
|
||||
// serialization problem.
|
||||
//
|
||||
// The book "Mastering ArduinoJson" contains a tutorial on deserialization
|
||||
// showing how to parse the response from GitHub's API. In the last chapter,
|
||||
// it shows how to parse the huge documents from OpenWeatherMap
|
||||
// and Reddit.
|
||||
// Learn more at https://arduinojson.org/book/
|
||||
// Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤
|
||||
|
@ -3,6 +3,8 @@
|
||||
// MIT License
|
||||
//
|
||||
// This example shows how to deserialize a JSON document with ArduinoJson.
|
||||
//
|
||||
// https://arduinojson.org/v6/example/parser/
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
@ -64,4 +66,15 @@ void loop() {
|
||||
// not used in this example
|
||||
}
|
||||
|
||||
// Visit https://arduinojson.org/v6/example/parser/ for more.
|
||||
// See also
|
||||
// --------
|
||||
//
|
||||
// https://arduinojson.org/ contains the documentation for all the functions
|
||||
// used above. It also includes an FAQ that will help you solve any
|
||||
// deserialization problem.
|
||||
//
|
||||
// The book "Mastering ArduinoJson" contains a tutorial on deserialization.
|
||||
// It begins with a simple example, like the one above, and then adds more
|
||||
// features like deserializing directly from a file or an HTTP request.
|
||||
// Learn more at https://arduinojson.org/book/
|
||||
// Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤
|
||||
|
@ -12,6 +12,8 @@
|
||||
// "analog": [0, 76, 123, 158, 192, 205],
|
||||
// "digital": [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0]
|
||||
// }
|
||||
//
|
||||
// https://arduinojson.org/v6/example/http-server/
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <Ethernet.h>
|
||||
@ -94,4 +96,15 @@ void loop() {
|
||||
client.stop();
|
||||
}
|
||||
|
||||
// Visit https://arduinojson.org/v6/example/http-server/ for more.
|
||||
// See also
|
||||
// --------
|
||||
//
|
||||
// https://arduinojson.org/ contains the documentation for all the functions
|
||||
// used above. It also includes an FAQ that will help you solve any
|
||||
// serialization problem.
|
||||
//
|
||||
// The book "Mastering ArduinoJson" contains a tutorial on serialization.
|
||||
// It begins with a simple example, then adds more features like serializing
|
||||
// directly to a file or an HTTP client.
|
||||
// Learn more at https://arduinojson.org/book/
|
||||
// Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤
|
||||
|
@ -16,6 +16,8 @@
|
||||
// For example, you can run netcat on your computer
|
||||
// $ ncat -ulp 8888
|
||||
// See https://nmap.org/ncat/
|
||||
//
|
||||
// https://arduinojson.org/v6/example/udp-beacon/
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <Ethernet.h>
|
||||
@ -84,4 +86,15 @@ void loop() {
|
||||
delay(10000);
|
||||
}
|
||||
|
||||
// Visit https://arduinojson.org/v6/example/udp-beacon/ for more.
|
||||
// See also
|
||||
// --------
|
||||
//
|
||||
// https://arduinojson.org/ contains the documentation for all the functions
|
||||
// used above. It also includes an FAQ that will help you solve any
|
||||
// serialization problem.
|
||||
//
|
||||
// The book "Mastering ArduinoJson" contains a tutorial on serialization.
|
||||
// It begins with a simple example, then adds more features like serializing
|
||||
// directly to a file or any stream.
|
||||
// Learn more at https://arduinojson.org/book/
|
||||
// Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤
|
||||
|
@ -4,6 +4,8 @@
|
||||
//
|
||||
// This example shows how to deserialize a MessagePack document with
|
||||
// ArduinoJson.
|
||||
//
|
||||
// https://arduinojson.org/v6/example/msgpack-parser/
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
@ -71,5 +73,3 @@ void setup() {
|
||||
void loop() {
|
||||
// not used in this example
|
||||
}
|
||||
|
||||
// Visit https://arduinojson.org/v6/example/msgpack-parser/ for more.
|
||||
|
@ -8,6 +8,8 @@
|
||||
// Use Flash strings sparingly, because ArduinoJson duplicates them in the
|
||||
// JsonDocument. Prefer plain old char*, as they are more efficient in term of
|
||||
// code size, speed, and memory usage.
|
||||
//
|
||||
// https://arduinojson.org/v6/example/progmem/
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
@ -56,4 +58,15 @@ void loop() {
|
||||
// not used in this example
|
||||
}
|
||||
|
||||
// Visit https://arduinojson.org/v6/example/progmem/ for more.
|
||||
// See also
|
||||
// --------
|
||||
//
|
||||
// https://arduinojson.org/ contains the documentation for all the functions
|
||||
// used above. It also includes an FAQ that will help you solve any memory
|
||||
// problem.
|
||||
//
|
||||
// The book "Mastering ArduinoJson" contains a quick C++ course that explains
|
||||
// how your microcontroller stores strings in memory. It also tells why you
|
||||
// should not abuse Flash strings with ArduinoJson.
|
||||
// Learn more at https://arduinojson.org/book/
|
||||
// Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤
|
||||
|
@ -7,6 +7,8 @@
|
||||
// Use String objects sparingly, because ArduinoJson duplicates them in the
|
||||
// JsonDocument. Prefer plain old char[], as they are more efficient in term of
|
||||
// code size, speed, and memory usage.
|
||||
//
|
||||
// https://arduinojson.org/v6/example/string/
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
@ -62,4 +64,14 @@ void loop() {
|
||||
// not used in this example
|
||||
}
|
||||
|
||||
// Visit https://arduinojson.org/v6/example/string/ for more.
|
||||
// See also
|
||||
// --------
|
||||
//
|
||||
// https://arduinojson.org/ contains the documentation for all the functions
|
||||
// used above. It also includes an FAQ that will help you solve any problem.
|
||||
//
|
||||
// The book "Mastering ArduinoJson" contains a quick C++ course that explains
|
||||
// how your microcontroller stores strings in memory. On several occasions, it
|
||||
// shows how you can avoid String in your program.
|
||||
// Learn more at https://arduinojson.org/book/
|
||||
// Use the coupon code TWENTY for a 20% discount ❤❤❤❤❤
|
||||
|
1
fuzzing/json_seed_corpus/IntegerOverflow.json
Normal file
1
fuzzing/json_seed_corpus/IntegerOverflow.json
Normal file
@ -0,0 +1 @@
|
||||
9720730739393920739
|
@ -7,7 +7,7 @@
|
||||
"type": "git",
|
||||
"url": "https://github.com/bblanchon/ArduinoJson.git"
|
||||
},
|
||||
"version": "6.9.0",
|
||||
"version": "6.10.0",
|
||||
"authors": {
|
||||
"name": "Benoit Blanchon",
|
||||
"url": "https://blog.benoitblanchon.fr"
|
||||
|
@ -1,5 +1,5 @@
|
||||
name=ArduinoJson
|
||||
version=6.9.0
|
||||
version=6.10.0
|
||||
author=Benoit Blanchon <blog.benoitblanchon.fr>
|
||||
maintainer=Benoit Blanchon <blog.benoitblanchon.fr>
|
||||
sentence=An efficient and elegant JSON library for Arduino.
|
||||
|
@ -18,12 +18,16 @@ update_version_in_source () {
|
||||
|
||||
sed -i~ -bE "4s/HEAD/$TAG ($DATE)/; 5s/-+/$UNDERLINE/" CHANGELOG.md
|
||||
rm CHANGELOG.md*~
|
||||
|
||||
sed -i~ -bE "s/\"version\":.*$/\"version\": \"$VERSION\",/" library.json
|
||||
rm library.json*~
|
||||
|
||||
sed -i~ -bE "s/version=.*$/version=$VERSION/" library.properties
|
||||
rm library.properties*~
|
||||
|
||||
sed -i~ -bE "s/version: .*$/version: $VERSION.{build}/" appveyor.yml
|
||||
rm appveyor.yml*~
|
||||
|
||||
sed -i~ -bE \
|
||||
-e "s/ARDUINOJSON_VERSION .*$/ARDUINOJSON_VERSION \"$VERSION\"/" \
|
||||
-e "s/ARDUINOJSON_VERSION_MAJOR .*$/ARDUINOJSON_VERSION_MAJOR $MAJOR/" \
|
||||
|
14
scripts/travis/build.sh
Executable file
14
scripts/travis/build.sh
Executable file
@ -0,0 +1,14 @@
|
||||
#!/bin/sh -ex
|
||||
|
||||
export CC="$_CC"
|
||||
export CXX="$_CXX"
|
||||
|
||||
if [ -n "$SANITIZE" ]; then
|
||||
export CXXFLAGS="-fsanitize=$SANITIZE"
|
||||
BUILD_TYPE="Debug"
|
||||
else
|
||||
BUILD_TYPE="Release"
|
||||
fi
|
||||
|
||||
cmake -DCMAKE_BUILD_TYPE=$BUILD_TYPE .
|
||||
cmake --build .
|
@ -1,30 +0,0 @@
|
||||
#!/bin/sh -ex
|
||||
|
||||
if [ $(uname) = 'Darwin' ]; then
|
||||
URL=https://cmake.org/files/v3.4/cmake-3.4.3-Darwin-x86_64.tar.gz
|
||||
CMAKE=/tmp/CMake.app/Contents/bin/cmake
|
||||
CTEST=/tmp/CMake.app/Contents/bin/ctest
|
||||
else
|
||||
URL=https://cmake.org/files/v3.4/cmake-3.4.3-Linux-x86_64.tar.gz
|
||||
CMAKE=/tmp/bin/cmake
|
||||
CTEST=/tmp/bin/ctest
|
||||
fi
|
||||
curl -sS $URL | tar xz -C /tmp --strip 1
|
||||
|
||||
if [ -n "$GCC" ]; then
|
||||
export CC="gcc-$GCC"
|
||||
export CXX="g++-$GCC"
|
||||
fi
|
||||
|
||||
if [ -n "$CLANG" ]; then
|
||||
export CC="clang-$CLANG"
|
||||
export CXX="clang++-$CLANG"
|
||||
fi
|
||||
|
||||
if [ -n "$SANITIZE" ]; then
|
||||
export CXXFLAGS="-fsanitize=$SANITIZE"
|
||||
fi
|
||||
|
||||
$CMAKE .
|
||||
$CMAKE --build .
|
||||
$CTEST --output-on-failure .
|
@ -1,8 +1,6 @@
|
||||
#!/bin/sh -eux
|
||||
|
||||
curl https://cmake.org/files/v3.4/cmake-3.4.0-Linux-x86_64.tar.gz | tar xz -C /tmp --strip 1
|
||||
|
||||
/tmp/bin/cmake -DCOVERAGE=true .
|
||||
cmake -DCOVERAGE=true .
|
||||
make
|
||||
make test
|
||||
|
||||
|
@ -3,7 +3,7 @@
|
||||
ROOT_DIR=$(dirname $0)/../../
|
||||
INCLUDE_DIR=${ROOT_DIR}/src/
|
||||
FUZZING_DIR=${ROOT_DIR}/fuzzing/
|
||||
CXXFLAGS="-g -fprofile-instr-generate -fcoverage-mapping -fsanitize=address,fuzzer"
|
||||
CXXFLAGS="-g -fprofile-instr-generate -fcoverage-mapping -fsanitize=address,undefined,fuzzer -fno-sanitize-recover=all"
|
||||
|
||||
fuzz() {
|
||||
NAME="$1"
|
||||
|
4
scripts/travis/test.sh
Executable file
4
scripts/travis/test.sh
Executable file
@ -0,0 +1,4 @@
|
||||
#!/bin/sh -ex
|
||||
|
||||
"$(dirname "$0")/build.sh"
|
||||
ctest --output-on-failure .
|
@ -8,11 +8,11 @@
|
||||
#include "ArduinoJson.h"
|
||||
|
||||
int main() {
|
||||
// The JSON document
|
||||
// Allocate the JSON document
|
||||
//
|
||||
// Inside the brackets, 200 is the RAM allocated to this document.
|
||||
// Don't forget to change this value to match your requirement.
|
||||
// Use arduinojson.org/assistant to compute the capacity.
|
||||
// Use arduinojson.org/v6/assistant to compute the capacity.
|
||||
StaticJsonDocument<200> doc;
|
||||
|
||||
// StaticJsonObject allocates memory on the stack, it can be
|
||||
@ -20,30 +20,35 @@ int main() {
|
||||
//
|
||||
// DynamicJsonDocument doc(200);
|
||||
|
||||
// Make our document be an object
|
||||
JsonObject root = doc.to<JsonObject>();
|
||||
|
||||
// Add values in the object
|
||||
// StaticJsonObject allocates memory on the stack, it can be
|
||||
// replaced by DynamicJsonDocument which allocates in the heap.
|
||||
//
|
||||
// Most of the time, you can rely on the implicit casts.
|
||||
// In other case, you can do root.set<long>("time", 1351824120);
|
||||
root["sensor"] = "gps";
|
||||
root["time"] = 1351824120;
|
||||
// DynamicJsonDocument doc(200);
|
||||
|
||||
// Add values in the document
|
||||
//
|
||||
doc["sensor"] = "gps";
|
||||
doc["time"] = 1351824120;
|
||||
|
||||
// Add an array.
|
||||
//
|
||||
JsonArray data = root.createNestedArray("data");
|
||||
JsonArray data = doc.createNestedArray("data");
|
||||
data.add(48.756080);
|
||||
data.add(2.302038);
|
||||
|
||||
serializeJson(root, std::cout);
|
||||
// This prints:
|
||||
// Generate the minified JSON and send it to STDOUT
|
||||
//
|
||||
serializeJson(doc, std::cout);
|
||||
// The above line prints:
|
||||
// {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]}
|
||||
|
||||
// Start a new line
|
||||
std::cout << std::endl;
|
||||
|
||||
serializeJsonPretty(root, std::cout);
|
||||
// This prints:
|
||||
// Generate the prettified JSON and send it to STDOUT
|
||||
//
|
||||
serializeJsonPretty(doc, std::cout);
|
||||
// The above line prints:
|
||||
// {
|
||||
// "sensor": "gps",
|
||||
// "time": 1351824120,
|
||||
@ -52,6 +57,4 @@ int main() {
|
||||
// 2.302038
|
||||
// ]
|
||||
// }
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -8,23 +8,26 @@
|
||||
#include "ArduinoJson.h"
|
||||
|
||||
int main() {
|
||||
// Root JSON object
|
||||
// Allocate the JSON document
|
||||
//
|
||||
// Inside the brackets, 300 is the size of the memory pool in bytes.
|
||||
// Inside the brackets, 200 is the capacity of the memory pool in bytes.
|
||||
// Don't forget to change this value to match your JSON document.
|
||||
// Use arduinojson.org/assistant to compute the capacity.
|
||||
// Use arduinojson.org/v6/assistant to compute the capacity.
|
||||
StaticJsonDocument<300> doc;
|
||||
|
||||
// StaticJsonDocument<N> allocates memory on the stack, it can be
|
||||
// replaced by DynamicJsonObject which allocates in the heap.
|
||||
// replaced by DynamicJsonDocument which allocates in the heap.
|
||||
//
|
||||
// DynamicJsonObject doc(200);
|
||||
// DynamicJsonDocument doc(200);
|
||||
|
||||
// JSON input string.
|
||||
//
|
||||
// It's better to use a char[] as shown here.
|
||||
// If you use a const char* or a String, ArduinoJson will
|
||||
// have to make a copy of the input in the JsonBuffer.
|
||||
// Using a char[], as shown here, enables the "zero-copy" mode. This mode uses
|
||||
// the minimal amount of memory because the JsonDocument stores pointers to
|
||||
// the input buffer.
|
||||
// If you use another type of input, ArduinoJson must copy the strings from
|
||||
// the input to the JsonDocument, so you need to increase the capacity of the
|
||||
// JsonDocument.
|
||||
char json[] =
|
||||
"{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
|
||||
|
||||
@ -37,17 +40,14 @@ int main() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Get the root object in the document
|
||||
JsonObject root = doc.as<JsonObject>();
|
||||
|
||||
// Fetch values.
|
||||
//
|
||||
// Most of the time, you can rely on the implicit casts.
|
||||
// In other case, you can do doc["time"].as<long>();
|
||||
const char* sensor = root["sensor"];
|
||||
long time = root["time"];
|
||||
double latitude = root["data"][0];
|
||||
double longitude = root["data"][1];
|
||||
const char* sensor = doc["sensor"];
|
||||
long time = doc["time"];
|
||||
double latitude = doc["data"][0];
|
||||
double longitude = doc["data"][1];
|
||||
|
||||
// Print values.
|
||||
std::cout << sensor << std::endl;
|
||||
|
@ -49,17 +49,14 @@ int main() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Get the root object in the document
|
||||
JsonObject root = doc.as<JsonObject>();
|
||||
|
||||
// Fetch values.
|
||||
//
|
||||
// Most of the time, you can rely on the implicit casts.
|
||||
// In other case, you can do doc["time"].as<long>();
|
||||
const char* sensor = root["sensor"];
|
||||
long time = root["time"];
|
||||
double latitude = root["data"][0];
|
||||
double longitude = root["data"][1];
|
||||
const char* sensor = doc["sensor"];
|
||||
long time = doc["time"];
|
||||
double latitude = doc["data"][0];
|
||||
double longitude = doc["data"][1];
|
||||
|
||||
// Print values.
|
||||
std::cout << sensor << std::endl;
|
||||
|
@ -4,6 +4,14 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifndef ARDUINOJSON_DEBUG
|
||||
#ifdef __clang__
|
||||
#pragma clang system_header
|
||||
#elif defined __GNUC__
|
||||
#pragma GCC system_header
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#include "ArduinoJson/Namespace.hpp"
|
||||
|
||||
#include "ArduinoJson/Array/ArrayRef.hpp"
|
||||
@ -28,6 +36,8 @@
|
||||
#include "ArduinoJson/MsgPack/MsgPackDeserializer.hpp"
|
||||
#include "ArduinoJson/MsgPack/MsgPackSerializer.hpp"
|
||||
|
||||
#include "ArduinoJson/compatibility.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
typedef ARDUINOJSON_NAMESPACE::ArrayConstRef JsonArrayConst;
|
||||
typedef ARDUINOJSON_NAMESPACE::ArrayRef JsonArray;
|
||||
@ -40,6 +50,7 @@ typedef ARDUINOJSON_NAMESPACE::String JsonString;
|
||||
typedef ARDUINOJSON_NAMESPACE::UInt JsonUInt;
|
||||
typedef ARDUINOJSON_NAMESPACE::VariantConstRef JsonVariantConst;
|
||||
typedef ARDUINOJSON_NAMESPACE::VariantRef JsonVariant;
|
||||
using ARDUINOJSON_NAMESPACE::BasicJsonDocument;
|
||||
using ARDUINOJSON_NAMESPACE::copyArray;
|
||||
using ARDUINOJSON_NAMESPACE::DeserializationError;
|
||||
using ARDUINOJSON_NAMESPACE::deserializeJson;
|
||||
|
@ -23,7 +23,8 @@ template <typename TData>
|
||||
class ArrayRefBase {
|
||||
public:
|
||||
operator VariantConstRef() const {
|
||||
return VariantConstRef(reinterpret_cast<const VariantData*>(_data));
|
||||
const void* data = _data; // prevent warning cast-align
|
||||
return VariantConstRef(reinterpret_cast<const VariantData*>(data));
|
||||
}
|
||||
|
||||
template <typename Visitor>
|
||||
@ -77,6 +78,10 @@ class ArrayConstRef : public ArrayRefBase<const CollectionData>,
|
||||
}
|
||||
|
||||
FORCE_INLINE VariantConstRef operator[](size_t index) const {
|
||||
return getElement(index);
|
||||
}
|
||||
|
||||
FORCE_INLINE VariantConstRef getElement(size_t index) const {
|
||||
return VariantConstRef(_data ? _data->get(index) : 0);
|
||||
}
|
||||
};
|
||||
@ -94,7 +99,8 @@ class ArrayRef : public ArrayRefBase<CollectionData>,
|
||||
: base_type(data), _pool(pool) {}
|
||||
|
||||
operator VariantRef() {
|
||||
return VariantRef(_pool, reinterpret_cast<VariantData*>(_data));
|
||||
void* data = _data; // prevent warning cast-align
|
||||
return VariantRef(_pool, reinterpret_cast<VariantData*>(data));
|
||||
}
|
||||
|
||||
operator ArrayConstRef() const {
|
||||
|
@ -26,7 +26,7 @@ inline VariantSlot* CollectionData::addSlot(MemoryPool* pool) {
|
||||
}
|
||||
|
||||
inline VariantData* CollectionData::add(MemoryPool* pool) {
|
||||
return addSlot(pool)->data();
|
||||
return slotData(addSlot(pool));
|
||||
}
|
||||
|
||||
template <typename TAdaptedString>
|
||||
|
@ -139,7 +139,7 @@
|
||||
#ifndef ARDUINOJSON_LITTLE_ENDIAN
|
||||
#if defined(_MSC_VER) || \
|
||||
(defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) || \
|
||||
(defined(__LITTLE_ENDIAN__))
|
||||
defined(__LITTLE_ENDIAN__) || defined(__i386) || defined(__x86_64)
|
||||
#define ARDUINOJSON_LITTLE_ENDIAN 1
|
||||
#else
|
||||
#define ARDUINOJSON_LITTLE_ENDIAN 0
|
||||
|
89
src/ArduinoJson/Document/BasicJsonDocument.hpp
Normal file
89
src/ArduinoJson/Document/BasicJsonDocument.hpp
Normal file
@ -0,0 +1,89 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2019
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "JsonDocument.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
template <typename TAllocator>
|
||||
class AllocatorOwner {
|
||||
protected:
|
||||
AllocatorOwner() {}
|
||||
AllocatorOwner(const AllocatorOwner& src) : _allocator(src._allocator) {}
|
||||
AllocatorOwner(TAllocator allocator) : _allocator(allocator) {}
|
||||
|
||||
void* allocate(size_t n) {
|
||||
return _allocator.allocate(n);
|
||||
}
|
||||
|
||||
void deallocate(void* p) {
|
||||
_allocator.deallocate(p);
|
||||
}
|
||||
|
||||
private:
|
||||
TAllocator _allocator;
|
||||
};
|
||||
|
||||
template <typename TAllocator>
|
||||
class BasicJsonDocument : AllocatorOwner<TAllocator>, public JsonDocument {
|
||||
public:
|
||||
explicit BasicJsonDocument(size_t capa, TAllocator allocator = TAllocator())
|
||||
: AllocatorOwner<TAllocator>(allocator), JsonDocument(allocPool(capa)) {}
|
||||
|
||||
BasicJsonDocument(const BasicJsonDocument& src)
|
||||
: AllocatorOwner<TAllocator>(src),
|
||||
JsonDocument(allocPool(src.memoryUsage())) {
|
||||
set(src);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
BasicJsonDocument(const T& src,
|
||||
typename enable_if<IsVisitable<T>::value>::type* = 0)
|
||||
: JsonDocument(allocPool(src.memoryUsage())) {
|
||||
set(src);
|
||||
}
|
||||
|
||||
// disambiguate
|
||||
BasicJsonDocument(VariantRef src)
|
||||
: JsonDocument(allocPool(src.memoryUsage())) {
|
||||
set(src);
|
||||
}
|
||||
|
||||
~BasicJsonDocument() {
|
||||
freePool();
|
||||
}
|
||||
|
||||
BasicJsonDocument& operator=(const BasicJsonDocument& src) {
|
||||
reallocPoolIfTooSmall(src.memoryUsage());
|
||||
set(src);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
BasicJsonDocument& operator=(const T& src) {
|
||||
reallocPoolIfTooSmall(src.memoryUsage());
|
||||
set(src);
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
MemoryPool allocPool(size_t requiredSize) {
|
||||
size_t capa = addPadding(requiredSize);
|
||||
return MemoryPool(reinterpret_cast<char*>(this->allocate(capa)), capa);
|
||||
}
|
||||
|
||||
void reallocPoolIfTooSmall(size_t requiredSize) {
|
||||
if (requiredSize <= capacity()) return;
|
||||
freePool();
|
||||
replacePool(allocPool(addPadding(requiredSize)));
|
||||
}
|
||||
|
||||
void freePool() {
|
||||
this->deallocate(memoryPool().buffer());
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
@ -4,66 +4,22 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "JsonDocument.hpp"
|
||||
#include "BasicJsonDocument.hpp"
|
||||
|
||||
#include <stdlib.h> // malloc, free
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
class DynamicJsonDocument : public JsonDocument {
|
||||
public:
|
||||
explicit DynamicJsonDocument(size_t capa) : JsonDocument(allocPool(capa)) {}
|
||||
|
||||
DynamicJsonDocument(const DynamicJsonDocument& src)
|
||||
: JsonDocument(allocPool(src.memoryUsage())) {
|
||||
set(src);
|
||||
struct DefaultAllocator {
|
||||
void* allocate(size_t n) {
|
||||
return malloc(n);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
DynamicJsonDocument(const T& src,
|
||||
typename enable_if<IsVisitable<T>::value>::type* = 0)
|
||||
: JsonDocument(allocPool(src.memoryUsage())) {
|
||||
set(src);
|
||||
}
|
||||
|
||||
// disambiguate
|
||||
DynamicJsonDocument(VariantRef src)
|
||||
: JsonDocument(allocPool(src.memoryUsage())) {
|
||||
set(src);
|
||||
}
|
||||
|
||||
~DynamicJsonDocument() {
|
||||
freePool();
|
||||
}
|
||||
|
||||
DynamicJsonDocument& operator=(const DynamicJsonDocument& src) {
|
||||
reallocPoolIfTooSmall(src.memoryUsage());
|
||||
set(src);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
DynamicJsonDocument& operator=(const T& src) {
|
||||
reallocPoolIfTooSmall(src.memoryUsage());
|
||||
set(src);
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
MemoryPool allocPool(size_t requiredSize) {
|
||||
size_t capa = addPadding(requiredSize);
|
||||
return MemoryPool(reinterpret_cast<char*>(malloc(capa)), capa);
|
||||
}
|
||||
|
||||
void reallocPoolIfTooSmall(size_t requiredSize) {
|
||||
if (requiredSize <= capacity()) return;
|
||||
freePool();
|
||||
replacePool(allocPool(addPadding(requiredSize)));
|
||||
}
|
||||
|
||||
void freePool() {
|
||||
free(memoryPool().buffer());
|
||||
void deallocate(void* p) {
|
||||
free(p);
|
||||
}
|
||||
};
|
||||
|
||||
typedef BasicJsonDocument<DefaultAllocator> DynamicJsonDocument;
|
||||
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
||||
|
@ -124,6 +124,21 @@ class JsonDocument : public Visitable {
|
||||
return getOrAddMember(key).template to<ObjectRef>();
|
||||
}
|
||||
|
||||
// containsKey(char*) const
|
||||
// containsKey(const char*) const
|
||||
// containsKey(const __FlashStringHelper*) const
|
||||
template <typename TChar>
|
||||
bool containsKey(TChar* key) const {
|
||||
return !getMember(key).isUndefined();
|
||||
}
|
||||
|
||||
// containsKey(const std::string&) const
|
||||
// containsKey(const String&) const
|
||||
template <typename TString>
|
||||
bool containsKey(const TString& key) const {
|
||||
return !getMember(key).isUndefined();
|
||||
}
|
||||
|
||||
// operator[](const std::string&)
|
||||
// operator[](const String&)
|
||||
template <typename TString>
|
||||
@ -150,7 +165,7 @@ class JsonDocument : public Visitable {
|
||||
FORCE_INLINE
|
||||
typename enable_if<IsString<TString>::value, VariantConstRef>::type
|
||||
operator[](const TString& key) const {
|
||||
return getVariant()[key];
|
||||
return getMember(key);
|
||||
}
|
||||
|
||||
// operator[](char*) const
|
||||
@ -160,7 +175,7 @@ class JsonDocument : public Visitable {
|
||||
FORCE_INLINE
|
||||
typename enable_if<IsString<TChar*>::value, VariantConstRef>::type
|
||||
operator[](TChar* key) const {
|
||||
return getVariant()[key];
|
||||
return getMember(key);
|
||||
}
|
||||
|
||||
FORCE_INLINE ElementProxy<JsonDocument&> operator[](size_t index) {
|
||||
@ -168,23 +183,44 @@ class JsonDocument : public Visitable {
|
||||
}
|
||||
|
||||
FORCE_INLINE VariantConstRef operator[](size_t index) const {
|
||||
return VariantConstRef(_data.getElement(index));
|
||||
return getElement(index);
|
||||
}
|
||||
|
||||
FORCE_INLINE VariantRef getElement(size_t index) {
|
||||
return VariantRef(&_pool, _data.getElement(index));
|
||||
}
|
||||
|
||||
// getMember(char*) const
|
||||
// getMember(const char*) const
|
||||
// getMember(const __FlashStringHelper*) const
|
||||
FORCE_INLINE VariantConstRef getElement(size_t index) const {
|
||||
return VariantConstRef(_data.getElement(index));
|
||||
}
|
||||
|
||||
// JsonVariantConst getMember(char*) const
|
||||
// JsonVariantConst getMember(const char*) const
|
||||
// JsonVariantConst getMember(const __FlashStringHelper*) const
|
||||
template <typename TChar>
|
||||
FORCE_INLINE VariantConstRef getMember(TChar* key) const {
|
||||
return VariantConstRef(_data.getMember(adaptString(key)));
|
||||
}
|
||||
|
||||
// JsonVariantConst getMember(const std::string&) const
|
||||
// JsonVariantConst getMember(const String&) const
|
||||
template <typename TString>
|
||||
FORCE_INLINE
|
||||
typename enable_if<IsString<TString>::value, VariantConstRef>::type
|
||||
getMember(const TString& key) const {
|
||||
return VariantConstRef(_data.getMember(adaptString(key)));
|
||||
}
|
||||
|
||||
// JsonVariant getMember(char*)
|
||||
// JsonVariant getMember(const char*)
|
||||
// JsonVariant getMember(const __FlashStringHelper*)
|
||||
template <typename TChar>
|
||||
FORCE_INLINE VariantRef getMember(TChar* key) {
|
||||
return VariantRef(&_pool, _data.getMember(adaptString(key)));
|
||||
}
|
||||
|
||||
// getMember(const std::string&) const
|
||||
// getMember(const String&) const
|
||||
// JsonVariant getMember(const std::string&)
|
||||
// JsonVariant getMember(const String&)
|
||||
template <typename TString>
|
||||
FORCE_INLINE typename enable_if<IsString<TString>::value, VariantRef>::type
|
||||
getMember(const TString& key) {
|
||||
|
@ -8,28 +8,28 @@
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
template <size_t CAPACITY>
|
||||
template <size_t desiredCapacity>
|
||||
class StaticJsonDocument : public JsonDocument {
|
||||
static const size_t ACTUAL_CAPACITY =
|
||||
AddPadding<Max<1, CAPACITY>::value>::value;
|
||||
static const size_t _capacity =
|
||||
AddPadding<Max<1, desiredCapacity>::value>::value;
|
||||
|
||||
public:
|
||||
StaticJsonDocument() : JsonDocument(_buffer, ACTUAL_CAPACITY) {}
|
||||
StaticJsonDocument() : JsonDocument(_buffer, _capacity) {}
|
||||
|
||||
StaticJsonDocument(const StaticJsonDocument& src)
|
||||
: JsonDocument(_buffer, ACTUAL_CAPACITY) {
|
||||
: JsonDocument(_buffer, _capacity) {
|
||||
set(src);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
StaticJsonDocument(const T& src,
|
||||
typename enable_if<IsVisitable<T>::value>::type* = 0)
|
||||
: JsonDocument(_buffer, ACTUAL_CAPACITY) {
|
||||
: JsonDocument(_buffer, _capacity) {
|
||||
set(src);
|
||||
}
|
||||
|
||||
// disambiguate
|
||||
StaticJsonDocument(VariantRef src) : JsonDocument(_buffer, ACTUAL_CAPACITY) {
|
||||
StaticJsonDocument(VariantRef src) : JsonDocument(_buffer, _capacity) {
|
||||
set(src);
|
||||
}
|
||||
|
||||
@ -45,7 +45,7 @@ class StaticJsonDocument : public JsonDocument {
|
||||
}
|
||||
|
||||
private:
|
||||
char _buffer[ACTUAL_CAPACITY];
|
||||
char _buffer[_capacity];
|
||||
};
|
||||
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
||||
|
@ -6,8 +6,7 @@
|
||||
|
||||
#include "../Deserialization/deserialize.hpp"
|
||||
#include "../Memory/MemoryPool.hpp"
|
||||
#include "../Numbers/isFloat.hpp"
|
||||
#include "../Numbers/isInteger.hpp"
|
||||
#include "../Numbers/parseNumber.hpp"
|
||||
#include "../Polyfills/type_traits.hpp"
|
||||
#include "../Variant/VariantData.hpp"
|
||||
#include "EscapeSequence.hpp"
|
||||
@ -251,14 +250,6 @@ class JsonDeserializer {
|
||||
}
|
||||
buffer[n] = 0;
|
||||
|
||||
if (isInteger(buffer)) {
|
||||
result.setInteger(parseInteger<Integer>(buffer));
|
||||
return DeserializationError::Ok;
|
||||
}
|
||||
if (isFloat(buffer)) {
|
||||
result.setFloat(parseFloat<Float>(buffer));
|
||||
return DeserializationError::Ok;
|
||||
}
|
||||
c = buffer[0];
|
||||
if (c == 't') { // true
|
||||
result.setBoolean(true);
|
||||
@ -275,6 +266,23 @@ class JsonDeserializer {
|
||||
return n == 4 ? DeserializationError::Ok
|
||||
: DeserializationError::IncompleteInput;
|
||||
}
|
||||
|
||||
ParsedNumber<Float, UInt> num = parseNumber<Float, UInt>(buffer);
|
||||
|
||||
switch (num.type()) {
|
||||
case VALUE_IS_NEGATIVE_INTEGER:
|
||||
result.setNegativeInteger(num.uintValue);
|
||||
return DeserializationError::Ok;
|
||||
|
||||
case VALUE_IS_POSITIVE_INTEGER:
|
||||
result.setPositiveInteger(num.uintValue);
|
||||
return DeserializationError::Ok;
|
||||
|
||||
case VALUE_IS_FLOAT:
|
||||
result.setFloat(num.floatValue);
|
||||
return DeserializationError::Ok;
|
||||
}
|
||||
|
||||
return DeserializationError::InvalidInput;
|
||||
}
|
||||
|
||||
@ -306,7 +314,7 @@ class JsonDeserializer {
|
||||
|
||||
static inline uint8_t decodeHex(char c) {
|
||||
if (c < 'A') return uint8_t(c - '0');
|
||||
c &= ~0x20; // uppercase
|
||||
c = char(c & ~0x20); // uppercase
|
||||
return uint8_t(c - 'A' + 10);
|
||||
}
|
||||
|
||||
|
@ -91,7 +91,7 @@ class MemoryPool {
|
||||
return reinterpret_cast<T*>(allocRight(sizeof(T)));
|
||||
}
|
||||
|
||||
char* allocRight(size_t bytes) {
|
||||
void* allocRight(size_t bytes) {
|
||||
if (!canAlloc(bytes)) return 0;
|
||||
_right -= bytes;
|
||||
return _right;
|
||||
|
@ -79,8 +79,7 @@ class MsgPackDeserializer {
|
||||
#if ARDUINOJSON_USE_LONG_LONG
|
||||
return readInteger<uint64_t>(variant);
|
||||
#else
|
||||
readInteger<uint32_t>();
|
||||
return readInteger<uint32_t>(variant);
|
||||
return DeserializationError::NotSupported;
|
||||
#endif
|
||||
|
||||
case 0xd0:
|
||||
@ -96,8 +95,7 @@ class MsgPackDeserializer {
|
||||
#if ARDUINOJSON_USE_LONG_LONG
|
||||
return readInteger<int64_t>(variant);
|
||||
#else
|
||||
if (!skip(4)) return DeserializationError::IncompleteInput;
|
||||
return readInteger<int32_t>(variant);
|
||||
return DeserializationError::NotSupported;
|
||||
#endif
|
||||
|
||||
case 0xca:
|
||||
|
@ -24,7 +24,8 @@ class MsgPackSerializer {
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
typename enable_if<sizeof(T) == 8>::type visitFloat(T value64) {
|
||||
typename enable_if<sizeof(T) == 8>::type visitFloat(T value64)
|
||||
ARDUINOJSON_NO_SANITIZE("float-cast-overflow") {
|
||||
float value32 = float(value64);
|
||||
if (value32 == value64) {
|
||||
writeByte(0xCA);
|
||||
|
@ -9,30 +9,33 @@
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
inline void fixEndianess(uint8_t* p, integral_constant<size_t, 8>) {
|
||||
#if ARDUINOJSON_LITTLE_ENDIAN
|
||||
inline void fixEndianess(uint8_t *p, integral_constant<size_t, 8>) {
|
||||
swap(p[0], p[7]);
|
||||
swap(p[1], p[6]);
|
||||
swap(p[2], p[5]);
|
||||
swap(p[3], p[4]);
|
||||
}
|
||||
|
||||
inline void fixEndianess(uint8_t* p, integral_constant<size_t, 4>) {
|
||||
inline void fixEndianess(uint8_t *p, integral_constant<size_t, 4>) {
|
||||
swap(p[0], p[3]);
|
||||
swap(p[1], p[2]);
|
||||
}
|
||||
|
||||
inline void fixEndianess(uint8_t* p, integral_constant<size_t, 2>) {
|
||||
inline void fixEndianess(uint8_t *p, integral_constant<size_t, 2>) {
|
||||
swap(p[0], p[1]);
|
||||
}
|
||||
|
||||
inline void fixEndianess(uint8_t*, integral_constant<size_t, 1>) {}
|
||||
inline void fixEndianess(uint8_t *, integral_constant<size_t, 1>) {}
|
||||
|
||||
template <typename T>
|
||||
inline void fixEndianess(T& value) {
|
||||
#if ARDUINOJSON_LITTLE_ENDIAN
|
||||
fixEndianess(reinterpret_cast<uint8_t*>(&value),
|
||||
inline void fixEndianess(T &value) {
|
||||
fixEndianess(reinterpret_cast<uint8_t *>(&value),
|
||||
integral_constant<size_t, sizeof(T)>());
|
||||
#endif
|
||||
}
|
||||
#else
|
||||
template <typename T>
|
||||
inline void fixEndianess(T &) {}
|
||||
#endif
|
||||
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
||||
|
@ -7,6 +7,7 @@
|
||||
#include <stddef.h> // for size_t
|
||||
#include <stdint.h>
|
||||
#include "../Configuration.hpp"
|
||||
#include "../Polyfills/alias_cast.hpp"
|
||||
#include "../Polyfills/math.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
@ -16,10 +17,10 @@ struct FloatTraits {};
|
||||
|
||||
template <typename T>
|
||||
struct FloatTraits<T, 8 /*64bits*/> {
|
||||
typedef int64_t mantissa_type;
|
||||
typedef uint64_t mantissa_type;
|
||||
static const short mantissa_bits = 52;
|
||||
static const mantissa_type mantissa_max =
|
||||
(static_cast<mantissa_type>(1) << mantissa_bits) - 1;
|
||||
(mantissa_type(1) << mantissa_bits) - 1;
|
||||
|
||||
typedef int16_t exponent_type;
|
||||
static const exponent_type exponent_max = 308;
|
||||
@ -94,25 +95,28 @@ struct FloatTraits<T, 8 /*64bits*/> {
|
||||
return forge(0x7ff00000, 0x00000000);
|
||||
}
|
||||
|
||||
static T highest() {
|
||||
return forge(0x7FEFFFFF, 0xFFFFFFFF);
|
||||
}
|
||||
|
||||
static T lowest() {
|
||||
return forge(0xFFEFFFFF, 0xFFFFFFFF);
|
||||
}
|
||||
|
||||
// constructs a double floating point values from its binary representation
|
||||
// we use this function to workaround platforms with single precision literals
|
||||
// (for example, when -fsingle-precision-constant is passed to GCC)
|
||||
static T forge(uint32_t msb, uint32_t lsb) {
|
||||
union {
|
||||
uint64_t integerBits;
|
||||
T floatBits;
|
||||
};
|
||||
integerBits = (uint64_t(msb) << 32) | lsb;
|
||||
return floatBits;
|
||||
return alias_cast<T>((uint64_t(msb) << 32) | lsb);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct FloatTraits<T, 4 /*32bits*/> {
|
||||
typedef int32_t mantissa_type;
|
||||
typedef uint32_t mantissa_type;
|
||||
static const short mantissa_bits = 23;
|
||||
static const mantissa_type mantissa_max =
|
||||
(static_cast<mantissa_type>(1) << mantissa_bits) - 1;
|
||||
(mantissa_type(1) << mantissa_bits) - 1;
|
||||
|
||||
typedef int8_t exponent_type;
|
||||
static const exponent_type exponent_max = 38;
|
||||
@ -150,12 +154,7 @@ struct FloatTraits<T, 4 /*32bits*/> {
|
||||
}
|
||||
|
||||
static T forge(uint32_t bits) {
|
||||
union {
|
||||
uint32_t integerBits;
|
||||
T floatBits;
|
||||
};
|
||||
integerBits = bits;
|
||||
return floatBits;
|
||||
return alias_cast<T>(bits);
|
||||
}
|
||||
|
||||
static T nan() {
|
||||
@ -165,5 +164,13 @@ struct FloatTraits<T, 4 /*32bits*/> {
|
||||
static T inf() {
|
||||
return forge(0x7f800000);
|
||||
}
|
||||
|
||||
static T highest() {
|
||||
return forge(0x7f7fffff);
|
||||
}
|
||||
|
||||
static T lowest() {
|
||||
return forge(0xFf7fffff);
|
||||
}
|
||||
};
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
||||
|
105
src/ArduinoJson/Numbers/convertNumber.hpp
Normal file
105
src/ArduinoJson/Numbers/convertNumber.hpp
Normal file
@ -0,0 +1,105 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2019
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#if defined(__clang__)
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Wconversion"
|
||||
#elif defined(__GNUC__)
|
||||
#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
|
||||
#pragma GCC diagnostic push
|
||||
#endif
|
||||
#pragma GCC diagnostic ignored "-Wconversion"
|
||||
#endif
|
||||
|
||||
#include "../Polyfills/limits.hpp"
|
||||
#include "Float.hpp"
|
||||
#include "FloatTraits.hpp"
|
||||
#include "Integer.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
template <typename TOut, typename TIn>
|
||||
typename enable_if<is_integral<TOut>::value && sizeof(TOut) <= sizeof(TIn),
|
||||
bool>::type
|
||||
canStorePositiveInteger(TIn value) {
|
||||
return value <= TIn(numeric_limits<TOut>::highest());
|
||||
}
|
||||
|
||||
template <typename TOut, typename TIn>
|
||||
typename enable_if<is_integral<TOut>::value && sizeof(TIn) < sizeof(TOut),
|
||||
bool>::type
|
||||
canStorePositiveInteger(TIn) {
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename TOut, typename TIn>
|
||||
typename enable_if<is_floating_point<TOut>::value, bool>::type
|
||||
canStorePositiveInteger(TIn) {
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename TOut, typename TIn>
|
||||
typename enable_if<is_floating_point<TOut>::value, bool>::type
|
||||
canStoreNegativeInteger(TIn) {
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename TOut, typename TIn>
|
||||
typename enable_if<is_integral<TOut>::value && is_signed<TOut>::value &&
|
||||
sizeof(TOut) <= sizeof(TIn),
|
||||
bool>::type
|
||||
canStoreNegativeInteger(TIn value) {
|
||||
return value <= TIn(numeric_limits<TOut>::highest()) + 1;
|
||||
}
|
||||
|
||||
template <typename TOut, typename TIn>
|
||||
typename enable_if<is_integral<TOut>::value && is_signed<TOut>::value &&
|
||||
sizeof(TIn) < sizeof(TOut),
|
||||
bool>::type
|
||||
canStoreNegativeInteger(TIn) {
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename TOut, typename TIn>
|
||||
typename enable_if<is_integral<TOut>::value && is_unsigned<TOut>::value,
|
||||
bool>::type
|
||||
canStoreNegativeInteger(TIn) {
|
||||
return false;
|
||||
}
|
||||
|
||||
template <typename TOut, typename TIn>
|
||||
TOut convertPositiveInteger(TIn value) {
|
||||
return canStorePositiveInteger<TOut>(value) ? TOut(value) : 0;
|
||||
}
|
||||
|
||||
template <typename TOut, typename TIn>
|
||||
TOut convertNegativeInteger(TIn value) {
|
||||
return canStoreNegativeInteger<TOut>(value) ? TOut(~value + 1) : 0;
|
||||
}
|
||||
|
||||
template <typename TOut, typename TIn>
|
||||
typename enable_if<is_floating_point<TOut>::value, TOut>::type convertFloat(
|
||||
TIn value) {
|
||||
return TOut(value);
|
||||
}
|
||||
|
||||
template <typename TOut, typename TIn>
|
||||
typename enable_if<!is_floating_point<TOut>::value, TOut>::type convertFloat(
|
||||
TIn value) {
|
||||
return value >= numeric_limits<TOut>::lowest() &&
|
||||
value <= numeric_limits<TOut>::highest()
|
||||
? TOut(value)
|
||||
: 0;
|
||||
}
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
||||
|
||||
#if defined(__clang__)
|
||||
#pragma clang diagnostic pop
|
||||
#elif defined(__GNUC__)
|
||||
#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
|
||||
#pragma GCC diagnostic pop
|
||||
#endif
|
||||
#endif
|
@ -1,36 +0,0 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2019
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string.h> // for strcmp
|
||||
#include "../Polyfills/ctype.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
inline bool isFloat(const char* s) {
|
||||
if (!s) return false;
|
||||
|
||||
if (!strcmp(s, "NaN")) return true;
|
||||
if (issign(*s)) s++;
|
||||
if (!strcmp(s, "Infinity")) return true;
|
||||
if (*s == '\0') return false;
|
||||
|
||||
while (isdigit(*s)) s++;
|
||||
|
||||
if (*s == '.') {
|
||||
s++;
|
||||
while (isdigit(*s)) s++;
|
||||
}
|
||||
|
||||
if (*s == 'e' || *s == 'E') {
|
||||
s++;
|
||||
if (issign(*s)) s++;
|
||||
if (!isdigit(*s)) return false;
|
||||
while (isdigit(*s)) s++;
|
||||
}
|
||||
|
||||
return *s == '\0';
|
||||
}
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
@ -1,17 +0,0 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2019
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../Polyfills/ctype.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
inline bool isInteger(const char* s) {
|
||||
if (!s || !*s) return false;
|
||||
if (issign(*s)) s++;
|
||||
while (isdigit(*s)) s++;
|
||||
return *s == '\0';
|
||||
}
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
@ -4,85 +4,15 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../Numbers/FloatTraits.hpp"
|
||||
#include "../Polyfills/ctype.hpp"
|
||||
#include "../Polyfills/math.hpp"
|
||||
#include "convertNumber.hpp"
|
||||
#include "parseNumber.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
template <typename T>
|
||||
inline T parseFloat(const char* s) {
|
||||
typedef FloatTraits<T> traits;
|
||||
typedef typename traits::mantissa_type mantissa_t;
|
||||
typedef typename traits::exponent_type exponent_t;
|
||||
|
||||
if (!s) return 0; // NULL
|
||||
|
||||
bool negative_result = false;
|
||||
switch (*s) {
|
||||
case '-':
|
||||
negative_result = true;
|
||||
s++;
|
||||
break;
|
||||
case '+':
|
||||
s++;
|
||||
break;
|
||||
}
|
||||
|
||||
if (*s == 't') return 1; // true
|
||||
if (*s == 'n' || *s == 'N') return traits::nan();
|
||||
if (*s == 'i' || *s == 'I')
|
||||
return negative_result ? -traits::inf() : traits::inf();
|
||||
|
||||
mantissa_t mantissa = 0;
|
||||
exponent_t exponent_offset = 0;
|
||||
|
||||
while (isdigit(*s)) {
|
||||
if (mantissa < traits::mantissa_max / 10)
|
||||
mantissa = mantissa * 10 + (*s - '0');
|
||||
else
|
||||
exponent_offset++;
|
||||
s++;
|
||||
}
|
||||
|
||||
if (*s == '.') {
|
||||
s++;
|
||||
while (isdigit(*s)) {
|
||||
if (mantissa < traits::mantissa_max / 10) {
|
||||
mantissa = mantissa * 10 + (*s - '0');
|
||||
exponent_offset--;
|
||||
}
|
||||
s++;
|
||||
}
|
||||
}
|
||||
|
||||
int exponent = 0;
|
||||
if (*s == 'e' || *s == 'E') {
|
||||
s++;
|
||||
bool negative_exponent = false;
|
||||
if (*s == '-') {
|
||||
negative_exponent = true;
|
||||
s++;
|
||||
} else if (*s == '+') {
|
||||
s++;
|
||||
}
|
||||
|
||||
while (isdigit(*s)) {
|
||||
exponent = exponent * 10 + (*s - '0');
|
||||
if (exponent + exponent_offset > traits::exponent_max) {
|
||||
if (negative_exponent)
|
||||
return negative_result ? -0.0f : 0.0f;
|
||||
else
|
||||
return negative_result ? -traits::inf() : traits::inf();
|
||||
}
|
||||
s++;
|
||||
}
|
||||
if (negative_exponent) exponent = -exponent;
|
||||
}
|
||||
exponent += exponent_offset;
|
||||
|
||||
T result = traits::make_float(static_cast<T>(mantissa), exponent);
|
||||
|
||||
return negative_result ? -result : result;
|
||||
// try to reuse the same parameters as JsonDeserializer
|
||||
typedef typename choose_largest<Float, T>::type TFloat;
|
||||
return parseNumber<TFloat, UInt>(s).template as<T>();
|
||||
}
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
||||
|
@ -4,34 +4,16 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../Configuration.hpp"
|
||||
#include "../Polyfills/ctype.hpp"
|
||||
#include "../Polyfills/type_traits.hpp"
|
||||
#include "convertNumber.hpp"
|
||||
#include "parseNumber.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
template <typename T>
|
||||
T parseInteger(const char *s) {
|
||||
if (!s) return 0; // NULL
|
||||
|
||||
if (*s == 't') return 1; // "true"
|
||||
|
||||
T result = 0;
|
||||
bool negative_result = false;
|
||||
|
||||
switch (*s) {
|
||||
case '-':
|
||||
negative_result = true;
|
||||
s++;
|
||||
break;
|
||||
case '+':
|
||||
s++;
|
||||
break;
|
||||
}
|
||||
|
||||
while (isdigit(*s)) {
|
||||
result = T(result * 10 + T(*s - '0'));
|
||||
s++;
|
||||
}
|
||||
|
||||
return negative_result ? T(~result + 1) : result;
|
||||
// try to reuse the same parameters as JsonDeserializer
|
||||
typedef typename choose_largest<UInt, typename make_unsigned<T>::type>::type
|
||||
TUInt;
|
||||
return parseNumber<Float, TUInt>(s).template as<T>();
|
||||
}
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
||||
|
147
src/ArduinoJson/Numbers/parseNumber.hpp
Normal file
147
src/ArduinoJson/Numbers/parseNumber.hpp
Normal file
@ -0,0 +1,147 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2019
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../Polyfills/assert.hpp"
|
||||
#include "../Polyfills/ctype.hpp"
|
||||
#include "../Polyfills/math.hpp"
|
||||
#include "../Polyfills/type_traits.hpp"
|
||||
#include "../Variant/VariantContent.hpp"
|
||||
#include "FloatTraits.hpp"
|
||||
#include "convertNumber.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
template <typename TFloat, typename TUInt>
|
||||
struct ParsedNumber {
|
||||
ParsedNumber() : uintValue(0), floatValue(0), _type(VALUE_IS_NULL) {}
|
||||
|
||||
ParsedNumber(TUInt value, bool is_negative)
|
||||
: uintValue(value),
|
||||
floatValue(TFloat(value)),
|
||||
_type(uint8_t(is_negative ? VALUE_IS_NEGATIVE_INTEGER
|
||||
: VALUE_IS_POSITIVE_INTEGER)) {}
|
||||
ParsedNumber(TFloat value) : floatValue(value), _type(VALUE_IS_FLOAT) {}
|
||||
|
||||
template <typename T>
|
||||
T as() const {
|
||||
switch (_type) {
|
||||
case VALUE_IS_NEGATIVE_INTEGER:
|
||||
return convertNegativeInteger<T>(uintValue);
|
||||
case VALUE_IS_POSITIVE_INTEGER:
|
||||
return convertPositiveInteger<T>(uintValue);
|
||||
case VALUE_IS_FLOAT:
|
||||
return convertFloat<T>(floatValue);
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t type() const {
|
||||
return _type;
|
||||
}
|
||||
|
||||
TUInt uintValue;
|
||||
TFloat floatValue;
|
||||
uint8_t _type;
|
||||
};
|
||||
|
||||
template <typename A, typename B>
|
||||
struct choose_largest : conditional<(sizeof(A) > sizeof(B)), A, B> {};
|
||||
|
||||
template <typename TFloat, typename TUInt>
|
||||
inline ParsedNumber<TFloat, TUInt> parseNumber(const char *s) {
|
||||
typedef FloatTraits<TFloat> traits;
|
||||
typedef typename choose_largest<typename traits::mantissa_type, TUInt>::type
|
||||
mantissa_t;
|
||||
typedef typename traits::exponent_type exponent_t;
|
||||
typedef ParsedNumber<TFloat, TUInt> return_type;
|
||||
|
||||
ARDUINOJSON_ASSERT(s != 0);
|
||||
|
||||
bool is_negative = false;
|
||||
switch (*s) {
|
||||
case '-':
|
||||
is_negative = true;
|
||||
s++;
|
||||
break;
|
||||
case '+':
|
||||
s++;
|
||||
break;
|
||||
}
|
||||
|
||||
if (*s == 'n' || *s == 'N') return traits::nan();
|
||||
if (*s == 'i' || *s == 'I')
|
||||
return is_negative ? -traits::inf() : traits::inf();
|
||||
if (!isdigit(*s) && *s != '.') return return_type();
|
||||
|
||||
mantissa_t mantissa = 0;
|
||||
exponent_t exponent_offset = 0;
|
||||
const mantissa_t maxUint = TUInt(-1);
|
||||
|
||||
while (isdigit(*s)) {
|
||||
uint8_t digit = uint8_t(*s - '0');
|
||||
if (mantissa > maxUint / 10) break;
|
||||
mantissa *= 10;
|
||||
if (mantissa > maxUint - digit) break;
|
||||
mantissa += digit;
|
||||
s++;
|
||||
}
|
||||
|
||||
if (*s == '\0') return return_type(TUInt(mantissa), is_negative);
|
||||
|
||||
// avoid mantissa overflow
|
||||
while (mantissa > traits::mantissa_max) {
|
||||
mantissa /= 10;
|
||||
exponent_offset++;
|
||||
}
|
||||
|
||||
// remaing digits can't fit in the mantissa
|
||||
while (isdigit(*s)) {
|
||||
exponent_offset++;
|
||||
s++;
|
||||
}
|
||||
|
||||
if (*s == '.') {
|
||||
s++;
|
||||
while (isdigit(*s)) {
|
||||
if (mantissa < traits::mantissa_max / 10) {
|
||||
mantissa = mantissa * 10 + uint8_t(*s - '0');
|
||||
exponent_offset--;
|
||||
}
|
||||
s++;
|
||||
}
|
||||
}
|
||||
|
||||
int exponent = 0;
|
||||
if (*s == 'e' || *s == 'E') {
|
||||
s++;
|
||||
bool negative_exponent = false;
|
||||
if (*s == '-') {
|
||||
negative_exponent = true;
|
||||
s++;
|
||||
} else if (*s == '+') {
|
||||
s++;
|
||||
}
|
||||
|
||||
while (isdigit(*s)) {
|
||||
exponent = exponent * 10 + (*s - '0');
|
||||
if (exponent + exponent_offset > traits::exponent_max) {
|
||||
if (negative_exponent)
|
||||
return is_negative ? -0.0f : 0.0f;
|
||||
else
|
||||
return is_negative ? -traits::inf() : traits::inf();
|
||||
}
|
||||
s++;
|
||||
}
|
||||
if (negative_exponent) exponent = -exponent;
|
||||
}
|
||||
exponent += exponent_offset;
|
||||
|
||||
TFloat result = traits::make_float(static_cast<TFloat>(mantissa), exponent);
|
||||
|
||||
return is_negative ? -result : result;
|
||||
}
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
@ -40,9 +40,9 @@ class MemberProxy : public VariantOperators<MemberProxy<TObject, TStringRef> >,
|
||||
return *this;
|
||||
}
|
||||
|
||||
// operator=(char*) const
|
||||
// operator=(const char*) const
|
||||
// operator=(const __FlashStringHelper*) const
|
||||
// operator=(char*)
|
||||
// operator=(const char*)
|
||||
// operator=(const __FlashStringHelper*)
|
||||
template <typename TChar>
|
||||
FORCE_INLINE this_type &operator=(TChar *src) {
|
||||
getOrAddUpstreamMember().set(src);
|
||||
|
@ -16,11 +16,6 @@ void objectAccept(const CollectionData *obj, Visitor &visitor) {
|
||||
visitor.visitNull();
|
||||
}
|
||||
|
||||
template <typename TAdaptedString>
|
||||
inline bool objectContainsKey(const CollectionData *obj, TAdaptedString key) {
|
||||
return obj && obj->containsKey(key);
|
||||
}
|
||||
|
||||
inline bool objectEquals(const CollectionData *lhs, const CollectionData *rhs) {
|
||||
if (lhs == rhs) return true;
|
||||
if (!lhs || !rhs) return false;
|
||||
|
@ -35,4 +35,18 @@ inline ObjectRef ObjectShortcuts<TObject>::createNestedObject(
|
||||
TChar* key) const {
|
||||
return impl()->getOrAddMember(key).template to<ObjectRef>();
|
||||
}
|
||||
|
||||
template <typename TObject>
|
||||
template <typename TString>
|
||||
inline typename enable_if<IsString<TString>::value, bool>::type
|
||||
ObjectShortcuts<TObject>::containsKey(const TString& key) const {
|
||||
return !impl()->getMember(key).isUndefined();
|
||||
}
|
||||
|
||||
template <typename TObject>
|
||||
template <typename TChar>
|
||||
inline typename enable_if<IsString<TChar*>::value, bool>::type
|
||||
ObjectShortcuts<TObject>::containsKey(TChar* key) const {
|
||||
return !impl()->getMember(key).isUndefined();
|
||||
}
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
||||
|
@ -18,7 +18,8 @@ template <typename TData>
|
||||
class ObjectRefBase {
|
||||
public:
|
||||
operator VariantConstRef() const {
|
||||
return VariantConstRef(reinterpret_cast<const VariantData*>(_data));
|
||||
const void* data = _data; // prevent warning cast-align
|
||||
return VariantConstRef(reinterpret_cast<const VariantData*>(data));
|
||||
}
|
||||
|
||||
template <typename Visitor>
|
||||
@ -26,21 +27,6 @@ class ObjectRefBase {
|
||||
objectAccept(_data, visitor);
|
||||
}
|
||||
|
||||
// containsKey(const std::string&) const
|
||||
// containsKey(const String&) const
|
||||
template <typename TString>
|
||||
FORCE_INLINE bool containsKey(const TString& key) const {
|
||||
return objectContainsKey(_data, adaptString(key));
|
||||
}
|
||||
|
||||
// containsKey(char*) const
|
||||
// containsKey(const char*) const
|
||||
// containsKey(const __FlashStringHelper*) const
|
||||
template <typename TChar>
|
||||
FORCE_INLINE bool containsKey(TChar* key) const {
|
||||
return objectContainsKey(_data, adaptString(key));
|
||||
}
|
||||
|
||||
FORCE_INLINE bool isNull() const {
|
||||
return _data == 0;
|
||||
}
|
||||
@ -82,6 +68,21 @@ class ObjectConstRef : public ObjectRefBase<const CollectionData>,
|
||||
return iterator();
|
||||
}
|
||||
|
||||
// containsKey(const std::string&) const
|
||||
// containsKey(const String&) const
|
||||
template <typename TString>
|
||||
FORCE_INLINE bool containsKey(const TString& key) const {
|
||||
return !getMember(key).isUndefined();
|
||||
}
|
||||
|
||||
// containsKey(char*) const
|
||||
// containsKey(const char*) const
|
||||
// containsKey(const __FlashStringHelper*) const
|
||||
template <typename TChar>
|
||||
FORCE_INLINE bool containsKey(TChar* key) const {
|
||||
return !getMember(key).isUndefined();
|
||||
}
|
||||
|
||||
// getMember(const std::string&) const
|
||||
// getMember(const String&) const
|
||||
template <typename TString>
|
||||
@ -140,7 +141,8 @@ class ObjectRef : public ObjectRefBase<CollectionData>,
|
||||
: base_type(data), _pool(buf) {}
|
||||
|
||||
operator VariantRef() const {
|
||||
return VariantRef(_pool, reinterpret_cast<VariantData*>(_data));
|
||||
void* data = _data; // prevent warning cast-align
|
||||
return VariantRef(_pool, reinterpret_cast<VariantData*>(data));
|
||||
}
|
||||
|
||||
operator ObjectConstRef() const {
|
||||
|
@ -15,6 +15,19 @@ class MemberProxy;
|
||||
template <typename TObject>
|
||||
class ObjectShortcuts {
|
||||
public:
|
||||
// containsKey(const std::string&) const
|
||||
// containsKey(const String&) const
|
||||
template <typename TString>
|
||||
FORCE_INLINE typename enable_if<IsString<TString>::value, bool>::type
|
||||
containsKey(const TString &key) const;
|
||||
|
||||
// containsKey(char*) const
|
||||
// containsKey(const char*) const
|
||||
// containsKey(const __FlashStringHelper*) const
|
||||
template <typename TChar>
|
||||
FORCE_INLINE typename enable_if<IsString<TChar *>::value, bool>::type
|
||||
containsKey(TChar *key) const;
|
||||
|
||||
// operator[](const std::string&) const
|
||||
// operator[](const String&) const
|
||||
template <typename TString>
|
||||
|
28
src/ArduinoJson/Polyfills/alias_cast.hpp
Normal file
28
src/ArduinoJson/Polyfills/alias_cast.hpp
Normal file
@ -0,0 +1,28 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2019
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h> // for size_t
|
||||
#include "../Configuration.hpp"
|
||||
#include "../Polyfills/math.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
template <typename T, typename F>
|
||||
struct alias_cast_t {
|
||||
union {
|
||||
F raw;
|
||||
T data;
|
||||
};
|
||||
};
|
||||
|
||||
template <typename T, typename F>
|
||||
T alias_cast(F raw_data) {
|
||||
alias_cast_t<T, F> ac;
|
||||
ac.raw = raw_data;
|
||||
return ac.data;
|
||||
}
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
@ -33,3 +33,13 @@
|
||||
#else
|
||||
#define NOEXCEPT throw()
|
||||
#endif
|
||||
|
||||
#if defined(__has_attribute)
|
||||
#if __has_attribute(no_sanitize)
|
||||
#define ARDUINOJSON_NO_SANITIZE(check) __attribute__((no_sanitize(check)))
|
||||
#else
|
||||
#define ARDUINOJSON_NO_SANITIZE(check)
|
||||
#endif
|
||||
#else
|
||||
#define ARDUINOJSON_NO_SANITIZE(check)
|
||||
#endif
|
||||
|
45
src/ArduinoJson/Polyfills/limits.hpp
Normal file
45
src/ArduinoJson/Polyfills/limits.hpp
Normal file
@ -0,0 +1,45 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2019
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../Polyfills/type_traits.hpp"
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(push)
|
||||
#pragma warning(disable : 4310)
|
||||
#endif
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
// Differs from standard because we can't use the symbols "min" and "max"
|
||||
template <typename T, typename Enable = void>
|
||||
struct numeric_limits;
|
||||
|
||||
template <typename T>
|
||||
struct numeric_limits<T, typename enable_if<is_unsigned<T>::value>::type> {
|
||||
static T lowest() {
|
||||
return 0;
|
||||
}
|
||||
static T highest() {
|
||||
return T(-1);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct numeric_limits<
|
||||
T, typename enable_if<is_integral<T>::value && is_signed<T>::value>::type> {
|
||||
static T lowest() {
|
||||
return T(T(1) << (sizeof(T) * 8 - 1));
|
||||
}
|
||||
static T highest() {
|
||||
return T(~lowest());
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#pragma warning(pop)
|
||||
#endif
|
@ -15,5 +15,6 @@
|
||||
#include "type_traits/is_same.hpp"
|
||||
#include "type_traits/is_signed.hpp"
|
||||
#include "type_traits/is_unsigned.hpp"
|
||||
#include "type_traits/make_unsigned.hpp"
|
||||
#include "type_traits/remove_const.hpp"
|
||||
#include "type_traits/remove_reference.hpp"
|
||||
|
49
src/ArduinoJson/Polyfills/type_traits/make_unsigned.hpp
Normal file
49
src/ArduinoJson/Polyfills/type_traits/make_unsigned.hpp
Normal file
@ -0,0 +1,49 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2019
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "type_identity.hpp"
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
template <typename T>
|
||||
struct make_unsigned;
|
||||
|
||||
template <>
|
||||
struct make_unsigned<char> : type_identity<unsigned char> {};
|
||||
|
||||
template <>
|
||||
struct make_unsigned<signed char> : type_identity<unsigned char> {};
|
||||
template <>
|
||||
struct make_unsigned<unsigned char> : type_identity<unsigned char> {};
|
||||
|
||||
template <>
|
||||
struct make_unsigned<signed short> : type_identity<unsigned short> {};
|
||||
template <>
|
||||
struct make_unsigned<unsigned short> : type_identity<unsigned short> {};
|
||||
|
||||
template <>
|
||||
struct make_unsigned<signed int> : type_identity<unsigned int> {};
|
||||
template <>
|
||||
struct make_unsigned<unsigned int> : type_identity<unsigned int> {};
|
||||
|
||||
template <>
|
||||
struct make_unsigned<signed long> : type_identity<unsigned long> {};
|
||||
template <>
|
||||
struct make_unsigned<unsigned long> : type_identity<unsigned long> {};
|
||||
|
||||
#if ARDUINOJSON_HAS_LONG_LONG
|
||||
template <>
|
||||
struct make_unsigned<signed long long> : type_identity<unsigned long long> {};
|
||||
template <>
|
||||
struct make_unsigned<unsigned long long> : type_identity<unsigned long long> {};
|
||||
#endif
|
||||
|
||||
#if ARDUINOJSON_HAS_INT64
|
||||
template <>
|
||||
struct make_unsigned<signed __int64> : type_identity<unsigned __int64> {};
|
||||
template <>
|
||||
struct make_unsigned<unsigned __int64> : type_identity<unsigned __int64> {};
|
||||
#endif
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
15
src/ArduinoJson/Polyfills/type_traits/type_identity.hpp
Normal file
15
src/ArduinoJson/Polyfills/type_traits/type_identity.hpp
Normal file
@ -0,0 +1,15 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2019
|
||||
// MIT License
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "integral_constant.hpp"
|
||||
|
||||
namespace ARDUINOJSON_NAMESPACE {
|
||||
|
||||
template <typename T>
|
||||
struct type_identity {
|
||||
typedef T type;
|
||||
};
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
@ -32,4 +32,8 @@ inline size_t slotSize(const VariantSlot* var) {
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
inline VariantData* slotData(VariantSlot* slot) {
|
||||
return reinterpret_cast<VariantData*>(slot);
|
||||
}
|
||||
} // namespace ARDUINOJSON_NAMESPACE
|
||||
|
@ -5,6 +5,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "../Misc/SerializedValue.hpp"
|
||||
#include "../Numbers/convertNumber.hpp"
|
||||
#include "../Polyfills/gsl/not_null.hpp"
|
||||
#include "VariantContent.hpp"
|
||||
|
||||
@ -63,9 +64,7 @@ class VariantData {
|
||||
|
||||
const char *asString() const;
|
||||
|
||||
bool asBoolean() const {
|
||||
return asIntegral<int>() != 0;
|
||||
}
|
||||
bool asBoolean() const;
|
||||
|
||||
CollectionData *asArray() {
|
||||
return isArray() ? &_content.asCollection : 0;
|
||||
@ -147,9 +146,18 @@ class VariantData {
|
||||
return (_flags & COLLECTION_MASK) != 0;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool isInteger() const {
|
||||
return type() == VALUE_IS_POSITIVE_INTEGER ||
|
||||
type() == VALUE_IS_NEGATIVE_INTEGER;
|
||||
switch (type()) {
|
||||
case VALUE_IS_POSITIVE_INTEGER:
|
||||
return canStorePositiveInteger<T>(_content.asInteger);
|
||||
|
||||
case VALUE_IS_NEGATIVE_INTEGER:
|
||||
return canStoreNegativeInteger<T>(_content.asInteger);
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool isFloat() const {
|
||||
@ -225,14 +233,22 @@ class VariantData {
|
||||
template <typename T>
|
||||
void setSignedInteger(T value) {
|
||||
if (value >= 0) {
|
||||
setType(VALUE_IS_POSITIVE_INTEGER);
|
||||
_content.asInteger = static_cast<UInt>(value);
|
||||
setPositiveInteger(static_cast<UInt>(value));
|
||||
} else {
|
||||
setType(VALUE_IS_NEGATIVE_INTEGER);
|
||||
_content.asInteger = ~static_cast<UInt>(value) + 1;
|
||||
setNegativeInteger(~static_cast<UInt>(value) + 1);
|
||||
}
|
||||
}
|
||||
|
||||
void setPositiveInteger(UInt value) {
|
||||
setType(VALUE_IS_POSITIVE_INTEGER);
|
||||
_content.asInteger = value;
|
||||
}
|
||||
|
||||
void setNegativeInteger(UInt value) {
|
||||
setType(VALUE_IS_NEGATIVE_INTEGER);
|
||||
_content.asInteger = value;
|
||||
}
|
||||
|
||||
void setLinkedString(const char *value) {
|
||||
if (value) {
|
||||
setType(VALUE_IS_LINKED_STRING);
|
||||
|
@ -52,8 +52,9 @@ inline bool variantIsBoolean(const VariantData *var) {
|
||||
return var && var->isBoolean();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline bool variantIsInteger(const VariantData *var) {
|
||||
return var && var->isInteger();
|
||||
return var && var->isInteger<T>();
|
||||
}
|
||||
|
||||
inline bool variantIsFloat(const VariantData *var) {
|
||||
|
@ -5,6 +5,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "../Configuration.hpp"
|
||||
#include "../Numbers/convertNumber.hpp"
|
||||
#include "../Numbers/parseFloat.hpp"
|
||||
#include "../Numbers/parseInteger.hpp"
|
||||
#include "VariantRef.hpp"
|
||||
@ -18,19 +19,35 @@ inline T VariantData::asIntegral() const {
|
||||
switch (type()) {
|
||||
case VALUE_IS_POSITIVE_INTEGER:
|
||||
case VALUE_IS_BOOLEAN:
|
||||
return T(_content.asInteger);
|
||||
return convertPositiveInteger<T>(_content.asInteger);
|
||||
case VALUE_IS_NEGATIVE_INTEGER:
|
||||
return T(~_content.asInteger + 1);
|
||||
return convertNegativeInteger<T>(_content.asInteger);
|
||||
case VALUE_IS_LINKED_STRING:
|
||||
case VALUE_IS_OWNED_STRING:
|
||||
return parseInteger<T>(_content.asString);
|
||||
case VALUE_IS_FLOAT:
|
||||
return T(_content.asFloat);
|
||||
return convertFloat<T>(_content.asFloat);
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
inline bool VariantData::asBoolean() const {
|
||||
switch (type()) {
|
||||
case VALUE_IS_POSITIVE_INTEGER:
|
||||
case VALUE_IS_BOOLEAN:
|
||||
case VALUE_IS_NEGATIVE_INTEGER:
|
||||
return _content.asInteger != 0;
|
||||
case VALUE_IS_FLOAT:
|
||||
return _content.asFloat != 0;
|
||||
case VALUE_IS_LINKED_STRING:
|
||||
case VALUE_IS_OWNED_STRING:
|
||||
return strcmp("true", _content.asString) == 0;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// T = float/double
|
||||
template <typename T>
|
||||
inline T VariantData::asFloat() const {
|
||||
|
@ -9,8 +9,6 @@
|
||||
|
||||
#include "../Memory/MemoryPool.hpp"
|
||||
#include "../Misc/Visitable.hpp"
|
||||
#include "../Numbers/parseFloat.hpp"
|
||||
#include "../Numbers/parseInteger.hpp"
|
||||
#include "../Operators/VariantOperators.hpp"
|
||||
#include "../Polyfills/type_traits.hpp"
|
||||
#include "VariantAs.hpp"
|
||||
@ -45,7 +43,7 @@ class VariantRefBase {
|
||||
template <typename T>
|
||||
FORCE_INLINE typename enable_if<is_integral<T>::value, bool>::type is()
|
||||
const {
|
||||
return variantIsInteger(_data);
|
||||
return variantIsInteger<T>(_data);
|
||||
}
|
||||
//
|
||||
// bool is<double>() const;
|
||||
@ -98,6 +96,10 @@ class VariantRefBase {
|
||||
return variantIsNull(_data);
|
||||
}
|
||||
|
||||
FORCE_INLINE bool isUndefined() const {
|
||||
return !_data;
|
||||
}
|
||||
|
||||
FORCE_INLINE size_t memoryUsage() const {
|
||||
return _data ? _data->memoryUsage() : 0;
|
||||
}
|
||||
|
23
src/ArduinoJson/compatibility.hpp
Normal file
23
src/ArduinoJson/compatibility.hpp
Normal file
@ -0,0 +1,23 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2019
|
||||
// MIT License
|
||||
//
|
||||
// clang-format off
|
||||
|
||||
#ifdef __GNUC__
|
||||
|
||||
#define ARDUINOJSON_PRAGMA(x) _Pragma(#x)
|
||||
|
||||
#define ARDUINOJSON_COMPILE_ERROR(msg) ARDUINOJSON_PRAGMA(GCC error msg)
|
||||
|
||||
#define ARDUINOJSON_STRINGIFY(S) #S
|
||||
|
||||
#define ARDUINOJSON_DEPRECATION_ERROR(X, Y) \
|
||||
ARDUINOJSON_COMPILE_ERROR(ARDUINOJSON_STRINGIFY(X is a Y from ArduinoJson 5. Please see arduinojson.org/upgrade to learn how to upgrade your program to ArduinoJson version 6))
|
||||
|
||||
#define StaticJsonBuffer ARDUINOJSON_DEPRECATION_ERROR(StaticJsonBuffer, class)
|
||||
#define DynamicJsonBuffer ARDUINOJSON_DEPRECATION_ERROR(DynamicJsonBuffer, class)
|
||||
#define JsonBuffer ARDUINOJSON_DEPRECATION_ERROR(JsonBuffer, class)
|
||||
#define RawJson ARDUINOJSON_DEPRECATION_ERROR(RawJson, function)
|
||||
|
||||
#endif
|
@ -4,7 +4,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#define ARDUINOJSON_VERSION "6.9.0"
|
||||
#define ARDUINOJSON_VERSION "6.10.0"
|
||||
#define ARDUINOJSON_VERSION_MAJOR 6
|
||||
#define ARDUINOJSON_VERSION_MINOR 9
|
||||
#define ARDUINOJSON_VERSION_MINOR 10
|
||||
#define ARDUINOJSON_VERSION_REVISION 0
|
||||
|
@ -24,7 +24,6 @@ if(CMAKE_CXX_COMPILER_ID MATCHES "(GNU|Clang)")
|
||||
-Wshadow
|
||||
-Wsign-promo
|
||||
-Wstrict-aliasing
|
||||
-Wstrict-overflow=5
|
||||
-Wundef
|
||||
)
|
||||
|
||||
|
@ -22,7 +22,8 @@ TEST_CASE("JsonArray::operator[]") {
|
||||
SECTION("long long") {
|
||||
array[0] = 9223372036854775807;
|
||||
REQUIRE(9223372036854775807 == array[0].as<int64_t>());
|
||||
REQUIRE(true == array[0].is<int>());
|
||||
REQUIRE(true == array[0].is<int64_t>());
|
||||
REQUIRE(false == array[0].is<int32_t>());
|
||||
REQUIRE(false == array[0].is<bool>());
|
||||
}
|
||||
#endif
|
||||
|
@ -3,17 +3,18 @@
|
||||
# MIT License
|
||||
|
||||
add_executable(JsonDeserializerTests
|
||||
array.cpp
|
||||
array_static.cpp
|
||||
DeserializationError.cpp
|
||||
deserializeJsonArray.cpp
|
||||
deserializeJsonArrayStatic.cpp
|
||||
deserializeJsonObject.cpp
|
||||
deserializeJsonObjectStatic.cpp
|
||||
deserializeJsonValue.cpp
|
||||
deserializeJsonString.cpp
|
||||
input_types.cpp
|
||||
incomplete_input.cpp
|
||||
input_types.cpp
|
||||
number.cpp
|
||||
invalid_input.cpp
|
||||
misc.cpp
|
||||
nestingLimit.cpp
|
||||
object.cpp
|
||||
object_static.cpp
|
||||
string.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(JsonDeserializerTests catch)
|
||||
|
@ -7,11 +7,6 @@
|
||||
|
||||
using namespace Catch::Matchers;
|
||||
|
||||
namespace my {
|
||||
using ARDUINOJSON_NAMESPACE::isinf;
|
||||
using ARDUINOJSON_NAMESPACE::isnan;
|
||||
} // namespace my
|
||||
|
||||
TEST_CASE("deserializeJson(DynamicJsonDocument&)") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
|
||||
@ -48,64 +43,6 @@ TEST_CASE("deserializeJson(DynamicJsonDocument&)") {
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("Integers") {
|
||||
SECTION("0") {
|
||||
DeserializationError err = deserializeJson(doc, "0");
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<int>() == true);
|
||||
REQUIRE(doc.as<int>() == 0);
|
||||
REQUIRE(doc.as<std::string>() == "0"); // issue #808
|
||||
}
|
||||
|
||||
SECTION("Negative") {
|
||||
DeserializationError err = deserializeJson(doc, "-42");
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<int>());
|
||||
REQUIRE_FALSE(doc.is<bool>());
|
||||
REQUIRE(doc.as<int>() == -42);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("Floats") {
|
||||
SECTION("Double") {
|
||||
DeserializationError err = deserializeJson(doc, "-1.23e+4");
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE_FALSE(doc.is<int>());
|
||||
REQUIRE(doc.is<double>());
|
||||
REQUIRE(doc.as<double>() == Approx(-1.23e+4));
|
||||
}
|
||||
|
||||
SECTION("NaN") {
|
||||
DeserializationError err = deserializeJson(doc, "NaN");
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<float>() == true);
|
||||
REQUIRE(my::isnan(doc.as<float>()));
|
||||
}
|
||||
|
||||
SECTION("Infinity") {
|
||||
DeserializationError err = deserializeJson(doc, "Infinity");
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<float>() == true);
|
||||
REQUIRE(my::isinf(doc.as<float>()));
|
||||
}
|
||||
|
||||
SECTION("+Infinity") {
|
||||
DeserializationError err = deserializeJson(doc, "+Infinity");
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<float>() == true);
|
||||
REQUIRE(my::isinf(doc.as<float>()));
|
||||
}
|
||||
|
||||
SECTION("-Infinity") {
|
||||
DeserializationError err = deserializeJson(doc, "-Infinity");
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<float>() == true);
|
||||
REQUIRE(my::isinf(doc.as<float>()));
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("Booleans") {
|
||||
SECTION("True") {
|
||||
DeserializationError err = deserializeJson(doc, "true");
|
131
test/JsonDeserializer/number.cpp
Normal file
131
test/JsonDeserializer/number.cpp
Normal file
@ -0,0 +1,131 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2019
|
||||
// MIT License
|
||||
|
||||
#define ARDUINOJSON_USE_LONG_LONG 0
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <limits.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
namespace my {
|
||||
using ARDUINOJSON_NAMESPACE::isinf;
|
||||
using ARDUINOJSON_NAMESPACE::isnan;
|
||||
} // namespace my
|
||||
|
||||
TEST_CASE("deserialize an integer") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
|
||||
SECTION("Integer") {
|
||||
SECTION("0") {
|
||||
DeserializationError err = deserializeJson(doc, "0");
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<int>() == true);
|
||||
REQUIRE(doc.as<int>() == 0);
|
||||
REQUIRE(doc.as<std::string>() == "0"); // issue #808
|
||||
}
|
||||
|
||||
SECTION("Negative") {
|
||||
DeserializationError err = deserializeJson(doc, "-42");
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<int>());
|
||||
REQUIRE_FALSE(doc.is<bool>());
|
||||
REQUIRE(doc.as<int>() == -42);
|
||||
}
|
||||
|
||||
#if LONG_MAX == 2147483647
|
||||
SECTION("LONG_MAX") {
|
||||
DeserializationError err = deserializeJson(doc, "2147483647");
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<long>() == true);
|
||||
REQUIRE(doc.as<long>() == LONG_MAX);
|
||||
}
|
||||
|
||||
SECTION("LONG_MAX + 1") {
|
||||
DeserializationError err = deserializeJson(doc, "2147483648");
|
||||
|
||||
CAPTURE(LONG_MIN);
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<long>() == false);
|
||||
REQUIRE(doc.is<float>() == true);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if LONG_MIN == -2147483648
|
||||
SECTION("LONG_MIN") {
|
||||
DeserializationError err = deserializeJson(doc, "-2147483648");
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<long>() == true);
|
||||
REQUIRE(doc.as<long>() == LONG_MIN);
|
||||
}
|
||||
|
||||
SECTION("LONG_MIN - 1") {
|
||||
DeserializationError err = deserializeJson(doc, "-2147483649");
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<long>() == false);
|
||||
REQUIRE(doc.is<float>() == true);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if ULONG_MAX == 4294967295
|
||||
SECTION("ULONG_MAX") {
|
||||
DeserializationError err = deserializeJson(doc, "4294967295");
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<unsigned long>() == true);
|
||||
REQUIRE(doc.as<unsigned long>() == ULONG_MAX);
|
||||
REQUIRE(doc.is<long>() == false);
|
||||
}
|
||||
|
||||
SECTION("ULONG_MAX + 1") {
|
||||
DeserializationError err = deserializeJson(doc, "4294967296");
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<unsigned long>() == false);
|
||||
REQUIRE(doc.is<float>() == true);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
SECTION("Floats") {
|
||||
SECTION("Double") {
|
||||
DeserializationError err = deserializeJson(doc, "-1.23e+4");
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE_FALSE(doc.is<int>());
|
||||
REQUIRE(doc.is<double>());
|
||||
REQUIRE(doc.as<double>() == Approx(-1.23e+4));
|
||||
}
|
||||
|
||||
SECTION("NaN") {
|
||||
DeserializationError err = deserializeJson(doc, "NaN");
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<float>() == true);
|
||||
REQUIRE(my::isnan(doc.as<float>()));
|
||||
}
|
||||
|
||||
SECTION("Infinity") {
|
||||
DeserializationError err = deserializeJson(doc, "Infinity");
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<float>() == true);
|
||||
REQUIRE(my::isinf(doc.as<float>()));
|
||||
}
|
||||
|
||||
SECTION("+Infinity") {
|
||||
DeserializationError err = deserializeJson(doc, "+Infinity");
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<float>() == true);
|
||||
REQUIRE(my::isinf(doc.as<float>()));
|
||||
}
|
||||
|
||||
SECTION("-Infinity") {
|
||||
DeserializationError err = deserializeJson(doc, "-Infinity");
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<float>() == true);
|
||||
REQUIRE(my::isinf(doc.as<float>()));
|
||||
}
|
||||
}
|
||||
}
|
49
test/JsonDocument/BasicJsonDocument.cpp
Normal file
49
test/JsonDocument/BasicJsonDocument.cpp
Normal file
@ -0,0 +1,49 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2019
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <stdlib.h> // malloc, free
|
||||
#include <catch.hpp>
|
||||
#include <sstream>
|
||||
|
||||
using ARDUINOJSON_NAMESPACE::addPadding;
|
||||
|
||||
class SpyingAllocator {
|
||||
public:
|
||||
SpyingAllocator(std::ostream& log) : _log(log) {}
|
||||
|
||||
void* allocate(size_t n) {
|
||||
_log << "A" << n;
|
||||
return malloc(n);
|
||||
}
|
||||
void deallocate(void* p) {
|
||||
_log << "F";
|
||||
free(p);
|
||||
}
|
||||
|
||||
private:
|
||||
SpyingAllocator& operator=(const SpyingAllocator& src);
|
||||
|
||||
std::ostream& _log;
|
||||
};
|
||||
|
||||
typedef BasicJsonDocument<SpyingAllocator> MyJsonDocument;
|
||||
|
||||
TEST_CASE("BasicJsonDocument") {
|
||||
std::stringstream log;
|
||||
|
||||
SECTION("Construct/Destruct") {
|
||||
{ MyJsonDocument doc(4096, log); }
|
||||
REQUIRE(log.str() == "A4096F");
|
||||
}
|
||||
|
||||
SECTION("Copy construct") {
|
||||
{
|
||||
MyJsonDocument doc1(4096, log);
|
||||
doc1.set(std::string("The size of this string is 32!!"));
|
||||
MyJsonDocument doc2(doc1);
|
||||
}
|
||||
REQUIRE(log.str() == "A4096A32FF");
|
||||
}
|
||||
}
|
@ -4,6 +4,8 @@
|
||||
|
||||
add_executable(JsonDocumentTests
|
||||
add.cpp
|
||||
BasicJsonDocument.cpp
|
||||
containsKey.cpp
|
||||
createNested.cpp
|
||||
DynamicJsonDocument.cpp
|
||||
isNull.cpp
|
||||
|
44
test/JsonDocument/containsKey.cpp
Normal file
44
test/JsonDocument/containsKey.cpp
Normal file
@ -0,0 +1,44 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2019
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("JsonDocument::containsKey()") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
|
||||
SECTION("returns true on object") {
|
||||
doc["hello"] = "world";
|
||||
|
||||
REQUIRE(doc.containsKey("hello") == true);
|
||||
}
|
||||
|
||||
SECTION("returns true when value is null") {
|
||||
doc["hello"] = static_cast<const char*>(0);
|
||||
|
||||
REQUIRE(doc.containsKey("hello") == true);
|
||||
}
|
||||
|
||||
SECTION("returns true when key is a std::string") {
|
||||
doc["hello"] = "world";
|
||||
|
||||
REQUIRE(doc.containsKey(std::string("hello")) == true);
|
||||
}
|
||||
|
||||
SECTION("returns false on object") {
|
||||
doc["world"] = "hello";
|
||||
|
||||
REQUIRE(doc.containsKey("hello") == false);
|
||||
}
|
||||
|
||||
SECTION("returns false on array") {
|
||||
doc.add("hello");
|
||||
|
||||
REQUIRE(doc.containsKey("hello") == false);
|
||||
}
|
||||
|
||||
SECTION("returns false on null") {
|
||||
REQUIRE(doc.containsKey("hello") == false);
|
||||
}
|
||||
}
|
@ -7,6 +7,7 @@ add_executable(JsonVariantTests
|
||||
as.cpp
|
||||
clear.cpp
|
||||
compare.cpp
|
||||
containsKey.cpp
|
||||
copy.cpp
|
||||
createNested.cpp
|
||||
is.cpp
|
||||
@ -15,6 +16,7 @@ add_executable(JsonVariantTests
|
||||
misc.cpp
|
||||
nesting.cpp
|
||||
or.cpp
|
||||
overflow.cpp
|
||||
remove.cpp
|
||||
set.cpp
|
||||
subscript.cpp
|
||||
|
@ -6,6 +6,10 @@
|
||||
#include <stdint.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
namespace my {
|
||||
using ARDUINOJSON_NAMESPACE::isinf;
|
||||
} // namespace my
|
||||
|
||||
static const char* null = 0;
|
||||
|
||||
TEST_CASE("JsonVariant::as()") {
|
||||
@ -94,7 +98,6 @@ TEST_CASE("JsonVariant::as()") {
|
||||
SECTION("set(\"42\")") {
|
||||
variant.set("42");
|
||||
|
||||
REQUIRE(variant.as<bool>());
|
||||
REQUIRE(variant.as<long>() == 42L);
|
||||
}
|
||||
|
||||
@ -111,7 +114,6 @@ TEST_CASE("JsonVariant::as()") {
|
||||
SECTION("set(std::string(\"4.2\"))") {
|
||||
variant.set(std::string("4.2"));
|
||||
|
||||
REQUIRE(variant.as<bool>() == true);
|
||||
REQUIRE(variant.as<long>() == 4L);
|
||||
REQUIRE(variant.as<double>() == 4.2);
|
||||
REQUIRE(variant.as<char*>() == std::string("4.2"));
|
||||
@ -121,8 +123,31 @@ TEST_CASE("JsonVariant::as()") {
|
||||
SECTION("set(\"true\")") {
|
||||
variant.set("true");
|
||||
|
||||
REQUIRE(variant.as<bool>());
|
||||
REQUIRE(variant.as<long>() == 1L);
|
||||
REQUIRE(variant.as<bool>() == true);
|
||||
REQUIRE(variant.as<int>() == 0);
|
||||
}
|
||||
|
||||
SECTION("set(-1e300)") {
|
||||
variant.set(-1e300);
|
||||
|
||||
REQUIRE(variant.as<double>() == -1e300);
|
||||
REQUIRE(variant.as<float>() < 0);
|
||||
REQUIRE(my::isinf(variant.as<float>()));
|
||||
}
|
||||
|
||||
SECTION("set(1e300)") {
|
||||
variant.set(1e300);
|
||||
|
||||
REQUIRE(variant.as<double>() == 1e300);
|
||||
REQUIRE(variant.as<float>() > 0);
|
||||
REQUIRE(my::isinf(variant.as<float>()));
|
||||
}
|
||||
|
||||
SECTION("set(1e300)") {
|
||||
variant.set(1e-300);
|
||||
|
||||
REQUIRE(variant.as<double>() == 1e-300);
|
||||
REQUIRE(variant.as<float>() == 0);
|
||||
}
|
||||
|
||||
SECTION("to<JsonObject>()") {
|
||||
|
28
test/JsonVariant/containsKey.cpp
Normal file
28
test/JsonVariant/containsKey.cpp
Normal file
@ -0,0 +1,28 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2019
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <stdint.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
static const char* null = 0;
|
||||
|
||||
TEST_CASE("JsonVariant::containsKey()") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
JsonVariant var = doc.to<JsonVariant>();
|
||||
|
||||
SECTION("containsKey(const char*) returns true") {
|
||||
var["hello"] = "world";
|
||||
|
||||
REQUIRE(var.containsKey("hello") == true);
|
||||
REQUIRE(var.containsKey("world") == false);
|
||||
}
|
||||
|
||||
SECTION("containsKey(std::string) returns true") {
|
||||
var["hello"] = "world";
|
||||
|
||||
REQUIRE(var.containsKey(std::string("hello")) == true);
|
||||
REQUIRE(var.containsKey(std::string("world")) == false);
|
||||
}
|
||||
}
|
72
test/JsonVariant/overflow.cpp
Normal file
72
test/JsonVariant/overflow.cpp
Normal file
@ -0,0 +1,72 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2019
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
template <typename TOut, typename TIn>
|
||||
void shouldBeOk(TIn value) {
|
||||
StaticJsonDocument<1> doc;
|
||||
JsonVariant var = doc.to<JsonVariant>();
|
||||
var.set(value);
|
||||
REQUIRE(var.as<TOut>() == TOut(value));
|
||||
}
|
||||
|
||||
template <typename TOut, typename TIn>
|
||||
void shouldOverflow(TIn value) {
|
||||
StaticJsonDocument<1> doc;
|
||||
JsonVariant var = doc.to<JsonVariant>();
|
||||
var.set(value);
|
||||
REQUIRE(var.as<TOut>() == 0);
|
||||
REQUIRE(var.is<TOut>() == false);
|
||||
}
|
||||
|
||||
TEST_CASE("Handle integer overflow in stored integer") {
|
||||
SECTION("int8_t") {
|
||||
// ok
|
||||
shouldBeOk<int8_t>(-128);
|
||||
shouldBeOk<int8_t>(42.0);
|
||||
shouldBeOk<int8_t>(127);
|
||||
|
||||
// too low
|
||||
shouldOverflow<int8_t>(-128.1);
|
||||
shouldOverflow<int8_t>(-129);
|
||||
|
||||
// too high
|
||||
shouldOverflow<int8_t>(128);
|
||||
shouldOverflow<int8_t>(127.1);
|
||||
}
|
||||
|
||||
SECTION("int16_t") {
|
||||
// ok
|
||||
shouldBeOk<int16_t>(-32768);
|
||||
shouldBeOk<int16_t>(-32767.9);
|
||||
shouldBeOk<int16_t>(32766.9);
|
||||
shouldBeOk<int16_t>(32767);
|
||||
|
||||
// too low
|
||||
shouldOverflow<int16_t>(-32768.1);
|
||||
shouldOverflow<int16_t>(-32769);
|
||||
|
||||
// too high
|
||||
shouldOverflow<int16_t>(32767.1);
|
||||
shouldOverflow<int16_t>(32768);
|
||||
}
|
||||
|
||||
SECTION("uint8_t") {
|
||||
// ok
|
||||
shouldBeOk<uint8_t>(1);
|
||||
shouldBeOk<uint8_t>(42.0);
|
||||
shouldBeOk<uint8_t>(255);
|
||||
|
||||
// too low
|
||||
shouldOverflow<uint8_t>(-1);
|
||||
shouldOverflow<uint8_t>(-0.1);
|
||||
|
||||
// to high
|
||||
shouldOverflow<uint8_t>(255.1);
|
||||
shouldOverflow<uint8_t>(256);
|
||||
shouldOverflow<uint8_t>(257);
|
||||
}
|
||||
}
|
@ -5,6 +5,7 @@
|
||||
add_executable(MemberProxyTests
|
||||
add.cpp
|
||||
clear.cpp
|
||||
containsKey.cpp
|
||||
remove.cpp
|
||||
set.cpp
|
||||
size.cpp
|
||||
|
27
test/MemberProxy/containsKey.cpp
Normal file
27
test/MemberProxy/containsKey.cpp
Normal file
@ -0,0 +1,27 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2019
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace ARDUINOJSON_NAMESPACE;
|
||||
|
||||
TEST_CASE("MemberProxy::containsKey()") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
MemberProxy<JsonDocument&, const char*> mp = doc["hello"];
|
||||
|
||||
SECTION("containsKey(const char*)") {
|
||||
mp["key"] = "value";
|
||||
|
||||
REQUIRE(mp.containsKey("key") == true);
|
||||
REQUIRE(mp.containsKey("key") == true);
|
||||
}
|
||||
|
||||
SECTION("containsKey(std::string)") {
|
||||
mp["key"] = "value";
|
||||
|
||||
REQUIRE(mp.containsKey(std::string("key")) == true);
|
||||
REQUIRE(mp.containsKey(std::string("key")) == true);
|
||||
}
|
||||
}
|
@ -3,6 +3,7 @@
|
||||
# MIT License
|
||||
|
||||
add_executable(MiscTests
|
||||
conflicts.cpp
|
||||
FloatParts.cpp
|
||||
StringWriter.cpp
|
||||
TypeTraits.cpp
|
||||
|
47
test/Misc/conflicts.cpp
Normal file
47
test/Misc/conflicts.cpp
Normal file
@ -0,0 +1,47 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2019
|
||||
// MIT License
|
||||
|
||||
// Include any header that might use the conflicting macros
|
||||
#include <cmath>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
// All cores
|
||||
#define bit()
|
||||
#define constrain()
|
||||
#define DEFAULT
|
||||
#define DISABLED
|
||||
#define HIGH
|
||||
#define INPUT
|
||||
#define LOW
|
||||
#define max()
|
||||
#define min()
|
||||
#define OUTPUT
|
||||
#define round()
|
||||
#define sq()
|
||||
#define word()
|
||||
#define bitRead()
|
||||
#define bitSet()
|
||||
#define bitClear()
|
||||
#define bitWrite()
|
||||
#define interrupts()
|
||||
#define lowByte()
|
||||
#define highByte()
|
||||
#define DEC
|
||||
#define HEX
|
||||
#define OCT
|
||||
#define BIN
|
||||
#define cbi()
|
||||
#define sbi()
|
||||
|
||||
// ESP8266
|
||||
#define _max()
|
||||
#define _min()
|
||||
|
||||
// issue #839
|
||||
#define BLOCKSIZE
|
||||
#define CAPACITY
|
||||
|
||||
// catch.hpp mutes several warnings, this file also allows to detect them
|
||||
#include "ArduinoJson.h"
|
@ -16,6 +16,14 @@ static void check(const char* input, U expected) {
|
||||
REQUIRE(doc.as<T>() == expected);
|
||||
}
|
||||
|
||||
#if ARDUINOJSON_USE_LONG_LONG == 0
|
||||
static void checkNotSupported(const char* input) {
|
||||
DynamicJsonDocument doc(4096);
|
||||
DeserializationError error = deserializeMsgPack(doc, input);
|
||||
REQUIRE(error == DeserializationError::NotSupported);
|
||||
}
|
||||
#endif
|
||||
|
||||
static void checkIsNull(const char* input) {
|
||||
DynamicJsonDocument doc(4096);
|
||||
|
||||
@ -70,9 +78,9 @@ TEST_CASE("deserialize MsgPack value") {
|
||||
check<uint64_t>("\xCF\x12\x34\x56\x78\x9A\xBC\xDE\xF0",
|
||||
0x123456789ABCDEF0U);
|
||||
#else
|
||||
check<uint32_t>("\xCF\x00\x00\x00\x00\x00\x00\x00\x00", 0U);
|
||||
check<uint32_t>("\xCF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 0xFFFFFFFF);
|
||||
check<uint32_t>("\xCF\x12\x34\x56\x78\x9A\xBC\xDE\xF0", 0x9ABCDEF0);
|
||||
checkNotSupported("\xCF\x00\x00\x00\x00\x00\x00\x00\x00");
|
||||
checkNotSupported("\xCF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF");
|
||||
checkNotSupported("\xCF\x12\x34\x56\x78\x9A\xBC\xDE\xF0");
|
||||
#endif
|
||||
}
|
||||
|
||||
@ -95,15 +103,15 @@ TEST_CASE("deserialize MsgPack value") {
|
||||
|
||||
SECTION("int 64") {
|
||||
#if ARDUINOJSON_USE_LONG_LONG
|
||||
check<uint64_t>("\xD3\x00\x00\x00\x00\x00\x00\x00\x00", 0U);
|
||||
check<uint64_t>("\xD3\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF",
|
||||
0xFFFFFFFFFFFFFFFFU);
|
||||
check<uint64_t>("\xD3\x12\x34\x56\x78\x9A\xBC\xDE\xF0",
|
||||
0x123456789ABCDEF0U);
|
||||
check<int64_t>("\xD3\x00\x00\x00\x00\x00\x00\x00\x00", int64_t(0U));
|
||||
check<int64_t>("\xD3\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF",
|
||||
int64_t(0xFFFFFFFFFFFFFFFFU));
|
||||
check<int64_t>("\xD3\x12\x34\x56\x78\x9A\xBC\xDE\xF0",
|
||||
int64_t(0x123456789ABCDEF0));
|
||||
#else
|
||||
check<uint32_t>("\xD3\x00\x00\x00\x00\x00\x00\x00\x00", 0U);
|
||||
check<uint32_t>("\xD3\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 0xFFFFFFFF);
|
||||
check<uint32_t>("\xD3\x12\x34\x56\x78\x9A\xBC\xDE\xF0", 0x9ABCDEF0);
|
||||
checkNotSupported("\xD3\x00\x00\x00\x00\x00\x00\x00\x00");
|
||||
checkNotSupported("\xD3\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF");
|
||||
checkNotSupported("\xD3\x12\x34\x56\x78\x9A\xBC\xDE\xF0");
|
||||
#endif
|
||||
}
|
||||
|
||||
|
@ -60,9 +60,11 @@ TEST_CASE("deserializeMsgPack() returns IncompleteInput") {
|
||||
checkAllSizes("\xCE\x00\x00\x00\x01", 5);
|
||||
}
|
||||
|
||||
#if ARDUINOJSON_USE_LONG_LONG
|
||||
SECTION("uint 64") {
|
||||
checkAllSizes("\xCF\x00\x00\x00\x00\x00\x00\x00\x00", 9);
|
||||
}
|
||||
#endif
|
||||
|
||||
SECTION("int 8") {
|
||||
checkAllSizes("\xD0\x01", 2);
|
||||
@ -76,9 +78,11 @@ TEST_CASE("deserializeMsgPack() returns IncompleteInput") {
|
||||
checkAllSizes("\xD2\x00\x00\x00\x01", 5);
|
||||
}
|
||||
|
||||
#if ARDUINOJSON_USE_LONG_LONG
|
||||
SECTION("int 64") {
|
||||
checkAllSizes("\xD3\x00\x00\x00\x00\x00\x00\x00\x00", 9);
|
||||
}
|
||||
#endif
|
||||
|
||||
SECTION("float 32") {
|
||||
checkAllSizes("\xCA\x40\x48\xF5\xC3", 5);
|
||||
|
@ -3,10 +3,9 @@
|
||||
# MIT License
|
||||
|
||||
add_executable(NumbersTests
|
||||
isFloat.cpp
|
||||
isInteger.cpp
|
||||
parseFloat.cpp
|
||||
parseInteger.cpp
|
||||
parseNumber.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(NumbersTests catch)
|
||||
|
@ -1,80 +0,0 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2019
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson/Numbers/isFloat.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace ARDUINOJSON_NAMESPACE;
|
||||
|
||||
TEST_CASE("isFloat()") {
|
||||
SECTION("Input is NULL") {
|
||||
REQUIRE(isFloat(NULL) == false);
|
||||
}
|
||||
|
||||
SECTION("Empty string") {
|
||||
REQUIRE(isFloat("") == false);
|
||||
}
|
||||
|
||||
SECTION("NoExponent") {
|
||||
REQUIRE(isFloat("3.14") == true);
|
||||
REQUIRE(isFloat("-3.14") == true);
|
||||
REQUIRE(isFloat("+3.14") == true);
|
||||
}
|
||||
|
||||
SECTION("IntegralPartMissing") {
|
||||
REQUIRE(isFloat(".14") == true);
|
||||
REQUIRE(isFloat("-.14") == true);
|
||||
REQUIRE(isFloat("+.14") == true);
|
||||
}
|
||||
|
||||
SECTION("FractionalPartMissing") {
|
||||
REQUIRE(isFloat("3.") == true);
|
||||
REQUIRE(isFloat("-3.e14") == true);
|
||||
REQUIRE(isFloat("+3.e-14") == true);
|
||||
}
|
||||
|
||||
SECTION("NoDot") {
|
||||
REQUIRE(isFloat("3e14") == true);
|
||||
REQUIRE(isFloat("3e-14") == true);
|
||||
REQUIRE(isFloat("3e+14") == true);
|
||||
}
|
||||
|
||||
SECTION("Integer") {
|
||||
REQUIRE(isFloat("14") == true);
|
||||
REQUIRE(isFloat("-14") == true);
|
||||
REQUIRE(isFloat("+14") == true);
|
||||
}
|
||||
|
||||
SECTION("ExponentMissing") {
|
||||
REQUIRE(isFloat("3.14e") == false);
|
||||
REQUIRE(isFloat("3.14e-") == false);
|
||||
REQUIRE(isFloat("3.14e+") == false);
|
||||
}
|
||||
|
||||
SECTION("JustASign") {
|
||||
REQUIRE(isFloat("-") == false);
|
||||
REQUIRE(isFloat("+") == false);
|
||||
}
|
||||
|
||||
SECTION("Empty") {
|
||||
REQUIRE(isFloat("") == false);
|
||||
}
|
||||
|
||||
SECTION("NaN") {
|
||||
REQUIRE(isFloat("NaN") == true);
|
||||
REQUIRE(isFloat("n") == false);
|
||||
REQUIRE(isFloat("N") == false);
|
||||
REQUIRE(isFloat("nan") == false);
|
||||
REQUIRE(isFloat("-NaN") == false);
|
||||
REQUIRE(isFloat("+NaN") == false);
|
||||
}
|
||||
|
||||
SECTION("Infinity") {
|
||||
REQUIRE(isFloat("Infinity") == true);
|
||||
REQUIRE(isFloat("+Infinity") == true);
|
||||
REQUIRE(isFloat("-Infinity") == true);
|
||||
REQUIRE(isFloat("infinity") == false);
|
||||
REQUIRE(isFloat("Inf") == false);
|
||||
}
|
||||
}
|
@ -1,40 +0,0 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2019
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson/Numbers/isInteger.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace ARDUINOJSON_NAMESPACE;
|
||||
|
||||
TEST_CASE("isInteger()") {
|
||||
SECTION("Null") {
|
||||
REQUIRE(isInteger(NULL) == false);
|
||||
}
|
||||
|
||||
SECTION("Empty string") {
|
||||
REQUIRE(isInteger("") == false);
|
||||
}
|
||||
|
||||
SECTION("FloatNotInteger") {
|
||||
REQUIRE(isInteger("3.14") == false);
|
||||
REQUIRE(isInteger("-3.14") == false);
|
||||
REQUIRE(isInteger("+3.14") == false);
|
||||
}
|
||||
|
||||
SECTION("Spaces") {
|
||||
REQUIRE(isInteger("42 ") == false);
|
||||
REQUIRE(isInteger(" 42") == false);
|
||||
}
|
||||
|
||||
SECTION("Valid") {
|
||||
REQUIRE(isInteger("42") == true);
|
||||
REQUIRE(isInteger("-42") == true);
|
||||
REQUIRE(isInteger("+42") == true);
|
||||
}
|
||||
|
||||
SECTION("ExtraSign") {
|
||||
REQUIRE(isInteger("--42") == false);
|
||||
REQUIRE(isInteger("++42") == false);
|
||||
}
|
||||
}
|
@ -2,6 +2,8 @@
|
||||
// Copyright Benoit Blanchon 2014-2019
|
||||
// MIT License
|
||||
|
||||
#define ARDUINOJSON_USE_DOUBLE 0
|
||||
|
||||
#include <ArduinoJson/Numbers/parseFloat.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
@ -33,10 +35,6 @@ void checkInf(const char* input, bool negative) {
|
||||
}
|
||||
|
||||
TEST_CASE("parseFloat<float>()") {
|
||||
SECTION("Null") {
|
||||
check<float>(NULL, 0);
|
||||
}
|
||||
|
||||
SECTION("Float_Short_NoExponent") {
|
||||
check<float>("3.14", 3.14f);
|
||||
check<float>("-3.14", -3.14f);
|
||||
@ -97,19 +95,13 @@ TEST_CASE("parseFloat<float>()") {
|
||||
checkInf<float>("inf", false);
|
||||
checkInf<float>("+inf", false);
|
||||
checkInf<float>("-inf", true);
|
||||
}
|
||||
|
||||
SECTION("Boolean") {
|
||||
check<float>("false", 0.0f);
|
||||
check<float>("true", 1.0f);
|
||||
checkInf<float>("1e300", false);
|
||||
checkInf<float>("-1e300", true);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("parseFloat<double>()") {
|
||||
SECTION("Null") {
|
||||
check<double>(NULL, 0);
|
||||
}
|
||||
|
||||
SECTION("Short_NoExponent") {
|
||||
check<double>("3.14", 3.14);
|
||||
check<double>("-3.14", -3.14);
|
||||
@ -169,9 +161,4 @@ TEST_CASE("parseFloat<double>()") {
|
||||
checkNaN<double>("NaN");
|
||||
checkNaN<double>("nan");
|
||||
}
|
||||
|
||||
SECTION("Boolean") {
|
||||
check<double>("false", 0.0);
|
||||
check<double>("true", 1.0);
|
||||
}
|
||||
}
|
||||
|
@ -21,11 +21,8 @@ TEST_CASE("parseInteger<int8_t>()") {
|
||||
check<int8_t>("+127", 127);
|
||||
check<int8_t>("3.14", 3);
|
||||
check<int8_t>("x42", 0);
|
||||
check<int8_t>("128", -128);
|
||||
check<int8_t>("-129", 127);
|
||||
check<int8_t>(NULL, 0);
|
||||
check<int8_t>("true", 1);
|
||||
check<int8_t>("false", 0);
|
||||
check<int8_t>("128", 0); // overflow
|
||||
check<int8_t>("-129", 0); // overflow
|
||||
}
|
||||
|
||||
TEST_CASE("parseInteger<int16_t>()") {
|
||||
@ -34,11 +31,8 @@ TEST_CASE("parseInteger<int16_t>()") {
|
||||
check<int16_t>("+32767", 32767);
|
||||
check<int16_t>("3.14", 3);
|
||||
check<int16_t>("x42", 0);
|
||||
check<int16_t>("-32769", 32767);
|
||||
check<int16_t>("32768", -32768);
|
||||
check<int16_t>(NULL, 0);
|
||||
check<int16_t>("true", 1);
|
||||
check<int16_t>("false", 0);
|
||||
check<int16_t>("-32769", 0); // overflow
|
||||
check<int16_t>("32768", 0); // overflow
|
||||
}
|
||||
|
||||
TEST_CASE("parseInteger<int32_t>()") {
|
||||
@ -47,10 +41,8 @@ TEST_CASE("parseInteger<int32_t>()") {
|
||||
check<int32_t>("+2147483647", 2147483647);
|
||||
check<int32_t>("3.14", 3);
|
||||
check<int32_t>("x42", 0);
|
||||
check<int32_t>("-2147483649", 2147483647);
|
||||
check<int32_t>("2147483648", (-2147483647 - 1));
|
||||
check<int32_t>("true", 1);
|
||||
check<int32_t>("false", 0);
|
||||
check<int32_t>("-2147483649", 0); // overflow
|
||||
check<int32_t>("2147483648", 0); // overflow
|
||||
}
|
||||
|
||||
TEST_CASE("parseInteger<uint8_t>()") {
|
||||
@ -59,10 +51,8 @@ TEST_CASE("parseInteger<uint8_t>()") {
|
||||
check<uint8_t>("+255", 255);
|
||||
check<uint8_t>("3.14", 3);
|
||||
check<uint8_t>("x42", 0);
|
||||
check<uint8_t>("-1", 255);
|
||||
check<uint8_t>("-1", 0);
|
||||
check<uint8_t>("256", 0);
|
||||
check<uint8_t>("true", 1);
|
||||
check<uint8_t>("false", 0);
|
||||
}
|
||||
|
||||
TEST_CASE("parseInteger<uint16_t>()") {
|
||||
@ -72,8 +62,6 @@ TEST_CASE("parseInteger<uint16_t>()") {
|
||||
check<uint16_t>("3.14", 3);
|
||||
// check<uint16_t>(" 42", 0);
|
||||
check<uint16_t>("x42", 0);
|
||||
check<uint16_t>("-1", 65535);
|
||||
check<uint16_t>("-1", 0);
|
||||
check<uint16_t>("65536", 0);
|
||||
check<uint16_t>("true", 1);
|
||||
check<uint16_t>("false", 0);
|
||||
}
|
||||
|
18
test/Numbers/parseNumber.cpp
Normal file
18
test/Numbers/parseNumber.cpp
Normal file
@ -0,0 +1,18 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2019
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson/Numbers/parseNumber.hpp>
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace ARDUINOJSON_NAMESPACE;
|
||||
|
||||
TEST_CASE("Test uint32_t overflow") {
|
||||
ParsedNumber<float, uint32_t> first =
|
||||
parseNumber<float, uint32_t>("4294967295");
|
||||
ParsedNumber<float, uint32_t> second =
|
||||
parseNumber<float, uint32_t>("4294967296");
|
||||
|
||||
REQUIRE(first.type() == uint8_t(VALUE_IS_POSITIVE_INTEGER));
|
||||
REQUIRE(second.type() == uint8_t(VALUE_IS_FLOAT));
|
||||
}
|
5
third-party/catch/CMakeLists.txt
vendored
5
third-party/catch/CMakeLists.txt
vendored
@ -11,3 +11,8 @@ target_include_directories(catch
|
||||
PUBLIC
|
||||
${CMAKE_CURRENT_SOURCE_DIR}
|
||||
)
|
||||
|
||||
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU")
|
||||
# prevent "xxx will change in GCC x.x" with arm-linux-gnueabihf-gcc
|
||||
target_compile_options(catch PRIVATE -Wno-psabi)
|
||||
endif()
|
||||
|
Reference in New Issue
Block a user