Skip to content

Commit 68c0097

Browse files
committed
Add support of multi-dim C-style array.
* Support up to 4 dimensional array.
1 parent a259ecc commit 68c0097

File tree

3 files changed

+154
-0
lines changed

3 files changed

+154
-0
lines changed

include/nlohmann/detail/conversions/from_json.hpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,54 @@ auto from_json(const BasicJsonType& j, T (&arr)[N]) // NOLINT(cppcoreguidelines
190190
}
191191
}
192192

193+
template<typename BasicJsonType, typename T, std::size_t N1, std::size_t N2>
194+
auto from_json(const BasicJsonType& j, T (&arr)[N1][N2]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
195+
-> decltype(j.template get<T>(), void())
196+
{
197+
for (std::size_t i1 = 0; i1 < N1; ++i1)
198+
{
199+
for (std::size_t i2 = 0; i2 < N2; ++i2)
200+
{
201+
arr[i1][i2] = j.at(i1).at(i2).template get<T>();
202+
}
203+
}
204+
}
205+
206+
template<typename BasicJsonType, typename T, std::size_t N1, std::size_t N2, std::size_t N3>
207+
auto from_json(const BasicJsonType& j, T (&arr)[N1][N2][N3]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
208+
-> decltype(j.template get<T>(), void())
209+
{
210+
for (std::size_t i1 = 0; i1 < N1; ++i1)
211+
{
212+
for (std::size_t i2 = 0; i2 < N2; ++i2)
213+
{
214+
for (std::size_t i3 = 0; i3 < N3; ++i3)
215+
{
216+
arr[i1][i2][i3] = j.at(i1).at(i2).at(i3).template get<T>();
217+
}
218+
}
219+
}
220+
}
221+
222+
template<typename BasicJsonType, typename T, std::size_t N1, std::size_t N2, std::size_t N3, std::size_t N4>
223+
auto from_json(const BasicJsonType& j, T (&arr)[N1][N2][N3][N4]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
224+
-> decltype(j.template get<T>(), void())
225+
{
226+
for (std::size_t i1 = 0; i1 < N1; ++i1)
227+
{
228+
for (std::size_t i2 = 0; i2 < N2; ++i2)
229+
{
230+
for (std::size_t i3 = 0; i3 < N3; ++i3)
231+
{
232+
for (std::size_t i4 = 0; i4 < N4; ++i4)
233+
{
234+
arr[i1][i2][i3][i4] = j.at(i1).at(i2).at(i3).at(i4).template get<T>();
235+
}
236+
}
237+
}
238+
}
239+
}
240+
193241
template<typename BasicJsonType>
194242
inline void from_json_array_impl(const BasicJsonType& j, typename BasicJsonType::array_t& arr, priority_tag<3> /*unused*/)
195243
{

single_include/nlohmann/json.hpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4817,6 +4817,54 @@ auto from_json(const BasicJsonType& j, T (&arr)[N]) // NOLINT(cppcoreguidelines
48174817
}
48184818
}
48194819

4820+
template<typename BasicJsonType, typename T, std::size_t N1, std::size_t N2>
4821+
auto from_json(const BasicJsonType& j, T (&arr)[N1][N2]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
4822+
-> decltype(j.template get<T>(), void())
4823+
{
4824+
for (std::size_t i1 = 0; i1 < N1; ++i1)
4825+
{
4826+
for (std::size_t i2 = 0; i2 < N2; ++i2)
4827+
{
4828+
arr[i1][i2] = j.at(i1).at(i2).template get<T>();
4829+
}
4830+
}
4831+
}
4832+
4833+
template<typename BasicJsonType, typename T, std::size_t N1, std::size_t N2, std::size_t N3>
4834+
auto from_json(const BasicJsonType& j, T (&arr)[N1][N2][N3]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
4835+
-> decltype(j.template get<T>(), void())
4836+
{
4837+
for (std::size_t i1 = 0; i1 < N1; ++i1)
4838+
{
4839+
for (std::size_t i2 = 0; i2 < N2; ++i2)
4840+
{
4841+
for (std::size_t i3 = 0; i3 < N3; ++i3)
4842+
{
4843+
arr[i1][i2][i3] = j.at(i1).at(i2).at(i3).template get<T>();
4844+
}
4845+
}
4846+
}
4847+
}
4848+
4849+
template<typename BasicJsonType, typename T, std::size_t N1, std::size_t N2, std::size_t N3, std::size_t N4>
4850+
auto from_json(const BasicJsonType& j, T (&arr)[N1][N2][N3][N4]) // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
4851+
-> decltype(j.template get<T>(), void())
4852+
{
4853+
for (std::size_t i1 = 0; i1 < N1; ++i1)
4854+
{
4855+
for (std::size_t i2 = 0; i2 < N2; ++i2)
4856+
{
4857+
for (std::size_t i3 = 0; i3 < N3; ++i3)
4858+
{
4859+
for (std::size_t i4 = 0; i4 < N4; ++i4)
4860+
{
4861+
arr[i1][i2][i3][i4] = j.at(i1).at(i2).at(i3).at(i4).template get<T>();
4862+
}
4863+
}
4864+
}
4865+
}
4866+
}
4867+
48204868
template<typename BasicJsonType>
48214869
inline void from_json_array_impl(const BasicJsonType& j, typename BasicJsonType::array_t& arr, priority_tag<3> /*unused*/)
48224870
{

tests/src/unit-conversions.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,64 @@ TEST_CASE("value conversion")
344344
CHECK(std::equal(std::begin(nbs), std::end(nbs), std::begin(nbs2)));
345345
}
346346

347+
SECTION("built-in arrays: 2D")
348+
{
349+
const int nbs[][3] = {{0, 1, 2}, {3, 4, 5}}; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
350+
int nbs2[][3] = {{0, 0, 0}, {0, 0, 0}}; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
351+
352+
const json j2 = nbs;
353+
j2.get_to(nbs2);
354+
CHECK(std::equal(std::begin(nbs[0]), std::end(nbs[1]), std::begin(nbs2[0])));
355+
}
356+
357+
SECTION("built-in arrays: 3D")
358+
{
359+
const int nbs[][2][3] = {\
360+
{{0, 1, 2}, {3, 4, 5}}, \
361+
{{10, 11, 12}, {13, 14, 15}}\
362+
}; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
363+
int nbs2[][2][3] = {\
364+
{{0, 0, 0}, {0, 0, 0}}, \
365+
{{0, 0, 0}, {0, 0, 0}}\
366+
}; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
367+
368+
const json j2 = nbs;
369+
j2.get_to(nbs2);
370+
CHECK(std::equal(std::begin(nbs[0][0]), std::end(nbs[1][1]), std::begin(nbs2[0][0])));
371+
}
372+
373+
SECTION("built-in arrays: 4D")
374+
{
375+
const int nbs[][2][2][3] = {\
376+
{
377+
\
378+
{{0, 1, 2}, {3, 4, 5}}, \
379+
{{10, 11, 12}, {13, 14, 15}}\
380+
}, \
381+
{
382+
\
383+
{{20, 21, 22}, {23, 24, 25}}, \
384+
{{30, 31, 32}, {33, 34, 35}}\
385+
}\
386+
}; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
387+
int nbs2[][2][2][3] = {\
388+
{
389+
\
390+
{{0, 0, 0}, {0, 0, 0}}, \
391+
{{0, 0, 0}, {0, 0, 0}}\
392+
}, \
393+
{
394+
\
395+
{{0, 0, 0}, {0, 0, 0}}, \
396+
{{0, 0, 0}, {0, 0, 0}}\
397+
}\
398+
}; // NOLINT(cppcoreguidelines-avoid-c-arrays,hicpp-avoid-c-arrays,modernize-avoid-c-arrays)
399+
400+
const json j2 = nbs;
401+
j2.get_to(nbs2);
402+
CHECK(std::equal(std::begin(nbs[0][0][0]), std::end(nbs[1][1][1]), std::begin(nbs2[0][0][0])));
403+
}
404+
347405
SECTION("std::deque<json>")
348406
{
349407
std::deque<json> a{"previous", "value"};

0 commit comments

Comments
 (0)