Compare commits

..

8 Commits

154 changed files with 1509 additions and 447 deletions

3
.gitignore vendored
View File

@ -6,3 +6,6 @@
/sftp-config.json
.tags
.tags_sorted_by_file
/fuzzing/*_fuzzer
/fuzzing/*_fuzzer.options
/fuzzing/*_fuzzer_seed_corpus.zip

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library

View File

@ -1,6 +1,14 @@
ArduinoJson: change log
=======================
v5.8.1
------
* Fixed error when assigning a `volatile int` to a `JsonVariant` (issue #415)
* Fixed errors with Variable Length Arrays (issue #416)
* Fixed error when both `ARDUINOJSON_ENABLE_STD_STREAM` and `ARDUINOJSON_ENABLE_ARDUINO_STREAM` are set to `1`
* Fixed error "Stream does not name a type" (issue #412)
v5.8.0
------
@ -8,6 +16,24 @@ v5.8.0
* Added support for `Stream` (issue #300)
* Reduced memory consumption by not duplicating spaces and comments
**BREAKING CHANGES**:
`JsonBuffer::parseObject()` and `JsonBuffer::parseArray()` have been pulled down to the derived classes `DynamicJsonBuffer` and `StaticJsonBufferBase`.
This means that if you have code like:
```c++
void myFunction(JsonBuffer& jsonBuffer);
```
you need to replace it with one of the following:
```c++
void myFunction(DynamicJsonBuffer& jsonBuffer);
void myFunction(StaticJsonBufferBase& jsonBuffer);
template<typename TJsonBuffer> void myFunction(TJsonBuffer& jsonBuffer);
```
v5.7.3
------

View File

@ -1,4 +1,4 @@
# Copyright Benoit Blanchon 2014-2016
# Copyright Benoit Blanchon 2014-2017
# MIT License
#
# Arduino JSON library

View File

@ -1,7 +1,7 @@
The MIT License (MIT)
---------------------
Copyright © 2014-2016 Benoit BLANCHON
Copyright © 2014-2017 Benoit BLANCHON
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

View File

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

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library

View File

@ -1,7 +1,7 @@
// Sample Arduino Json Web Client
// Downloads and parse http://jsonplaceholder.typicode.com/users/1
//
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library

View File

@ -3,11 +3,11 @@
// You can easily test this program with netcat:
// $ nc -ulp 8888
//
// by Benoit Blanchon, MIT License 2015-2016
// by Benoit Blanchon, MIT License 2015-2017
#include <SPI.h>
#include <Ethernet.h>
#include <ArduinoJson.h>
#include <Ethernet.h>
#include <SPI.h>
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress localIp(192, 168, 0, 177);

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library

19
fuzzing/Makefile Normal file
View File

@ -0,0 +1,19 @@
# CAUTION: this file is invoked by https://github.com/google/oss-fuzz
CXXFLAGS += -I../include
all: \
$(OUT)/json_fuzzer \
$(OUT)/json_fuzzer_seed_corpus.zip \
$(OUT)/json_fuzzer.options
$(OUT)/json_fuzzer: fuzzer.cpp
$(CXX) $(CXXFLAGS) $< -o$@ $(LIB_FUZZING_ENGINE)
$(OUT)/json_fuzzer_seed_corpus.zip: seed_corpus/*
zip -j $@ $?
$(OUT)/json_fuzzer.options:
@echo "[libfuzzer]" > $@
@echo "max_len = 256" >> $@
@echo "timeout = 10" >> $@

8
fuzzing/fuzz.sh Executable file
View File

@ -0,0 +1,8 @@
#!/bin/bash
# This script mimics an invocation from https://github.com/google/oss-fuzz
export CXX='clang++'
export CXXFLAGS='-fsanitize-coverage=trace-pc-guard -fsanitize=address'
export LIB_FUZZING_ENGINE=-lFuzzer
make OUT=.
./json_fuzzer my_corpus seed_corpus

23
fuzzing/fuzzer.cpp Normal file
View File

@ -0,0 +1,23 @@
#include <ArduinoJson.h>
class memstream : public std::istream {
struct membuf : std::streambuf {
membuf(const uint8_t *p, size_t l) {
setg((char *)p, (char *)p, (char *)p + l);
}
};
membuf _buffer;
public:
memstream(const uint8_t *p, size_t l)
: std::istream(&_buffer), _buffer(p, l) {
rdbuf(&_buffer);
}
};
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
DynamicJsonBuffer jsonBuffer;
memstream json(data, size);
jsonBuffer.parse(json);
return 0;
}

2
fuzzing/my_corpus/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
*
!.gitignore

View File

@ -0,0 +1,8 @@
{
"sensor": "gps",
"time": 1351824120,
"data": [
48.75608,
2.302038
]
}

View File

@ -0,0 +1,53 @@
{
"coord": {
"lon": -0.13,
"lat": 51.51
},
"weather": [
{
"id": 301,
"main": "Drizzle",
"description": "drizzle",
"icon": "09n"
},
{
"id": 701,
"main": "Mist",
"description": "mist",
"icon": "50n"
},
{
"id": 741,
"main": "Fog",
"description": "fog",
"icon": "50n"
}
],
"base": "stations",
"main": {
"temp": 281.87,
"pressure": 1032,
"humidity": 100,
"temp_min": 281.15,
"temp_max": 283.15
},
"visibility": 2900,
"wind": {
"speed": 1.5
},
"clouds": {
"all": 90
},
"dt": 1483820400,
"sys": {
"type": 1,
"id": 5091,
"message": 0.0226,
"country": "GB",
"sunrise": 1483776245,
"sunset": 1483805443
},
"id": 2643743,
"name": "London",
"cod": 200
}

View File

@ -0,0 +1,90 @@
{
"response": {
"version": "0.1",
"termsofService": "http://www.wunderground.com/weather/api/d/terms.html",
"features": {
"conditions": 1
}
},
"current_observation": {
"image": {
"url": "http://icons-ak.wxug.com/graphics/wu2/logo_130x80.png",
"title": "Weather Underground",
"link": "http://www.wunderground.com"
},
"display_location": {
"full": "San Francisco, CA",
"city": "San Francisco",
"state": "CA",
"state_name": "California",
"country": "US",
"country_iso3166": "US",
"zip": "94101",
"latitude": "37.77500916",
"longitude": "-122.41825867",
"elevation": "47.00000000"
},
"observation_location": {
"full": "SOMA - Near Van Ness, San Francisco, California",
"city": "SOMA - Near Van Ness, San Francisco",
"state": "California",
"country": "US",
"country_iso3166": "US",
"latitude": "37.773285",
"longitude": "-122.417725",
"elevation": "49 ft"
},
"estimated": {},
"station_id": "KCASANFR58",
"observation_time": "Last Updated on June 27, 5:27 PM PDT",
"observation_time_rfc822": "Wed, 27 Jun 2012 17:27:13 -0700",
"observation_epoch": "1340843233",
"local_time_rfc822": "Wed, 27 Jun 2012 17:27:14 -0700",
"local_epoch": "1340843234",
"local_tz_short": "PDT",
"local_tz_long": "America/Los_Angeles",
"local_tz_offset": "-0700",
"weather": "Partly Cloudy",
"temperature_string": "66.3 F (19.1 C)",
"temp_f": 66.3,
"temp_c": 19.1,
"relative_humidity": "65%",
"wind_string": "From the NNW at 22.0 MPH Gusting to 28.0 MPH",
"wind_dir": "NNW",
"wind_degrees": 346,
"wind_mph": 22,
"wind_gust_mph": "28.0",
"wind_kph": 35.4,
"wind_gust_kph": "45.1",
"pressure_mb": "1013",
"pressure_in": "29.93",
"pressure_trend": "+",
"dewpoint_string": "54 F (12 C)",
"dewpoint_f": 54,
"dewpoint_c": 12,
"heat_index_string": "NA",
"heat_index_f": "NA",
"heat_index_c": "NA",
"windchill_string": "NA",
"windchill_f": "NA",
"windchill_c": "NA",
"feelslike_string": "66.3 F (19.1 C)",
"feelslike_f": "66.3",
"feelslike_c": "19.1",
"visibility_mi": "10.0",
"visibility_km": "16.1",
"solarradiation": "",
"UV": "5",
"precip_1hr_string": "0.00 in ( 0 mm)",
"precip_1hr_in": "0.00",
"precip_1hr_metric": " 0",
"precip_today_string": "0.00 in (0 mm)",
"precip_today_in": "0.00",
"precip_today_metric": "0",
"icon": "partlycloudy",
"icon_url": "http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
"forecast_url": "http://www.wunderground.com/US/CA/San_Francisco.html",
"history_url": "http://www.wunderground.com/history/airport/KCASANFR58/2012/6/27/DailyHistory.html",
"ob_url": "http://www.wunderground.com/cgi-bin/findweather/getForecast?query=37.773285,-122.417725"
}
}

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library
@ -19,7 +19,7 @@ class JsonBufferAllocated {
return jsonBuffer->alloc(n);
}
void operator delete(void *, JsonBuffer *) throw() {}
void operator delete(void *, JsonBuffer *)throw() {}
};
}
}

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library
@ -18,8 +18,12 @@ class ListConstIterator {
public:
explicit ListConstIterator(const ListNode<T> *node = NULL) : _node(node) {}
const T &operator*() const { return _node->content; }
const T *operator->() { return &_node->content; }
const T &operator*() const {
return _node->content;
}
const T *operator->() {
return &_node->content;
}
bool operator==(const ListConstIterator<T> &other) const {
return _node == other._node;

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library
@ -7,8 +7,8 @@
#pragma once
#include "ListNode.hpp"
#include "ListConstIterator.hpp"
#include "ListNode.hpp"
namespace ArduinoJson {
namespace Internals {
@ -19,8 +19,12 @@ class ListIterator {
public:
explicit ListIterator(ListNode<T> *node = NULL) : _node(node) {}
T &operator*() const { return _node->content; }
T *operator->() { return &_node->content; }
T &operator*() const {
return _node->content;
}
T *operator->() {
return &_node->content;
}
bool operator==(const ListIterator<T> &other) const {
return _node == other._node;
@ -35,7 +39,9 @@ class ListIterator {
return *this;
}
operator ListConstIterator<T>() const { return ListConstIterator<T>(_node); }
operator ListConstIterator<T>() const {
return ListConstIterator<T>(_node);
}
private:
ListNode<T> *_node;

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library
@ -19,7 +19,9 @@ class ReferenceType {
return this == &other;
}
bool operator!=(const ReferenceType& other) const { return this != &other; }
bool operator!=(const ReferenceType& other) const {
return this != &other;
}
protected:
ReferenceType() {}

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library
@ -26,8 +26,8 @@ struct ValueSetter {
};
template <typename TSource>
struct ValueSetter<TSource, typename TypeTraits::EnableIf<
StringFuncs<TSource>::should_duplicate>::type> {
struct ValueSetter<TSource, typename TypeTraits::EnableIf<StringTraits<
TSource>::should_duplicate>::type> {
template <typename TDestination>
static bool set(JsonBuffer* buffer, TDestination& destination,
const TSource& source) {

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library
@ -74,7 +74,7 @@ class JsonParser {
template <typename TJsonBuffer, typename TString>
struct JsonParserBuilder {
typedef typename Internals::StringFuncs<TString>::Iterator InputIterator;
typedef typename Internals::StringTraits<TString>::Iterator InputIterator;
typedef JsonParser<StringReader<InputIterator>, TJsonBuffer &> TParser;
static TParser makeParser(TJsonBuffer *buffer, TString &json,
@ -85,7 +85,7 @@ struct JsonParserBuilder {
template <typename TJsonBuffer>
struct JsonParserBuilder<TJsonBuffer, char *> {
typedef typename Internals::StringFuncs<char *>::Iterator InputIterator;
typedef typename Internals::StringTraits<char *>::Iterator InputIterator;
typedef JsonParser<StringReader<InputIterator>, StringWriter> TParser;
static TParser makeParser(TJsonBuffer *buffer, char *json,
@ -94,10 +94,6 @@ struct JsonParserBuilder<TJsonBuffer, char *> {
}
};
template <typename TJsonBuffer, size_t N>
struct JsonParserBuilder<TJsonBuffer, char[N]>
: JsonParserBuilder<TJsonBuffer, char *> {};
template <typename TJsonBuffer, typename TString>
inline typename JsonParserBuilder<TJsonBuffer, TString>::TParser makeParser(
TJsonBuffer *buffer, TString &json, uint8_t nestingLimit) {

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library
@ -14,8 +14,8 @@
#include "JsonVariant.hpp"
#include "Serialization/JsonPrintable.hpp"
#include "StringTraits/StringTraits.hpp"
#include "TypeTraits/ConstRefOrConstPtr.hpp"
#include "TypeTraits/EnableIf.hpp"
#include "TypeTraits/IsArray.hpp"
#include "TypeTraits/IsFloatingPoint.hpp"
#include "TypeTraits/IsSame.hpp"
@ -58,73 +58,70 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
// Adds the specified value at the end of the array.
//
// bool add(bool);
// bool add(char);
// bool add(long);
// bool add(int);
// bool add(short);
// bool add(float value);
// bool add(double value);
// bool add(const char*);
// bool add(const char[]);
// bool add(const char[N]);
// bool add(RawJson);
// bool add(const std::string&)
// bool add(const String&)
// bool add(const JsonVariant&);
// bool add(JsonArray&);
// bool add(JsonObject&);
// bool add(TValue);
// TValue = bool, long, int, short, float, double, RawJson, JsonVariant,
// const std::string&, const String&,
// const JsonArray&, const JsonObject&
template <typename T>
bool add(const T &value) {
// reduce the number of template function instanciation to reduce code size
return addNodeImpl<typename TypeTraits::ConstRefOrConstPtr<T>::type>(value);
typename TypeTraits::EnableIf<!TypeTraits::IsArray<T>::value, bool>::type add(
const T &value) {
return add_impl<const T &>(value);
}
// bool add(float value, uint8_t decimals);
// bool add(double value, uint8_t decimals);
//
// bool add(TValue);
// TValue = const char*, const char[N], const FlashStringHelper*
template <typename T>
bool add(const T *value) {
return add_impl<const T *>(value);
}
//
// bool add(TValue value, uint8_t decimals);
// TValue = float, double
template <typename T>
bool add(T value, uint8_t decimals) {
return add(JsonVariant(value, decimals));
return add_impl<const JsonVariant &>(JsonVariant(value, decimals));
}
// Sets the value at specified index.
//
// bool set(size_t index, bool value);
// bool set(size_t index, long value);
// bool set(size_t index, int value);
// bool set(size_t index, short value);
// bool set(size_t index, const std::string&)
// bool set(size_t index, const String&)
// bool set(size_t index, const JsonVariant&);
// bool set(size_t index, JsonArray&);
// bool set(size_t index, JsonObject&);
// bool add(size_t index, TValue);
// TValue = bool, long, int, short, float, double, RawJson, JsonVariant,
// const std::string&, const String&,
// const JsonArray&, const JsonObject&
template <typename T>
bool set(size_t index, const T &value) {
// reduce the number of template function instanciation to reduce code size
return setNodeAt<typename TypeTraits::ConstRefOrConstPtr<T>::type>(index,
value);
typename TypeTraits::EnableIf<!TypeTraits::IsArray<T>::value, bool>::type set(
size_t index, const T &value) {
return set_impl<const T &>(index, value);
}
// bool set(size_t index, float value, uint8_t decimals = 2);
// bool set(size_t index, double value, uint8_t decimals = 2);
//
// bool add(size_t index, TValue);
// TValue = const char*, const char[N], const FlashStringHelper*
template <typename T>
bool set(size_t index, const T *value) {
return set_impl<const T *>(index, value);
}
//
// bool set(size_t index, TValue value, uint8_t decimals);
// TValue = float, double
template <typename T>
typename TypeTraits::EnableIf<TypeTraits::IsFloatingPoint<T>::value,
bool>::type
set(size_t index, T value, uint8_t decimals) {
return set(index, JsonVariant(value, decimals));
return set_impl<const JsonVariant &>(index, JsonVariant(value, decimals));
}
// Gets the value at the specified index.
template <typename T>
typename Internals::JsonVariantAs<T>::type get(size_t index) const {
node_type *node = getNodeAt(index);
node_type *node = findNode(index);
return node ? node->content.as<T>()
: Internals::JsonVariantDefault<T>::get();
;
}
// Check the type of the value at specified index.
template <typename T>
bool is(size_t index) const {
node_type *node = getNodeAt(index);
node_type *node = findNode(index);
return node ? node->content.is<T>() : false;
}
@ -138,7 +135,7 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
// Removes element at specified index.
void removeAt(size_t index) {
removeNode(getNodeAt(index));
removeNode(findNode(index));
}
// Returns a reference an invalid JsonArray.
@ -203,15 +200,15 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
}
private:
node_type *getNodeAt(size_t index) const {
node_type *findNode(size_t index) const {
node_type *node = _firstNode;
while (node && index--) node = node->next;
return node;
}
template <typename TValueRef>
bool setNodeAt(size_t index, TValueRef value) {
node_type *node = getNodeAt(index);
bool set_impl(size_t index, TValueRef value) {
node_type *node = findNode(index);
if (!node) return false;
return Internals::ValueSetter<TValueRef>::set(_buffer, node->content,
@ -219,7 +216,7 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
}
template <typename TValueRef>
bool addNodeImpl(TValueRef value) {
bool add_impl(TValueRef value) {
node_type *node = addNewNode();
if (!node) return false;

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library
@ -26,11 +26,25 @@ class JsonArraySubscript : public JsonVariantBase<JsonArraySubscript> {
return *this;
}
// Replaces the value
//
// operator=(TValue)
// TValue = bool, long, int, short, float, double, RawJson, JsonVariant,
// const std::string&, const String&,
// const JsonArray&, const JsonObject&
template <typename T>
FORCE_INLINE JsonArraySubscript& operator=(const T& src) {
_array.set(_index, src);
return *this;
}
//
// operator=(TValue)
// TValue = const char*, const char[N], const FlashStringHelper*
template <typename T>
FORCE_INLINE JsonArraySubscript& operator=(const T* src) {
_array.set(_index, src);
return *this;
}
FORCE_INLINE bool success() const {
return _index < _array.size();
@ -46,9 +60,29 @@ class JsonArraySubscript : public JsonVariantBase<JsonArraySubscript> {
return _array.is<T>(_index);
}
// Replaces the value
//
// bool set(TValue)
// TValue = bool, long, int, short, float, double, RawJson, JsonVariant,
// const std::string&, const String&,
// const JsonArray&, const JsonObject&
template <typename TValue>
FORCE_INLINE void set(const TValue& value) {
_array.set(_index, value);
FORCE_INLINE bool set(const TValue& value) {
return _array.set(_index, value);
}
//
// bool set(TValue)
// TValue = const char*, const char[N], const FlashStringHelper*
template <typename TValue>
FORCE_INLINE bool set(const TValue* value) {
return _array.set(_index, value);
}
//
// bool set(TValue, uint8_t decimals);
// TValue = float, double
template <typename TValue>
FORCE_INLINE bool set(const TValue& value, uint8_t decimals) {
return _array.set(_index, value, decimals);
}
private:

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library
@ -12,6 +12,8 @@
#include <string.h>
#include "JsonVariant.hpp"
#include "TypeTraits/EnableIf.hpp"
#include "TypeTraits/IsArray.hpp"
#if defined(__clang__)
#pragma clang diagnostic push
@ -51,10 +53,22 @@ class JsonBuffer {
// allocation fails.
JsonObject &createObject();
// Duplicate a string
// Duplicates a string
//
// char* strdup(TValue);
// TValue = const std::string&, const String&,
template <typename TString>
char *strdup(const TString &src) {
return Internals::StringFuncs<TString>::duplicate(src, this);
typename TypeTraits::EnableIf<!TypeTraits::IsArray<TString>::value,
char *>::type
strdup(const TString &src) {
return Internals::StringTraits<TString>::duplicate(src, this);
}
//
// char* strdup(TValue);
// TValue = const char*, const char[N], const FlashStringHelper*
template <typename TString>
char *strdup(const TString *src) {
return Internals::StringTraits<const TString *>::duplicate(src, this);
}
// Allocates n bytes in the JsonBuffer.

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library
@ -34,12 +34,27 @@ class JsonBufferBase : public JsonBuffer {
// Returns a reference to the new JsonObject or JsonObject::invalid() if the
// allocation fails.
// With this overload, the JsonBuffer will make a copy of the string
//
// JsonArray& parseArray(TString);
// TString = const std::string&, const String&
template <typename TString>
JsonArray &parseArray(
const TString &json,
uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) {
typename TypeTraits::EnableIf<!TypeTraits::IsArray<TString>::value,
JsonArray &>::type
parseArray(const TString &json,
uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) {
return Internals::makeParser(that(), json, nestingLimit).parseArray();
}
//
// JsonArray& parseArray(TString);
// TString = const char*, const char[N], const FlashStringHelper*
template <typename TString>
JsonArray &parseArray(
TString *json, uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) {
return Internals::makeParser(that(), json, nestingLimit).parseArray();
}
//
// JsonArray& parseArray(TString);
// TString = std::istream&, Stream&
template <typename TString>
JsonArray &parseArray(
TString &json, uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) {
@ -56,12 +71,27 @@ class JsonBufferBase : public JsonBuffer {
//
// Returns a reference to the new JsonObject or JsonObject::invalid() if the
// allocation fails.
//
// JsonObject& parseObject(TString);
// TString = const std::string&, const String&
template <typename TString>
JsonObject &parseObject(
const TString &json,
uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) {
typename TypeTraits::EnableIf<!TypeTraits::IsArray<TString>::value,
JsonObject &>::type
parseObject(const TString &json,
uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) {
return Internals::makeParser(that(), json, nestingLimit).parseObject();
}
//
// JsonObject& parseObject(TString);
// TString = const char*, const char[N], const FlashStringHelper*
template <typename TString>
JsonObject &parseObject(
TString *json, uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) {
return Internals::makeParser(that(), json, nestingLimit).parseObject();
}
//
// JsonObject& parseObject(TString);
// TString = std::istream&, Stream&
template <typename TString>
JsonObject &parseObject(
TString &json, uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) {
@ -70,11 +100,27 @@ class JsonBufferBase : public JsonBuffer {
// Generalized version of parseArray() and parseObject(), also works for
// integral types.
//
// JsonVariant parse(TString);
// TString = const std::string&, const String&
template <typename TString>
JsonVariant parse(const TString &json,
typename TypeTraits::EnableIf<!TypeTraits::IsArray<TString>::value,
JsonVariant>::type
parse(const TString &json,
uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) {
return Internals::makeParser(that(), json, nestingLimit).parseVariant();
}
//
// JsonVariant parse(TString);
// TString = const char*, const char[N], const FlashStringHelper*
template <typename TString>
JsonVariant parse(TString *json,
uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) {
return Internals::makeParser(that(), json, nestingLimit).parseVariant();
}
//
// JsonVariant parse(TString);
// TString = std::istream&, Stream&
template <typename TString>
JsonVariant parse(TString &json,
uint8_t nestingLimit = ARDUINOJSON_DEFAULT_NESTING_LIMIT) {

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library
@ -14,8 +14,8 @@
#include "JsonPair.hpp"
#include "Serialization/JsonPrintable.hpp"
#include "StringTraits/StringTraits.hpp"
#include "TypeTraits/ConstRefOrConstPtr.hpp"
#include "TypeTraits/EnableIf.hpp"
#include "TypeTraits/IsArray.hpp"
#include "TypeTraits/IsFloatingPoint.hpp"
#include "TypeTraits/IsSame.hpp"
@ -47,81 +47,226 @@ class JsonObject : public Internals::JsonPrintable<JsonObject>,
explicit JsonObject(JsonBuffer* buffer) : Internals::List<JsonPair>(buffer) {}
// Gets or sets the value associated with the specified key.
//
// JsonObjectSubscript operator[](TKey)
// TKey = const std::string&, const String&
template <typename TString>
JsonObjectSubscript<TString> operator[](const TString& key);
typename TypeTraits::EnableIf<!TypeTraits::IsArray<TString>::value,
JsonObjectSubscript<const TString&> >::type
operator[](const TString& key) {
return JsonObjectSubscript<const TString&>(*this, key);
}
//
// JsonObjectSubscript operator[](TKey)
// TKey = const char*, const char[N], const FlashStringHelper*
template <typename TString>
JsonObjectSubscript<const TString*> operator[](const TString* key) {
return JsonObjectSubscript<const TString*>(*this, key);
}
// Gets the value associated with the specified key.
//
// JsonVariant operator[](TKey) const;
// TKey = const std::string&, const String&
template <typename TString>
JsonVariant operator[](const TString& key) const {
return get<JsonVariant>(key);
const JsonVariant operator[](const TString& key) const {
return get_impl<const TString&, JsonVariant>(key);
}
//
// JsonVariant operator[](TKey) const;
// TKey = const char*, const char[N], const FlashStringHelper*
template <typename TString>
const JsonVariant operator[](const TString* key) const {
return get_impl<const TString*, JsonVariant>(key);
}
// Sets the specified key with the specified value.
// bool set(TKey key, bool value);
// bool set(TKey key, char value);
// bool set(TKey key, long value);
// bool set(TKey key, int value);
// bool set(TKey key, short value);
// bool set(TKey key, float value);
// bool set(TKey key, double value);
// bool set(TKey key, const char* value);
// bool set(TKey key, RawJson value);
// bool set(Key, String&);
// bool set(Key, JsonArray&);
// bool set(Key, JsonObject&);
// bool set(Key, JsonVariant&);
//
// bool set(TKey, TValue);
// TKey = const std::string&, const String&
// TValue = bool, long, int, short, float, double, RawJson, JsonVariant,
// const std::string&, const String&,
// const JsonArray&, const JsonObject&
template <typename TValue, typename TString>
bool set(const TString& key, const TValue& value) {
// reduce the number of template function instanciation to reduce code size
return setNodeAt<typename TypeTraits::ConstRefOrConstPtr<TString>::type,
typename TypeTraits::ConstRefOrConstPtr<TValue>::type>(
key, value);
typename TypeTraits::EnableIf<!TypeTraits::IsArray<TString>::value &&
!TypeTraits::IsArray<TValue>::value,
bool>::type
set(const TString& key, const TValue& value) {
return set_impl<const TString&, const TValue&>(key, value);
}
// bool set(Key, float value, uint8_t decimals);
// bool set(Key, double value, uint8_t decimals);
//
// bool set(TKey, TValue);
// TKey = const std::string&, const String&
// TValue = const char*, const char[N], const FlashStringHelper*
template <typename TValue, typename TString>
typename TypeTraits::EnableIf<!TypeTraits::IsArray<TString>::value,
bool>::type
set(const TString& key, const TValue* value) {
return set_impl<const TString&, const TValue*>(key, value);
}
//
// bool set(TKey, TValue);
// TKey = const char*, const char[N], const FlashStringHelper*
// TValue = bool, long, int, short, float, double, RawJson, JsonVariant,
// const std::string&, const String&,
// const JsonArray&, const JsonObject&
template <typename TValue, typename TString>
typename TypeTraits::EnableIf<!TypeTraits::IsArray<TValue>::value, bool>::type
set(const TString* key, const TValue& value) {
return set_impl<const TString*, const TValue&>(key, value);
}
//
// bool set(TKey, TValue);
// TKey = const char*, const char[N], const FlashStringHelper*
// TValue = const char*, const char[N], const FlashStringHelper*
template <typename TValue, typename TString>
bool set(const TString* key, const TValue* value) {
return set_impl<const TString*, const TValue*>(key, value);
}
//
// bool set(TKey, TValue, uint8_t decimals);
// TKey = const std::string&, const String&
// TValue = float, double
template <typename TValue, typename TString>
typename TypeTraits::EnableIf<TypeTraits::IsFloatingPoint<TValue>::value &&
!TypeTraits::IsArray<TString>::value,
bool>::type
set(const TString& key, TValue value, uint8_t decimals) {
return set_impl<const TString&, const JsonVariant&>(
key, JsonVariant(value, decimals));
}
//
// bool set(TKey, TValue, uint8_t decimals);
// TKey = const char*, const char[N], const FlashStringHelper*
// TValue = float, double
template <typename TValue, typename TString>
typename TypeTraits::EnableIf<TypeTraits::IsFloatingPoint<TValue>::value,
bool>::type
set(const TString& key, TValue value, uint8_t decimals) {
return set(key, JsonVariant(value, decimals));
set(const TString* key, TValue value, uint8_t decimals) {
return set_impl<const TString*, const JsonVariant&>(
key, JsonVariant(value, decimals));
}
// Gets the value associated with the specified key.
//
// TValue get<TValue>(TKey);
// TKey = const std::string&, const String&
// TValue = bool, char, long, int, short, float, double,
// const std::string&, const String&,
// const JsonArray&, const JsonObject&
template <typename TValue, typename TString>
typename TypeTraits::EnableIf<
!TypeTraits::IsArray<TString>::value,
typename Internals::JsonVariantAs<TValue>::type>::type
get(const TString& key) const {
return get_impl<const TString&, TValue>(key);
}
//
// TValue get<TValue>(TKey);
// TKey = const char*, const char[N], const FlashStringHelper*
// TValue = bool, char, long, int, short, float, double,
// const std::string&, const String&,
// const JsonArray&, const JsonObject&
template <typename TValue, typename TString>
typename Internals::JsonVariantAs<TValue>::type get(
const TString& key) const {
node_type* node = getNodeAt(key);
return node ? node->content.value.as<TValue>()
: Internals::JsonVariantDefault<TValue>::get();
const TString* key) const {
return get_impl<const TString*, TValue>(key);
}
// Checks the type of the value associated with the specified key.
//
//
// bool is<TValue>(TKey) const;
// TKey = const std::string&, const String&
// TValue = bool, char, long, int, short, float, double,
// const std::string&, const String&,
// const JsonArray&, const JsonObject&
template <typename TValue, typename TString>
bool is(const TString& key) const {
node_type* node = getNodeAt(key);
return node ? node->content.value.is<TValue>() : false;
typename TypeTraits::EnableIf<!TypeTraits::IsArray<TString>::value,
bool>::type
is(const TString& key) const {
return is_impl<const TString&, TValue>(key);
}
//
// bool is<TValue>(TKey) const;
// TKey = const char*, const char[N], const FlashStringHelper*
// TValue = bool, char, long, int, short, float, double,
// const std::string&, const String&,
// const JsonArray&, const JsonObject&
template <typename TValue, typename TString>
bool is(const TString* key) const {
return is_impl<const TString*, TValue>(key);
}
// Creates and adds a JsonArray.
// This is a shortcut for JsonBuffer::createArray() and JsonObject::add().
//
// JsonArray& createNestedArray(TKey);
// TKey = const std::string&, const String&
template <typename TString>
JsonArray& createNestedArray(const TString& key);
typename TypeTraits::EnableIf<!TypeTraits::IsArray<TString>::value,
JsonArray&>::type
createNestedArray(const TString& key) {
return createNestedArray_impl<const TString&>(key);
}
// JsonArray& createNestedArray(TKey);
// TKey = const char*, const char[N], const FlashStringHelper*
template <typename TString>
JsonArray& createNestedArray(const TString* key) {
return createNestedArray_impl<const TString*>(key);
}
// Creates and adds a JsonObject.
// This is a shortcut for JsonBuffer::createObject() and JsonObject::add().
//
// JsonObject& createNestedObject(TKey);
// TKey = const std::string&, const String&
template <typename TString>
JsonObject& createNestedObject(const TString& key);
typename TypeTraits::EnableIf<!TypeTraits::IsArray<TString>::value,
JsonObject&>::type
createNestedObject(const TString& key) {
return createNestedObject_impl<const TString&>(key);
}
//
// JsonObject& createNestedObject(TKey);
// TKey = const char*, const char[N], const FlashStringHelper*
template <typename TString>
JsonObject& createNestedObject(const TString* key) {
return createNestedObject_impl<const TString*>(key);
}
// Tells weither the specified key is present and associated with a value.
//
// bool containsKey(TKey);
// TKey = const std::string&, const String&
template <typename TString>
bool containsKey(const TString& key) const {
return getNodeAt(key) != NULL;
typename TypeTraits::EnableIf<!TypeTraits::IsArray<TString>::value,
bool>::type
containsKey(const TString& key) const {
return findNode<const TString&>(key) != NULL;
}
//
// bool containsKey(TKey);
// TKey = const char*, const char[N], const FlashStringHelper*
template <typename TString>
bool containsKey(const TString* key) const {
return findNode<const TString*>(key) != NULL;
}
// Removes the specified key and the associated value.
//
// void remove(TKey);
// TKey = const std::string&, const String&
template <typename TString>
void remove(const TString& key) {
removeNode(getNodeAt(key));
typename TypeTraits::EnableIf<!TypeTraits::IsArray<TString>::value,
void>::type
remove(const TString& key) {
removeNode(findNode<const TString&>(key));
}
//
// void remove(TKey);
// TKey = const char*, const char[N], const FlashStringHelper*
template <typename TString>
void remove(const TString* key) {
removeNode(findNode<const TString*>(key));
}
// Returns a reference an invalid JsonObject.
@ -134,25 +279,26 @@ class JsonObject : public Internals::JsonPrintable<JsonObject>,
private:
// Returns the list node that matches the specified key.
template <typename TString>
node_type* getNodeAt(const TString& key) const {
// reduce the number of template function instanciation to reduce code size
return getNodeAtImpl<
typename TypeTraits::ConstRefOrConstPtr<TString>::type>(key);
}
template <typename TStringRef>
node_type* getNodeAtImpl(TStringRef key) const {
node_type* findNode(TStringRef key) const {
for (node_type* node = _firstNode; node; node = node->next) {
if (Internals::StringFuncs<TStringRef>::equals(key, node->content.key))
if (Internals::StringTraits<TStringRef>::equals(key, node->content.key))
return node;
}
return NULL;
}
template <typename TStringRef, typename TValue>
typename Internals::JsonVariantAs<TValue>::type get_impl(
TStringRef key) const {
node_type* node = findNode<TStringRef>(key);
return node ? node->content.value.as<TValue>()
: Internals::JsonVariantDefault<TValue>::get();
}
template <typename TStringRef, typename TValueRef>
bool setNodeAt(TStringRef key, TValueRef value) {
node_type* node = getNodeAtImpl<TStringRef>(key);
bool set_impl(TStringRef key, TValueRef value) {
node_type* node = findNode<TStringRef>(key);
if (!node) {
node = addNewNode();
if (!node) return false;
@ -164,6 +310,18 @@ class JsonObject : public Internals::JsonPrintable<JsonObject>,
return Internals::ValueSetter<TValueRef>::set(_buffer, node->content.value,
value);
}
template <typename TStringRef, typename TValue>
bool is_impl(TStringRef key) const {
node_type* node = findNode<TStringRef>(key);
return node ? node->content.value.is<TValue>() : false;
}
template <typename TStringRef>
JsonArray& createNestedArray_impl(TStringRef key);
template <typename TStringRef>
JsonObject& createNestedObject_impl(TStringRef key);
};
namespace Internals {

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library
@ -13,19 +13,19 @@
namespace ArduinoJson {
template <typename TString>
inline JsonObject &JsonObject::createNestedObject(const TString &key) {
if (!_buffer) return JsonObject::invalid();
JsonObject &object = _buffer->createObject();
set(key, object);
return object;
}
template <typename TString>
inline JsonArray &JsonObject::createNestedArray(const TString &key) {
template <typename TStringRef>
inline JsonArray &JsonObject::createNestedArray_impl(TStringRef key) {
if (!_buffer) return JsonArray::invalid();
JsonArray &array = _buffer->createArray();
set(key, array);
return array;
}
template <typename TStringRef>
inline JsonObject &JsonObject::createNestedObject_impl(TStringRef key) {
if (!_buffer) return JsonObject::invalid();
JsonObject &object = _buffer->createObject();
set(key, object);
return object;
}
}

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library
@ -9,7 +9,6 @@
#include "Configuration.hpp"
#include "JsonVariantBase.hpp"
#include "TypeTraits/ConstRefOrConstPtr.hpp"
#include "TypeTraits/EnableIf.hpp"
#ifdef _MSC_VER
@ -19,26 +18,39 @@
namespace ArduinoJson {
template <typename TString>
template <typename TStringRef>
class JsonObjectSubscript
: public JsonVariantBase<JsonObjectSubscript<TString> > {
// const String&
// const std::string&
// const char*
typedef typename TypeTraits::ConstRefOrConstPtr<TString>::type TStringRef;
: public JsonVariantBase<JsonObjectSubscript<TStringRef> > {
typedef JsonObjectSubscript<TStringRef> this_type;
public:
FORCE_INLINE JsonObjectSubscript(JsonObject& object, TStringRef key)
: _object(object), _key(key) {}
FORCE_INLINE JsonObjectSubscript<TString>& operator=(
const JsonObjectSubscript<TString>& src) {
FORCE_INLINE this_type& operator=(const this_type& src) {
_object.set(_key, src);
return *this;
}
template <typename T>
FORCE_INLINE JsonObjectSubscript<TString>& operator=(const T& src) {
// Set the specified value
//
// operator=(TValue);
// TValue = bool, char, long, int, short, float, double,
// const std::string&, const String&,
// const JsonArray&, const JsonObject&
template <typename TValue>
FORCE_INLINE
typename TypeTraits::EnableIf<!TypeTraits::IsArray<TValue>::value,
this_type&>::type
operator=(const TValue& src) {
_object.set(_key, src);
return *this;
}
//
// operator=(TValue);
// TValue = const char*, const char[N], const FlashStringHelper*
template <typename TValue>
FORCE_INLINE this_type& operator=(const TValue* src) {
_object.set(_key, src);
return *this;
}
@ -49,7 +61,7 @@ class JsonObjectSubscript
template <typename TValue>
FORCE_INLINE typename Internals::JsonVariantAs<TValue>::type as() const {
return _object.get<TValue, TStringRef>(_key);
return _object.get<TValue>(_key);
}
template <typename TValue>
@ -57,11 +69,29 @@ class JsonObjectSubscript
return _object.is<TValue>(_key);
}
// Sets the specified value.
//
// bool set(TValue);
// TValue = bool, char, long, int, short, float, double, RawJson, JsonVariant,
// const std::string&, const String&,
// const JsonArray&, const JsonObject&
template <typename TValue>
FORCE_INLINE bool set(const TValue& value) {
FORCE_INLINE
typename TypeTraits::EnableIf<!TypeTraits::IsArray<TValue>::value,
bool>::type
set(const TValue& value) {
return _object.set(_key, value);
}
//
// bool set(TValue);
// TValue = const char*, const char[N], const FlashStringHelper*
template <typename TValue>
FORCE_INLINE bool set(const TValue* value) {
return _object.set(_key, value);
}
//
// bool set(TValue, uint8_t decimals);
// TValue = float, double
template <typename TValue>
FORCE_INLINE bool set(const TValue& value, uint8_t decimals) {
return _object.set(_key, value, decimals);
@ -73,17 +103,12 @@ class JsonObjectSubscript
};
#if ARDUINOJSON_ENABLE_STD_STREAM
template <typename TString>
template <typename TStringRef>
inline std::ostream& operator<<(std::ostream& os,
const JsonObjectSubscript<TString>& source) {
const JsonObjectSubscript<TStringRef>& source) {
return source.printTo(os);
}
#endif
template <typename TString>
inline JsonObjectSubscript<TString> JsonObject::operator[](const TString& key) {
return JsonObjectSubscript<TString>(*this, key);
}
} // namespace ArduinoJson
#ifdef _MSC_VER

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library
@ -163,7 +163,7 @@ class JsonVariant : public JsonVariantBase<JsonVariant> {
// std::string as<std::string>() const;
// String as<String>() const;
template <typename T>
typename TypeTraits::EnableIf<Internals::StringFuncs<T>::has_append, T>::type
typename TypeTraits::EnableIf<Internals::StringTraits<T>::has_append, T>::type
as() const {
const char *cstr = asString();
if (cstr) return T(cstr);

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library
@ -78,18 +78,44 @@ class JsonVariantBase : public Internals::JsonPrintable<TImpl> {
// Returns the value associated with the specified key if the variant is
// an object.
// Return JsonVariant::invalid() if the variant is not an object.
//
// const JsonObjectSubscript operator[](TKey) const;
// TKey = const std::string&, const String&
template <typename TString>
FORCE_INLINE
typename TypeTraits::EnableIf<Internals::StringFuncs<TString>::has_equals,
const JsonObjectSubscript<TString> >::type
operator[](const TString &key) const {
FORCE_INLINE typename TypeTraits::EnableIf<
Internals::StringTraits<TString>::has_equals,
const JsonObjectSubscript<const TString &> >::type
operator[](const TString &key) const {
return asObject()[key];
}
//
// const JsonObjectSubscript operator[](TKey) const;
// TKey = const char*, const char[N], const FlashStringHelper*
template <typename TString>
FORCE_INLINE
typename TypeTraits::EnableIf<Internals::StringFuncs<TString>::has_equals,
JsonObjectSubscript<TString> >::type
operator[](const TString &key) {
FORCE_INLINE typename TypeTraits::EnableIf<
Internals::StringTraits<TString>::has_equals,
JsonObjectSubscript<const TString &> >::type
operator[](const TString &key) {
return asObject()[key];
}
//
// JsonObjectSubscript operator[](TKey);
// TKey = const std::string&, const String&
template <typename TString>
FORCE_INLINE typename TypeTraits::EnableIf<
Internals::StringTraits<TString *>::has_equals,
JsonObjectSubscript<const TString *> >::type
operator[](const TString *key) {
return asObject()[key];
}
//
// JsonObjectSubscript operator[](TKey);
// TKey = const char*, const char[N], const FlashStringHelper*
template <typename TString>
FORCE_INLINE typename TypeTraits::EnableIf<
Internals::StringTraits<TString *>::has_equals,
const JsonObjectSubscript<const TString *> >::type
operator[](const TString *key) const {
return asObject()[key];
}

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library
@ -21,11 +21,11 @@ struct JsonVariantComparer {
template <typename TVariant, typename TString>
struct JsonVariantComparer<
TVariant, TString, typename TypeTraits::EnableIf<
Internals::StringFuncs<TString>::has_equals>::type> {
TVariant, TString, typename TypeTraits::EnableIf<Internals::StringTraits<
TString>::has_equals>::type> {
static bool equals(const TVariant &variant, const TString &comparand) {
const char *value = variant.template as<const char *>();
return Internals::StringFuncs<TString>::equals(comparand, value);
return Internals::StringTraits<TString>::equals(comparand, value);
}
};

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library
@ -15,7 +15,9 @@ namespace Internals {
// A dummy Print implementation used in JsonPrintable::measureLength()
class DummyPrint : public Print {
public:
virtual size_t write(uint8_t) { return 1; }
virtual size_t write(uint8_t) {
return 1;
}
};
}
}

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library
@ -20,7 +20,7 @@ class DynamicStringBuilder : public Print {
DynamicStringBuilder(TString &str) : _str(str) {}
virtual size_t write(uint8_t c) {
StringFuncs<TString>::append(_str, static_cast<char>(c));
StringTraits<TString>::append(_str, static_cast<char>(c));
return 1;
}

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library
@ -56,7 +56,7 @@ class JsonPrintable {
}
template <typename TString>
typename TypeTraits::EnableIf<StringFuncs<TString>::has_append, size_t>::type
typename TypeTraits::EnableIf<StringTraits<TString>::has_append, size_t>::type
printTo(TString &str) const {
DynamicStringBuilder<TString> sb(str);
return printTo(sb);
@ -83,7 +83,7 @@ class JsonPrintable {
}
template <typename TString>
typename TypeTraits::EnableIf<StringFuncs<TString>::has_append, size_t>::type
typename TypeTraits::EnableIf<StringTraits<TString>::has_append, size_t>::type
prettyPrintTo(TString &str) const {
DynamicStringBuilder<TString> sb(str);
return prettyPrintTo(sb);

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library
@ -7,6 +7,8 @@
#pragma once
#include <Stream.h>
#include "../TypeTraits/EnableIf.hpp"
#include "../TypeTraits/IsBaseOf.hpp"
#include "../TypeTraits/RemoveReference.hpp"
@ -14,7 +16,7 @@
namespace ArduinoJson {
namespace Internals {
struct StdStreamFuncs {
struct ArduinoStreamTraits {
class Iterator {
Stream& _stream;
@ -29,11 +31,11 @@ struct StdStreamFuncs {
};
template <typename TStream>
struct StringFuncs<TStream,
// match any type that is derived from std::istream:
typename TypeTraits::EnableIf<TypeTraits::IsBaseOf<
Stream, typename TypeTraits::RemoveReference<
TStream>::type>::value>::type>
: StdStreamFuncs {};
struct StringTraits<TStream,
// match any type that is derived from Stream:
typename TypeTraits::EnableIf<TypeTraits::IsBaseOf<
Stream, typename TypeTraits::RemoveReference<
TStream>::type>::value>::type>
: ArduinoStreamTraits {};
}
}

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library
@ -10,7 +10,7 @@
namespace ArduinoJson {
namespace Internals {
struct CharPtrFuncs {
struct CharPointerTraits {
class Iterator {
const char* _ptr;
@ -43,15 +43,9 @@ struct CharPtrFuncs {
};
template <>
struct StringFuncs<const char*, void> : CharPtrFuncs {};
struct StringTraits<const char*, void> : CharPointerTraits {};
template <>
struct StringFuncs<char*, void> : CharPtrFuncs {};
template <size_t N>
struct StringFuncs<char[N], void> : CharPtrFuncs {};
template <size_t N>
struct StringFuncs<const char[N], void> : CharPtrFuncs {};
struct StringTraits<char*, void> : CharPointerTraits {};
}
}

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library
@ -10,7 +10,7 @@
namespace ArduinoJson {
namespace Internals {
template <>
struct StringFuncs<const __FlashStringHelper*, void> {
struct StringTraits<const __FlashStringHelper*, void> {
class Iterator {
const char* _ptr;

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library
@ -15,7 +15,7 @@
namespace ArduinoJson {
namespace Internals {
struct StdStreamFuncs {
struct StdStreamTraits {
class Iterator {
std::istream& _stream;
@ -32,11 +32,11 @@ struct StdStreamFuncs {
};
template <typename TStream>
struct StringFuncs<TStream,
// match any type that is derived from std::istream:
typename TypeTraits::EnableIf<TypeTraits::IsBaseOf<
std::istream, typename TypeTraits::RemoveReference<
TStream>::type>::value>::type>
: StdStreamFuncs {};
struct StringTraits<TStream,
// match any type that is derived from std::istream:
typename TypeTraits::EnableIf<TypeTraits::IsBaseOf<
std::istream, typename TypeTraits::RemoveReference<
TStream>::type>::value>::type>
: StdStreamTraits {};
}
}

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library
@ -19,7 +19,7 @@ namespace ArduinoJson {
namespace Internals {
template <typename TString>
struct StdStringFuncs {
struct StdStringTraits {
template <typename Buffer>
static char* duplicate(const TString& str, Buffer* buffer) {
if (!str.c_str()) return NULL; // <- Arduino string can return NULL
@ -29,8 +29,8 @@ struct StdStringFuncs {
return static_cast<char*>(dup);
}
struct Iterator : CharPtrFuncs::Iterator {
Iterator(const TString& str) : CharPtrFuncs::Iterator(str.c_str()) {}
struct Iterator : CharPointerTraits::Iterator {
Iterator(const TString& str) : CharPointerTraits::Iterator(str.c_str()) {}
};
static bool equals(const TString& str, const char* expected) {
@ -48,14 +48,15 @@ struct StdStringFuncs {
#if ARDUINOJSON_ENABLE_ARDUINO_STRING
template <>
struct StringFuncs<String, void> : StdStringFuncs<String> {};
struct StringTraits<String, void> : StdStringTraits<String> {};
template <>
struct StringFuncs<StringSumHelper, void> : StdStringFuncs<StringSumHelper> {};
struct StringTraits<StringSumHelper, void> : StdStringTraits<StringSumHelper> {
};
#endif
#if ARDUINOJSON_ENABLE_STD_STRING
template <>
struct StringFuncs<std::string, void> : StdStringFuncs<std::string> {};
struct StringTraits<std::string, void> : StdStringTraits<std::string> {};
#endif
}
}

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library
@ -13,13 +13,13 @@ namespace ArduinoJson {
namespace Internals {
template <typename TString, typename Enable = void>
struct StringFuncs {};
struct StringTraits {};
template <typename TString>
struct StringFuncs<const TString, void> : StringFuncs<TString> {};
struct StringTraits<const TString, void> : StringTraits<TString> {};
template <typename TString>
struct StringFuncs<TString&, void> : StringFuncs<TString> {};
struct StringTraits<TString&, void> : StringTraits<TString> {};
}
}

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library
@ -12,20 +12,16 @@ namespace TypeTraits {
// A meta-function that return the type T without the const modifier
template <typename T>
struct ConstRefOrConstPtr {
typedef const T& type;
struct IsArray {
static const bool value = false;
};
template <typename T>
struct ConstRefOrConstPtr<T*> {
typedef const T* type;
};
template <typename T>
struct ConstRefOrConstPtr<T[]> {
typedef const T* type;
struct IsArray<T[]> {
static const bool value = true;
};
template <typename T, size_t N>
struct ConstRefOrConstPtr<T[N]> {
typedef const T* type;
struct IsArray<T[N]> {
static const bool value = true;
};
}
}

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library
@ -19,7 +19,7 @@ class IsBaseOf {
typedef char No[2];
static Yes &probe(const TBase *);
static No &probe(const void *);
static No &probe(...);
public:
enum {

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library

View File

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

View File

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

2
scripts/oss-fuzz/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/.vagrant/
*.log

30
scripts/oss-fuzz/Vagrantfile vendored Normal file
View File

@ -0,0 +1,30 @@
# A virtual machine to run https://github.com/google/oss-fuzz
Vagrant.configure(2) do |config|
config.vm.box = "ubuntu/xenial64"
config.vm.synced_folder "E:\\Git\\Arduino\\libraries\\ArduinoJson", "/host/ArduinoJson"
config.vm.synced_folder "E:\\Git\\oss-fuzz", "/host/oss-fuzz"
config.vm.network "forwarded_port", guest: 8001, host: 8001
config.vm.provision "shell", privileged: false, inline: <<-SHELL
set -x
sudo apt-get update
sudo apt-get install -y make git docker.io zip
sudo groupadd docker
sudo usermod -aG docker $USER
git clone https://github.com/google/fuzzer-test-suite.git FTS
./FTS/tutorial/install-deps.sh # Get deps
./FTS/tutorial/install-clang.sh # Get fresh clang binaries
# Get libFuzzer sources and build it
svn co http://llvm.org/svn/llvm-project/llvm/trunk/lib/Fuzzer
Fuzzer/build.sh
sudo mv libFuzzer.a /usr/local/lib/
echo "export PROJECT_NAME='arduinojson'" >> $HOME/.profile
echo "export CC='clang'" >> $HOME/.profile
echo "export CXX='clang++'" >> $HOME/.profile
SHELL
end

View File

@ -1,4 +1,4 @@
# Copyright Benoit Blanchon 2014-2016
# Copyright Benoit Blanchon 2014-2017
# MIT License
#
# Arduino JSON library

View File

@ -1,12 +1,12 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library
// 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>
TEST(DynamicJsonBuffer_Array_Tests, GrowsWithArray) {
DynamicJsonBuffer jsonBuffer;

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library

View File

@ -1,12 +1,12 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library
// 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>
TEST(DynamicJsonBuffer_Object_Tests, GrowsWithObject) {
DynamicJsonBuffer json;

View File

@ -1,4 +1,4 @@
// Copyright Benoit Blanchon 2014-2016
// Copyright Benoit Blanchon 2014-2017
// MIT License
//
// Arduino JSON library

Some files were not shown because too many files have changed in this diff Show More