Compare commits

..

3 Commits

10 changed files with 111 additions and 13 deletions

View File

@ -1,6 +1,12 @@
ArduinoJson: change log
=======================
v5.7.1
------
* Added support for PROGMEM (issue #76)
* Fixed compilation error when index is not an `int` (issue #381)
v5.7.0
------

View File

@ -1,4 +1,4 @@
version: 5.7.0.{build}
version: 5.7.1.{build}
environment:
matrix:
- CMAKE_GENERATOR: Visual Studio 14 2015

View 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() {
#if ARDUINO_ARCH_AVR
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
}

View File

@ -27,6 +27,15 @@
#define ARDUINOJSON_ENABLE_ARDUINO_STRING 1
#endif
// On AVR archiecture, we can use PROGMEM
#ifndef ARDUINOJSON_ENABLE_PROGMEM
#ifdef ARDUINO_ARCH_AVR
#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

View File

@ -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((PGM_P)str, expected) == 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
}
}

View File

@ -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

View File

@ -77,8 +77,12 @@ class JsonVariantBase : public Internals::JsonPrintable<TImpl> {
// 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];
}
private:
const TImpl *impl() const {

View File

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

View File

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

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>
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());