Compare commits

..

2 Commits

13 changed files with 108 additions and 107 deletions

16
appveyor.yml Normal file
View File

@ -0,0 +1,16 @@
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,11 +9,19 @@
#ifndef ARDUINO
#include "../TypeTraits/EnableIf.hpp"
#include "../TypeTraits/IsIntegral.hpp"
#include "../Internals/JsonFloat.hpp"
#include "../Internals/JsonInteger.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 {
@ -22,13 +30,33 @@ class Print {
virtual size_t write(uint8_t) = 0;
size_t print(const char[]);
size_t print(double, int = 2);
size_t print(const char* s) {
size_t n = 0;
while (*s) {
n += write(*s++);
}
return n;
}
template <typename TIntegral>
typename ArduinoJson::TypeTraits::EnableIf<
ArduinoJson::TypeTraits::IsIntegral<TIntegral>::value, size_t>::type
print(TIntegral value) {
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) {
// see http://clc-wiki.net/wiki/K%26R2_solutions:Chapter_3:Exercise_4
char buffer[22];
@ -39,7 +67,7 @@ class Print {
}
uint8_t i = 0;
do {
TIntegral digit = value % 10;
ArduinoJson::Internals::JsonInteger digit = value % 10;
value /= 10;
buffer[i++] = static_cast<char>(digit >= 0 ? '0' + digit : '0' - digit);
} while (value);
@ -51,7 +79,7 @@ class Print {
return n;
}
size_t println();
size_t println() { return write('\r') + write('\n'); }
};
#else

View File

@ -77,11 +77,6 @@ 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

@ -92,10 +92,6 @@ 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 {
@ -191,17 +191,24 @@ 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, JsonObject &>::value ||
TypeTraits::IsSame<T, const JsonObject &>::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, 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;
};
}

View File

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

View File

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

View File

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

View File

@ -1,60 +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!
#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 <gtest/gtest.h>
#include <ArduinoJson.h>
#include <gtest/gtest.h>
#include <stdint.h>
class JsonArray_Subscript_Tests : public ::testing::Test {
@ -33,12 +33,14 @@ TEST_(StoreInteger) {
EXPECT_FALSE(_array[0].is<double>());
}
TEST_(StoreInt64) {
#if ARDUINOJSON_USE_LONG_LONG || ARDUINOJSON_USE_INT64
TEST_(StoreLongLong) {
_array[0] = 9223372036854775807;
EXPECT_EQ(9223372036854775807, _array[0].as<int64_t>());
EXPECT_EQ(9223372036854775807, _array[0].as<long long>());
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,15 +137,17 @@ 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<int64_t>());
ASSERT_EQ(-9223372036854775807 - 1, variant.as<long long>());
}
TEST(JsonVariant_As_Tests, NumberStringAsInt64Positive) {
JsonVariant variant = "9223372036854775807";
ASSERT_EQ(9223372036854775807, variant.as<int64_t>());
ASSERT_EQ(9223372036854775807, variant.as<long long>());
}
#endif
TEST(JsonVariant_As_Tests, RandomStringAsBool) {
JsonVariant variant = "hello";

View File

@ -72,6 +72,7 @@ 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");
@ -81,3 +82,4 @@ 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,9 +37,11 @@ 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"); }
@ -57,15 +59,21 @@ 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, Int16) { testNumericType<int16_t>(); }
TEST_F(JsonVariant_Storage_Tests, Int32) { testNumericType<int32_t>(); }
TEST_F(JsonVariant_Storage_Tests, Int64) { testNumericType<int64_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, Uint64) { testNumericType<uint64_t>(); }
#endif
TEST_F(JsonVariant_Storage_Tests, CanStoreObject) {
DynamicJsonBuffer jsonBuffer;