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

bblanchon / ArduinoJson / 18593280678

17 Oct 2025 12:52PM UTC coverage: 99.417% (+0.02%) from 99.399%
18593280678

push

github

bblanchon
ResourceManager: move out-of-class definitions back in the class

24 of 24 new or added lines in 1 file covered. (100.0%)

5 existing lines in 3 files now uncovered.

3925 of 3948 relevant lines covered (99.42%)

10567.54 hits per line

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

97.5
/src/ArduinoJson/MsgPack/MsgPackSerializer.hpp
1
// ArduinoJson - https://arduinojson.org
2
// Copyright © 2014-2025, Benoit BLANCHON
3
// MIT License
4

5
#pragma once
6

7
#include <ArduinoJson/MsgPack/endianness.hpp>
8
#include <ArduinoJson/Polyfills/assert.hpp>
9
#include <ArduinoJson/Polyfills/type_traits.hpp>
10
#include <ArduinoJson/Serialization/CountingDecorator.hpp>
11
#include <ArduinoJson/Serialization/measure.hpp>
12
#include <ArduinoJson/Serialization/serialize.hpp>
13
#include <ArduinoJson/Variant/VariantData.hpp>
14

15
ARDUINOJSON_BEGIN_PRIVATE_NAMESPACE
16

17
template <typename TWriter>
18
class MsgPackSerializer : public VariantDataVisitor<size_t> {
19
 public:
20
  static const bool producesText = false;
21

22
  MsgPackSerializer(TWriter writer, ResourceManager* resources)
85✔
23
      : writer_(writer), resources_(resources) {}
85✔
24

25
  template <typename T>
26
  enable_if_t<is_floating_point<T>::value && sizeof(T) == 4, size_t> visit(
18✔
27
      T value32) {
28
    if (canConvertNumber<JsonInteger>(value32)) {
18✔
29
      JsonInteger truncatedValue = JsonInteger(value32);
17✔
30
      if (value32 == T(truncatedValue))
17✔
31
        return visit(truncatedValue);
11✔
32
    }
33
    writeByte(0xCA);
7✔
34
    writeInteger(value32);
7✔
35
    return bytesWritten();
7✔
36
  }
37

38
  template <typename T>
39
  ARDUINOJSON_NO_SANITIZE("float-cast-overflow")
40
  enable_if_t<is_floating_point<T>::value && sizeof(T) == 8, size_t> visit(
1✔
41
      T value64) {
42
    float value32 = float(value64);
1✔
43
    if (value32 == value64)
1✔
44
      return visit(value32);
×
45
    writeByte(0xCB);
1✔
46
    writeInteger(value64);
1✔
47
    return bytesWritten();
1✔
48
  }
49

50
  size_t visitArray(VariantData* array) {
4✔
51
    ARDUINOJSON_ASSERT(array != nullptr);
52
    ARDUINOJSON_ASSERT(array->isArray());
53

54
    auto n = VariantImpl::size(array, resources_);
4✔
55
    if (n < 0x10) {
4✔
56
      writeByte(uint8_t(0x90 + n));
2✔
57
    } else if (n < 0x10000) {
2✔
58
      writeByte(0xDC);
1✔
59
      writeInteger(uint16_t(n));
1✔
60
    } else {
61
      writeByte(0xDD);
1✔
62
      writeInteger(uint32_t(n));
1✔
63
    }
64

65
    auto slotId = array->content.asCollection.head;
4✔
66
    while (slotId != NULL_SLOT) {
65,558✔
67
      auto slot = resources_->getVariant(slotId);
65,554✔
68
      VariantImpl::accept(*this, slot, resources_);
65,554✔
69
      slotId = slot->next;
65,554✔
70
    }
71

72
    return bytesWritten();
4✔
73
  }
74

75
  size_t visitObject(VariantData* object) {
15✔
76
    ARDUINOJSON_ASSERT(object != nullptr);
77
    ARDUINOJSON_ASSERT(object->isObject());
78

79
    auto n = VariantImpl::size(object, resources_);
15✔
80
    if (n < 0x10) {
15✔
81
      writeByte(uint8_t(0x80 + n));
14✔
82
    } else if (n < 0x10000) {
1✔
83
      writeByte(0xDE);
1✔
84
      writeInteger(uint16_t(n));
1✔
85
    } else {
UNCOV
86
      writeByte(0xDF);
×
UNCOV
87
      writeInteger(uint32_t(n));
×
88
    }
89

90
    auto slotId = object->content.asCollection.head;
15✔
91
    while (slotId != NULL_SLOT) {
95✔
92
      auto slot = resources_->getVariant(slotId);
80✔
93
      VariantImpl::accept(*this, slot, resources_);
80✔
94
      slotId = slot->next;
80✔
95
    }
96

97
    return bytesWritten();
15✔
98
  }
99

100
  size_t visit(const char* value) {
101
    return visit(JsonString(value));
102
  }
103

104
  size_t visit(JsonString value) {
59✔
105
    ARDUINOJSON_ASSERT(!value.isNull());
106

107
    auto n = value.size();
59✔
108

109
    if (n < 0x20) {
59✔
110
      writeByte(uint8_t(0xA0 + n));
55✔
111
    } else if (n < 0x100) {
4✔
112
      writeByte(0xD9);
1✔
113
      writeInteger(uint8_t(n));
1✔
114
    } else if (n < 0x10000) {
3✔
115
      writeByte(0xDA);
2✔
116
      writeInteger(uint16_t(n));
2✔
117
    } else {
118
      writeByte(0xDB);
1✔
119
      writeInteger(uint32_t(n));
1✔
120
    }
121
    writeBytes(reinterpret_cast<const uint8_t*>(value.c_str()), n);
59✔
122
    return bytesWritten();
59✔
123
  }
124

125
  size_t visit(RawString value) {
20✔
126
    writeBytes(reinterpret_cast<const uint8_t*>(value.data()), value.size());
20✔
127
    return bytesWritten();
20✔
128
  }
129

130
  size_t visit(JsonInteger value) {
58✔
131
    if (value > 0) {
58✔
132
      visit(static_cast<JsonUInt>(value));
39✔
133
    } else if (value >= -0x20) {
19✔
134
      writeInteger(int8_t(value));
8✔
135
    } else if (value >= -0x80) {
11✔
136
      writeByte(0xD0);
4✔
137
      writeInteger(int8_t(value));
4✔
138
    } else if (value >= -0x8000) {
7✔
139
      writeByte(0xD1);
4✔
140
      writeInteger(int16_t(value));
4✔
141
    }
142
#if ARDUINOJSON_USE_LONG_LONG
143
    else if (value >= -0x80000000LL)
3✔
144
#else
145
    else
146
#endif
147
    {
148
      writeByte(0xD2);
2✔
149
      writeInteger(int32_t(value));
2✔
150
    }
151
#if ARDUINOJSON_USE_LONG_LONG
152
    else {
153
      writeByte(0xD3);
1✔
154
      writeInteger(int64_t(value));
1✔
155
    }
156
#endif
157
    return bytesWritten();
58✔
158
  }
159

160
  size_t visit(JsonUInt value) {
54✔
161
    if (value <= 0x7F) {
54✔
162
      writeInteger(uint8_t(value));
39✔
163
    } else if (value <= 0xFF) {
15✔
164
      writeByte(0xCC);
5✔
165
      writeInteger(uint8_t(value));
5✔
166
    } else if (value <= 0xFFFF) {
10✔
167
      writeByte(0xCD);
4✔
168
      writeInteger(uint16_t(value));
4✔
169
    }
170
#if ARDUINOJSON_USE_LONG_LONG
171
    else if (value <= 0xFFFFFFFF)
6✔
172
#else
173
    else
174
#endif
175
    {
176
      writeByte(0xCE);
3✔
177
      writeInteger(uint32_t(value));
3✔
178
    }
179
#if ARDUINOJSON_USE_LONG_LONG
180
    else {
181
      writeByte(0xCF);
3✔
182
      writeInteger(uint64_t(value));
3✔
183
    }
184
#endif
185
    return bytesWritten();
54✔
186
  }
187

188
  size_t visit(bool value) {
2✔
189
    writeByte(value ? 0xC3 : 0xC2);
2✔
190
    return bytesWritten();
2✔
191
  }
192

193
  size_t visit(nullptr_t) {
65,538✔
194
    writeByte(0xC0);
65,538✔
195
    return bytesWritten();
65,538✔
196
  }
197

198
 private:
199
  size_t bytesWritten() const {
65,758✔
200
    return writer_.count();
65,758✔
201
  }
202

203
  void writeByte(uint8_t c) {
65,652✔
204
    writer_.write(c);
65,652✔
205
  }
65,652✔
206

207
  void writeBytes(const uint8_t* p, size_t n) {
167✔
208
    writer_.write(p, n);
167✔
209
  }
167✔
210

211
  template <typename T>
212
  void writeInteger(T value) {
88✔
213
    fixEndianness(value);
88✔
214
    writeBytes(reinterpret_cast<uint8_t*>(&value), sizeof(value));
88✔
215
  }
88✔
216

217
  CountingDecorator<TWriter> writer_;
218
  ResourceManager* resources_;
219
};
220

221
ARDUINOJSON_END_PRIVATE_NAMESPACE
222

223
ARDUINOJSON_BEGIN_PUBLIC_NAMESPACE
224

225
// Produces a MessagePack document.
226
// https://arduinojson.org/v7/api/msgpack/serializemsgpack/
227
template <
228
    typename TDestination,
229
    detail::enable_if_t<!detail::is_pointer<TDestination>::value, int> = 0>
230
inline size_t serializeMsgPack(JsonVariantConst source, TDestination& output) {
82✔
231
  using namespace ArduinoJson::detail;
232
  return serialize<MsgPackSerializer>(source, output);
82✔
233
}
234

235
// Produces a MessagePack document.
236
// https://arduinojson.org/v7/api/msgpack/serializemsgpack/
237
inline size_t serializeMsgPack(JsonVariantConst source, void* output,
2✔
238
                               size_t size) {
239
  using namespace ArduinoJson::detail;
240
  return serialize<MsgPackSerializer>(source, output, size);
2✔
241
}
242

243
// Computes the length of the document that serializeMsgPack() produces.
244
// https://arduinojson.org/v7/api/msgpack/measuremsgpack/
245
inline size_t measureMsgPack(JsonVariantConst source) {
1✔
246
  using namespace ArduinoJson::detail;
247
  return measure<MsgPackSerializer>(source);
1✔
248
}
249

250
ARDUINOJSON_END_PUBLIC_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