8000 'size' and 'toString' functions have been added to JSONArray · copercini/esp32-snippets@505213c · GitHub
[go: up one dir, main page]

Skip to content

Commit 505213c

Browse files
author
Ferran Verdés
committed
'size' and 'toString' functions have been added to JSONArray
1 parent 88f59b5 commit 505213c

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

cpp_utils/JSON.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,27 @@ std::string JsonArray::getString(int item) {
175175
} // getString
176176

177177

178+
/**
179+
* @brief Convert the JSON array to a string.
180+
* @return A JSON string representation of the array.
181+
*/
182+
std::string JsonArray::toString() {
183+
char *data = cJSON_Print(m_node);
184+
std::string ret(data);
185+
free(data);
186+
return ret;
187+
} // toString
188+
189+
190+
/**
191+
* @brief Get the number of elements from the array.
192+
* @return The int value that represents the number of elements.
193+
*/
194+
std::size_t JsonArray::size() {
195+
return cJSON_GetArraySize(m_node);
196+
} // size
197+
198+
178199
JsonObject::JsonObject(cJSON* node) {
179200
m_node = node;
180201
}

cpp_utils/JSON.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ class JsonArray {
4444
void addInt(int value);
4545
void addObject(JsonObject value);
4646
void addString(std::string value);
47+
std::string toString();
48+
std::size_t size();
4749
/**
4850
* @brief The underlying cJSON node.
4951
*/

0 commit comments

Comments
 (0)
0