Description

copyArray() copies values between a JsonArray and a regular array.

It works in both directions:

It supports both 1D and 2D arrays.

Signatures

// 1D array -> JsonArray
template <typename T, size_t N>
bool copyArray(T (&src)[N], JsonArray dst);

// 1D array -> JsonArray
template <typename T>
bool copyArray(T* src, size_t len, JsonArray dst);

// 2D array -> JsonArray
template <typename T, size_t N1, size_t N2>
bool copyArray(T (&src)[N1][N2], JsonArray dst);

// JsonArray -> 1D array
template <typename T, size_t N>
size_t copyArray(JsonArrayConst src, T (&dst)[N]);

// JsonArray -> 1D array
template <typename T>
size_t copyArray(JsonArrayConst src, T* dst, size_t len);

// JsonArray -> 2D array
template <typename T, size_t N1, size_t N2>
void copyArray(JsonArrayConst src, T (&dst)[N1][N2]);

Return value

Some overloads of copyArray() return a bool that tells whether the copy was successful.

Some overloads of copyArray() return a size_t that contains the number of elements copied.

Example

int values[] = {1,2,3};

StaticJsonDocument<256> doc;
copyArray(values, doc.to<JsonArray>());
serializeJson(doc, Serial);

will write the following string to the serial port:

[1,2,3]