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

bblanchon / ArduinoJson / 6770901880

06 Nov 2023 12:24PM CUT coverage: 99.545%. Remained the same
6770901880

push

github

bblanchon
CI: always use libc++ with clang

3497 of 3513 relevant lines covered (99.54%)

10637.15 hits per line

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

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

5
#pragma once
6

7
#include <ArduinoJson/MsgPack/endianess.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)
69✔
23
      : writer_(writer), resources_(resources) {}
69✔
24

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

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

51
  size_t visit(const ArrayData& array) {
4✔
52
    size_t n = array.size(resources_);
4✔
53
    if (n < 0x10) {
4✔
54
      writeByte(uint8_t(0x90 + n));
2✔
55
    } else if (n < 0x10000) {
2✔
56
      writeByte(0xDC);
1✔
57
      writeInteger(uint16_t(n));
1✔
58
    } else {
59
      writeByte(0xDD);
1✔
60
      writeInteger(uint32_t(n));
1✔
61
    }
62
    for (auto it = array.createIterator(resources_); !it.done();
65,558✔
63
         it.next(resources_)) {
65,554✔
64
      it->accept(*this);
65,554✔
65
    }
66
    return bytesWritten();
4✔
67
  }
68

69
  size_t visit(const ObjectData& object) {
15✔
70
    size_t n = object.size(resources_);
15✔
71
    if (n < 0x10) {
15✔
72
      writeByte(uint8_t(0x80 + n));
14✔
73
    } else if (n < 0x10000) {
1✔
74
      writeByte(0xDE);
1✔
75
      writeInteger(uint16_t(n));
1✔
76
    } else {
77
      writeByte(0xDF);
×
78
      writeInteger(uint32_t(n));
×
79
    }
80
    for (auto it = object.createIterator(resources_); !it.done();
55✔
81
         it.next(resources_)) {
40✔
82
      visit(it.key());
40✔
83
      it->accept(*this);
40✔
84
    }
85
    return bytesWritten();
15✔
86
  }
87

88
  size_t visit(const char* value) {
40✔
89
    return visit(JsonString(value));
40✔
90
  }
91

92
  size_t visit(JsonString value) {
59✔
93
    ARDUINOJSON_ASSERT(value != NULL);
94

95
    auto n = value.size();
59✔
96

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

113
  size_t visit(RawString value) {
4✔
114
    writeBytes(reinterpret_cast<const uint8_t*>(value.data()), value.size());
4✔
115
    return bytesWritten();
4✔
116
  }
117

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

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

176
  size_t visit(bool value) {
2✔
177
    writeByte(value ? 0xC3 : 0xC2);
2✔
178
    return bytesWritten();
2✔
179
  }
180

181
  size_t visit(nullptr_t) {
65,538✔
182
    writeByte(0xC0);
65,538✔
183
    return bytesWritten();
65,538✔
184
  }
185

186
 private:
187
  size_t bytesWritten() const {
65,742✔
188
    return writer_.count();
65,742✔
189
  }
190

191
  void writeByte(uint8_t c) {
65,652✔
192
    writer_.write(c);
65,652✔
193
  }
65,652✔
194

195
  void writeBytes(const uint8_t* p, size_t n) {
151✔
196
    writer_.write(p, n);
151✔
197
  }
151✔
198

199
  template <typename T>
200
  void writeInteger(T value) {
88✔
201
    fixEndianess(value);
88✔
202
    writeBytes(reinterpret_cast<uint8_t*>(&value), sizeof(value));
88✔
203
  }
88✔
204

205
  CountingDecorator<TWriter> writer_;
206
  const ResourceManager* resources_;
207
};
208

209
ARDUINOJSON_END_PRIVATE_NAMESPACE
210

211
ARDUINOJSON_BEGIN_PUBLIC_NAMESPACE
212

213
// Produces a MessagePack document.
214
// https://arduinojson.org/v7/api/msgpack/serializemsgpack/
215
template <typename TDestination>
216
inline size_t serializeMsgPack(JsonVariantConst source, TDestination& output) {
66✔
217
  using namespace ArduinoJson::detail;
218
  return serialize<MsgPackSerializer>(source, output);
66✔
219
}
220

221
// Produces a MessagePack document.
222
// https://arduinojson.org/v7/api/msgpack/serializemsgpack/
223
inline size_t serializeMsgPack(JsonVariantConst source, void* output,
2✔
224
                               size_t size) {
225
  using namespace ArduinoJson::detail;
226
  return serialize<MsgPackSerializer>(source, output, size);
2✔
227
}
228

229
// Computes the length of the document that serializeMsgPack() produces.
230
// https://arduinojson.org/v7/api/msgpack/measuremsgpack/
231
inline size_t measureMsgPack(JsonVariantConst source) {
1✔
232
  using namespace ArduinoJson::detail;
233
  return measure<MsgPackSerializer>(source);
1✔
234
}
235

236
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