Skip to content

Commit

Permalink
Implement #254 for a one-D array
Browse files Browse the repository at this point in the history
  • Loading branch information
bblanchon committed Apr 4, 2016
1 parent ba6e885 commit 3a493a3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
9 changes: 9 additions & 0 deletions include/ArduinoJson/JsonArray.hpp
Expand Up @@ -161,6 +161,15 @@ class JsonArray : public Internals::JsonPrintable<JsonArray>,
// Serialize the array to the specified JsonWriter.
void writeTo(Internals::JsonWriter &writer) const;

// Extract a C array
template <typename T, size_t N>
size_t copyTo(T(&destination)[N]) const {
size_t i = 0;
for (const_iterator it = begin(); it != end() && i < N; ++it)
destination[i++] = *it;
return i;
}

private:
node_type *getNodeAt(size_t index) const;

Expand Down
24 changes: 24 additions & 0 deletions test/JsonArray_CopyTo_Tests.cpp
@@ -0,0 +1,24 @@
// Copyright Benoit Blanchon 2014-2016
// MIT License
//
// Arduino JSON library
// https://github.com/bblanchon/ArduinoJson
// If you like this project, please add a star!

#include <ArduinoJson.h>
#include <gtest/gtest.h>

TEST(JsonArray_CopyTo_Tests, OneDimensionIntegerArray) {
char json[] = "[1,2,3,4]";

DynamicJsonBuffer jsonBuffer;
JsonArray& array = jsonBuffer.parseArray(json);

int destination[4];
array.copyTo(destination);

ASSERT_EQ(1, destination[0]);
ASSERT_EQ(2, destination[1]);
ASSERT_EQ(3, destination[2]);
ASSERT_EQ(4, destination[3]);
}

0 comments on commit 3a493a3

Please sign in to comment.