forked from bblanchon/ArduinoJson
Compare commits
16 Commits
Author | SHA1 | Date | |
---|---|---|---|
45bbf6db86 | |||
432476c98a | |||
1f3e227a8b | |||
66c28020c5 | |||
6cfe2a58eb | |||
fc6ad51e68 | |||
d3bc52951a | |||
2f7232859e | |||
4a7232ac99 | |||
72d78432c9 | |||
f6cd42d916 | |||
542dff2a08 | |||
e75e843c88 | |||
146a76247c | |||
f28157cab7 | |||
1ce16ce449 |
@ -104,10 +104,8 @@ matrix:
|
||||
osx_image: xcode7.3
|
||||
compiler: clang
|
||||
env: SCRIPT=cmake-osx
|
||||
- env: SCRIPT=arduino VERSION=1.5.8 BOARD=arduino:avr:uno
|
||||
- env: SCRIPT=arduino VERSION=1.6.7 BOARD=arduino:avr:uno
|
||||
- env: SCRIPT=arduino VERSION=1.6.8 BOARD=arduino:avr:uno
|
||||
- env: SCRIPT=arduino VERSION=1.6.9 BOARD=arduino:avr:uno
|
||||
- env: SCRIPT=arduino VERSION=1.6.12 BOARD=arduino:avr:uno
|
||||
- env: SCRIPT=platformio BOARD=uno
|
||||
- env: SCRIPT=platformio BOARD=due
|
||||
- env: SCRIPT=platformio BOARD=esp01
|
||||
|
19
CHANGELOG.md
19
CHANGELOG.md
@ -1,6 +1,25 @@
|
||||
ArduinoJson: change log
|
||||
=======================
|
||||
|
||||
v5.7.3
|
||||
------
|
||||
|
||||
* Added an `printTo(char[N])` and `prettyPrintTo(char[N])` (issue #292)
|
||||
* Added ability to set a nested value like this: `root["A"]["B"] = "C"` (issue #352)
|
||||
* Renamed `*.ipp` to `*Impl.hpp` because they were ignored by Arduino IDE (issue #396)
|
||||
|
||||
v5.7.2
|
||||
------
|
||||
|
||||
* Made PROGMEM available on more platforms (issue #381)
|
||||
* Fixed PROGMEM causing an exception on ESP8266 (issue #383)
|
||||
|
||||
v5.7.1
|
||||
------
|
||||
|
||||
* Added support for PROGMEM (issue #76)
|
||||
* Fixed compilation error when index is not an `int` (issue #381)
|
||||
|
||||
v5.7.0
|
||||
------
|
||||
|
||||
|
@ -103,6 +103,9 @@ From GitHub user `zacsketches`:
|
||||
[From Twitter user `@hemalchevli`](https://twitter.com/hemalchevli/status/715788439397011456):
|
||||
> ArduinoJson library should be used as a benchmark/reference for making libraries. Truly elegant.
|
||||
|
||||
[From GitHub user `sticilface`](https://github.com/bblanchon/ArduinoJson/issues/381#issuecomment-260203594):
|
||||
> its a great lib:) and i use it in everything!
|
||||
|
||||
Donators
|
||||
--------
|
||||
|
||||
@ -123,6 +126,8 @@ Special thanks to the following persons and companies who made generous donation
|
||||
* 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'>
|
||||
* Yoeri Kroon <img alt='Netherlands' src='https://cdnjs.cloudflare.com/ajax/libs/emojione/2.1.4/assets/svg/1f1f3-1f1f1.svg' width='18' height='18'>
|
||||
* Andrew Melvin <img alt='United Kingdom' src='https://cdnjs.cloudflare.com/ajax/libs/emojione/2.1.4/assets/svg/1f1ec-1f1e7.svg' width='18' height='18'>
|
||||
* Doanh Luong <img alt ='Vietnam' src='https://cdnjs.cloudflare.com/ajax/libs/emojione/2.1.4/assets/svg/1f1fb-1f1f3.svg' width='18' height='18'>
|
||||
|
||||
---
|
||||
|
||||
|
@ -1,4 +1,4 @@
|
||||
version: 5.7.0.{build}
|
||||
version: 5.7.3.{build}
|
||||
environment:
|
||||
matrix:
|
||||
- CMAKE_GENERATOR: Visual Studio 14 2015
|
||||
|
51
examples/ProgmemExample/ProgmemExample.ino
Normal file
51
examples/ProgmemExample/ProgmemExample.ino
Normal file
@ -0,0 +1,51 @@
|
||||
// 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>
|
||||
|
||||
// About
|
||||
// -----
|
||||
// This example shows the different ways you can use PROGMEM with ArduinoJson.
|
||||
// Please don't see this as an invitation to use PROGMEM.
|
||||
// On the contrary, you should always use char[] when possible, it's much more
|
||||
// efficient in term of code size, speed and memory usage.
|
||||
|
||||
void setup() {
|
||||
#ifdef PROGMEM
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
|
||||
// You can use a Flash String as your JSON input.
|
||||
// WARNING: the content of the Flash String will be duplicated in the
|
||||
// JsonBuffer.
|
||||
JsonObject& root =
|
||||
jsonBuffer.parseObject(F("{\"sensor\":\"gps\",\"time\":1351824120,"
|
||||
"\"data\":[48.756080,2.302038]}"));
|
||||
|
||||
// You can use a Flash String to get an element of a JsonObject
|
||||
// No duplication is done.
|
||||
long time = root[F("time")];
|
||||
|
||||
// You can use a Flash String to set an element of a JsonObject
|
||||
// WARNING: the content of the Flash String will be duplicated in the
|
||||
// JsonBuffer.
|
||||
root[F("time")] = time;
|
||||
|
||||
// You can set a Flash String to a JsonObject or JsonArray:
|
||||
// WARNING: the content of the Flash String will be duplicated in the
|
||||
// JsonBuffer.
|
||||
root["sensor"] = F("gps");
|
||||
|
||||
#else
|
||||
|
||||
#warning PROGMEM is only supported on AVR architecture
|
||||
|
||||
#endif
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// not used in this example
|
||||
}
|
@ -33,7 +33,12 @@ void setup() {
|
||||
|
||||
// You can get a String from a JsonObject or JsonArray:
|
||||
// No duplication is done, at least not in the JsonBuffer.
|
||||
String sensor = root[String("sensor")];
|
||||
String sensor = root["sensor"];
|
||||
|
||||
// Unfortunately, the following doesn't work (issue #118):
|
||||
// sensor = root["sensor"]; // <- error "ambiguous overload for 'operator='"
|
||||
// As a workaround, you need to replace by:
|
||||
sensor = root["sensor"].as<String>();
|
||||
|
||||
// You can set a String to a JsonObject or JsonArray:
|
||||
// WARNING: the content of the String will be duplicated in the JsonBuffer.
|
||||
|
@ -12,11 +12,11 @@
|
||||
#include "ArduinoJson/JsonObject.hpp"
|
||||
#include "ArduinoJson/StaticJsonBuffer.hpp"
|
||||
|
||||
#include "ArduinoJson/Internals/JsonParser.ipp"
|
||||
#include "ArduinoJson/Internals/JsonSerializer.ipp"
|
||||
#include "ArduinoJson/JsonArray.ipp"
|
||||
#include "ArduinoJson/JsonBuffer.ipp"
|
||||
#include "ArduinoJson/JsonObject.ipp"
|
||||
#include "ArduinoJson/JsonVariant.ipp"
|
||||
#include "ArduinoJson/Internals/JsonParserImpl.hpp"
|
||||
#include "ArduinoJson/Internals/JsonSerializerImpl.hpp"
|
||||
#include "ArduinoJson/JsonArrayImpl.hpp"
|
||||
#include "ArduinoJson/JsonBufferImpl.hpp"
|
||||
#include "ArduinoJson/JsonObjectImpl.hpp"
|
||||
#include "ArduinoJson/JsonVariantImpl.hpp"
|
||||
|
||||
using namespace ArduinoJson;
|
||||
|
@ -27,6 +27,15 @@
|
||||
#define ARDUINOJSON_ENABLE_ARDUINO_STRING 1
|
||||
#endif
|
||||
|
||||
// On AVR archiecture, we can use PROGMEM
|
||||
#ifndef ARDUINOJSON_ENABLE_PROGMEM
|
||||
#ifdef PROGMEM
|
||||
#define ARDUINOJSON_ENABLE_PROGMEM 1
|
||||
#else
|
||||
#define ARDUINOJSON_ENABLE_PROGMEM 0
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// Arduino doesn't have std::string
|
||||
#ifndef ARDUINOJSON_ENABLE_STD_STRING
|
||||
#define ARDUINOJSON_ENABLE_STD_STRING 0
|
||||
@ -87,6 +96,11 @@
|
||||
#define ARDUINOJSON_ENABLE_ARDUINO_STRING 0
|
||||
#endif
|
||||
|
||||
// PROGMEM is only available on AVR architecture
|
||||
#ifndef ARDUINOJSON_ENABLE_PROGMEM
|
||||
#define ARDUINOJSON_ENABLE_PROGMEM 0
|
||||
#endif
|
||||
|
||||
// on a computer, we can assume that the STL is there
|
||||
#ifndef ARDUINOJSON_ENABLE_STD_STREAM
|
||||
#define ARDUINOJSON_ENABLE_STD_STREAM 1
|
||||
|
@ -50,6 +50,11 @@ class JsonPrintable {
|
||||
return printTo(sb);
|
||||
}
|
||||
|
||||
template <size_t N>
|
||||
size_t printTo(char (&buffer)[N]) const {
|
||||
return printTo(buffer, N);
|
||||
}
|
||||
|
||||
template <typename TString>
|
||||
typename TypeTraits::EnableIf<StringFuncs<TString>::has_append, size_t>::type
|
||||
printTo(TString &str) const {
|
||||
@ -67,6 +72,11 @@ class JsonPrintable {
|
||||
return prettyPrintTo(sb);
|
||||
}
|
||||
|
||||
template <size_t N>
|
||||
size_t prettyPrintTo(char (&buffer)[N]) const {
|
||||
return prettyPrintTo(buffer, N);
|
||||
}
|
||||
|
||||
size_t prettyPrintTo(Print &print) const {
|
||||
IndentedPrint indentedPrint = IndentedPrint(print);
|
||||
return prettyPrintTo(indentedPrint);
|
||||
|
@ -44,6 +44,7 @@ struct CharPtrFuncs {
|
||||
}
|
||||
|
||||
static const bool has_append = false;
|
||||
static const bool has_equals = true;
|
||||
static const bool should_duplicate = false;
|
||||
};
|
||||
|
||||
@ -56,6 +57,9 @@ struct StringFuncs<char*> : CharPtrFuncs {};
|
||||
template <size_t N>
|
||||
struct StringFuncs<char[N]> : CharPtrFuncs {};
|
||||
|
||||
template <size_t N>
|
||||
struct StringFuncs<const char[N]> : CharPtrFuncs {};
|
||||
|
||||
template <typename TString>
|
||||
struct StdStringFuncs {
|
||||
template <typename Buffer>
|
||||
@ -76,6 +80,7 @@ struct StdStringFuncs {
|
||||
}
|
||||
|
||||
static const bool has_append = true;
|
||||
static const bool has_equals = true;
|
||||
static const bool should_duplicate = true;
|
||||
};
|
||||
|
||||
@ -90,5 +95,27 @@ struct StringFuncs<StringSumHelper> : StdStringFuncs<StringSumHelper> {};
|
||||
template <>
|
||||
struct StringFuncs<std::string> : StdStringFuncs<std::string> {};
|
||||
#endif
|
||||
|
||||
#if ARDUINOJSON_ENABLE_PROGMEM
|
||||
template <>
|
||||
struct StringFuncs<const __FlashStringHelper*> {
|
||||
static bool equals(const __FlashStringHelper* str, const char* expected) {
|
||||
return strcmp_P(expected, (PGM_P)str) == 0;
|
||||
}
|
||||
|
||||
template <typename Buffer>
|
||||
static char* duplicate(const __FlashStringHelper* str, Buffer* buffer) {
|
||||
if (!str) return NULL;
|
||||
size_t size = strlen_P((PGM_P)str) + 1;
|
||||
void* dup = buffer->alloc(size);
|
||||
if (dup != NULL) memcpy_P(dup, (PGM_P)str, size);
|
||||
return static_cast<char*>(dup);
|
||||
}
|
||||
|
||||
static const bool has_append = false;
|
||||
static const bool has_equals = true;
|
||||
static const bool should_duplicate = true;
|
||||
};
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
@ -227,4 +227,13 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
|
||||
value);
|
||||
}
|
||||
};
|
||||
|
||||
namespace Internals {
|
||||
template <>
|
||||
struct JsonVariantDefault<JsonArray> {
|
||||
static JsonArray &get() {
|
||||
return JsonArray::invalid();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -1,62 +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
|
||||
|
||||
#include "JsonArray.hpp"
|
||||
#include "JsonArraySubscript.hpp"
|
||||
#include "JsonObject.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
|
||||
inline JsonVariant::JsonVariant(const JsonArray &array) {
|
||||
if (array.success()) {
|
||||
_type = Internals::JSON_ARRAY;
|
||||
_content.asArray = const_cast<JsonArray *>(&array);
|
||||
} else {
|
||||
_type = Internals::JSON_UNDEFINED;
|
||||
}
|
||||
}
|
||||
|
||||
inline JsonVariant::JsonVariant(const JsonObject &object) {
|
||||
if (object.success()) {
|
||||
_type = Internals::JSON_OBJECT;
|
||||
_content.asObject = const_cast<JsonObject *>(&object);
|
||||
} else {
|
||||
_type = Internals::JSON_UNDEFINED;
|
||||
}
|
||||
}
|
||||
|
||||
namespace Internals {
|
||||
template <>
|
||||
struct JsonVariantDefault<JsonArray> {
|
||||
static JsonArray &get() {
|
||||
return JsonArray::invalid();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
inline JsonArray &JsonVariant::asArray() const {
|
||||
if (_type == Internals::JSON_ARRAY) return *_content.asArray;
|
||||
return JsonArray::invalid();
|
||||
}
|
||||
|
||||
inline JsonArray &JsonArray::createNestedArray() {
|
||||
if (!_buffer) return JsonArray::invalid();
|
||||
JsonArray &array = _buffer->createArray();
|
||||
add(array);
|
||||
return array;
|
||||
}
|
||||
|
||||
template <typename TString>
|
||||
inline JsonArray &JsonObject::createNestedArray(const TString &key) {
|
||||
if (!_buffer) return JsonArray::invalid();
|
||||
JsonArray &array = _buffer->createArray();
|
||||
set(key, array);
|
||||
return array;
|
||||
}
|
||||
}
|
29
include/ArduinoJson/JsonArrayImpl.hpp
Normal file
29
include/ArduinoJson/JsonArrayImpl.hpp
Normal file
@ -0,0 +1,29 @@
|
||||
// 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
|
||||
|
||||
#include "JsonArray.hpp"
|
||||
#include "JsonArraySubscript.hpp"
|
||||
#include "JsonObject.hpp"
|
||||
|
||||
namespace ArduinoJson {
|
||||
|
||||
inline JsonArray &JsonArray::createNestedArray() {
|
||||
if (!_buffer) return JsonArray::invalid();
|
||||
JsonArray &array = _buffer->createArray();
|
||||
add(array);
|
||||
return array;
|
||||
}
|
||||
|
||||
inline JsonObject &JsonArray::createNestedObject() {
|
||||
if (!_buffer) return JsonObject::invalid();
|
||||
JsonObject &object = _buffer->createObject();
|
||||
add(object);
|
||||
return object;
|
||||
}
|
||||
}
|
@ -67,6 +67,11 @@ inline JsonArraySubscript JsonArray::operator[](size_t index) {
|
||||
return JsonArraySubscript(*this, index);
|
||||
}
|
||||
|
||||
template <typename TImplem>
|
||||
inline JsonArraySubscript JsonVariantBase<TImplem>::operator[](int index) {
|
||||
return asArray()[index];
|
||||
}
|
||||
|
||||
template <typename TImplem>
|
||||
inline const JsonArraySubscript JsonVariantBase<TImplem>::operator[](
|
||||
int index) const {
|
||||
|
@ -165,4 +165,13 @@ class JsonObject : public Internals::JsonPrintable<JsonObject>,
|
||||
value);
|
||||
}
|
||||
};
|
||||
|
||||
namespace Internals {
|
||||
template <>
|
||||
struct JsonVariantDefault<JsonObject> {
|
||||
static JsonObject& get() {
|
||||
return JsonObject::invalid();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
@ -13,20 +13,6 @@
|
||||
|
||||
namespace ArduinoJson {
|
||||
|
||||
namespace Internals {
|
||||
template <>
|
||||
struct JsonVariantDefault<JsonObject> {
|
||||
static JsonObject &get() {
|
||||
return JsonObject::invalid();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
inline JsonObject &JsonVariant::asObject() const {
|
||||
if (_type == Internals::JSON_OBJECT) return *_content.asObject;
|
||||
return JsonObject::invalid();
|
||||
}
|
||||
|
||||
template <typename TString>
|
||||
inline JsonObject &JsonObject::createNestedObject(const TString &key) {
|
||||
if (!_buffer) return JsonObject::invalid();
|
||||
@ -35,10 +21,11 @@ inline JsonObject &JsonObject::createNestedObject(const TString &key) {
|
||||
return object;
|
||||
}
|
||||
|
||||
inline JsonObject &JsonArray::createNestedObject() {
|
||||
if (!_buffer) return JsonObject::invalid();
|
||||
JsonObject &object = _buffer->createObject();
|
||||
add(object);
|
||||
return object;
|
||||
template <typename TString>
|
||||
inline JsonArray &JsonObject::createNestedArray(const TString &key) {
|
||||
if (!_buffer) return JsonArray::invalid();
|
||||
JsonArray &array = _buffer->createArray();
|
||||
set(key, array);
|
||||
return array;
|
||||
}
|
||||
}
|
@ -84,13 +84,6 @@ template <typename TString>
|
||||
inline JsonObjectSubscript<TString> JsonObject::operator[](const TString& key) {
|
||||
return JsonObjectSubscript<TString>(*this, key);
|
||||
}
|
||||
|
||||
template <typename TImplem>
|
||||
template <class TString>
|
||||
inline const JsonObjectSubscript<TString> JsonVariantBase<TImplem>::operator[](
|
||||
const TString& key) const {
|
||||
return asObject()[key];
|
||||
}
|
||||
} // namespace ArduinoJson
|
||||
|
||||
#ifdef _MSC_VER
|
||||
|
@ -71,14 +71,26 @@ class JsonVariantBase : public Internals::JsonPrintable<TImpl> {
|
||||
// Returns the element at specified index if the variant is an array.
|
||||
// Returns JsonVariant::invalid() if the variant is not an array.
|
||||
FORCE_INLINE const JsonArraySubscript operator[](int index) const;
|
||||
FORCE_INLINE JsonArraySubscript operator[](int index);
|
||||
|
||||
// Mimics an object.
|
||||
// Returns the value associated with the specified key if the variant is
|
||||
// an object.
|
||||
// Return JsonVariant::invalid() if the variant is not an object.
|
||||
template <typename TString>
|
||||
FORCE_INLINE const JsonObjectSubscript<TString> operator[](
|
||||
const TString &key) const;
|
||||
FORCE_INLINE
|
||||
typename TypeTraits::EnableIf<Internals::StringFuncs<TString>::has_equals,
|
||||
const JsonObjectSubscript<TString> >::type
|
||||
operator[](const TString &key) const {
|
||||
return asObject()[key];
|
||||
}
|
||||
template <typename TString>
|
||||
FORCE_INLINE
|
||||
typename TypeTraits::EnableIf<Internals::StringFuncs<TString>::has_equals,
|
||||
JsonObjectSubscript<TString> >::type
|
||||
operator[](const TString &key) {
|
||||
return asObject()[key];
|
||||
}
|
||||
|
||||
private:
|
||||
const TImpl *impl() const {
|
||||
|
@ -19,6 +19,34 @@
|
||||
|
||||
namespace ArduinoJson {
|
||||
|
||||
inline JsonVariant::JsonVariant(const JsonArray &array) {
|
||||
if (array.success()) {
|
||||
_type = Internals::JSON_ARRAY;
|
||||
_content.asArray = const_cast<JsonArray *>(&array);
|
||||
} else {
|
||||
_type = Internals::JSON_UNDEFINED;
|
||||
}
|
||||
}
|
||||
|
||||
inline JsonVariant::JsonVariant(const JsonObject &object) {
|
||||
if (object.success()) {
|
||||
_type = Internals::JSON_OBJECT;
|
||||
_content.asObject = const_cast<JsonObject *>(&object);
|
||||
} else {
|
||||
_type = Internals::JSON_UNDEFINED;
|
||||
}
|
||||
}
|
||||
|
||||
inline JsonArray &JsonVariant::asArray() const {
|
||||
if (_type == Internals::JSON_ARRAY) return *_content.asArray;
|
||||
return JsonArray::invalid();
|
||||
}
|
||||
|
||||
inline JsonObject &JsonVariant::asObject() const {
|
||||
if (_type == Internals::JSON_OBJECT) return *_content.asObject;
|
||||
return JsonObject::invalid();
|
||||
}
|
||||
|
||||
inline Internals::JsonInteger JsonVariant::asInteger() const {
|
||||
using namespace Internals;
|
||||
switch (_type) {
|
@ -2,6 +2,7 @@ JsonArray KEYWORD1
|
||||
JsonObject KEYWORD1
|
||||
JsonVariant KEYWORD1
|
||||
StaticJsonBuffer KEYWORD1
|
||||
DynamicJsonBuffer KEYWORD1
|
||||
add KEYWORD2
|
||||
createArray KEYWORD2
|
||||
createNestedArray KEYWORD2
|
||||
|
@ -6,7 +6,7 @@
|
||||
"type": "git",
|
||||
"url": "https://github.com/bblanchon/ArduinoJson.git"
|
||||
},
|
||||
"version": "5.7.0",
|
||||
"version": "5.7.3",
|
||||
"authors": {
|
||||
"name": "Benoit Blanchon",
|
||||
"url": "http://blog.benoitblanchon.fr"
|
||||
|
@ -1,5 +1,5 @@
|
||||
name=ArduinoJson
|
||||
version=5.7.0
|
||||
version=5.7.3
|
||||
author=Benoit Blanchon <blog.benoitblanchon.fr>
|
||||
maintainer=Benoit Blanchon <blog.benoitblanchon.fr>
|
||||
sentence=An efficient and elegant JSON library for Arduino.
|
||||
|
@ -1,143 +0,0 @@
|
||||
<!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 Recipe() {
|
||||
var arrays = [];
|
||||
var objects = [];
|
||||
|
||||
this.addJsonArray = function(size) {
|
||||
if (arrays[size])
|
||||
arrays[size]++;
|
||||
else
|
||||
arrays[size] = 1;
|
||||
}
|
||||
|
||||
this.addJsonObject = function(size) {
|
||||
if (objects[size])
|
||||
objects[size]++;
|
||||
else
|
||||
objects[size] = 1;
|
||||
}
|
||||
|
||||
this.getExpression = function() {
|
||||
var elements = [];
|
||||
for (var size in arrays) {
|
||||
var count = arrays[size];
|
||||
if (count > 1)
|
||||
elements.push(count + "*JSON_ARRAY_SIZE("+size+")");
|
||||
else
|
||||
elements.push("JSON_ARRAY_SIZE("+size+")");
|
||||
}
|
||||
for (var size in objects) {
|
||||
var count = objects[size];
|
||||
if (count > 1)
|
||||
elements.push(count + "*JSON_OBJECT_SIZE("+size+")");
|
||||
else
|
||||
elements.push("JSON_OBJECT_SIZE("+size+")");
|
||||
}
|
||||
return elements.join(" + ");
|
||||
}
|
||||
}
|
||||
|
||||
function scanJson(recipe, obj) {
|
||||
if (obj instanceof Array) {
|
||||
recipe.addJsonArray(obj.length);
|
||||
for (var i = 0; i<obj.length; i++)
|
||||
scanJson(recipe, obj[i]);
|
||||
}
|
||||
else if (obj instanceof Object) {
|
||||
recipe.addJsonObject(Object.keys(obj).length);
|
||||
for (var key in obj)
|
||||
scanJson(recipe, obj[key]);
|
||||
}
|
||||
}
|
||||
|
||||
input.oninput = function(e) {
|
||||
results.style.display = 'none';
|
||||
error.style.visibility = 'hidden';
|
||||
|
||||
try {
|
||||
var recipe = new Recipe();
|
||||
scanJson(recipe, JSON.parse(input.value));
|
||||
var expression = recipe.getExpression();
|
||||
|
||||
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>
|
@ -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>
|
||||
|
||||
class JsonArray_PrettyPrintTo_Tests : public testing::Test {
|
||||
public:
|
||||
@ -19,7 +19,7 @@ class JsonArray_PrettyPrintTo_Tests : public testing::Test {
|
||||
void outputMustBe(const char* expected) {
|
||||
char actual[256];
|
||||
|
||||
size_t actualLen = array.prettyPrintTo(actual, sizeof(actual));
|
||||
size_t actualLen = array.prettyPrintTo(actual);
|
||||
size_t measuredLen = array.measurePrettyLength();
|
||||
|
||||
EXPECT_STREQ(expected, actual);
|
||||
@ -28,7 +28,9 @@ class JsonArray_PrettyPrintTo_Tests : public testing::Test {
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(JsonArray_PrettyPrintTo_Tests, Empty) { outputMustBe("[]"); }
|
||||
TEST_F(JsonArray_PrettyPrintTo_Tests, Empty) {
|
||||
outputMustBe("[]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_PrettyPrintTo_Tests, OneElement) {
|
||||
array.add(1);
|
||||
|
@ -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>
|
||||
|
||||
class JsonArray_PrintTo_Tests : public testing::Test {
|
||||
public:
|
||||
@ -17,7 +17,7 @@ class JsonArray_PrintTo_Tests : public testing::Test {
|
||||
JsonArray &array;
|
||||
|
||||
void outputMustBe(const char *expected) {
|
||||
size_t actualLen = array.printTo(buffer, sizeof(buffer));
|
||||
size_t actualLen = array.printTo(buffer);
|
||||
size_t measuredLen = array.measureLength();
|
||||
|
||||
EXPECT_STREQ(expected, buffer);
|
||||
@ -29,7 +29,9 @@ class JsonArray_PrintTo_Tests : public testing::Test {
|
||||
char buffer[256];
|
||||
};
|
||||
|
||||
TEST_F(JsonArray_PrintTo_Tests, Empty) { outputMustBe("[]"); }
|
||||
TEST_F(JsonArray_PrintTo_Tests, Empty) {
|
||||
outputMustBe("[]");
|
||||
}
|
||||
|
||||
TEST_F(JsonArray_PrintTo_Tests, Null) {
|
||||
array.add(static_cast<char *>(0));
|
||||
|
@ -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>
|
||||
|
||||
class JsonObject_PrettyPrintTo_Tests : public testing::Test {
|
||||
public:
|
||||
@ -19,7 +19,7 @@ class JsonObject_PrettyPrintTo_Tests : public testing::Test {
|
||||
void outputMustBe(const char *expected) {
|
||||
char buffer[256];
|
||||
|
||||
size_t actualLen = _object.prettyPrintTo(buffer, sizeof(buffer));
|
||||
size_t actualLen = _object.prettyPrintTo(buffer);
|
||||
size_t measuredLen = _object.measurePrettyLength();
|
||||
|
||||
EXPECT_STREQ(expected, buffer);
|
||||
@ -28,7 +28,9 @@ class JsonObject_PrettyPrintTo_Tests : public testing::Test {
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(JsonObject_PrettyPrintTo_Tests, EmptyObject) { outputMustBe("{}"); }
|
||||
TEST_F(JsonObject_PrettyPrintTo_Tests, EmptyObject) {
|
||||
outputMustBe("{}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_PrettyPrintTo_Tests, OneMember) {
|
||||
_object["key"] = "value";
|
||||
|
@ -15,7 +15,7 @@ class JsonObject_PrintTo_Tests : public testing::Test {
|
||||
protected:
|
||||
void outputMustBe(const char *expected) {
|
||||
char actual[256];
|
||||
size_t actualLen = _object.printTo(actual, sizeof(actual));
|
||||
size_t actualLen = _object.printTo(actual);
|
||||
size_t measuredLen = _object.measureLength();
|
||||
|
||||
EXPECT_STREQ(expected, actual);
|
||||
@ -27,7 +27,9 @@ class JsonObject_PrintTo_Tests : public testing::Test {
|
||||
JsonObject &_object;
|
||||
};
|
||||
|
||||
TEST_F(JsonObject_PrintTo_Tests, EmptyObject) { outputMustBe("{}"); }
|
||||
TEST_F(JsonObject_PrintTo_Tests, EmptyObject) {
|
||||
outputMustBe("{}");
|
||||
}
|
||||
|
||||
TEST_F(JsonObject_PrintTo_Tests, TwoStrings) {
|
||||
_object["key1"] = "value1";
|
||||
|
@ -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>
|
||||
|
||||
class JsonObject_Subscript_Tests : public ::testing::Test {
|
||||
public:
|
||||
|
@ -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>
|
||||
|
||||
class JsonVariant_Subscript_Tests : public ::testing::Test {
|
||||
protected:
|
||||
@ -24,6 +24,9 @@ TEST_F(JsonVariant_Subscript_Tests, Array) {
|
||||
EXPECT_EQ(2, _variant.size());
|
||||
EXPECT_STREQ("element at index 0", _variant[0].asString());
|
||||
EXPECT_STREQ("element at index 1", _variant[1].asString());
|
||||
EXPECT_STREQ(
|
||||
"element at index 0",
|
||||
_variant[static_cast<unsigned char>(0)].asString()); // issue #381
|
||||
EXPECT_FALSE(_variant[-1].success());
|
||||
EXPECT_FALSE(_variant[3].success());
|
||||
EXPECT_FALSE(_variant["0"].success());
|
||||
@ -56,3 +59,25 @@ TEST_F(JsonVariant_Subscript_Tests, String) {
|
||||
EXPECT_FALSE(_variant["0"].success());
|
||||
EXPECT_FALSE(_variant[0].success());
|
||||
}
|
||||
|
||||
TEST_F(JsonVariant_Subscript_Tests, ObjectSetValue) {
|
||||
_variant = _jsonBuffer.createObject();
|
||||
_variant["hello"] = "world";
|
||||
EXPECT_EQ(1, _variant.size());
|
||||
EXPECT_STREQ("world", _variant["hello"]);
|
||||
}
|
||||
|
||||
TEST_F(JsonVariant_Subscript_Tests, ArraySetValue) {
|
||||
_variant = _jsonBuffer.parseArray("[\"hello\"]");
|
||||
_variant[0] = "world";
|
||||
EXPECT_EQ(1, _variant.size());
|
||||
EXPECT_STREQ("world", _variant[0]);
|
||||
}
|
||||
|
||||
TEST_F(JsonVariant_Subscript_Tests, NestedObjectSetValue) {
|
||||
_variant = _jsonBuffer.parseArray("[{}]");
|
||||
_variant[0]["hello"] = "world";
|
||||
EXPECT_EQ(1, _variant.size());
|
||||
EXPECT_EQ(1, _variant[0].size());
|
||||
EXPECT_STREQ("world", _variant[0]["hello"]);
|
||||
}
|
||||
|
Reference in New Issue
Block a user