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

bblanchon / ArduinoJson / 13696030297

06 Mar 2025 10:00AM CUT coverage: 99.309%. Remained the same
13696030297

push

github

bblanchon
Fix CLang Tidy error

4023 of 4051 relevant lines covered (99.31%)

10751.08 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, const 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 visit(const ArrayData& array) {
4✔
51
    size_t n = array.size(resources_);
4✔
52
    if (n < 0x10) {
4✔
53
      writeByte(uint8_t(0x90 + n));
2✔
54
    } else if (n < 0x10000) {
2✔
55
      writeByte(0xDC);
1✔
56
      writeInteger(uint16_t(n));
1✔
57
    } else {
58
      writeByte(0xDD);
1✔
59
      writeInteger(uint32_t(n));
1✔
60
    }
61

62
    auto slotId = array.head();
4✔
63
    while (slotId != NULL_SLOT) {
65,558✔
64
      auto slot = resources_->getVariant(slotId);
65,554✔
65
      slot->accept(*this, resources_);
65,554✔
66
      slotId = slot->next();
65,554✔
67
    }
68

69
    return bytesWritten();
4✔
70
  }
71

72
  size_t visit(const ObjectData& object) {
15✔
73
    size_t n = object.size(resources_);
15✔
74
    if (n < 0x10) {
15✔
75
      writeByte(uint8_t(0x80 + n));
14✔
76
    } else if (n < 0x10000) {
1✔
77
      writeByte(0xDE);
1✔
78
      writeInteger(uint16_t(n));
1✔
79
    } else {
80
      writeByte(0xDF);
×
81
      writeInteger(uint32_t(n));
×
82
    }
83

84
    auto slotId = object.head();
15✔
85
    while (slotId != NULL_SLOT) {
95✔
86
      auto slot = resources_->getVariant(slotId);
80✔
87
      slot->accept(*this, resources_);
80✔
88
      slotId = slot->next();
80✔
89
    }
90

91
    return bytesWritten();
15✔
92
  }
93

94
  size_t visit(const char* value) {
95
    return visit(JsonString(value));
96
  }
97

98
  size_t visit(JsonString value) {
59✔
99
    ARDUINOJSON_ASSERT(!value.isNull());
100

101
    auto n = value.size();
59✔
102

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

119
  size_t visit(RawString value) {
20✔
120
    writeBytes(reinterpret_cast<const uint8_t*>(value.data()), value.size());
20✔
121
    return bytesWritten();
20✔
122
  }
123

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

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

182
  size_t visit(bool value) {
2✔
183
    writeByte(value ? 0xC3 : 0xC2);
2✔
184
    return bytesWritten();
2✔
185
  }
186

187
  size_t visit(nullptr_t) {
65,538✔
188
    writeByte(0xC0);
65,538✔
189
    return bytesWritten();
65,538✔
190
  }
191

192
 private:
193
  size_t bytesWritten() const {
65,758✔
194
    return writer_.count();
65,758✔
195
  }
196

197
  void writeByte(uint8_t c) {
65,652✔
198
    writer_.write(c);
65,652✔
199
  }
65,652✔
200

201
  void writeBytes(const uint8_t* p, size_t n) {
167✔
202
    writer_.write(p, n);
167✔
203
  }
167✔
204

205
  template <typename T>
206
  void writeInteger(T value) {
88✔
207
    fixEndianness(value);
88✔
208
    writeBytes(reinterpret_cast<uint8_t*>(&value), sizeof(value));
88✔
209
  }
88✔
210

211
  CountingDecorator<TWriter> writer_;
212
  const ResourceManager* resources_;
213
};
214

215
ARDUINOJSON_END_PRIVATE_NAMESPACE
216

217
ARDUINOJSON_BEGIN_PUBLIC_NAMESPACE
218

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

229
// Produces a MessagePack document.
230
// https://arduinojson.org/v7/api/msgpack/serializemsgpack/
231
inline size_t serializeMsgPack(JsonVariantConst source, void* output,
2✔
232
                               size_t size) {
233
  using namespace ArduinoJson::detail;
234
  return serialize<MsgPackSerializer>(source, output, size);
2✔
235
}
236

237
// Computes the length of the document that serializeMsgPack() produces.
238
// https://arduinojson.org/v7/api/msgpack/measuremsgpack/
239
inline size_t measureMsgPack(JsonVariantConst source) {
1✔
240
  using namespace ArduinoJson::detail;
241
  return measure<MsgPackSerializer>(source);
1✔
242
}
243

244
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