Description

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

It works in both directions:

It supports both 1D and 2D arrays.

Signatures

// In the following lines T can be any type supported by JsonVariant
// (for example: bool, int, float, or const char*).
// N, N1, and N2 are integer constants.

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

// 1D array -> JsonArray/JsonDocument
bool copyArray(T* src, size_t len, JsonArray dst);
bool copyArray(T* src, size_t len, JsonDocument& dst);

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

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

// JsonArray/JsonDocument -> 1D array
size_t copyArray(JsonArrayConst src, T* dst, size_t len);
size_t copyArray(const JsonDocument& src, T* dst, size_t len);

// JsonArray -> 2D array
void copyArray(JsonArrayConst src, T[N1][N2] dst);
void copyArray(const JsonDocument& src, T[N1][N2] dst);

// these function also support JsonDocument

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

1D array

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

JsonDocument doc;
copyArray(values, doc);
serializeJson(doc, Serial);

will write the following string to the serial port:

[1,2,3]

2D array

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

JsonDocument doc;
copyArray(values, doc);
serializeJson(doc, Serial);

will write the following string to the serial port:

[[1,2,3],[4,5,6]]