8000 Changed the rules of string duplication (fixes #658) · janelia-arduino/ArduinoJson@e92612b · GitHub
[go: up one dir, main page]

Skip to content

Commit e92612b

Browse files
committed
Changed the rules of string duplication (fixes bblanchon#658)
1 parent 5c33fd4 commit e92612b

22 files changed

+416
-224
lines changed

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
ArduinoJson: change log
22
=======================
33

4+
HEAD
5+
----
6+
7+
* Changed the rules of string duplication (issue #658)
8+
9+
> ### New rules for string duplication
10+
>
11+
> | type | duplication |
12+
> |:-------------|:------------|
13+
> | const char* | no |
14+
> | char* | ~~no~~ yes |
15+
> | String | yes |
16+
> | std::string | yes |
17+
>
18+
> These new rules make `JsonBuffer::strdup()` useless.
19+
420
v5.12.0
521
-------
622

src/ArduinoJson/Data/ValueSaver.hpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// ArduinoJson - arduinojson.org
2+
// Copyright Benoit Blanchon 2014-2018
3+
// MIT License
4+
5+
#pragma once
6+
7+
#include "../JsonBuffer.hpp"
8+
#include "../JsonVariant.hpp"
9+
#include "../StringTraits/StringTraits.hpp"
10+
#include "../TypeTraits/EnableIf.hpp"
11+
12+
namespace ArduinoJson {
13+
namespace Internals {
14+
15+
template <typename Source, typename Enable = void>
16+
struct ValueSaver {
17+
template <typename Destination>
18+
static bool save(JsonBuffer*, Destination& destination, Source source) {
19+
destination = source;
20+
return true;
21+
}
22+
};
23+
24+
template <typename Source>
25+
struct ValueSaver<Source, typename TypeTraits::EnableIf<
26+
TypeTraits::IsString<Source>::value>::type> {
27+
template <typename Destination>
28+
static bool save(JsonBuffer* buffer, Destination& destination,
29+
Source source) {
30+
return StringTraits<Source>::save(source, destination, buffer);
31+
}
32+
};
33+
}
34+
}

src/ArduinoJson/Data/ValueSetter.hpp

Lines changed: 0 additions & 48 deletions
This file was deleted.

src/ArduinoJson/JsonArray.hpp

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include "Data/JsonBufferAllocated.hpp"
88
#include "Data/List.hpp"
99
#include "Data/ReferenceType.hpp"
10-
#include "Data/ValueSetter.hpp"
10+
#include "Data/ValueSaver.hpp"
1111
#include "JsonVariant.hpp"
1212
#include "Serialization/JsonPrintable.hpp"
1313
#include "StringTraits/StringTraits.hpp"
@@ -56,19 +56,17 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
5656
//
5757
// bool add(TValue);
5858
// TValue = bool, long, int, short, float, double, RawJson, JsonVariant,
59-
// const std::string&, const String&,
60-
// const JsonArray&, const JsonObject&
59+
// std::string, String, JsonArray, JsonObject
6160
template <typename T>
62-
typename TypeTraits::EnableIf<!TypeTraits::IsArray<T>::value, bool>::type add(
63-
const T &value) {
61+
bool add(const T &value) {
6462
return add_impl<const T &>(value);
6563
}
6664
//
6765
// bool add(TValue);
68-
// TValue = const char*, const char[N], const FlashStringHelper*
66+
// TValue = char*, const char*, const FlashStringHelper*
6967
template <typename T>
70-
bool add(const T *value) {
71-
return add_impl<const T *>(value);
68+
bool add(T *value) {
69+
return add_impl<T *>(value);
7270
}
7371
//
7472
// bool add(TValue value, uint8_t decimals);
@@ -81,21 +79,19 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
8179

8280
// Sets the value at specified index.
8381
//
84-
// bool add(size_t index, TValue);
82+
// bool add(size_t index, const TValue&);
8583
// TValue = bool, long, int, short, float, double, RawJson, JsonVariant,
86-
// const std::string&, const String&,
87-
// const JsonArray&, const JsonObject&
84+
// std::string, String, JsonArray, JsonObject
8885
template <typename T>
89-
typename TypeTraits::EnableIf<!TypeTraits::IsArray<T>::value, bool>::type set(
90-
size_t index, const T &value) {
86+
bool set(size_t index, const T &value) {
9187
return set_impl<const T &>(index, value);
9288
}
9389
//
9490
// bool add(size_t index, TValue);
95-
// TValue = const char*, const char[N], const FlashStringHelper*
91+
// TValue = char*, const char*, const FlashStringHelper*
9692
template <typename T>
97-
bool set(size_t index, const T *value) {
98-
return set_impl<const T *>(index, value);
93+
bool set(size_t index, T *value) {
94+
return set_impl<T *>(index, value);
9995
}
10096
//
10197
// bool set(size_t index, TValue value, uint8_t decimals);
@@ -208,14 +204,14 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
208204
bool set_impl(size_t index, TValueRef value) {
209205
iterator it = begin() += index;
210206
if (it == end()) return false;
211-
return Internals::ValueSetter<TValueRef>::set(_buffer, *it, value);
207+
return Internals::ValueSaver<TValueRef>::save(_buffer, *it, value);
212208
}
213209

214210
template <typename TValueRef>
215211
bool add_impl(TValueRef value) {
216212
iterator it = Internals::List<JsonVariant>::add();
217213
if (it == end()) return false;
218-
return Internals::ValueSetter<TValueRef>::set(_buffer, *it, value);
214+
return Internals::ValueSaver<TValueRef>::save(_buffer, *it, value);
219215
}
220216
};
221217

src/ArduinoJson/JsonArraySubscript.hpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,19 @@ class JsonArraySubscript : public JsonVariantBase<JsonArraySubscript> {
2525

2626
// Replaces the value
2727
//
28-
// operator=(TValue)
28+
// operator=(const TValue&)
2929
// TValue = bool, long, int, short, float, double, RawJson, JsonVariant,
30-
// const std::string&, const String&,
31-
// const JsonArray&, const JsonObject&
30+
// std::string, String, JsonArray, JsonObject
3231
template <typename T>
3332
FORCE_INLINE JsonArraySubscript& operator=(const T& src) {
3433
_array.set(_index, src);
3534
return *this;
3635
}
3736
//
3837
// operator=(TValue)
39-
// TValue = const char*, const char[N], const FlashStringHelper*
38+
// TValue = char*, const char*, const FlashStringHelper*
4039
template <typename T>
41-
FORCE_INLINE JsonArraySubscript& operator=(const T* src) {
40+
FORCE_INLINE JsonArraySubscript& operator=(T* src) {
4241
_array.set(_index, src);
4342
return *this;
4443
}
@@ -59,19 +58,18 @@ class JsonArraySubscript : public JsonVariantBase<JsonArraySubscript> {
5958

6059
// Replaces the value
6160
//
62-
// bool set(TValue)
61+
// bool set(const TValue&)
6362
// TValue = bool, long, int, short, float, double, RawJson, JsonVariant,
64-
// const std::string&, const String&,
65-
// const JsonArray&, const JsonObject&
63+
// std::string, String, JsonArray, JsonObject
6664
template <typename TValue>
6765
FORCE_INLINE bool set(const TValue& value) {
6866
return _array.set(_index, value);
6967
}
7068
//
7169
// bool set(TValue)
72-
// TValue = const char*, const char[N], const FlashStringHelper*
70+
// TValue = char*, const char*, const FlashStringHelper*
7371
template <typename TValue>
74-
FORCE_INLINE bool set(const TValue* value) {
72+
FORCE_INLINE bool set(TValue* value) {
7573
return _array.set(_index, value);
7674
}
7775
//

0 commit comments

Comments
 (0)
0