• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

bblanchon / ArduinoJson / 3619649488

pending completion
3619649488

push

github

Benoit Blanchon
CI: use `ubuntu-20.04` for GCC

3373 of 3389 relevant lines covered (99.53%)

5926.06 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/ArduinoJson/Array/Utilities.hpp
1
// ArduinoJson - https://arduinojson.org
2
// Copyright © 2014-2022, Benoit BLANCHON
3
// MIT License
4

5
#pragma once
6

7
#include <ArduinoJson/Array/ArrayRef.hpp>
8
#include <ArduinoJson/Document/JsonDocument.hpp>
9

10
namespace ARDUINOJSON_NAMESPACE {
11

12
// Trivial form to stop the recursion
13
template <typename T>
14
inline typename enable_if<!is_array<T>::value, bool>::type copyArray(
42✔
15
    const T& src, VariantRef dst) {
16
  return dst.set(src);
45✔
17
}
18

19
// Copy array to a JsonArray/JsonVariant/MemberProxy/ElementProxy
20
template <typename T, size_t N, typename TDestination>
21
inline typename enable_if<!is_base_of<JsonDocument, TDestination>::value,
22
                          bool>::type
23
copyArray(T (&src)[N], const TDestination& dst) {
30✔
24
  return copyArray(src, N, dst);
30✔
25
}
26

27
// Copy ptr+size to a JsonArray/JsonVariant/MemberProxy/ElementProxy
28
template <typename T, typename TDestination>
29
inline typename enable_if<!is_base_of<JsonDocument, TDestination>::value,
30
                          bool>::type
31
copyArray(const T* src, size_t len, const TDestination& dst) {
21✔
32
  bool ok = true;
21✔
33
  for (size_t i = 0; i < len; i++) {
80✔
34
    ok &= copyArray(src[i], dst.add());
59✔
35
  }
36
  return ok;
21✔
37
}
38

39
// Special case for char[] which much be treated as const char*
40
template <typename TDestination>
41
inline bool copyArray(const char* src, size_t, const TDestination& dst) {
9✔
42
  return dst.set(src);
9✔
43
}
44

45
// Copy array to a JsonDocument
46
template <typename T>
47
inline bool copyArray(const T& src, JsonDocument& dst) {
3✔
48
  return copyArray(src, dst.to<ArrayRef>());
3✔
49
}
50

51
// Copy a ptr+size array to a JsonDocument
52
template <typename T>
53
inline bool copyArray(const T* src, size_t len, JsonDocument& dst) {
54
  return copyArray(src, len, dst.to<ArrayRef>());
55
}
56

57
// Trivial case form to stop the recursion
58
template <typename T>
59
inline typename enable_if<!is_array<T>::value, size_t>::type copyArray(
58✔
60
    VariantConstRef src, T& dst) {
61
  dst = src.as<T>();
29✔
62
  return 1;
29✔
63
}
64

65
// Copy a JsonArray to array
66
template <typename T, size_t N>
67
inline size_t copyArray(ArrayConstRef src, T (&dst)[N]) {
19✔
68
  return copyArray(src, dst, N);
19✔
69
}
70

71
// Copy a JsonArray to ptr+size
72
template <typename T>
73
inline size_t copyArray(ArrayConstRef src, T* dst, size_t len) {
19✔
74
  size_t i = 0;
19✔
75
  for (ArrayConstRef::iterator it = src.begin(); it != src.end() && i < len;
120✔
76
       ++it)
41✔
77
    copyArray(*it, dst[i++]);
50✔
78
  return i;
19✔
79
}
80

81
// Special case for char[] which must be treated as a string
82
template <size_t N>
83
inline size_t copyArray(VariantConstRef src, char (&dst)[N]) {
3✔
84
  String s = src;
85
  size_t len = N - 1;
3✔
86
  if (len > s.size())
3✔
87
    len = s.size();
1✔
88
  memcpy(dst, s.c_str(), len);
3✔
89
  dst[len] = 0;
3✔
90
  return 1;
3✔
91
}
92

93
// Copy a JsonDocument to an array
94
// (JsonDocument doesn't implicitly convert to JsonArrayConst)
95
template <typename TSource, typename T>
96
inline typename enable_if<is_array<T>::value &&
97
                              is_base_of<JsonDocument, TSource>::value,
98
                          size_t>::type
99
copyArray(const TSource& src, T& dst) {
2✔
100
  return copyArray(src.template as<ArrayConstRef>(), dst);
2✔
101
}
102

103
}  // namespace ARDUINOJSON_NAMESPACE
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc