Skip to content

Commit 9625314

Browse files
committed
Implement JsonString from RamString
1 parent 6a32cdf commit 9625314

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

src/ArduinoJson/Strings/Adapters/JsonString.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ struct StringAdapter<JsonString> {
1515
using AdaptedString = RamString;
1616

1717
static AdaptedString adapt(const JsonString& s) {
18-
return AdaptedString(s.c_str(), s.size(), s.isLinked());
18+
return s.str_;
1919
}
2020
};
2121

src/ArduinoJson/Strings/JsonString.hpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,54 +13,56 @@ ARDUINOJSON_BEGIN_PUBLIC_NAMESPACE
1313
// A string.
1414
// https://arduinojson.org/v7/api/jsonstring/
1515
class JsonString {
16+
friend struct detail::StringAdapter<JsonString>;
17+
1618
public:
1719
enum Ownership { Copied, Linked };
1820

19-
JsonString() : data_(0), size_(0), ownership_(Linked) {}
21+
JsonString() : str_(nullptr, 0, true) {}
2022

2123
JsonString(const char* data, Ownership ownership = Linked)
22-
: data_(data), size_(data ? ::strlen(data) : 0), ownership_(ownership) {}
24+
: str_(data, data ? ::strlen(data) : 0, ownership == Linked) {}
2325

2426
JsonString(const char* data, size_t sz, Ownership ownership = Linked)
25-
: data_(data), size_(sz), ownership_(ownership) {}
27+
: str_(data, sz, ownership == Linked) {}
2628

2729
// Returns a pointer to the characters.
2830
const char* c_str() const {
29-
return data_;
31+
return str_.data();
3032
}
3133

3234
// Returns true if the string is null.
3335
bool isNull() const {
34-
return !data_;
36+
return str_.isNull();
3537
}
3638

3739
// Returns true if the string is stored by address.
3840
// Returns false if the string is stored by copy.
3941
bool isLinked() const {
40-
return ownership_ == Linked;
42+
return str_.isLinked();
4143
}
4244

4345
// Returns length of the string.
4446
size_t size() const {
45-
return size_;
47+
return str_.size();
4648
}
4749

4850
// Returns true if the string is non-null
4951
explicit operator bool() const {
50-
return data_ != 0;
52+
return str_.data() != 0;
5153
}
5254

5355
// Returns true if strings are equal.
5456
friend bool operator==(JsonString lhs, JsonString rhs) {
55-
if (lhs.size_ != rhs.size_)
57+
if (lhs.size() != rhs.size())
5658
return false;
57-
if (lhs.data_ == rhs.data_)
59+
if (lhs.c_str() == rhs.c_str())
5860
return true;
59-
if (!lhs.data_)
61+
if (!lhs.c_str())
6062
return false;
61-
if (!rhs.data_)
63+
if (!rhs.c_str())
6264
return false;
63-
return memcmp(lhs.data_, rhs.data_, lhs.size_) == 0;
65+
return memcmp(lhs.c_str(), rhs.c_str(), lhs.size()) == 0;
6466
}
6567

6668
// Returns true if strings differs.
@@ -76,9 +78,7 @@ class JsonString {
7678
#endif
7779

7880
private:
79-
const char* data_;
80-
size_t size_;
81-
Ownership ownership_;
81+
detail::RamString str_;
8282
};
8383

8484
ARDUINOJSON_END_PUBLIC_NAMESPACE

0 commit comments

Comments
 (0)