mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-03 13:46:46 +02:00
Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
a498abc14a | |||
c64340a9bb | |||
79d80a5dbf | |||
7ebff5949f | |||
434080572c | |||
c87a0e97ab | |||
2dbd94951c |
20
CHANGELOG.md
20
CHANGELOG.md
@ -1,6 +1,18 @@
|
||||
ArduinoJson: change log
|
||||
=======================
|
||||
|
||||
v5.6.4
|
||||
------
|
||||
|
||||
* Fixed error in float serialization (issue #324)
|
||||
|
||||
v5.6.3
|
||||
------
|
||||
|
||||
* Improved speed of float serialization (about twice faster)
|
||||
* Added `as<JsonArray>()` as a synonym for `as<JsonArray&>()`... (issue #291)
|
||||
* Fixed `call of overloaded isinf(double&) is ambiguous` (issue #284)
|
||||
|
||||
v5.6.2
|
||||
------
|
||||
|
||||
@ -105,7 +117,7 @@ v5.0.3
|
||||
v5.0.2
|
||||
------
|
||||
|
||||
* Fixed segmentation fault in `parseObject(String)` and `parseArray(String)`, when the
|
||||
* Fixed segmentation fault in `parseObject(String)` and `parseArray(String)`, when the
|
||||
`StaticJsonBuffer` is too small to hold a copy of the string
|
||||
* Fixed Clang warning "register specifier is deprecated" (issue #102)
|
||||
* Fixed GCC warning "declaration shadows a member" (issue #103)
|
||||
@ -221,14 +233,14 @@ v3.1
|
||||
|
||||
Old generator API:
|
||||
|
||||
JsonObject<3> root;
|
||||
JsonObject<3> root;
|
||||
root.add("sensor", "gps");
|
||||
root.add("time", 1351824120);
|
||||
root.add("data", array);
|
||||
|
||||
New generator API:
|
||||
|
||||
JsonObject<3> root;
|
||||
JsonObject<3> root;
|
||||
root["sensor"] = "gps";
|
||||
root["time"] = 1351824120;
|
||||
root["data"] = array;
|
||||
@ -285,7 +297,7 @@ v1.1
|
||||
* Example: changed `char* json` into `char[] json` so that the bytes are not write protected
|
||||
* Fixed parsing bug when the JSON contains multi-dimensional arrays
|
||||
|
||||
v1.0
|
||||
v1.0
|
||||
----
|
||||
|
||||
Initial release
|
||||
|
@ -1,11 +1,13 @@
|
||||
version: 5.6.2.{build}
|
||||
version: 5.6.4.{build}
|
||||
environment:
|
||||
matrix:
|
||||
- CMAKE_GENERATOR: Visual Studio 14 2015
|
||||
- CMAKE_GENERATOR: Visual Studio 12 2013
|
||||
- CMAKE_GENERATOR: Visual Studio 11 2012
|
||||
- CMAKE_GENERATOR: Visual Studio 10 2010
|
||||
- CMAKE_GENERATOR: MinGW Makefiles
|
||||
# - CMAKE_GENERATOR: MinGW Makefiles
|
||||
# Note: Disabled because of unexplicated error -1073741511
|
||||
# This used to work fine with GCC 4.8.2 then failed after they upgraded to GCC 4.9.3
|
||||
configuration: Debug
|
||||
before_build:
|
||||
- set PATH=C:\MinGW\bin;%PATH:C:\Program Files\Git\usr\bin;=% # Workaround for CMake not wanting sh.exe on PATH for MinGW
|
||||
|
45
include/ArduinoJson/Internals/JsonVariantAs.hpp
Normal file
45
include/ArduinoJson/Internals/JsonVariantAs.hpp
Normal file
@ -0,0 +1,45 @@
|
||||
// Copyright Benoit Blanchon 2014-2016
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace ArduinoJson {
|
||||
namespace Internals {
|
||||
|
||||
// A metafunction that returns the type of the value returned by
|
||||
// JsonVariant::as<T>()
|
||||
template <typename T>
|
||||
struct JsonVariantAs {
|
||||
typedef T type;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct JsonVariantAs<char*> {
|
||||
typedef const char* type;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct JsonVariantAs<JsonArray> {
|
||||
typedef JsonArray& type;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct JsonVariantAs<const JsonArray> {
|
||||
typedef const JsonArray& type;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct JsonVariantAs<JsonObject> {
|
||||
typedef JsonObject& type;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct JsonVariantAs<const JsonObject> {
|
||||
typedef const JsonObject& type;
|
||||
};
|
||||
}
|
||||
}
|
@ -81,7 +81,7 @@ class JsonWriter {
|
||||
}
|
||||
}
|
||||
|
||||
void writeFloat(JsonFloat value, int digits = 2) {
|
||||
void writeFloat(JsonFloat value, uint8_t digits = 2) {
|
||||
if (Polyfills::isNaN(value)) return writeRaw("NaN");
|
||||
|
||||
if (value < 0.0) {
|
||||
@ -98,11 +98,8 @@ class JsonWriter {
|
||||
powersOf10 = 0;
|
||||
}
|
||||
|
||||
// Round correctly so that print(1.999, 2) prints as "2.00"
|
||||
JsonFloat rounding = 0.5;
|
||||
for (uint8_t i = 0; i < digits; ++i) rounding /= 10.0;
|
||||
|
||||
value += rounding;
|
||||
// Round up last digit (so that print(1.999, 2) prints as "2.00")
|
||||
value += getRoundingBias(digits);
|
||||
|
||||
// Extract the integer part of the value and print it
|
||||
JsonUInt int_part = static_cast<JsonUInt>(value);
|
||||
@ -116,10 +113,13 @@ class JsonWriter {
|
||||
|
||||
// Extract digits from the remainder one at a time
|
||||
while (digits-- > 0) {
|
||||
// Extract digit
|
||||
remainder *= 10.0;
|
||||
JsonUInt toPrint = JsonUInt(remainder);
|
||||
writeInteger(JsonUInt(remainder));
|
||||
remainder -= static_cast<JsonFloat>(toPrint);
|
||||
char currentDigit = char(remainder);
|
||||
remainder -= static_cast<JsonFloat>(currentDigit);
|
||||
|
||||
// Print
|
||||
writeRaw(char('0' + currentDigit));
|
||||
}
|
||||
|
||||
if (powersOf10 < 0) {
|
||||
@ -135,16 +135,15 @@ class JsonWriter {
|
||||
|
||||
void writeInteger(JsonUInt value) {
|
||||
char buffer[22];
|
||||
char *ptr = buffer + sizeof(buffer) - 1;
|
||||
|
||||
uint8_t i = 0;
|
||||
*ptr = 0;
|
||||
do {
|
||||
buffer[i++] = static_cast<char>(value % 10 + '0');
|
||||
*--ptr = static_cast<char>(value % 10 + '0');
|
||||
value /= 10;
|
||||
} while (value);
|
||||
|
||||
while (i > 0) {
|
||||
writeRaw(buffer[--i]);
|
||||
}
|
||||
writeRaw(ptr);
|
||||
}
|
||||
|
||||
void writeRaw(const char *s) {
|
||||
@ -160,6 +159,26 @@ class JsonWriter {
|
||||
|
||||
private:
|
||||
JsonWriter &operator=(const JsonWriter &); // cannot be assigned
|
||||
|
||||
static JsonFloat getLastDigit(uint8_t digits) {
|
||||
// Designed as a compromise between code size and speed
|
||||
switch (digits) {
|
||||
case 0:
|
||||
return 1e-0;
|
||||
case 1:
|
||||
return 1e-1;
|
||||
case 2:
|
||||
return 1e-2;
|
||||
case 3:
|
||||
return 1e-3;
|
||||
default:
|
||||
return getLastDigit(uint8_t(digits - 4)) * 1e-4;
|
||||
}
|
||||
}
|
||||
|
||||
FORCE_INLINE static JsonFloat getRoundingBias(uint8_t digits) {
|
||||
return 0.5 * getLastDigit(digits);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -137,7 +137,7 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
|
||||
|
||||
// Gets the value at the specified index.
|
||||
template <typename T>
|
||||
T get(size_t index) const {
|
||||
typename Internals::JsonVariantAs<T>::type get(size_t index) const {
|
||||
node_type *node = getNodeAt(index);
|
||||
return node ? node->content.as<T>() : JsonVariant::defaultValue<T>();
|
||||
}
|
||||
|
@ -21,13 +21,23 @@ inline bool JsonArray::setNodeValue(node_type *node, String &value) {
|
||||
return true;
|
||||
}
|
||||
|
||||
template <>
|
||||
inline JsonArray &JsonVariant::defaultValue<JsonArray>() {
|
||||
return JsonArray::invalid();
|
||||
}
|
||||
|
||||
template <>
|
||||
inline JsonArray &JsonVariant::defaultValue<JsonArray &>() {
|
||||
return JsonArray::invalid();
|
||||
}
|
||||
|
||||
template <>
|
||||
inline JsonArray const &JsonVariant::defaultValue<JsonArray const &>() {
|
||||
inline const JsonArray &JsonVariant::defaultValue<const JsonArray>() {
|
||||
return JsonArray::invalid();
|
||||
}
|
||||
|
||||
template <>
|
||||
inline const JsonArray &JsonVariant::defaultValue<const JsonArray &>() {
|
||||
return JsonArray::invalid();
|
||||
}
|
||||
|
||||
|
@ -42,12 +42,16 @@ class JsonArraySubscript : public JsonVariantBase<JsonArraySubscript> {
|
||||
return *this;
|
||||
}
|
||||
|
||||
FORCE_INLINE bool success() const { return _index < _array.size(); }
|
||||
FORCE_INLINE bool success() const {
|
||||
return _index < _array.size();
|
||||
}
|
||||
|
||||
FORCE_INLINE operator JsonVariant() const { return _array.get(_index); }
|
||||
FORCE_INLINE operator JsonVariant() const {
|
||||
return _array.get(_index);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
FORCE_INLINE T as() const {
|
||||
FORCE_INLINE typename Internals::JsonVariantAs<T>::type as() const {
|
||||
return _array.get<T>(_index);
|
||||
}
|
||||
|
||||
|
@ -106,7 +106,7 @@ class JsonObject : public Internals::JsonPrintable<JsonObject>,
|
||||
|
||||
// Gets the value associated with the specified key.
|
||||
template <typename T>
|
||||
T get(JsonObjectKey key) const {
|
||||
typename Internals::JsonVariantAs<T>::type get(JsonObjectKey key) const {
|
||||
node_type* node = getNodeAt(key.c_str());
|
||||
return node ? node->content.value.as<T>() : JsonVariant::defaultValue<T>();
|
||||
}
|
||||
|
@ -26,7 +26,12 @@ inline bool JsonObject::setNodeValue(node_type *node, const String &value) {
|
||||
}
|
||||
|
||||
template <>
|
||||
inline JsonObject const &JsonVariant::defaultValue<JsonObject const &>() {
|
||||
inline const JsonObject &JsonVariant::defaultValue<const JsonObject &>() {
|
||||
return JsonObject::invalid();
|
||||
}
|
||||
|
||||
template <>
|
||||
inline const JsonObject &JsonVariant::defaultValue<const JsonObject>() {
|
||||
return JsonObject::invalid();
|
||||
}
|
||||
|
||||
@ -35,6 +40,11 @@ inline JsonObject &JsonVariant::defaultValue<JsonObject &>() {
|
||||
return JsonObject::invalid();
|
||||
}
|
||||
|
||||
template <>
|
||||
inline JsonObject &JsonVariant::defaultValue<JsonObject>() {
|
||||
return JsonObject::invalid();
|
||||
}
|
||||
|
||||
inline JsonObject &JsonVariant::asObject() const {
|
||||
if (_type == Internals::JSON_OBJECT) return *_content.asObject;
|
||||
return JsonObject::invalid();
|
||||
|
@ -54,7 +54,7 @@ class JsonObjectSubscript : public JsonVariantBase<JsonObjectSubscript<TKey> > {
|
||||
}
|
||||
|
||||
template <typename TValue>
|
||||
FORCE_INLINE TValue as() const {
|
||||
FORCE_INLINE typename Internals::JsonVariantAs<TValue>::type as() const {
|
||||
return _object.get<TValue>(_key);
|
||||
}
|
||||
|
||||
|
@ -177,28 +177,43 @@ class JsonVariant : public JsonVariantBase<JsonVariant> {
|
||||
//
|
||||
// JsonArray& as<JsonArray> const;
|
||||
// JsonArray& as<JsonArray&> const;
|
||||
// JsonArray& as<const JsonArray&> const;
|
||||
template <typename T>
|
||||
typename TypeTraits::EnableIf<
|
||||
TypeTraits::IsSame<
|
||||
typename TypeTraits::RemoveConst<
|
||||
typename TypeTraits::RemoveReference<T>::type>::type,
|
||||
JsonArray>::value,
|
||||
TypeTraits::IsSame<typename TypeTraits::RemoveReference<T>::type,
|
||||
JsonArray>::value,
|
||||
JsonArray &>::type
|
||||
as() const {
|
||||
return asArray();
|
||||
}
|
||||
//
|
||||
// const JsonArray& as<const JsonArray&> const;
|
||||
template <typename T>
|
||||
typename TypeTraits::EnableIf<
|
||||
TypeTraits::IsSame<typename TypeTraits::RemoveReference<T>::type,
|
||||
const JsonArray>::value,
|
||||
const JsonArray &>::type
|
||||
as() const {
|
||||
return asArray();
|
||||
}
|
||||
//
|
||||
// JsonObject& as<JsonObject> const;
|
||||
// JsonObject& as<JsonObject&> const;
|
||||
template <typename T>
|
||||
typename TypeTraits::EnableIf<
|
||||
TypeTraits::IsSame<typename TypeTraits::RemoveReference<T>::type,
|
||||
JsonObject>::value,
|
||||
JsonObject &>::type
|
||||
as() const {
|
||||
return asObject();
|
||||
}
|
||||
//
|
||||
// JsonObject& as<const JsonObject> const;
|
||||
// JsonObject& as<const JsonObject&> const;
|
||||
template <typename T>
|
||||
typename TypeTraits::EnableIf<
|
||||
TypeTraits::IsSame<
|
||||
typename TypeTraits::RemoveConst<
|
||||
typename TypeTraits::RemoveReference<T>::type>::type,
|
||||
JsonObject>::value,
|
||||
JsonObject &>::type
|
||||
TypeTraits::IsSame<typename TypeTraits::RemoveReference<T>::type,
|
||||
const JsonObject>::value,
|
||||
const JsonObject &>::type
|
||||
as() const {
|
||||
return asObject();
|
||||
}
|
||||
@ -281,7 +296,7 @@ class JsonVariant : public JsonVariantBase<JsonVariant> {
|
||||
|
||||
// Value returned if the variant has an incompatible type
|
||||
template <typename T>
|
||||
static T defaultValue() {
|
||||
static typename Internals::JsonVariantAs<T>::type defaultValue() {
|
||||
return T();
|
||||
}
|
||||
|
||||
|
@ -7,8 +7,9 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Polyfills/attributes.hpp"
|
||||
#include "Internals/JsonVariantAs.hpp"
|
||||
#include "JsonObjectKey.hpp"
|
||||
#include "Polyfills/attributes.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
|
||||
@ -20,20 +21,35 @@ class JsonObjectSubscript;
|
||||
template <typename TImpl>
|
||||
class JsonVariantBase : public Internals::JsonPrintable<TImpl> {
|
||||
public:
|
||||
FORCE_INLINE const char *asString() const { return as<const char *>(); }
|
||||
// DEPRECATED: use as<char*>() instead
|
||||
FORCE_INLINE const char *asString() const {
|
||||
return as<const char *>();
|
||||
}
|
||||
|
||||
// Gets the variant as an array.
|
||||
// Returns a reference to the JsonArray or JsonArray::invalid() if the
|
||||
// variant
|
||||
// is not an array.
|
||||
FORCE_INLINE operator JsonArray &() const { return as<JsonArray &>(); }
|
||||
FORCE_INLINE JsonArray &asArray() const { return as<JsonArray &>(); }
|
||||
FORCE_INLINE operator JsonArray &() const {
|
||||
return as<JsonArray &>();
|
||||
}
|
||||
|
||||
// DEPRECATED: use as<JsonArray>() instead
|
||||
FORCE_INLINE JsonArray &asArray() const {
|
||||
return as<JsonArray &>();
|
||||
}
|
||||
|
||||
// Gets the variant as an object.
|
||||
// Returns a reference to the JsonObject or JsonObject::invalid() if the
|
||||
// variant is not an object.
|
||||
FORCE_INLINE operator JsonObject &() const { return as<JsonObject &>(); }
|
||||
FORCE_INLINE JsonObject &asObject() const { return as<JsonObject &>(); }
|
||||
FORCE_INLINE operator JsonObject &() const {
|
||||
return as<JsonObject &>();
|
||||
}
|
||||
|
||||
// DEPRECATED: use as<JsonObject>() instead
|
||||
FORCE_INLINE JsonObject &asObject() const {
|
||||
return as<JsonObject &>();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
FORCE_INLINE operator T() const {
|
||||
@ -41,14 +57,16 @@ class JsonVariantBase : public Internals::JsonPrintable<TImpl> {
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
FORCE_INLINE const T as() const {
|
||||
FORCE_INLINE const typename Internals::JsonVariantAs<T>::type as() const {
|
||||
return impl()->template as<T>();
|
||||
}
|
||||
|
||||
// Mimics an array or an object.
|
||||
// Returns the size of the array or object if the variant has that type.
|
||||
// Returns 0 if the variant is neither an array nor an object
|
||||
size_t size() const { return asArray().size() + asObject().size(); }
|
||||
size_t size() const {
|
||||
return asArray().size() + asObject().size();
|
||||
}
|
||||
|
||||
// Mimics an array.
|
||||
// Returns the element at specified index if the variant is an array.
|
||||
@ -68,7 +86,9 @@ class JsonVariantBase : public Internals::JsonPrintable<TImpl> {
|
||||
void writeTo(Internals::JsonWriter &writer) const;
|
||||
|
||||
private:
|
||||
const TImpl *impl() const { return static_cast<const TImpl *>(this); }
|
||||
const TImpl *impl() const {
|
||||
return static_cast<const TImpl *>(this);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename TImpl, typename TComparand>
|
||||
|
@ -62,6 +62,20 @@ bool isNaN(T x) {
|
||||
return isnan(x);
|
||||
}
|
||||
|
||||
#if defined(_GLIBCXX_HAVE_ISNANL) && _GLIBCXX_HAVE_ISNANL
|
||||
template <>
|
||||
inline bool isNaN<double>(double x) {
|
||||
return isnanl(x);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(_GLIBCXX_HAVE_ISNANF) && _GLIBCXX_HAVE_ISNANF
|
||||
template <>
|
||||
inline bool isNaN<float>(float x) {
|
||||
return isnanf(x);
|
||||
}
|
||||
#endif
|
||||
|
||||
template <typename T>
|
||||
bool isInfinity(T x) {
|
||||
// Workaround for libs that #undef isinf
|
||||
@ -73,6 +87,20 @@ bool isInfinity(T x) {
|
||||
return isinf(x);
|
||||
}
|
||||
|
||||
#if defined(_GLIBCXX_HAVE_ISINFL) && _GLIBCXX_HAVE_ISINFL
|
||||
template <>
|
||||
inline bool isInfinity<double>(double x) {
|
||||
return isinfl(x);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(_GLIBCXX_HAVE_ISINFF) && _GLIBCXX_HAVE_ISINFF
|
||||
template <>
|
||||
inline bool isInfinity<float>(float x) {
|
||||
return isinff(x);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(__GNUC__)
|
||||
#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
|
||||
#pragma GCC diagnostic pop
|
||||
|
@ -6,7 +6,7 @@
|
||||
"type": "git",
|
||||
"url": "https://github.com/bblanchon/ArduinoJson.git"
|
||||
},
|
||||
"version": "5.6.2",
|
||||
"version": "5.6.4",
|
||||
"authors": {
|
||||
"name": "Benoit Blanchon",
|
||||
"url": "http://blog.benoitblanchon.fr"
|
||||
|
@ -1,5 +1,5 @@
|
||||
name=ArduinoJson
|
||||
version=5.6.2
|
||||
version=5.6.4
|
||||
author=Benoit Blanchon <blog.benoitblanchon.fr>
|
||||
maintainer=Benoit Blanchon <blog.benoitblanchon.fr>
|
||||
sentence=An efficient and elegant JSON library for Arduino.
|
||||
|
@ -59,6 +59,7 @@ TEST_(StoreBoolean) {
|
||||
TEST_(StoreString) {
|
||||
_array[0] = "hello";
|
||||
EXPECT_STREQ("hello", _array[0].as<const char*>());
|
||||
EXPECT_STREQ("hello", _array[0].as<char*>()); // <- short hand
|
||||
EXPECT_TRUE(_array[0].is<const char*>());
|
||||
EXPECT_FALSE(_array[0].is<int>());
|
||||
}
|
||||
@ -69,6 +70,9 @@ TEST_(StoreNestedArray) {
|
||||
_array[0] = arr;
|
||||
|
||||
EXPECT_EQ(&arr, &_array[0].as<JsonArray&>());
|
||||
EXPECT_EQ(&arr, &_array[0].as<JsonArray>()); // <- short hand
|
||||
EXPECT_EQ(&arr, &_array[0].as<const JsonArray&>());
|
||||
EXPECT_EQ(&arr, &_array[0].as<const JsonArray>()); // <- short hand
|
||||
EXPECT_TRUE(_array[0].is<JsonArray&>());
|
||||
EXPECT_FALSE(_array[0].is<int>());
|
||||
}
|
||||
@ -79,6 +83,9 @@ TEST_(StoreNestedObject) {
|
||||
_array[0] = obj;
|
||||
|
||||
EXPECT_EQ(&obj, &_array[0].as<JsonObject&>());
|
||||
EXPECT_EQ(&obj, &_array[0].as<JsonObject>()); // <- short hand
|
||||
EXPECT_EQ(&obj, &_array[0].as<const JsonObject&>());
|
||||
EXPECT_EQ(&obj, &_array[0].as<const JsonObject>()); // <- short hand
|
||||
EXPECT_TRUE(_array[0].is<JsonObject&>());
|
||||
EXPECT_FALSE(_array[0].is<int>());
|
||||
}
|
||||
|
@ -68,6 +68,7 @@ TEST_(StoreString) {
|
||||
EXPECT_TRUE(_object["hello"].is<const char*>());
|
||||
EXPECT_FALSE(_object["hello"].is<long>());
|
||||
EXPECT_STREQ("h3110", _object["hello"].as<const char*>());
|
||||
EXPECT_STREQ("h3110", _object["hello"].as<char*>()); // <- short hand
|
||||
}
|
||||
|
||||
TEST_(StoreArray) {
|
||||
@ -75,8 +76,15 @@ TEST_(StoreArray) {
|
||||
|
||||
_object["hello"] = arr;
|
||||
|
||||
EXPECT_EQ(&arr, &_object["hello"].asArray());
|
||||
EXPECT_EQ(&arr, &_object["hello"].asArray()); // <- DEPRECATED
|
||||
EXPECT_EQ(&arr, &_object["hello"].as<JsonArray&>());
|
||||
EXPECT_EQ(&arr, &_object["hello"].as<JsonArray>()); // <- short hand
|
||||
EXPECT_EQ(&arr, &_object["hello"].as<const JsonArray&>());
|
||||
EXPECT_EQ(&arr, &_object["hello"].as<const JsonArray>()); // <- short hand
|
||||
EXPECT_TRUE(_object["hello"].is<JsonArray&>());
|
||||
EXPECT_TRUE(_object["hello"].is<JsonArray>());
|
||||
EXPECT_TRUE(_object["hello"].is<const JsonArray&>());
|
||||
EXPECT_TRUE(_object["hello"].is<const JsonArray>());
|
||||
EXPECT_FALSE(_object["hello"].is<JsonObject&>());
|
||||
}
|
||||
|
||||
@ -85,8 +93,15 @@ TEST_(StoreObject) {
|
||||
|
||||
_object["hello"] = obj;
|
||||
|
||||
EXPECT_EQ(&obj, &_object["hello"].asObject());
|
||||
EXPECT_EQ(&obj, &_object["hello"].asObject()); // DEPRECATED
|
||||
EXPECT_EQ(&obj, &_object["hello"].as<JsonObject&>());
|
||||
EXPECT_EQ(&obj, &_object["hello"].as<JsonObject>()); // <- short hand
|
||||
EXPECT_EQ(&obj, &_object["hello"].as<const JsonObject&>());
|
||||
EXPECT_EQ(&obj, &_object["hello"].as<const JsonObject>()); // <- short hand
|
||||
EXPECT_TRUE(_object["hello"].is<JsonObject&>());
|
||||
EXPECT_TRUE(_object["hello"].is<JsonObject>());
|
||||
EXPECT_TRUE(_object["hello"].is<const JsonObject&>());
|
||||
EXPECT_TRUE(_object["hello"].is<const JsonObject>());
|
||||
EXPECT_FALSE(_object["hello"].is<JsonArray&>());
|
||||
}
|
||||
|
||||
|
@ -214,3 +214,21 @@ TEST(JsonVariant_As_Tests, ArrayAsString) {
|
||||
JsonVariant variant = arr;
|
||||
ASSERT_EQ(String("[4,2]"), variant.as<String>());
|
||||
}
|
||||
|
||||
TEST(JsonVariant_As_Tests, ArrayAsJsonArray) {
|
||||
DynamicJsonBuffer buffer;
|
||||
JsonArray& arr = buffer.createArray();
|
||||
|
||||
JsonVariant variant = arr;
|
||||
ASSERT_EQ(&arr, &variant.as<JsonArray&>());
|
||||
ASSERT_EQ(&arr, &variant.as<JsonArray>()); // <- shorthand
|
||||
}
|
||||
|
||||
TEST(JsonVariant_As_Tests, ObjectAsJsonObject) {
|
||||
DynamicJsonBuffer buffer;
|
||||
JsonObject& arr = buffer.createObject();
|
||||
|
||||
JsonVariant variant = arr;
|
||||
ASSERT_EQ(&arr, &variant.as<JsonObject&>());
|
||||
ASSERT_EQ(&arr, &variant.as<JsonObject>()); // <- shorthand
|
||||
}
|
||||
|
113
test/JsonWriter_WriteFloat_Tests.cpp
Normal file
113
test/JsonWriter_WriteFloat_Tests.cpp
Normal file
@ -0,0 +1,113 @@
|
||||
// Copyright Benoit Blanchon 2014-2016
|
||||
// MIT License
|
||||
//
|
||||
// Arduino JSON library
|
||||
// https://github.com/bblanchon/ArduinoJson
|
||||
// If you like this project, please add a star!
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <limits>
|
||||
|
||||
#include <ArduinoJson/Internals/JsonWriter.hpp>
|
||||
#include <ArduinoJson/Internals/StaticStringBuilder.hpp>
|
||||
|
||||
using namespace ArduinoJson::Internals;
|
||||
|
||||
class JsonWriter_WriteFloat_Tests : public testing::Test {
|
||||
protected:
|
||||
void whenInputIs(double input, uint8_t digits = 2) {
|
||||
StaticStringBuilder sb(buffer, sizeof(buffer));
|
||||
JsonWriter writer(sb);
|
||||
writer.writeFloat(input, digits);
|
||||
returnValue = writer.bytesWritten();
|
||||
}
|
||||
|
||||
void outputMustBe(const char *expected) {
|
||||
EXPECT_STREQ(expected, buffer);
|
||||
EXPECT_EQ(strlen(expected), returnValue);
|
||||
}
|
||||
|
||||
private:
|
||||
char buffer[1024];
|
||||
size_t returnValue;
|
||||
};
|
||||
|
||||
TEST_F(JsonWriter_WriteFloat_Tests, NaN) {
|
||||
whenInputIs(std::numeric_limits<double>::signaling_NaN());
|
||||
outputMustBe("NaN");
|
||||
}
|
||||
|
||||
TEST_F(JsonWriter_WriteFloat_Tests, PositiveInfinity) {
|
||||
whenInputIs(std::numeric_limits<double>::infinity());
|
||||
outputMustBe("Infinity");
|
||||
}
|
||||
|
||||
TEST_F(JsonWriter_WriteFloat_Tests, NegativeInfinity) {
|
||||
whenInputIs(-std::numeric_limits<double>::infinity());
|
||||
outputMustBe("-Infinity");
|
||||
}
|
||||
|
||||
TEST_F(JsonWriter_WriteFloat_Tests, Zero) {
|
||||
whenInputIs(0);
|
||||
outputMustBe("0.00");
|
||||
}
|
||||
|
||||
TEST_F(JsonWriter_WriteFloat_Tests, ZeroDigits_Rounding) {
|
||||
whenInputIs(9.5, 0);
|
||||
outputMustBe("10");
|
||||
}
|
||||
|
||||
TEST_F(JsonWriter_WriteFloat_Tests, ZeroDigits_NoRounding) {
|
||||
whenInputIs(9.4, 0);
|
||||
outputMustBe("9");
|
||||
}
|
||||
|
||||
TEST_F(JsonWriter_WriteFloat_Tests, OneDigit_Rounding) {
|
||||
whenInputIs(9.95, 1);
|
||||
outputMustBe("10.0");
|
||||
}
|
||||
|
||||
TEST_F(JsonWriter_WriteFloat_Tests, OneDigit_NoRounding) {
|
||||
whenInputIs(9.94, 1);
|
||||
outputMustBe("9.9");
|
||||
}
|
||||
|
||||
TEST_F(JsonWriter_WriteFloat_Tests, TwoDigits_Rounding) {
|
||||
whenInputIs(9.995, 2);
|
||||
outputMustBe("10.00");
|
||||
}
|
||||
|
||||
TEST_F(JsonWriter_WriteFloat_Tests, TwoDigits_NoRounding) {
|
||||
whenInputIs(9.994, 2);
|
||||
outputMustBe("9.99");
|
||||
}
|
||||
|
||||
TEST_F(JsonWriter_WriteFloat_Tests, ThreeDigits_Rounding) {
|
||||
whenInputIs(9.9995, 3);
|
||||
outputMustBe("10.000");
|
||||
}
|
||||
|
||||
TEST_F(JsonWriter_WriteFloat_Tests, ThreeDigits_NoRounding) {
|
||||
whenInputIs(9.9994, 3);
|
||||
outputMustBe("9.999");
|
||||
}
|
||||
|
||||
TEST_F(JsonWriter_WriteFloat_Tests, FourDigits_Rounding) {
|
||||
whenInputIs(9.99995, 4);
|
||||
outputMustBe("10.0000");
|
||||
}
|
||||
|
||||
TEST_F(JsonWriter_WriteFloat_Tests, FourDigits_NoRounding) {
|
||||
whenInputIs(9.99994, 4);
|
||||
outputMustBe("9.9999");
|
||||
}
|
||||
|
||||
TEST_F(JsonWriter_WriteFloat_Tests, FiveDigits_Rounding) {
|
||||
whenInputIs(9.999995, 5);
|
||||
outputMustBe("10.00000");
|
||||
}
|
||||
|
||||
TEST_F(JsonWriter_WriteFloat_Tests, FiveDigits_NoRounding) {
|
||||
whenInputIs(9.999994, 5);
|
||||
outputMustBe("9.99999");
|
||||
}
|
Reference in New Issue
Block a user