Skip to content

Commit 4b89878

Browse files
committed
Added a deprecation warning for as<char*>()
1 parent 06fad30 commit 4b89878

File tree

6 files changed

+75
-20
lines changed

6 files changed

+75
-20
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ HEAD
6161
> Serial.println(doc["sensor"].as<const char*>()); // OK
6262
> ```
6363
>
64+
> A deprecation warning with the message "Replace `as<char*>()` with `as<const char*>()`" was added to allow a smooth transition.
6465
>
6566
> #### `DeserializationError::NotSupported` removed
6667
>

extras/tests/Misc/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
add_executable(MiscTests
66
arithmeticCompare.cpp
77
conflicts.cpp
8+
deprecated.cpp
89
FloatParts.cpp
910
JsonString.cpp
1011
printable.cpp

extras/tests/Misc/deprecated.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// ArduinoJson - https://arduinojson.org
2+
// Copyright Benoit Blanchon 2014-2021
3+
// MIT License
4+
5+
#include <ArduinoJson.h>
6+
#include <catch.hpp>
7+
8+
#if defined(__GNUC__)
9+
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
10+
#endif
11+
12+
#ifdef _MSC_VER
13+
#pragma warning(disable : 4996) // deprecation warning
14+
#endif
15+
16+
TEST_CASE("Deprecated features") {
17+
StaticJsonDocument<256> doc;
18+
const char* s = "hello";
19+
doc["s"] = s;
20+
doc["a"].add(s);
21+
22+
SECTION("JsonVariant::as<char*>()") {
23+
JsonVariant v = doc["s"];
24+
REQUIRE(v.as<char*>() == s);
25+
}
26+
27+
SECTION("JsonVariantConst::as<char*>()") {
28+
JsonVariantConst v = doc["s"];
29+
REQUIRE(v.as<char*>() == s);
30+
}
31+
32+
SECTION("MemberProxy::as<char*>()") {
33+
REQUIRE(doc["s"].as<char*>() == s);
34+
}
35+
36+
SECTION("ElementProxy::as<char*>()") {
37+
REQUIRE(doc["a"][0].as<char*>() == s);
38+
}
39+
}

src/ArduinoJson/Array/ElementProxy.hpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,17 @@ class ElementProxy : public VariantOperators<ElementProxy<TArray> >,
6565
}
6666

6767
template <typename T>
68-
FORCE_INLINE T as() const {
68+
FORCE_INLINE typename enable_if<!is_same<T, char*>::value, T>::type as()
69+
const {
6970
return getUpstreamElement().template as<T>();
7071
}
7172

73+
template <typename T>
74+
FORCE_INLINE typename enable_if<is_same<T, char*>::value, const char*>::type
75+
DEPRECATED("Replace as<char*>() with as<const char*>()") as() const {
76+
return as<const char*>();
77+
}
78+
7279
template <typename T>
7380
FORCE_INLINE operator T() const {
7481
return getUpstreamElement();

src/ArduinoJson/Object/MemberProxy.hpp

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,20 +46,6 @@ class MemberProxy : public VariantOperators<MemberProxy<TObject, TStringRef> >,
4646
template <typename TValue>
4747
FORCE_INLINE typename enable_if<!is_array<TValue>::value, this_type &>::type
4848
operator=(const TValue &src) {
49-
/********************************************************************
50-
** THIS IS NOT A BUG IN THE LIBRARY **
51-
** -------------------------------- **
52-
** Get a compilation error pointing here? **
53-
** It doesn't mean the error *is* here. **
54-
** Often, it's because you try to assign the wrong value type. **
55-
** **
56-
** For example: **
57-
** char age = 42 **
58-
** doc["age"] = age; **
59-
** Instead, use: **
60-
** int8_t age = 42; **
61-
** doc["age"] = age; **
62-
********************************************************************/
6349
getOrAddUpstreamMember().set(src);
6450
return *this;
6551
}
@@ -81,9 +67,16 @@ class MemberProxy : public VariantOperators<MemberProxy<TObject, TStringRef> >,
8167
return getUpstreamMember().isNull();
8268
}
8369

84-
template <typename TValue>
85-
FORCE_INLINE TValue as() const {
86-
return getUpstreamMember().template as<TValue>();
70+
template <typename T>
71+
FORCE_INLINE typename enable_if<!is_same<T, char *>::value, T>::type as()
72+
const {
73+
return getUpstreamMember().template as<T>();
74+
}
75+
76+
template <typename T>
77+
FORCE_INLINE typename enable_if<is_same<T, char *>::value, const char *>::type
78+
DEPRECATED("Replace as<char*>() with as<const char*>()") as() const {
79+
return as<const char *>();
8780
}
8881

8982
template <typename T>

src/ArduinoJson/Variant/VariantRef.hpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,17 @@ class VariantRef : public VariantRefBase<VariantData>,
9494
}
9595

9696
template <typename T>
97-
FORCE_INLINE T as() const {
97+
FORCE_INLINE typename enable_if<!is_same<T, char *>::value, T>::type as()
98+
const {
9899
return Converter<T>::fromJson(*this);
99100
}
100101

102+
template <typename T>
103+
FORCE_INLINE typename enable_if<is_same<T, char *>::value, const char *>::type
104+
DEPRECATED("Replace as<char*>() with as<const char*>()") as() const {
105+
return as<const char *>();
106+
}
107+
101108
template <typename T>
102109
FORCE_INLINE bool is() const {
103110
return Converter<T>::checkJson(*this);
@@ -204,10 +211,17 @@ class VariantConstRef : public VariantRefBase<const VariantData>,
204211
}
205212

206213
template <typename T>
207-
FORCE_INLINE T as() const {
214+
FORCE_INLINE typename enable_if<!is_same<T, char *>::value, T>::type as()
215+
const {
208216
return Converter<T>::fromJson(*this);
209217
}
210218

219+
template <typename T>
220+
FORCE_INLINE typename enable_if<is_same<T, char *>::value, const char *>::type
221+
DEPRECATED("Replace as<char*>() with as<const char*>()") as() const {
222+
return as<const char *>();
223+
}
224+
211225
template <typename T>
212226
FORCE_INLINE bool is() const {
213227
return Converter<T>::checkJson(*this);

0 commit comments

Comments
 (0)