forked from bblanchon/ArduinoJson
Compare commits
12 Commits
Author | SHA1 | Date | |
---|---|---|---|
6757f35a3a | |||
ffb9b6d1ba | |||
e401498e4a | |||
d30e940b3b | |||
05ea5e04c8 | |||
a7ef99d0fe | |||
f2a8b52c2c | |||
409ca7ee4e | |||
387b565705 | |||
96f486001d | |||
a498abc14a | |||
c64340a9bb |
5
.mbedignore
Normal file
5
.mbedignore
Normal file
@ -0,0 +1,5 @@
|
||||
.github/
|
||||
examples/
|
||||
scripts/
|
||||
test/
|
||||
third-party/
|
25
CHANGELOG.md
25
CHANGELOG.md
@ -1,6 +1,23 @@
|
||||
ArduinoJson: change log
|
||||
=======================
|
||||
|
||||
v5.6.6
|
||||
------
|
||||
|
||||
* Fixed `-Wparentheses` warning introduced in v5.6.5 (PR #335 by @nuket)
|
||||
* Added `.mbedignore` for ARM mbdeb (PR #334 by @nuket)
|
||||
* Fixed `JsonVariant::success()` which didn't propagate `JsonArray::success()` nor `JsonObject::success()` (issue #342).
|
||||
|
||||
v5.6.5
|
||||
------
|
||||
|
||||
* `as<char*>()` now returns `true` when input is `null` (issue #330)
|
||||
|
||||
v5.6.4
|
||||
------
|
||||
|
||||
* Fixed error in float serialization (issue #324)
|
||||
|
||||
v5.6.3
|
||||
------
|
||||
|
||||
@ -112,7 +129,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)
|
||||
@ -228,14 +245,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;
|
||||
@ -292,7 +309,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
|
||||
|
@ -86,9 +86,6 @@ From Arduino's Forum user `jflaplante`:
|
||||
> I tried aJson json-arduino before trying your library. I always ran into memory problem after a while.
|
||||
> I have no such problem so far with your library. It is working perfectly with my web services.
|
||||
|
||||
From Arduino's Forum user `gbathree`:
|
||||
> Thanks so much - this is an awesome library! If you want to see what we're doing with it - the project is located at www.photosynq.org.
|
||||
|
||||
From StackOverflow user `thegreendroid`:
|
||||
> It has a really elegant, simple API and it works like a charm on embedded and Windows/Linux platforms. We recently started using this on an embedded project and I can vouch for its quality.
|
||||
|
||||
@ -124,6 +121,7 @@ Special thanks to the following persons and companies who made generous donation
|
||||
* Nick Koumaris <img alt='Greece' src='https://cdnjs.cloudflare.com/ajax/libs/emojione/2.1.4/assets/svg/1f1ec-1f1f7.svg' width='18' height='18'>
|
||||
* Jon Williams <img alt='USA' src='https://cdnjs.cloudflare.com/ajax/libs/emojione/2.1.4/assets/svg/1f1fa-1f1f8.svg' width='18' height='18'>
|
||||
* Kestutis Liaugminas <img alt='Lithuania' src='https://cdnjs.cloudflare.com/ajax/libs/emojione/2.1.4/assets/svg/1f1f1-1f1f9.svg' width='18' height='18'>
|
||||
* Darlington Adibe <img alt='Nigeria' src='https://cdnjs.cloudflare.com/ajax/libs/emojione/2.1.4/assets/svg/1f1f3-1f1ec.svg' width='18' height='18'>
|
||||
|
||||
---
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
version: 5.6.3.{build}
|
||||
version: 5.6.6.{build}
|
||||
environment:
|
||||
matrix:
|
||||
- CMAKE_GENERATOR: Visual Studio 14 2015
|
||||
|
@ -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,6 +98,9 @@ class JsonWriter {
|
||||
powersOf10 = 0;
|
||||
}
|
||||
|
||||
// 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);
|
||||
JsonFloat remainder = value - static_cast<JsonFloat>(int_part);
|
||||
@ -115,9 +118,6 @@ class JsonWriter {
|
||||
char currentDigit = char(remainder);
|
||||
remainder -= static_cast<JsonFloat>(currentDigit);
|
||||
|
||||
// Round up last digit (so that print(1.999, 2) prints as "2.00")
|
||||
if (digits == 0 && remainder >= 0.5) currentDigit++;
|
||||
|
||||
// Print
|
||||
writeRaw(char('0' + currentDigit));
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -8,11 +8,29 @@
|
||||
#pragma once
|
||||
|
||||
#include "JsonArray.hpp"
|
||||
#include "JsonObject.hpp"
|
||||
#include "JsonArraySubscript.hpp"
|
||||
#include "JsonObject.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
|
||||
inline JsonVariant::JsonVariant(JsonArray &array) {
|
||||
if (array.success()) {
|
||||
_type = Internals::JSON_ARRAY;
|
||||
_content.asArray = &array;
|
||||
} else {
|
||||
_type = Internals::JSON_UNDEFINED;
|
||||
}
|
||||
}
|
||||
|
||||
inline JsonVariant::JsonVariant(JsonObject &object) {
|
||||
if (object.success()) {
|
||||
_type = Internals::JSON_OBJECT;
|
||||
_content.asObject = &object;
|
||||
} else {
|
||||
_type = Internals::JSON_UNDEFINED;
|
||||
}
|
||||
}
|
||||
|
||||
template <>
|
||||
inline bool JsonArray::setNodeValue(node_type *node, String &value) {
|
||||
const char *copy = _buffer->strdup(value);
|
||||
|
@ -107,16 +107,10 @@ class JsonVariant : public JsonVariantBase<JsonVariant> {
|
||||
}
|
||||
|
||||
// Create a JsonVariant containing a reference to an array.
|
||||
JsonVariant(JsonArray &array) {
|
||||
_type = Internals::JSON_ARRAY;
|
||||
_content.asArray = &array;
|
||||
}
|
||||
JsonVariant(JsonArray &array);
|
||||
|
||||
// Create a JsonVariant containing a reference to an object.
|
||||
JsonVariant(JsonObject &object) {
|
||||
_type = Internals::JSON_OBJECT;
|
||||
_content.asObject = &object;
|
||||
}
|
||||
JsonVariant(JsonObject &object);
|
||||
|
||||
// Get the variant as the specified type.
|
||||
//
|
||||
@ -300,8 +294,13 @@ class JsonVariant : public JsonVariantBase<JsonVariant> {
|
||||
return T();
|
||||
}
|
||||
|
||||
// DEPRECATED: use as<char*>() instead
|
||||
const char *asString() const;
|
||||
|
||||
// DEPRECATED: use as<JsonArray>() instead
|
||||
JsonArray &asArray() const;
|
||||
|
||||
// DEPRECATED: use as<JsonObject>() instead
|
||||
JsonObject &asObject() const;
|
||||
|
||||
private:
|
||||
@ -324,7 +323,9 @@ class JsonVariant : public JsonVariantBase<JsonVariant> {
|
||||
return _type == Internals::JSON_OBJECT;
|
||||
}
|
||||
bool isString() const {
|
||||
return _type == Internals::JSON_STRING;
|
||||
return _type == Internals::JSON_STRING ||
|
||||
(_type == Internals::JSON_UNPARSED && _content.asString &&
|
||||
!strcmp("null", _content.asString));
|
||||
}
|
||||
|
||||
// The current type of the variant
|
||||
|
@ -1,12 +1,12 @@
|
||||
{
|
||||
"name": "Json",
|
||||
"name": "ArduinoJson",
|
||||
"keywords": "json, rest, http, web",
|
||||
"description": "An elegant and efficient JSON library for embedded systems",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/bblanchon/ArduinoJson.git"
|
||||
},
|
||||
"version": "5.6.3",
|
||||
"version": "5.6.6",
|
||||
"authors": {
|
||||
"name": "Benoit Blanchon",
|
||||
"url": "http://blog.benoitblanchon.fr"
|
||||
|
@ -1,5 +1,5 @@
|
||||
name=ArduinoJson
|
||||
version=5.6.3
|
||||
version=5.6.6
|
||||
author=Benoit Blanchon <blog.benoitblanchon.fr>
|
||||
maintainer=Benoit Blanchon <blog.benoitblanchon.fr>
|
||||
sentence=An efficient and elegant JSON library for Arduino.
|
||||
|
108
scripts/buffer-size-calculator.html
Normal file
108
scripts/buffer-size-calculator.html
Normal file
@ -0,0 +1,108 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>ArduinoJson - JsonBuffer size calculator</title>
|
||||
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<div class="jumbotron">
|
||||
<h1>JsonBuffer size calculator</h1>
|
||||
</div>
|
||||
<div id='error' class="alert alert-danger" role="alert">
|
||||
Please paste your JSON in the "input" box
|
||||
</div>
|
||||
<div class="row">
|
||||
<div class="col-md-6">
|
||||
<h2>Input</h2>
|
||||
<textarea class="form-control" rows=30 id='input'></textarea><br>
|
||||
</div>
|
||||
<div id='results' class="col-md-6" style='display:none'>
|
||||
<h2>Result</h2>
|
||||
<h3>Expression</h3>
|
||||
<p><code id='resultexpr'></code></p>
|
||||
<table class="table">
|
||||
<thead>
|
||||
<th>Platform</th>
|
||||
<th>Size (in bytes)</th>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td>AVR 8-bit</td>
|
||||
<td id='sizeavr8'></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>ESP8266</td>
|
||||
<td id='sizeesp8266'></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>x86</td>
|
||||
<td id='sizex86'></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>x64</td>
|
||||
<td id='sizex64'></td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
<script type="text/javascript">
|
||||
function getExpression(obj) {
|
||||
var elements = [];
|
||||
if (obj instanceof Array) {
|
||||
elements.push("JSON_ARRAY_SIZE(" + obj.length + ")");
|
||||
for (var i = 0; i<obj.length; i++) {
|
||||
elements.push(getExpression(obj[i]))
|
||||
}
|
||||
}
|
||||
else if (obj instanceof Object) {
|
||||
elements.push("JSON_OBJECT_SIZE(" + Object.keys(obj).length + ")");
|
||||
for (var key in obj) {
|
||||
elements.push(getExpression(obj[key]))
|
||||
}
|
||||
}
|
||||
return elements.filter(function(x){return x.length > 0}).join(" + ");
|
||||
}
|
||||
|
||||
input.oninput = function(e) {
|
||||
results.style.display = 'none';
|
||||
error.style.visibility = 'hidden';
|
||||
|
||||
try {
|
||||
var obj = JSON.parse(input.value);
|
||||
var expression = getExpression(obj);
|
||||
|
||||
resultexpr.innerText = expression;
|
||||
sizeavr8.innerText = eval(
|
||||
"function JSON_ARRAY_SIZE(n) { return 4 + 8*n }" +
|
||||
"function JSON_OBJECT_SIZE(n) { return 4 + 10*n }" +
|
||||
expression
|
||||
);
|
||||
sizeesp8266.innerText = eval(
|
||||
"function JSON_ARRAY_SIZE(n) { return 8 + 12*n }" +
|
||||
"function JSON_OBJECT_SIZE(n) { return 8 + 16*n }" +
|
||||
expression
|
||||
);
|
||||
sizex86.innerText = eval(
|
||||
"function JSON_ARRAY_SIZE(n) { return 12 + 24*n }" +
|
||||
"function JSON_OBJECT_SIZE(n) { return 12 + 32*n }" +
|
||||
expression
|
||||
);
|
||||
sizex64.innerText = eval(
|
||||
"function JSON_ARRAY_SIZE(n) { return 24 + 24*n }" +
|
||||
"function JSON_OBJECT_SIZE(n) { return 24 + 32*n }" +
|
||||
expression
|
||||
);
|
||||
results.style.display = 'block';
|
||||
}
|
||||
catch (ex) {
|
||||
error.innerText = "ERROR: " + ex.message;
|
||||
error.style.visibility = 'visible';
|
||||
}
|
||||
}
|
||||
</script>
|
||||
</html>
|
@ -1,6 +1,6 @@
|
||||
# Copyright Benoit Blanchon 2014-2016
|
||||
# MIT License
|
||||
#
|
||||
#
|
||||
# Arduino JSON library
|
||||
# https://github.com/bblanchon/ArduinoJson
|
||||
# If you like this project, please add a star!
|
||||
@ -25,7 +25,7 @@ if(CMAKE_CXX_COMPILER_ID MATCHES "(GNU|Clang)")
|
||||
-Wformat=2
|
||||
-Winit-self
|
||||
-Wmissing-include-dirs
|
||||
-Wno-parentheses
|
||||
-Wparentheses
|
||||
-Wno-sign-conversion
|
||||
-Wno-unused
|
||||
-Wno-variadic-macros
|
||||
|
@ -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>
|
||||
|
||||
#define SUITE JsonVariant_Is_Tests
|
||||
|
||||
@ -34,75 +34,229 @@ void assertIs(JsonArray& value) {
|
||||
ASSERT_TRUE(variant.is<TTo>());
|
||||
}
|
||||
|
||||
TEST(SUITE, ArrayIsArry) { assertIs<JsonArray&>(JsonArray::invalid()); }
|
||||
TEST(SUITE, ArrayIsBool) { assertIsNot<bool>(JsonArray::invalid()); }
|
||||
TEST(SUITE, ArrayIsDouble) { assertIsNot<double>(JsonArray::invalid()); }
|
||||
TEST(SUITE, ArrayIsFloat) { assertIsNot<float>(JsonArray::invalid()); }
|
||||
TEST(SUITE, ArrayIsInt) { assertIsNot<int>(JsonArray::invalid()); }
|
||||
TEST(SUITE, ArrayIsLong) { assertIsNot<long>(JsonArray::invalid()); }
|
||||
TEST(SUITE, ArrayIsString) { assertIsNot<const char*>(JsonArray::invalid()); }
|
||||
TEST(SUITE, ArrayIsArray) {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
assertIs<JsonArray&>(jsonBuffer.createArray());
|
||||
}
|
||||
TEST(SUITE, ArrayIsNotBool) {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
assertIsNot<bool>(jsonBuffer.createArray());
|
||||
}
|
||||
TEST(SUITE, ArrayIsNotDouble) {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
assertIsNot<double>(jsonBuffer.createArray());
|
||||
}
|
||||
TEST(SUITE, ArrayIsNotFloat) {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
assertIsNot<float>(jsonBuffer.createArray());
|
||||
}
|
||||
TEST(SUITE, ArrayIsNotInt) {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
assertIsNot<int>(jsonBuffer.createArray());
|
||||
}
|
||||
TEST(SUITE, ArrayIsNotLong) {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
assertIsNot<long>(jsonBuffer.createArray());
|
||||
}
|
||||
TEST(SUITE, ArrayIsNotString) {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
assertIsNot<const char*>(jsonBuffer.createArray());
|
||||
}
|
||||
|
||||
TEST(SUITE, BoolIsArray) { assertIsNot<JsonArray&>(true); }
|
||||
TEST(SUITE, BoolIsBool) { assertIs<bool>(true); }
|
||||
TEST(SUITE, BoolIsDouble) { assertIsNot<double>(true); }
|
||||
TEST(SUITE, BoolIsFloat) { assertIsNot<float>(true); }
|
||||
TEST(SUITE, BoolIsInt) { assertIsNot<int>(true); }
|
||||
TEST(SUITE, BoolIsLong) { assertIsNot<long>(true); }
|
||||
TEST(SUITE, BoolIsString) { assertIsNot<const char*>(true); }
|
||||
TEST(SUITE, BoolIsArray) {
|
||||
assertIsNot<JsonArray&>(true);
|
||||
}
|
||||
TEST(SUITE, BoolIsBool) {
|
||||
assertIs<bool>(true);
|
||||
}
|
||||
TEST(SUITE, BoolIsDouble) {
|
||||
assertIsNot<double>(true);
|
||||
}
|
||||
TEST(SUITE, BoolIsFloat) {
|
||||
assertIsNot<float>(true);
|
||||
}
|
||||
TEST(SUITE, BoolIsInt) {
|
||||
assertIsNot<int>(true);
|
||||
}
|
||||
TEST(SUITE, BoolIsLong) {
|
||||
assertIsNot<long>(true);
|
||||
}
|
||||
TEST(SUITE, BoolIsString) {
|
||||
assertIsNot<const char*>(true);
|
||||
}
|
||||
|
||||
TEST(SUITE, DoubleIsArray) { assertIsNot<JsonArray&>(4.2); }
|
||||
TEST(SUITE, DoubleIsBool) { assertIsNot<bool>(4.2); }
|
||||
TEST(SUITE, DoubleIsDouble) { assertIs<double>(4.2); }
|
||||
TEST(SUITE, DoubleIsFloat) { assertIs<float>(4.2); }
|
||||
TEST(SUITE, DoubleIsInt) { assertIsNot<int>(4.2); }
|
||||
TEST(SUITE, DoubleIsLong) { assertIsNot<long>(4.2); }
|
||||
TEST(SUITE, DoubleIsString) { assertIsNot<const char*>(4.2); }
|
||||
TEST(SUITE, DoubleIsArray) {
|
||||
assertIsNot<JsonArray&>(4.2);
|
||||
}
|
||||
TEST(SUITE, DoubleIsBool) {
|
||||
assertIsNot<bool>(4.2);
|
||||
}
|
||||
TEST(SUITE, DoubleIsDouble) {
|
||||
assertIs<double>(4.2);
|
||||
}
|
||||
TEST(SUITE, DoubleIsFloat) {
|
||||
assertIs<float>(4.2);
|
||||
}
|
||||
TEST(SUITE, DoubleIsInt) {
|
||||
assertIsNot<int>(4.2);
|
||||
}
|
||||
TEST(SUITE, DoubleIsLong) {
|
||||
assertIsNot<long>(4.2);
|
||||
}
|
||||
TEST(SUITE, DoubleIsString) {
|
||||
assertIsNot<const char*>(4.2);
|
||||
}
|
||||
|
||||
TEST(SUITE, LongIsArray) { assertIsNot<JsonArray&>(42L); }
|
||||
TEST(SUITE, LongIsBool) { assertIsNot<bool>(42L); }
|
||||
TEST(SUITE, LongIsDouble) { assertIsNot<double>(42L); }
|
||||
TEST(SUITE, LongIsFloat) { assertIsNot<float>(42L); }
|
||||
TEST(SUITE, LongIsInt) { assertIs<int>(42L); }
|
||||
TEST(SUITE, LongIsLong) { assertIs<long>(42L); }
|
||||
TEST(SUITE, LongIsString) { assertIsNot<const char*>(42L); }
|
||||
TEST(SUITE, LongIsArray) {
|
||||
assertIsNot<JsonArray&>(42L);
|
||||
}
|
||||
TEST(SUITE, LongIsBool) {
|
||||
assertIsNot<bool>(42L);
|
||||
}
|
||||
TEST(SUITE, LongIsDouble) {
|
||||
assertIsNot<double>(42L);
|
||||
}
|
||||
TEST(SUITE, LongIsFloat) {
|
||||
assertIsNot<float>(42L);
|
||||
}
|
||||
TEST(SUITE, LongIsInt) {
|
||||
assertIs<int>(42L);
|
||||
}
|
||||
TEST(SUITE, LongIsLong) {
|
||||
assertIs<long>(42L);
|
||||
}
|
||||
TEST(SUITE, LongIsString) {
|
||||
assertIsNot<const char*>(42L);
|
||||
}
|
||||
|
||||
TEST(SUITE, StringIsArray) { assertIsNot<JsonArray&>("42"); }
|
||||
TEST(SUITE, StringIsBool) { assertIsNot<bool>("42"); }
|
||||
TEST(SUITE, StringIsDouble) { assertIsNot<double>("42"); }
|
||||
TEST(SUITE, StringIsFloat) { assertIsNot<float>("42"); }
|
||||
TEST(SUITE, StringIsInt) { assertIsNot<int>("42"); }
|
||||
TEST(SUITE, StringIsLong) { assertIsNot<long>("42"); }
|
||||
TEST(SUITE, StringIsString) { assertIs<const char*>("42"); }
|
||||
TEST(SUITE, StringIsArray) {
|
||||
assertIsNot<JsonArray&>("42");
|
||||
}
|
||||
TEST(SUITE, StringIsBool) {
|
||||
assertIsNot<bool>("42");
|
||||
}
|
||||
TEST(SUITE, StringIsDouble) {
|
||||
assertIsNot<double>("42");
|
||||
}
|
||||
TEST(SUITE, StringIsFloat) {
|
||||
assertIsNot<float>("42");
|
||||
}
|
||||
TEST(SUITE, StringIsInt) {
|
||||
assertIsNot<int>("42");
|
||||
}
|
||||
TEST(SUITE, StringIsLong) {
|
||||
assertIsNot<long>("42");
|
||||
}
|
||||
TEST(SUITE, StringIsString) {
|
||||
assertIs<const char*>("42");
|
||||
}
|
||||
|
||||
TEST(SUITE, UnparsedTrueIsArra) { assertIsNot<JsonArray&>(RawJson("true")); }
|
||||
TEST(SUITE, UnparsedTrueIsBool) { assertIs<bool>(RawJson("true")); }
|
||||
TEST(SUITE, UnparsedTrueIsDouble) { assertIsNot<double>(RawJson("true")); }
|
||||
TEST(SUITE, UnparsedTrueIsFloat) { assertIsNot<float>(RawJson("true")); }
|
||||
TEST(SUITE, UnparsedTrueIsInt) { assertIsNot<int>(RawJson("true")); }
|
||||
TEST(SUITE, UnparsedTrueIsLong) { assertIsNot<long>(RawJson("true")); }
|
||||
TEST(SUITE, UnparsedTrueIsString) { assertIsNot<const char*>(RawJson("true")); }
|
||||
TEST(SUITE, UnparsedTrueIsArray) {
|
||||
assertIsNot<JsonArray&>(RawJson("true"));
|
||||
}
|
||||
TEST(SUITE, UnparsedTrueIsBool) {
|
||||
assertIs<bool>(RawJson("true"));
|
||||
}
|
||||
TEST(SUITE, UnparsedTrueIsDouble) {
|
||||
assertIsNot<double>(RawJson("true"));
|
||||
}
|
||||
TEST(SUITE, UnparsedTrueIsFloat) {
|
||||
assertIsNot<float>(RawJson("true"));
|
||||
}
|
||||
TEST(SUITE, UnparsedTrueIsInt) {
|
||||
assertIsNot<int>(RawJson("true"));
|
||||
}
|
||||
TEST(SUITE, UnparsedTrueIsLong) {
|
||||
assertIsNot<long>(RawJson("true"));
|
||||
}
|
||||
TEST(SUITE, UnparsedTrueIsString) {
|
||||
assertIsNot<const char*>(RawJson("true"));
|
||||
}
|
||||
|
||||
TEST(SUITE, UnparsedFalseIsArra) { assertIsNot<JsonArray&>(RawJson("false")); }
|
||||
TEST(SUITE, UnparsedFalseIsBool) { assertIs<bool>(RawJson("false")); }
|
||||
TEST(SUITE, UnparsedFalseIsDouble) { assertIsNot<double>(RawJson("false")); }
|
||||
TEST(SUITE, UnparsedFalseIsFloat) { assertIsNot<float>(RawJson("false")); }
|
||||
TEST(SUITE, UnparsedFalseIsInt) { assertIsNot<int>(RawJson("false")); }
|
||||
TEST(SUITE, UnparsedFalseIsLong) { assertIsNot<long>(RawJson("false")); }
|
||||
TEST(SUITE, UnparsedFalseIsArray) {
|
||||
assertIsNot<JsonArray&>(RawJson("false"));
|
||||
}
|
||||
TEST(SUITE, UnparsedFalseIsBool) {
|
||||
assertIs<bool>(RawJson("false"));
|
||||
}
|
||||
TEST(SUITE, UnparsedFalseIsDouble) {
|
||||
assertIsNot<double>(RawJson("false"));
|
||||
}
|
||||
TEST(SUITE, UnparsedFalseIsFloat) {
|
||||
assertIsNot<float>(RawJson("false"));
|
||||
}
|
||||
TEST(SUITE, UnparsedFalseIsInt) {
|
||||
assertIsNot<int>(RawJson("false"));
|
||||
}
|
||||
TEST(SUITE, UnparsedFalseIsLong) {
|
||||
assertIsNot<long>(RawJson("false"));
|
||||
}
|
||||
TEST(SUITE, UnparsedFalseIsString) {
|
||||
assertIsNot<const char*>(RawJson("false"));
|
||||
}
|
||||
|
||||
TEST(SUITE, UnparsedIntIsArra) { assertIsNot<JsonArray&>(RawJson("42")); }
|
||||
TEST(SUITE, UnparsedIntIsBool) { assertIsNot<bool>(RawJson("42")); }
|
||||
TEST(SUITE, UnparsedIntIsDouble) { assertIsNot<double>(RawJson("42")); }
|
||||
TEST(SUITE, UnparsedIntIsFloat) { assertIsNot<float>(RawJson("42")); }
|
||||
TEST(SUITE, UnparsedIntIsInt) { assertIs<int>(RawJson("42")); }
|
||||
TEST(SUITE, UnparsedIntIsLong) { assertIs<long>(RawJson("42")); }
|
||||
TEST(SUITE, UnparsedIntIsString) { assertIsNot<const char*>(RawJson("42")); }
|
||||
TEST(SUITE, UnparsedIntIsArray) {
|
||||
assertIsNot<JsonArray&>(RawJson("42"));
|
||||
}
|
||||
TEST(SUITE, UnparsedIntIsBool) {
|
||||
assertIsNot<bool>(RawJson("42"));
|
||||
}
|
||||
TEST(SUITE, UnparsedIntIsDouble) {
|
||||
assertIsNot<double>(RawJson("42"));
|
||||
}
|
||||
TEST(SUITE, UnparsedIntIsFloat) {
|
||||
assertIsNot<float>(RawJson("42"));
|
||||
}
|
||||
TEST(SUITE, UnparsedIntIsInt) {
|
||||
assertIs<int>(RawJson("42"));
|
||||
}
|
||||
TEST(SUITE, UnparsedIntIsLong) {
|
||||
assertIs<long>(RawJson("42"));
|
||||
}
|
||||
TEST(SUITE, UnparsedIntIsString) {
|
||||
assertIsNot<const char*>(RawJson("42"));
|
||||
}
|
||||
|
||||
TEST(SUITE, UnparsedFloatIsBool) { assertIsNot<bool>(RawJson("4.2e-10")); }
|
||||
TEST(SUITE, UnparsedFloatIsDouble) { assertIs<double>(RawJson("4.2e-10")); }
|
||||
TEST(SUITE, UnparsedFloatIsFloat) { assertIs<float>(RawJson("4.2e-10")); }
|
||||
TEST(SUITE, UnparsedFloatIsInt) { assertIsNot<int>(RawJson("4.2e-10")); }
|
||||
TEST(SUITE, UnparsedFloatIsLong) { assertIsNot<long>(RawJson("4.2e-10")); }
|
||||
TEST(SUITE, UnparsedFloatIsStr) { assertIsNot<const char*>(RawJson("4.2")); }
|
||||
TEST(SUITE, UnparsedFloatIsBool) {
|
||||
assertIsNot<bool>(RawJson("4.2e-10"));
|
||||
}
|
||||
TEST(SUITE, UnparsedFloatIsDouble) {
|
||||
assertIs<double>(RawJson("4.2e-10"));
|
||||
}
|
||||
TEST(SUITE, UnparsedFloatIsFloat) {
|
||||
assertIs<float>(RawJson("4.2e-10"));
|
||||
}
|
||||
TEST(SUITE, UnparsedFloatIsInt) {
|
||||
assertIsNot<int>(RawJson("4.2e-10"));
|
||||
}
|
||||
TEST(SUITE, UnparsedFloatIsLong) {
|
||||
assertIsNot<long>(RawJson("4.2e-10"));
|
||||
}
|
||||
TEST(SUITE, UnparsedFloatIsStr) {
|
||||
assertIsNot<const char*>(RawJson("4.2"));
|
||||
}
|
||||
|
||||
TEST(SUITE, UnparsedNullIsArray) {
|
||||
assertIsNot<JsonArray&>(RawJson("null"));
|
||||
}
|
||||
TEST(SUITE, UnparsedNullIsBool) {
|
||||
assertIsNot<bool>(RawJson("null"));
|
||||
}
|
||||
TEST(SUITE, UnparsedNullIsDouble) {
|
||||
assertIsNot<double>(RawJson("null"));
|
||||
}
|
||||
TEST(SUITE, UnparsedNullIsFloat) {
|
||||
assertIsNot<float>(RawJson("null"));
|
||||
}
|
||||
TEST(SUITE, UnparsedNullIsInt) {
|
||||
assertIsNot<int>(RawJson("null"));
|
||||
}
|
||||
TEST(SUITE, UnparsedNullIsLong) {
|
||||
assertIsNot<long>(RawJson("null"));
|
||||
}
|
||||
TEST(SUITE, UnparsedNullIsConstCharPtr) {
|
||||
assertIs<const char*>(RawJson("null"));
|
||||
}
|
||||
TEST(SUITE, UnparsedNullIsCharPtr) {
|
||||
assertIs<char*>(RawJson("null"));
|
||||
}
|
||||
|
43
test/JsonVariant_Success_Tests.cpp
Normal file
43
test/JsonVariant_Success_Tests.cpp
Normal file
@ -0,0 +1,43 @@
|
||||
// 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 <ArduinoJson.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
TEST(JsonVariant_Success_Tests, ReturnsFalse_WhenUndefined) {
|
||||
JsonVariant variant;
|
||||
EXPECT_FALSE(variant.success());
|
||||
}
|
||||
|
||||
TEST(JsonVariant_Success_Tests, ReturnsTrue_WhenInteger) {
|
||||
JsonVariant variant = 0;
|
||||
EXPECT_TRUE(variant.success());
|
||||
}
|
||||
|
||||
TEST(JsonVariant_Success_Tests, ReturnsTrue_WhenEmptyArray) {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
|
||||
JsonVariant variant = jsonBuffer.createArray();
|
||||
EXPECT_TRUE(variant.success());
|
||||
}
|
||||
|
||||
TEST(JsonVariant_Success_Tests, ReturnsTrue_WhenEmptyObject) {
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
|
||||
JsonVariant variant = jsonBuffer.createObject();
|
||||
EXPECT_TRUE(variant.success());
|
||||
}
|
||||
|
||||
TEST(JsonVariant_Success_Tests, ReturnsFalse_WhenInvalidArray) {
|
||||
JsonVariant variant = JsonArray::invalid();
|
||||
EXPECT_FALSE(variant.success());
|
||||
}
|
||||
|
||||
TEST(JsonVariant_Success_Tests, ReturnsFalse_WhenInvalidObject) {
|
||||
JsonVariant variant = JsonObject::invalid();
|
||||
EXPECT_FALSE(variant.success());
|
||||
}
|
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