Compare commits

..

2 Commits

Author SHA1 Message Date
5f589d3836 Set version to 5.1.0-beta.1 2016-02-01 21:26:15 +01:00
ed4ec6ed2e Added support of long long (issue #171) 2016-02-01 21:17:32 +01:00
17 changed files with 127 additions and 164 deletions

View File

@ -1,16 +0,0 @@
version: 5.1.0.{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
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
- cmake -DCMAKE_BUILD_TYPE=%CONFIGURATION% -G "%CMAKE_GENERATOR%" .
build_script:
- cmake --build . --config %CONFIGURATION%
test_script:
- ctest -V .

View File

@ -9,19 +9,11 @@
#ifndef ARDUINO
#include "../Internals/JsonFloat.hpp"
#include "../Internals/JsonInteger.hpp"
#include "../TypeTraits/EnableIf.hpp"
#include "../TypeTraits/IsIntegral.hpp"
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#if defined(_MSC_VER) && _MSC_VER <= 1800
// snprintf has been added in Visual Studio 2015
#define ARDUINOJSON_SNPRINTF _snprintf
#else
#define ARDUINOJSON_SNPRINTF snprintf
#endif
// This class reproduces Arduino's Print class
class Print {
@ -30,33 +22,13 @@ class Print {
virtual size_t write(uint8_t) = 0;
size_t print(const char* s) {
size_t n = 0;
while (*s) {
n += write(*s++);
}
return n;
}
size_t print(const char[]);
size_t print(double, int = 2);
size_t print(ArduinoJson::Internals::JsonFloat value, int digits = 2) {
char tmp[32];
// https://github.com/arduino/Arduino/blob/db8cbf24c99dc930b9ccff1a43d018c81f178535/hardware/arduino/sam/cores/arduino/Print.cpp#L220
bool isBigDouble = value > 4294967040.0 || value < -4294967040.0;
if (isBigDouble) {
// Arduino's implementation prints "ovf"
// We prefer using the scientific notation, since we have sprintf
ARDUINOJSON_SNPRINTF(tmp, sizeof(tmp), "%g", value);
} else {
// Here we have the exact same output as Arduino's implementation
ARDUINOJSON_SNPRINTF(tmp, sizeof(tmp), "%.*f", digits, value);
}
return print(tmp);
}
size_t print(ArduinoJson::Internals::JsonInteger value) {
template <typename TIntegral>
typename ArduinoJson::TypeTraits::EnableIf<
ArduinoJson::TypeTraits::IsIntegral<TIntegral>::value, size_t>::type
print(TIntegral value) {
// see http://clc-wiki.net/wiki/K%26R2_solutions:Chapter_3:Exercise_4
char buffer[22];
@ -67,7 +39,7 @@ class Print {
}
uint8_t i = 0;
do {
ArduinoJson::Internals::JsonInteger digit = value % 10;
TIntegral digit = value % 10;
value /= 10;
buffer[i++] = static_cast<char>(digit >= 0 ? '0' + digit : '0' - digit);
} while (value);
@ -79,7 +51,7 @@ class Print {
return n;
}
size_t println() { return write('\r') + write('\n'); }
size_t println();
};
#else

View File

@ -12,10 +12,6 @@
#include "Internals/List.hpp"
#include "Internals/ReferenceType.hpp"
#include "JsonVariant.hpp"
#include "TypeTraits/EnableIf.hpp"
#include "TypeTraits/IsFloatingPoint.hpp"
#include "TypeTraits/IsReference.hpp"
#include "TypeTraits/IsSame.hpp"
// Returns the size (in bytes) of an array with n elements.
// Can be very handy to determine the size of a StaticJsonBuffer.
@ -74,8 +70,7 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
template <typename T>
FORCE_INLINE bool add(
T value,
typename TypeTraits::EnableIf<
CanSet<T>::value && !TypeTraits::IsReference<T>::value>::type * = 0) {
typename TypeTraits::EnableIf<CanSet<T>::value, void>::type * = 0) {
return addNode<T>(value);
}
// bool add(const String&)
@ -85,16 +80,16 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
template <typename T>
FORCE_INLINE bool add(
const T &value,
typename TypeTraits::EnableIf<CanSet<T &>::value>::type * = 0) {
typename TypeTraits::EnableIf<CanSet<T &>::value, T>::type * = 0) {
return addNode<T &>(const_cast<T &>(value));
}
// bool add(float value, uint8_t decimals);
// bool add(double value, uint8_t decimals);
template <typename T>
FORCE_INLINE bool add(
T value, uint8_t decimals,
typename TypeTraits::EnableIf<TypeTraits::IsFloatingPoint<T>::value>::type
* = 0) {
T value,
typename TypeTraits::EnableIf<TypeTraits::IsFloatingPoint<T>::value,
uint8_t>::type decimals) {
return addNode<JsonVariant>(JsonVariant(value, decimals));
}
@ -107,8 +102,7 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
template <typename T>
FORCE_INLINE bool set(
size_t index, T value,
typename TypeTraits::EnableIf<
CanSet<T>::value && !TypeTraits::IsReference<T>::value>::type * = 0) {
typename TypeTraits::EnableIf<CanSet<T>::value, void>::type * = 0) {
return setNodeAt<T>(index, value);
}
// bool set(size_t index, const String&)
@ -118,16 +112,16 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
template <typename T>
FORCE_INLINE bool set(
size_t index, const T &value,
typename TypeTraits::EnableIf<CanSet<T &>::value>::type * = 0) {
typename TypeTraits::EnableIf<CanSet<T &>::value, T>::type * = 0) {
return setNodeAt<T &>(index, const_cast<T &>(value));
}
// bool set(size_t index, float value, uint8_t decimals = 2);
// bool set(size_t index, double value, uint8_t decimals = 2);
template <typename T>
FORCE_INLINE bool set(
size_t index, T value, uint8_t decimals,
typename TypeTraits::EnableIf<TypeTraits::IsFloatingPoint<T>::value>::type
* = 0) {
size_t index, T value,
typename TypeTraits::EnableIf<TypeTraits::IsFloatingPoint<T>::value,
uint8_t>::type decimals) {
return setNodeAt<const JsonVariant &>(index, JsonVariant(value, decimals));
}

View File

@ -77,6 +77,11 @@ inline std::ostream& operator<<(std::ostream& os,
}
#endif
template <>
struct JsonVariant::IsConstructibleFrom<JsonArraySubscript> {
static const bool value = true;
};
} // namespace ArduinoJson
#ifdef _MSC_VER

View File

@ -13,10 +13,6 @@
#include "Internals/List.hpp"
#include "Internals/ReferenceType.hpp"
#include "JsonPair.hpp"
#include "TypeTraits/EnableIf.hpp"
#include "TypeTraits/IsFloatingPoint.hpp"
#include "TypeTraits/IsReference.hpp"
#include "TypeTraits/IsSame.hpp"
// Returns the size (in bytes) of an object with n elements.
// Can be very handy to determine the size of a StaticJsonBuffer.
@ -74,8 +70,7 @@ class JsonObject : public Internals::JsonPrintable<JsonObject>,
template <typename T>
FORCE_INLINE bool set(
JsonObjectKey key, T value,
typename TypeTraits::EnableIf<
CanSet<T>::value && !TypeTraits::IsReference<T>::value>::type* = 0) {
typename TypeTraits::EnableIf<CanSet<T>::value, void>::type* = 0) {
return setNodeAt<T>(key, value);
}
// bool set(Key, String&);
@ -85,16 +80,16 @@ class JsonObject : public Internals::JsonPrintable<JsonObject>,
template <typename T>
FORCE_INLINE bool set(
JsonObjectKey key, const T& value,
typename TypeTraits::EnableIf<CanSet<T&>::value>::type* = 0) {
typename TypeTraits::EnableIf<CanSet<T&>::value, T>::type* = 0) {
return setNodeAt<T&>(key, const_cast<T&>(value));
}
// bool set(Key, float value, uint8_t decimals);
// bool set(Key, double value, uint8_t decimals);
template <typename TValue>
FORCE_INLINE bool set(
JsonObjectKey key, TValue value, uint8_t decimals,
typename TypeTraits::EnableIf<
TypeTraits::IsFloatingPoint<TValue>::value>::type* = 0) {
JsonObjectKey key, TValue value,
typename TypeTraits::EnableIf<TypeTraits::IsFloatingPoint<TValue>::value,
uint8_t>::type decimals) {
return setNodeAt<const JsonVariant&>(key, JsonVariant(value, decimals));
}

View File

@ -92,6 +92,10 @@ inline std::ostream& operator<<(
}
#endif
template <typename T>
struct JsonVariant::IsConstructibleFrom<JsonObjectSubscript<T> > {
static const bool value = true;
};
} // namespace ArduinoJson
#ifdef _MSC_VER

View File

@ -18,9 +18,9 @@
#include "TypeTraits/EnableIf.hpp"
#include "TypeTraits/IsFloatingPoint.hpp"
#include "TypeTraits/IsIntegral.hpp"
#include "TypeTraits/IsSame.hpp"
#include "TypeTraits/RemoveConst.hpp"
#include "TypeTraits/RemoveReference.hpp"
#include "TypeTraits/IsSame.hpp"
namespace ArduinoJson {
@ -55,8 +55,8 @@ class JsonVariant : public JsonVariantBase<JsonVariant> {
template <typename T>
FORCE_INLINE JsonVariant(
T value, uint8_t decimals = 2,
typename TypeTraits::EnableIf<TypeTraits::IsFloatingPoint<T>::value>::type
* = 0) {
typename TypeTraits::EnableIf<TypeTraits::IsFloatingPoint<T>::value,
T>::type * = 0) {
using namespace Internals;
_type = static_cast<JsonVariantType>(JSON_FLOAT_0_DECIMALS + decimals);
_content.asFloat = static_cast<JsonFloat>(value);
@ -68,9 +68,8 @@ class JsonVariant : public JsonVariantBase<JsonVariant> {
// JsonVariant(long)
template <typename T>
FORCE_INLINE JsonVariant(
T value,
typename TypeTraits::EnableIf<TypeTraits::IsIntegral<T>::value>::type * =
0) {
T value, typename TypeTraits::EnableIf<TypeTraits::IsIntegral<T>::value,
T>::type * = 0) {
using namespace Internals;
_type = JSON_INTEGER;
_content.asInteger = static_cast<JsonInteger>(value);
@ -192,24 +191,17 @@ inline JsonVariant double_with_n_digits(double value, uint8_t digits) {
template <typename T>
struct JsonVariant::IsConstructibleFrom {
static const bool value =
TypeTraits::IsIntegral<T>::value ||
TypeTraits::IsFloatingPoint<T>::value ||
TypeTraits::IsSame<T, bool>::value ||
TypeTraits::IsSame<T, char *>::value ||
TypeTraits::IsSame<T, const char *>::value ||
TypeTraits::IsSame<T, JsonArray &>::value ||
TypeTraits::IsSame<T, const JsonArray &>::value ||
TypeTraits::IsSame<T, JsonArraySubscript &>::value ||
TypeTraits::IsSame<T, const JsonArraySubscript &>::value ||
TypeTraits::IsSame<T, JsonObject &>::value ||
TypeTraits::IsSame<T, const JsonObject &>::value ||
TypeTraits::IsSame<T, JsonObjectSubscript<const char *> &>::value ||
TypeTraits::IsSame<T, const JsonObjectSubscript<const char *> &>::value ||
TypeTraits::IsSame<T, JsonObjectSubscript<String> &>::value ||
TypeTraits::IsSame<T, const JsonObjectSubscript<String> &>::value ||
TypeTraits::IsSame<T, JsonVariant &>::value ||
TypeTraits::IsSame<T, const JsonVariant &>::value;
static const bool value = TypeTraits::IsIntegral<T>::value ||
TypeTraits::IsFloatingPoint<T>::value ||
TypeTraits::IsSame<T, bool>::value ||
TypeTraits::IsSame<T, char *>::value ||
TypeTraits::IsSame<T, const char *>::value ||
TypeTraits::IsSame<T, JsonArray &>::value ||
TypeTraits::IsSame<T, const JsonArray &>::value ||
TypeTraits::IsSame<T, JsonObject &>::value ||
TypeTraits::IsSame<T, const JsonObject &>::value ||
TypeTraits::IsSame<T, JsonVariant &>::value ||
TypeTraits::IsSame<T, const JsonVariant &>::value;
};
}

View File

@ -11,7 +11,7 @@ namespace ArduinoJson {
namespace TypeTraits {
// A meta-function that return the type T if Condition is true.
template <bool Condition, typename T = void>
template <bool Condition, typename T>
struct EnableIf {};
template <typename T>

View File

@ -7,7 +7,6 @@
#pragma once
#include "../Configuration.hpp"
#include "IsSame.hpp"
#include <stdint.h>
@ -26,14 +25,10 @@ struct IsIntegral {
TypeTraits::IsSame<T, unsigned int>::value ||
TypeTraits::IsSame<T, signed long>::value ||
TypeTraits::IsSame<T, unsigned long>::value ||
#if ARDUINOJSON_USE_LONG_LONG
TypeTraits::IsSame<T, long long>::value ||
TypeTraits::IsSame<T, unsigned long long>::value ||
#endif
#if ARDUINOJSON_USE_INT64
TypeTraits::IsSame<T, __int64>::value ||
TypeTraits::IsSame<T, unsigned __int64>::value ||
#ifndef ARDUINO
// on a computer add support for 64 bit
TypeTraits::IsSame<T, int64_t>::value ||
TypeTraits::IsSame<T, uint64_t>::value ||
#endif
TypeTraits::IsSame<T, char>::value;
};

View File

@ -1,24 +0,0 @@
// 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 TypeTraits {
// A meta-function that returns true if T is a reference
template <typename T>
struct IsReference {
static const bool value = false;
};
template <typename T>
struct IsReference<T&> {
static const bool value = true;
};
}
}

View File

@ -6,7 +6,7 @@
"type": "git",
"url": "https://github.com/bblanchon/ArduinoJson.git"
},
"version": "5.1.0",
"version": "5.1.0-beta.1",
"authors": {
"name": "Benoit Blanchon",
"url": "http://blog.benoitblanchon.fr"

View File

@ -1,5 +1,5 @@
name=ArduinoJson
version=5.1.0
version=5.1.0-beta.1
author=Benoit Blanchon <blog.benoitblanchon.fr>
maintainer=Benoit Blanchon <blog.benoitblanchon.fr>
sentence=An efficient and elegant JSON library for Arduino.

60
src/Arduino/Print.cpp Normal file
View File

@ -0,0 +1,60 @@
// Copyright Benoit Blanchon 2014-2016
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#ifndef ARDUINO
#include "../../include/ArduinoJson/Arduino/Print.hpp"
#include <stdio.h>
#include <math.h> // for isnan() and isinf()
// only for GCC 4.9+
#if defined(__GNUC__) && \
(__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9))
#pragma GCC diagnostic ignored "-Wfloat-conversion"
#endif
// Visual Studo 2012 didn't have isnan, nor isinf
#if defined(_MSC_VER) && _MSC_VER <= 1700
#include <float.h>
#define isnan(x) _isnan(x)
#define isinf(x) (!_finite(x))
#endif
size_t Print::print(const char s[]) {
size_t n = 0;
while (*s) {
n += write(*s++);
}
return n;
}
size_t Print::print(double value, int digits) {
// https://github.com/arduino/Arduino/blob/db8cbf24c99dc930b9ccff1a43d018c81f178535/hardware/arduino/sam/cores/arduino/Print.cpp#L218
if (isnan(value)) return print("nan");
if (isinf(value)) return print("inf");
char tmp[32];
// https://github.com/arduino/Arduino/blob/db8cbf24c99dc930b9ccff1a43d018c81f178535/hardware/arduino/sam/cores/arduino/Print.cpp#L220
bool isBigDouble = value > 4294967040.0 || value < -4294967040.0;
if (isBigDouble) {
// Arduino's implementation prints "ovf"
// We prefer trying to use scientific notation, since we have sprintf
sprintf(tmp, "%g", value);
} else {
// Here we have the exact same output as Arduino's implementation
sprintf(tmp, "%.*f", digits, value);
}
return print(tmp);
}
size_t Print::println() { return write('\r') + write('\n'); }
#endif

View File

@ -5,8 +5,8 @@
// https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#include <ArduinoJson.h>
#include <gtest/gtest.h>
#include <ArduinoJson.h>
#include <stdint.h>
class JsonArray_Subscript_Tests : public ::testing::Test {
@ -33,14 +33,12 @@ TEST_(StoreInteger) {
EXPECT_FALSE(_array[0].is<double>());
}
#if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_INT64
TEST_(StoreLongLong) {
TEST_(StoreInt64) {
_array[0] = 9223372036854775807;
EXPECT_EQ(9223372036854775807, _array[0].as<long long>());
EXPECT_EQ(9223372036854775807, _array[0].as<int64_t>());
EXPECT_TRUE(_array[0].is<int>());
EXPECT_FALSE(_array[0].is<double>());
}
#endif
TEST_(StoreDouble) {
_array[0] = 123.45;

View File

@ -5,9 +5,9 @@
// https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#include <gtest/gtest.h>
#include <ArduinoJson.h>
#include <gtest/gtest.h>
#include <stdint.h>
static const char* null = 0;
@ -137,17 +137,15 @@ TEST(JsonVariant_As_Tests, NumberStringAsLong) {
ASSERT_EQ(42L, variant.as<long>());
}
#if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_INT64
TEST(JsonVariant_As_Tests, NumberStringAsInt64Negative) {
JsonVariant variant = "-9223372036854775808";
ASSERT_EQ(-9223372036854775807 - 1, variant.as<long long>());
ASSERT_EQ(-9223372036854775807 - 1, variant.as<int64_t>());
}
TEST(JsonVariant_As_Tests, NumberStringAsInt64Positive) {
JsonVariant variant = "9223372036854775807";
ASSERT_EQ(9223372036854775807, variant.as<long long>());
ASSERT_EQ(9223372036854775807, variant.as<int64_t>());
}
#endif
TEST(JsonVariant_As_Tests, RandomStringAsBool) {
JsonVariant variant = "hello";

View File

@ -72,7 +72,6 @@ TEST_F(JsonVariant_PrintTo_Tests, OneFalse) {
outputMustBe("false");
}
#if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_INT64
TEST_F(JsonVariant_PrintTo_Tests, NegativeInt64) {
variant = -9223372036854775807 - 1;
outputMustBe("-9223372036854775808");
@ -82,4 +81,3 @@ TEST_F(JsonVariant_PrintTo_Tests, PositiveInt64) {
variant = 9223372036854775807;
outputMustBe("9223372036854775807");
}
#endif

View File

@ -5,9 +5,9 @@
// https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!
#include <gtest/gtest.h>
#include <stdint.h>
#include <limits>
#include <gtest/gtest.h>
#include <ArduinoJson.h>
class JsonVariant_Storage_Tests : public ::testing::Test {
@ -37,11 +37,9 @@ class JsonVariant_Storage_Tests : public ::testing::Test {
}
};
#if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_INT64
TEST_F(JsonVariant_Storage_Tests, SizeOfJsonInteger) {
ASSERT_EQ(8, sizeof(Internals::JsonInteger));
}
#endif
TEST_F(JsonVariant_Storage_Tests, Null) { testValue<const char *>(NULL); }
TEST_F(JsonVariant_Storage_Tests, String) { testValue<const char *>("hello"); }
@ -59,21 +57,15 @@ TEST_F(JsonVariant_Storage_Tests, UChar) { testNumericType<unsigned char>(); }
TEST_F(JsonVariant_Storage_Tests, UInt) { testNumericType<unsigned int>(); }
TEST_F(JsonVariant_Storage_Tests, ULong) { testNumericType<unsigned long>(); }
TEST_F(JsonVariant_Storage_Tests, UShort) { testNumericType<unsigned short>(); }
#if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_INT64
TEST_F(JsonVariant_Storage_Tests, LongLong) { testNumericType<unsigned long long>(); }
TEST_F(JsonVariant_Storage_Tests, ULongLong) { testNumericType<unsigned long long>(); }
#endif
TEST_F(JsonVariant_Storage_Tests, Int8) { testNumericType<int8_t>(); }
TEST_F(JsonVariant_Storage_Tests, Uint8) { testNumericType<uint8_t>(); }
TEST_F(JsonVariant_Storage_Tests, Int16) { testNumericType<int16_t>(); }
TEST_F(JsonVariant_Storage_Tests, Uint16) { testNumericType<uint16_t>(); }
TEST_F(JsonVariant_Storage_Tests, Int32) { testNumericType<int32_t>(); }
TEST_F(JsonVariant_Storage_Tests, Uint32) { testNumericType<uint32_t>(); }
#if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_INT64
TEST_F(JsonVariant_Storage_Tests, Int64) { testNumericType<int64_t>(); }
TEST_F(JsonVariant_Storage_Tests, Uint8) { testNumericType<uint8_t>(); }
TEST_F(JsonVariant_Storage_Tests, Uint16) { testNumericType<uint16_t>(); }
TEST_F(JsonVariant_Storage_Tests, Uint32) { testNumericType<uint32_t>(); }
TEST_F(JsonVariant_Storage_Tests, Uint64) { testNumericType<uint64_t>(); }
#endif
TEST_F(JsonVariant_Storage_Tests, CanStoreObject) {
DynamicJsonBuffer jsonBuffer;