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

bblanchon / ArduinoJson / 16401353174

20 Jul 2025 03:16PM UTC coverage: 99.197% (-0.02%) from 99.215%
16401353174

push

github

bblanchon
Done

32 of 35 new or added lines in 5 files covered. (91.43%)

6 existing lines in 4 files now uncovered.

3955 of 3987 relevant lines covered (99.2%)

10923.11 hits per line

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

94.87
/src/ArduinoJson/Object/ObjectImpl.hpp
1
// ArduinoJson - https://arduinojson.org
2
// Copyright © 2014-2025, Benoit BLANCHON
3
// MIT License
4

5
#pragma once
6

7
#include <ArduinoJson/Variant/VariantCompare.hpp>
8
#include <ArduinoJson/Variant/VariantImpl.hpp>
9

10
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
11

12
inline bool VariantImpl::copyObject(const VariantImpl& src) {
33✔
13
  ARDUINOJSON_ASSERT(isNull());
14

15
  if (!data_)
33✔
NEW
16
    return false;
×
17

18
  data_->toObject();
33✔
19

20
  for (auto it = src.createIterator(); !it.done(); it.move()) {
55✔
21
    auto keySlot = allocVariant();
27✔
22
    if (!keySlot)
27✔
23
      return false;
5✔
24

25
    auto key = VariantImpl(keySlot.ptr(), resources_);
27✔
26
    if (!key.copyVariant(*it)) {
27✔
27
      freeVariant(keySlot);
1✔
28
      return false;
1✔
29
    }
30

31
    it.move();  // move to value
26✔
32
    ARDUINOJSON_ASSERT(!it.done());
33

34
    auto valueSlot = allocVariant();
26✔
35
    if (!valueSlot) {
26✔
NEW
36
      freeVariant(keySlot);
×
NEW
37
      return false;
×
38
    }
39

40
    // TODO: we add the pair before copying the value to be keep the old
41
    // behavior but this is not consistent with issue #2081
42
    appendPair(keySlot, valueSlot);
26✔
43

44
    auto value = VariantImpl(valueSlot.ptr(), resources_);
26✔
45
    if (!value.copyVariant(*it))
26✔
46
      return false;
4✔
47
  }
48

49
  return true;
28✔
50
}
51

52
template <typename TAdaptedString>
53
inline VariantData* VariantImpl::getMember(TAdaptedString key) const {
4,734✔
54
  auto it = findKey(key);
4,734✔
55
  if (it.done())
4,734✔
56
    return nullptr;
3,472✔
57
  it.move();
1,262✔
58
  return it->data();
1,262✔
59
}
60

61
template <typename TAdaptedString>
62
VariantData* VariantImpl::getOrAddMember(TAdaptedString key) {
935✔
63
  auto data = getMember(key);
935✔
64
  if (data)
935✔
65
    return data;
34✔
66
  return addMember(key);
901✔
67
}
68

69
template <typename TAdaptedString>
70
inline VariantImpl::iterator VariantImpl::findKey(TAdaptedString key) const {
4,765✔
71
  if (!isObject())
4,765✔
72
    return iterator();
63✔
73
  if (key.isNull())
4,702✔
74
    return iterator();
11✔
75
  bool isKey = true;
4,691✔
76
  for (auto it = createIterator(); !it.done(); it.move()) {
25,531✔
77
    if (isKey && stringEquals(key, adaptString(it->asString())))
22,128✔
78
      return it;
1,288✔
79
    isKey = !isKey;
20,840✔
80
  }
81
  return iterator();
3,403✔
82
}
83

84
template <typename TAdaptedString>
85
inline void VariantImpl::removeMember(TAdaptedString key) {
31✔
86
  removeMember(findKey(key));
31✔
87
}
31✔
88

89
inline void VariantImpl::removeMember(iterator it) {
35✔
90
  removePair(it);
35✔
91
}
35✔
92

93
template <typename TAdaptedString>
94
inline VariantData* VariantImpl::addMember(TAdaptedString key) {
901✔
95
  if (!isObject())
901✔
96
    return nullptr;
3✔
97

98
  if (key.isNull())
898✔
99
    return nullptr;  // Ignore null key
1✔
100

101
  auto keySlot = allocVariant();
897✔
102
  if (!keySlot)
897✔
103
    return nullptr;
2✔
104

105
  auto valueSlot = allocVariant();
895✔
106
  if (!valueSlot)
895✔
107
    return nullptr;
1✔
108

109
  VariantImpl keyImpl(keySlot.ptr(), resources_);
894✔
110
  if (!keyImpl.setString(key))
894✔
111
    return nullptr;
1✔
112

113
  VariantImpl::appendPair(keySlot, valueSlot);
893✔
114

115
  return valueSlot.ptr();
893✔
116
}
117

118
inline VariantData* VariantImpl::addPair(VariantData** value) {
1,336✔
119
  ARDUINOJSON_ASSERT(isObject());
120

121
  auto keySlot = allocVariant();
1,336✔
122
  if (!keySlot)
1,336✔
123
    return nullptr;
4✔
124

125
  auto valueSlot = allocVariant();
1,332✔
126
  if (!valueSlot)
1,332✔
127
    return nullptr;
×
128
  *value = valueSlot.ptr();
1,332✔
129

130
  VariantImpl::appendPair(keySlot, valueSlot);
1,332✔
131

132
  return keySlot.ptr();
1,332✔
133
}
134

135
// Returns the size (in bytes) of an object with n members.
136
constexpr size_t sizeofObject(size_t n) {
1,688✔
137
  return 2 * n * sizeof(VariantData);
1,688✔
138
}
139

140
ARDUINOJSON_END_PRIVATE_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