Compare commits

...

13 Commits

28 changed files with 275 additions and 164 deletions

View File

@ -1,17 +1,20 @@
sudo: false
language: c++ language: c++
compiler: compiler:
- gcc - gcc
- clang - clang
before_install: before_install:
- pip install --user cpp-coveralls
- mkdir -p /tmp/cmake
- curl https://cmake.org/files/v3.4/cmake-3.4.0-Linux-x86_64.tar.gz | tar xz -C /tmp/cmake --strip 1
- export PATH=/tmp/cmake/bin:$PATH
- "/sbin/start-stop-daemon --start --quiet --pidfile /tmp/custom_xvfb_1.pid --make-pidfile --background --exec /usr/bin/Xvfb -- :1 -ac -screen 0 1280x1024x16" - "/sbin/start-stop-daemon --start --quiet --pidfile /tmp/custom_xvfb_1.pid --make-pidfile --background --exec /usr/bin/Xvfb -- :1 -ac -screen 0 1280x1024x16"
- sleep 3 - sleep 3
- export DISPLAY=:1.0 - export DISPLAY=:1.0
- wget http://downloads.arduino.cc/arduino-1.6.5-linux64.tar.xz - mkdir -p /tmp/arduino
- tar xf arduino-1.6.5-linux64.tar.xz - curl http://downloads.arduino.cc/arduino-1.6.5-linux64.tar.xz | tar xJ -C /tmp/arduino --strip 1
- sudo mv arduino-1.6.5 /usr/local/share/arduino - export PATH=$PATH:/tmp/arduino/
- sudo ln -s /usr/local/share/arduino/arduino /usr/local/bin/arduino - ln -s $PWD /tmp/arduino/libraries/ArduinoJson
- sudo ln -s $PWD /usr/local/share/arduino/libraries/ArduinoJson
- sudo pip install cpp-coveralls
script: script:
- cmake -DCOVERAGE=true . && make && make test - cmake -DCOVERAGE=true . && make && make test
- arduino --verify --board arduino:avr:uno $PWD/examples/JsonParserExample/JsonParserExample.ino - arduino --verify --board arduino:avr:uno $PWD/examples/JsonParserExample/JsonParserExample.ino

View File

@ -1,6 +1,29 @@
ArduinoJson: change log ArduinoJson: change log
======================= =======================
v5.0.7
------
* Made library easier to use from a CMake project: simply `add_subdirectory(ArduinoJson/src)`
* Changed `String` to be a `typedef` of `std::string` (issues #142 and #161)
**BREAKING CHANGES**:
- `JsonVariant(true).as<String>()` now returns `"true"` instead of `"1"`
- `JsonVariant(false).as<String>()` now returns `"false"` instead of `"0"`
v5.0.6
------
* Added parameter to `DynamicJsonBuffer` constructor to set initial size (issue #152)
* Fixed warning about library category in Arduino 1.6.6 (issue #147)
* Examples: Added a loop to wait for serial port to be ready (issue #156)
v5.0.5
------
* Added overload `JsonObjectSuscript::set(value, decimals)` (issue #143)
* Use `float` instead of `double` to reduce the size of `JsonVariant` (issue #134)
v5.0.4 v5.0.4
------ ------

View File

@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 2.8.4) cmake_minimum_required(VERSION 2.8.12)
project(ArduinoJson) project(ArduinoJson)
enable_testing() enable_testing()
@ -7,10 +7,6 @@ set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/lib) set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
if(MSVC)
add_definitions(-D_CRT_SECURE_NO_WARNINGS -W4)
endif()
if(${COVERAGE}) if(${COVERAGE})
set(CMAKE_CXX_FLAGS "-g -O0 -fprofile-arcs -ftest-coverage") set(CMAKE_CXX_FLAGS "-g -O0 -fprofile-arcs -ftest-coverage")
endif() endif()

View File

@ -34,32 +34,36 @@ Quick start
#### Decoding / Parsing #### Decoding / Parsing
char json[] = "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}"; ```c++
char json[] = "{\"sensor\":\"gps\",\"time\":1351824120,\"data\":[48.756080,2.302038]}";
StaticJsonBuffer<200> jsonBuffer; StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(json); JsonObject& root = jsonBuffer.parseObject(json);
const char* sensor = root["sensor"]; const char* sensor = root["sensor"];
long time = root["time"]; long time = root["time"];
double latitude = root["data"][0]; double latitude = root["data"][0];
double longitude = root["data"][1]; double longitude = root["data"][1];
```
#### Encoding / Generating #### Encoding / Generating
StaticJsonBuffer<200> jsonBuffer; ```c++
StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.createObject(); JsonObject& root = jsonBuffer.createObject();
root["sensor"] = "gps"; root["sensor"] = "gps";
root["time"] = 1351824120; root["time"] = 1351824120;
JsonArray& data = root.createNestedArray("data"); JsonArray& data = root.createNestedArray("data");
data.add(48.756080, 6); // 6 is the number of decimals to print data.add(48.756080, 6); // 6 is the number of decimals to print
data.add(2.302038, 6); // if not specified, 2 digits are printed data.add(2.302038, 6); // if not specified, 2 digits are printed
root.printTo(Serial); root.printTo(Serial);
// This prints: // This prints:
// {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]} // {"sensor":"gps","time":1351824120,"data":[48.756080,2.302038]}
```
Documentation Documentation
@ -94,6 +98,18 @@ From GitHub user `zacsketches`:
> I am just starting an ESP8266 clock project and now I can output JSON from my server script and interpret it painlessly. > I am just starting an ESP8266 clock project and now I can output JSON from my server script and interpret it painlessly.
Donators
--------
Special thanks to the following persons and companies who made generous donations to the library author:
* Robert Murphy
* Surge Communications
* Alex Scott
* Firepick Services LLC
* A B Doodkorte
* Scott Smith
--- ---
Found this library useful? Please star this project or [help me back with a donation!](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=donate%40benoitblanchon%2efr&lc=GB&item_name=Benoit%20Blanchon&item_number=Arduino%20JSON&currency_code=EUR&bn=PP%2dDonationsBF%3abtn_donate_LG%2egif%3aNonHosted) :smile: Found this library useful? Please star this project or [help me back with a donation!](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=donate%40benoitblanchon%2efr&lc=GB&item_name=Benoit%20Blanchon&item_number=Arduino%20JSON&currency_code=EUR&bn=PP%2dDonationsBF%3abtn_donate_LG%2egif%3aNonHosted) :smile:

View File

@ -10,7 +10,9 @@ using namespace ArduinoJson::Internals;
void setup() { void setup() {
Serial.begin(9600); Serial.begin(9600);
// delay(1000); <--needed for some boards (like Teensy) while (!Serial) {
// wait serial port initialization
}
IndentedPrint serial(Serial); IndentedPrint serial(Serial);
serial.setTabSize(4); serial.setTabSize(4);

View File

@ -8,7 +8,9 @@
void setup() { void setup() {
Serial.begin(9600); Serial.begin(9600);
// delay(1000); <--needed for some boards (like Teensy) while (!Serial) {
// wait serial port initialization
}
StaticJsonBuffer<200> jsonBuffer; StaticJsonBuffer<200> jsonBuffer;

View File

@ -8,7 +8,9 @@
void setup() { void setup() {
Serial.begin(9600); Serial.begin(9600);
// delay(1000); <--needed for some boards (like Teensy) while (!Serial) {
// wait serial port initialization
}
StaticJsonBuffer<200> jsonBuffer; StaticJsonBuffer<200> jsonBuffer;

View File

@ -20,6 +20,7 @@ class Print {
size_t print(const char[]); size_t print(const char[]);
size_t print(double, int = 2); size_t print(double, int = 2);
size_t print(int);
size_t print(long); size_t print(long);
size_t println(); size_t println();
}; };

View File

@ -9,15 +9,7 @@
#ifndef ARDUINO #ifndef ARDUINO
#include <string> #include <string>
typedef std::string String;
// This class reproduces Arduino's String class
class String : public std::string {
public:
String(const char *cstr = "") : std::string(cstr) {}
String(const String &str) : std::string(str) {}
explicit String(long);
explicit String(double, unsigned char decimalPlaces = 2);
};
#else #else

View File

@ -31,7 +31,8 @@ class BlockJsonBuffer : public JsonBuffer {
}; };
public: public:
BlockJsonBuffer() : _head(NULL) {} BlockJsonBuffer(size_t initialSize = 256)
: _head(NULL), _nextBlockSize(initialSize) {}
~BlockJsonBuffer() { ~BlockJsonBuffer() {
Block* currentBlock = _head; Block* currentBlock = _head;
@ -55,8 +56,6 @@ class BlockJsonBuffer : public JsonBuffer {
} }
private: private:
static const size_t FIRST_BLOCK_CAPACITY = 32;
bool canAllocInHead(size_t bytes) const { bool canAllocInHead(size_t bytes) const {
return _head != NULL && _head->size + bytes <= _head->capacity; return _head != NULL && _head->size + bytes <= _head->capacity;
} }
@ -68,10 +67,10 @@ class BlockJsonBuffer : public JsonBuffer {
} }
void* allocInNewBlock(size_t bytes) { void* allocInNewBlock(size_t bytes) {
size_t capacity = FIRST_BLOCK_CAPACITY; size_t capacity = _nextBlockSize;
if (_head != NULL) capacity = _head->capacity * 2;
if (bytes > capacity) capacity = bytes; if (bytes > capacity) capacity = bytes;
if (!addNewBlock(capacity)) return NULL; if (!addNewBlock(capacity)) return NULL;
_nextBlockSize *= 2;
return allocInHead(bytes); return allocInHead(bytes);
} }
@ -86,8 +85,9 @@ class BlockJsonBuffer : public JsonBuffer {
return true; return true;
} }
Block* _head;
TAllocator _allocator; TAllocator _allocator;
Block* _head;
size_t _nextBlockSize;
}; };
} }
} }

View File

@ -0,0 +1,20 @@
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
#pragma once
namespace ArduinoJson {
namespace Internals {
#ifdef ARDUINO
// On embedded platform, we with use float instead of double to keep JsonVariant
// small (issue #134)
typedef float JsonFloat;
#else
typedef double JsonFloat;
#endif
}
}

View File

@ -0,0 +1,13 @@
// Copyright Benoit Blanchon 2014-2015
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
#pragma once
namespace ArduinoJson {
namespace Internals {
typedef long JsonInteger;
}
}

View File

@ -6,6 +6,9 @@
#pragma once #pragma once
#include "JsonFloat.hpp"
#include "JsonInteger.hpp"
namespace ArduinoJson { namespace ArduinoJson {
// Forward declarations // Forward declarations
@ -13,12 +16,11 @@ class JsonArray;
class JsonObject; class JsonObject;
namespace Internals { namespace Internals {
// A union that defines the actual content of a JsonVariant. // A union that defines the actual content of a JsonVariant.
// The enum JsonVariantType determines which member is in use. // The enum JsonVariantType determines which member is in use.
union JsonVariantContent { union JsonVariantContent {
double asDouble; // asDouble is also used for float JsonFloat asFloat; // used for double and float
long asLong; // asLong is also used for bool, char, short and int JsonInteger asInteger; // used for bool, char, short, int and longs
const char* asString; // asString can be null const char* asString; // asString can be null
JsonArray* asArray; // asArray cannot be null JsonArray* asArray; // asArray cannot be null
JsonObject* asObject; // asObject cannot be null JsonObject* asObject; // asObject cannot be null

View File

@ -19,17 +19,17 @@ enum JsonVariantType {
JSON_UNPARSED, // the JsonVariant contains an unparsed string JSON_UNPARSED, // the JsonVariant contains an unparsed string
JSON_STRING, // the JsonVariant stores a const char* JSON_STRING, // the JsonVariant stores a const char*
JSON_BOOLEAN, // the JsonVariant stores a bool JSON_BOOLEAN, // the JsonVariant stores a bool
JSON_LONG, // the JsonVariant stores a long JSON_INTEGER, // the JsonVariant stores an integer
JSON_ARRAY, // the JsonVariant stores a pointer to a JsonArray JSON_ARRAY, // the JsonVariant stores a pointer to a JsonArray
JSON_OBJECT, // the JsonVariant stores a pointer to a JsonObject JSON_OBJECT, // the JsonVariant stores a pointer to a JsonObject
// The following values are reserved for double values // The following values are reserved for float values
// Multiple values are used for double, depending on the number of decimal // Multiple values are used for double, depending on the number of decimal
// digits that must be printed in the JSON output. // digits that must be printed in the JSON output.
// This little trick allow to save one extra member in JsonVariant // This little trick allow to save one extra member in JsonVariant
JSON_DOUBLE_0_DECIMALS JSON_FLOAT_0_DECIMALS
// JSON_DOUBLE_1_DECIMAL // JSON_FLOAT_1_DECIMAL
// JSON_DOUBLE_2_DECIMALS // JSON_FLOAT_2_DECIMALS
// ... // ...
}; };
} }

View File

@ -9,6 +9,8 @@
#include "../Arduino/Print.hpp" #include "../Arduino/Print.hpp"
#include "Encoding.hpp" #include "Encoding.hpp"
#include "ForceInline.hpp" #include "ForceInline.hpp"
#include "JsonFloat.hpp"
#include "JsonInteger.hpp"
namespace ArduinoJson { namespace ArduinoJson {
namespace Internals { namespace Internals {
@ -60,9 +62,9 @@ class JsonWriter {
} }
} }
void writeLong(long value) { _length += _sink.print(value); } void writeInteger(JsonInteger value) { _length += _sink.print(value); }
void writeDouble(double value, uint8_t decimals) { void writeFloat(JsonFloat value, uint8_t decimals) {
_length += _sink.print(value, decimals); _length += _sink.print(value, decimals);
} }

View File

@ -14,7 +14,7 @@ namespace Internals {
// A Print implementation that allows to write in a char[] // A Print implementation that allows to write in a char[]
class StaticStringBuilder : public Print { class StaticStringBuilder : public Print {
public: public:
StaticStringBuilder(char *buf, int size) StaticStringBuilder(char *buf, size_t size)
: buffer(buf), capacity(size - 1), length(0) { : buffer(buf), capacity(size - 1), length(0) {
buffer[0] = '\0'; buffer[0] = '\0';
} }
@ -23,8 +23,8 @@ class StaticStringBuilder : public Print {
private: private:
char *buffer; char *buffer;
int capacity; size_t capacity;
int length; size_t length;
}; };
} }
} }

View File

@ -54,6 +54,11 @@ class JsonObjectSubscript
return _object.set(_key, value); return _object.set(_key, value);
} }
template <typename TValue>
FORCE_INLINE bool set(TValue value, uint8_t decimals) {
return _object.set(_key, value, decimals);
}
FORCE_INLINE JsonVariant get() { return _object.get(_key); } FORCE_INLINE JsonVariant get() { return _object.get(_key); }
void writeTo(Internals::JsonWriter& writer) const { void writeTo(Internals::JsonWriter& writer) const {

View File

@ -83,6 +83,9 @@ class JsonVariant : public JsonVariantBase<JsonVariant> {
static T invalid(); static T invalid();
private: private:
Internals::JsonFloat asFloat() const;
Internals::JsonInteger asInteger() const;
// The current type of the variant // The current type of the variant
Internals::JsonVariantType _type; Internals::JsonVariantType _type;

View File

@ -11,8 +11,9 @@
namespace ArduinoJson { namespace ArduinoJson {
inline JsonVariant::JsonVariant(bool value) { inline JsonVariant::JsonVariant(bool value) {
_type = Internals::JSON_BOOLEAN; using namespace Internals;
_content.asLong = value; _type = JSON_BOOLEAN;
_content.asInteger = static_cast<JsonInteger>(value);
} }
inline JsonVariant::JsonVariant(const char *value) { inline JsonVariant::JsonVariant(const char *value) {
@ -26,15 +27,15 @@ inline JsonVariant::JsonVariant(Internals::Unparsed value) {
} }
inline JsonVariant::JsonVariant(double value, uint8_t decimals) { inline JsonVariant::JsonVariant(double value, uint8_t decimals) {
_type = static_cast<Internals::JsonVariantType>( using namespace Internals;
Internals::JSON_DOUBLE_0_DECIMALS + decimals); _type = static_cast<JsonVariantType>(JSON_FLOAT_0_DECIMALS + decimals);
_content.asDouble = value; _content.asFloat = static_cast<JsonFloat>(value);
} }
inline JsonVariant::JsonVariant(float value, uint8_t decimals) { inline JsonVariant::JsonVariant(float value, uint8_t decimals) {
_type = static_cast<Internals::JsonVariantType>( using namespace Internals;
Internals::JSON_DOUBLE_0_DECIMALS + decimals); _type = static_cast<JsonVariantType>(JSON_FLOAT_0_DECIMALS + decimals);
_content.asDouble = value; _content.asFloat = static_cast<JsonFloat>(value);
} }
inline JsonVariant::JsonVariant(JsonArray &array) { inline JsonVariant::JsonVariant(JsonArray &array) {
@ -48,51 +49,53 @@ inline JsonVariant::JsonVariant(JsonObject &object) {
} }
inline JsonVariant::JsonVariant(signed char value) { inline JsonVariant::JsonVariant(signed char value) {
_type = Internals::JSON_LONG; using namespace Internals;
_content.asLong = value; _type = JSON_INTEGER;
_content.asInteger = static_cast<JsonInteger>(value);
} }
inline JsonVariant::JsonVariant(signed int value) { inline JsonVariant::JsonVariant(signed int value) {
_type = Internals::JSON_LONG; using namespace Internals;
_content.asLong = value; _type = JSON_INTEGER;
_content.asInteger = static_cast<JsonInteger>(value);
} }
inline JsonVariant::JsonVariant(signed long value) { inline JsonVariant::JsonVariant(signed long value) {
_type = Internals::JSON_LONG; using namespace Internals;
_content.asLong = value; _type = JSON_INTEGER;
_content.asInteger = static_cast<JsonInteger>(value);
} }
inline JsonVariant::JsonVariant(signed short value) { inline JsonVariant::JsonVariant(signed short value) {
_type = Internals::JSON_LONG; using namespace Internals;
_content.asLong = value; _type = JSON_INTEGER;
_content.asInteger = static_cast<JsonInteger>(value);
} }
inline JsonVariant::JsonVariant(unsigned char value) { inline JsonVariant::JsonVariant(unsigned char value) {
_type = Internals::JSON_LONG; using namespace Internals;
_content.asLong = value; _type = JSON_INTEGER;
_content.asInteger = static_cast<JsonInteger>(value);
} }
inline JsonVariant::JsonVariant(unsigned int value) { inline JsonVariant::JsonVariant(unsigned int value) {
_type = Internals::JSON_LONG; using namespace Internals;
_content.asLong = value; _type = JSON_INTEGER;
_content.asInteger = static_cast<JsonInteger>(value);
} }
inline JsonVariant::JsonVariant(unsigned long value) { inline JsonVariant::JsonVariant(unsigned long value) {
_type = Internals::JSON_LONG; using namespace Internals;
_content.asLong = value; _type = JSON_INTEGER;
_content.asInteger = static_cast<JsonInteger>(value);
} }
inline JsonVariant::JsonVariant(unsigned short value) { inline JsonVariant::JsonVariant(unsigned short value) {
_type = Internals::JSON_LONG; using namespace Internals;
_content.asLong = value; _type = JSON_INTEGER;
_content.asInteger = static_cast<JsonInteger>(value);
} }
template <>
double JsonVariant::as<double>() const;
template <>
long JsonVariant::as<long>() const;
template <> template <>
String JsonVariant::as<String>() const; String JsonVariant::as<String>() const;
@ -101,47 +104,57 @@ const char *JsonVariant::as<const char *>() const;
template <> template <>
inline bool JsonVariant::as<bool>() const { inline bool JsonVariant::as<bool>() const {
return as<long>() != 0; return asInteger() != 0;
} }
template <> template <>
inline signed char JsonVariant::as<signed char>() const { inline signed char JsonVariant::as<signed char>() const {
return static_cast<signed char>(as<long>()); return static_cast<signed char>(asInteger());
} }
template <> template <>
inline unsigned char JsonVariant::as<unsigned char>() const { inline unsigned char JsonVariant::as<unsigned char>() const {
return static_cast<unsigned char>(as<long>()); return static_cast<unsigned char>(asInteger());
} }
template <> template <>
inline signed short JsonVariant::as<signed short>() const { inline signed short JsonVariant::as<signed short>() const {
return static_cast<signed short>(as<long>()); return static_cast<signed short>(asInteger());
} }
template <> template <>
inline unsigned short JsonVariant::as<unsigned short>() const { inline unsigned short JsonVariant::as<unsigned short>() const {
return static_cast<unsigned short>(as<long>()); return static_cast<unsigned short>(asInteger());
} }
template <> template <>
inline signed int JsonVariant::as<signed int>() const { inline signed int JsonVariant::as<signed int>() const {
return static_cast<signed int>(as<long>()); return static_cast<signed int>(asInteger());
} }
template <> template <>
inline unsigned int JsonVariant::as<unsigned int>() const { inline unsigned int JsonVariant::as<unsigned int>() const {
return static_cast<unsigned int>(as<long>()); return static_cast<unsigned int>(asInteger());
} }
template <> template <>
inline unsigned long JsonVariant::as<unsigned long>() const { inline unsigned long JsonVariant::as<unsigned long>() const {
return static_cast<unsigned long>(as<long>()); return static_cast<unsigned long>(asInteger());
}
template <>
inline signed long JsonVariant::as<signed long>() const {
return static_cast<unsigned long>(asInteger());
}
template <>
inline double JsonVariant::as<double>() const {
return static_cast<double>(asFloat());
} }
template <> template <>
inline float JsonVariant::as<float>() const { inline float JsonVariant::as<float>() const {
return static_cast<float>(as<double>()); return static_cast<float>(asFloat());
} }
template <typename T> template <typename T>

View File

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

View File

@ -60,6 +60,12 @@ size_t Print::print(long value) {
return print(tmp); return print(tmp);
} }
size_t Print::print(int value) {
char tmp[32];
sprintf(tmp, "%d", value);
return print(tmp);
}
size_t Print::println() { return write('\r') + write('\n'); } size_t Print::println() { return write('\r') + write('\n'); }
#endif #endif

View File

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

View File

@ -48,4 +48,12 @@ if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
) )
endif() endif()
if(MSVC)
add_definitions(
-D_CRT_SECURE_NO_WARNINGS
-W4)
endif()
add_library(ArduinoJson ${CPP_FILES} ${HPP_FILES} ${IPP_FILES}) add_library(ArduinoJson ${CPP_FILES} ${HPP_FILES} ${IPP_FILES})
target_include_directories(ArduinoJson INTERFACE ${CMAKE_CURRENT_LIST_DIR}/../include)

View File

@ -16,6 +16,29 @@ using namespace ArduinoJson::Internals;
namespace ArduinoJson { namespace ArduinoJson {
template <typename TFloat>
static TFloat parse(const char *);
template <>
float parse<float>(const char *s) {
return static_cast<float>(strtod(s, NULL));
}
template <>
double parse<double>(const char *s) {
return strtod(s, NULL);
}
template <>
long parse<long>(const char *s) {
return strtol(s, NULL, 10);
}
template <>
int parse<int>(const char *s) {
return atoi(s);
}
template <> template <>
const char *JsonVariant::as<const char *>() const { const char *JsonVariant::as<const char *>() const {
if (_type == JSON_UNPARSED && _content.asString && if (_type == JSON_UNPARSED && _content.asString &&
@ -25,29 +48,27 @@ const char *JsonVariant::as<const char *>() const {
return NULL; return NULL;
} }
template <> JsonFloat JsonVariant::asFloat() const {
double JsonVariant::as<double>() const { if (_type >= JSON_FLOAT_0_DECIMALS) return _content.asFloat;
if (_type >= JSON_DOUBLE_0_DECIMALS) return _content.asDouble;
if (_type == JSON_LONG || _type == JSON_BOOLEAN) if (_type == JSON_INTEGER || _type == JSON_BOOLEAN)
return static_cast<double>(_content.asLong); return static_cast<JsonFloat>(_content.asInteger);
if ((_type == JSON_STRING || _type == JSON_UNPARSED) && _content.asString) if ((_type == JSON_STRING || _type == JSON_UNPARSED) && _content.asString)
return strtod(_content.asString, NULL); return parse<JsonFloat>(_content.asString);
return 0.0; return 0.0;
} }
template <> JsonInteger JsonVariant::asInteger() const {
long JsonVariant::as<long>() const { if (_type == JSON_INTEGER || _type == JSON_BOOLEAN) return _content.asInteger;
if (_type == JSON_LONG || _type == JSON_BOOLEAN) return _content.asLong;
if (_type >= JSON_DOUBLE_0_DECIMALS) if (_type >= JSON_FLOAT_0_DECIMALS)
return static_cast<long>(_content.asDouble); return static_cast<JsonInteger>(_content.asFloat);
if ((_type == JSON_STRING || _type == JSON_UNPARSED) && _content.asString) { if ((_type == JSON_STRING || _type == JSON_UNPARSED) && _content.asString) {
if (!strcmp("true", _content.asString)) return 1; if (!strcmp("true", _content.asString)) return 1;
return strtol(_content.asString, NULL, 10); return parse<JsonInteger>(_content.asString);
} }
return 0L; return 0L;
@ -55,26 +76,18 @@ long JsonVariant::as<long>() const {
template <> template <>
String JsonVariant::as<String>() const { String JsonVariant::as<String>() const {
String s;
if ((_type == JSON_STRING || _type == JSON_UNPARSED) && if ((_type == JSON_STRING || _type == JSON_UNPARSED) &&
_content.asString != NULL) _content.asString != NULL)
return String(_content.asString); s = _content.asString;
else
if (_type == JSON_LONG || _type == JSON_BOOLEAN)
return String(_content.asLong);
if (_type >= JSON_DOUBLE_0_DECIMALS) {
uint8_t decimals = static_cast<uint8_t>(_type - JSON_DOUBLE_0_DECIMALS);
return String(_content.asDouble, decimals);
}
String s;
printTo(s); printTo(s);
return s; return s;
} }
template <> template <>
bool JsonVariant::is<signed long>() const { bool JsonVariant::is<signed long>() const {
if (_type == JSON_LONG) return true; if (_type == JSON_INTEGER) return true;
if (_type != JSON_UNPARSED || _content.asString == NULL) return false; if (_type != JSON_UNPARSED || _content.asString == NULL) return false;
@ -87,7 +100,7 @@ bool JsonVariant::is<signed long>() const {
template <> template <>
bool JsonVariant::is<double>() const { bool JsonVariant::is<double>() const {
if (_type >= JSON_DOUBLE_0_DECIMALS) return true; if (_type >= JSON_FLOAT_0_DECIMALS) return true;
if (_type != JSON_UNPARSED || _content.asString == NULL) return false; if (_type != JSON_UNPARSED || _content.asString == NULL) return false;
@ -111,15 +124,15 @@ void JsonVariant::writeTo(JsonWriter &writer) const {
else if (_type == JSON_UNPARSED) else if (_type == JSON_UNPARSED)
writer.writeRaw(_content.asString); writer.writeRaw(_content.asString);
else if (_type == JSON_LONG) else if (_type == JSON_INTEGER)
writer.writeLong(_content.asLong); writer.writeInteger(_content.asInteger);
else if (_type == JSON_BOOLEAN) else if (_type == JSON_BOOLEAN)
writer.writeBoolean(_content.asLong != 0); writer.writeBoolean(_content.asInteger != 0);
else if (_type >= JSON_DOUBLE_0_DECIMALS) { else if (_type >= JSON_FLOAT_0_DECIMALS) {
uint8_t decimals = static_cast<uint8_t>(_type - JSON_DOUBLE_0_DECIMALS); uint8_t decimals = static_cast<uint8_t>(_type - JSON_FLOAT_0_DECIMALS);
writer.writeDouble(_content.asDouble, decimals); writer.writeFloat(_content.asFloat, decimals);
} }
} }
} }

View File

@ -1,10 +1,8 @@
set(GTEST_DIR ../third-party/gtest-1.7.0) set(GTEST_DIR ../third-party/gtest-1.7.0)
file(GLOB_RECURSE INC_FILES ../include/*.h)
file(GLOB TESTS_FILES *.hpp *.cpp) file(GLOB TESTS_FILES *.hpp *.cpp)
include_directories( include_directories(
../include
${GTEST_DIR} ${GTEST_DIR}
${GTEST_DIR}/include) ${GTEST_DIR}/include)
@ -17,7 +15,6 @@ endif()
add_executable(ArduinoJsonTests add_executable(ArduinoJsonTests
${TESTS_FILES} ${TESTS_FILES}
${INC_FILES}
${GTEST_DIR}/src/gtest-all.cc ${GTEST_DIR}/src/gtest-all.cc
${GTEST_DIR}/src/gtest_main.cc) ${GTEST_DIR}/src/gtest_main.cc)

View File

@ -46,6 +46,14 @@ TEST_(StoreDouble) {
EXPECT_FALSE(_object["hello"].is<long>()); EXPECT_FALSE(_object["hello"].is<long>());
} }
TEST_(StoreDoubleWithDigits) {
_object.set("hello", 123.45, 2);
EXPECT_EQ(123.45, _object["hello"].as<double>());
EXPECT_TRUE(_object["hello"].is<double>());
EXPECT_FALSE(_object["hello"].is<long>());
}
TEST_(StoreBoolean) { TEST_(StoreBoolean) {
_object.set("hello", true); _object.set("hello", true);

View File

@ -46,6 +46,14 @@ TEST_(StoreDouble) {
EXPECT_EQ(123.45, _object["hello"].as<double>()); EXPECT_EQ(123.45, _object["hello"].as<double>());
} }
TEST_(StoreDoubleWithDigits) {
_object["hello"].set(123.45, 2);
EXPECT_TRUE(_object["hello"].is<double>());
EXPECT_FALSE(_object["hello"].is<long>());
EXPECT_EQ(123.45, _object["hello"].as<double>());
}
TEST_(StoreBoolean) { TEST_(StoreBoolean) {
_object["hello"] = true; _object["hello"] = true;

View File

@ -57,7 +57,7 @@ TEST(JsonVariant_As_Tests, FalseAsLong) {
TEST(JsonVariant_As_Tests, FalseAsString) { TEST(JsonVariant_As_Tests, FalseAsString) {
JsonVariant variant = false; JsonVariant variant = false;
ASSERT_EQ(String("0"), variant.as<String>()); ASSERT_EQ(String("false"), variant.as<String>());
} }
TEST(JsonVariant_As_Tests, TrueAsBool) { TEST(JsonVariant_As_Tests, TrueAsBool) {
@ -77,7 +77,7 @@ TEST(JsonVariant_As_Tests, TrueAsLong) {
TEST(JsonVariant_As_Tests, TrueAsString) { TEST(JsonVariant_As_Tests, TrueAsString) {
JsonVariant variant = true; JsonVariant variant = true;
ASSERT_EQ(String("1"), variant.as<String>()); ASSERT_EQ(String("true"), variant.as<String>());
} }
TEST(JsonVariant_As_Tests, LongAsBool) { TEST(JsonVariant_As_Tests, LongAsBool) {