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

bblanchon / ArduinoJson / 9550679578

17 Jun 2024 03:44PM CUT coverage: 99.484%. Remained the same
9550679578

push

github

bblanchon
Add DevContainer files for Clang 13 to 17

3854 of 3874 relevant lines covered (99.48%)

10863.07 hits per line

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

98.37
/src/ArduinoJson/MsgPack/MsgPackSerializer.hpp
1
// ArduinoJson - https://arduinojson.org
2
// Copyright © 2014-2024, 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(
13✔
27
      T value32) {
28
    if (canConvertNumber<JsonInteger>(value32)) {
13✔
29
      JsonInteger truncatedValue = JsonInteger(value32);
12✔
30
      if (value32 == T(truncatedValue))
12✔
31
        return visit(truncatedValue);
11✔
32
    }
33
    writeByte(0xCA);
2✔
34
    writeInteger(value32);
2✔
35
    return bytesWritten();
2✔
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(
19✔
41
      T value64) {
42
    float value32 = float(value64);
19✔
43
    if (value32 == value64)
19✔
44
      return visit(value32);
13✔
45
    writeByte(0xCB);
6✔
46
    writeInteger(value64);
6✔
47
    return bytesWritten();
6✔
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_->getSlot(slotId);
65,554✔
65
      slot->data()->accept(*this);
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) {
55✔
86
      auto slot = resources_->getSlot(slotId);
40✔
87
      visit(slot->key());
40✔
88
      slot->data()->accept(*this);
40✔
89
      slotId = slot->next();
40✔
90
    }
91

92
    return bytesWritten();
15✔
93
  }
94

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

99
  size_t visit(JsonString value) {
59✔
100
    ARDUINOJSON_ASSERT(value != NULL);
101

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

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

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

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

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

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

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

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

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

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

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

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

216
ARDUINOJSON_END_PRIVATE_NAMESPACE
217

218
ARDUINOJSON_BEGIN_PUBLIC_NAMESPACE
219

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

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

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

243
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