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

bblanchon / ArduinoJson / 5059873495

pending completion
5059873495

push

github

Benoit Blanchon
Remove VariantImpl.hpp

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

3280 of 3299 relevant lines covered (99.42%)

6312.26 hits per line

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

98.26
/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 Visitor<size_t> {
19
 public:
20
  static const bool producesText = false;
21

22
  MsgPackSerializer(TWriter writer) : writer_(writer) {}
69✔
23

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

36
  template <typename T>
37
  ARDUINOJSON_NO_SANITIZE("float-cast-overflow")
38
  typename enable_if<sizeof(T) == 8, size_t>::type visitFloat(T value64) {
19✔
39
    float value32 = float(value64);
19✔
40
    if (value32 == value64)
19✔
41
      return visitFloat(value32);
13✔
42
    writeByte(0xCB);
6✔
43
    writeInteger(value64);
6✔
44
    return bytesWritten();
6✔
45
  }
46

47
  size_t visitArray(const CollectionData& array) {
4✔
48
    size_t n = array.size();
4✔
49
    if (n < 0x10) {
4✔
50
      writeByte(uint8_t(0x90 + array.size()));
2✔
51
    } else if (n < 0x10000) {
2✔
52
      writeByte(0xDC);
1✔
53
      writeInteger(uint16_t(n));
1✔
54
    } else {
55
      writeByte(0xDD);
1✔
56
      writeInteger(uint32_t(n));
1✔
57
    }
58
    for (const VariantSlot* slot = array.head(); slot; slot = slot->next()) {
65,558✔
59
      slot->data()->accept(*this);
65,554✔
60
    }
61
    return bytesWritten();
4✔
62
  }
63

64
  size_t visitObject(const CollectionData& object) {
15✔
65
    size_t n = object.size();
15✔
66
    if (n < 0x10) {
15✔
67
      writeByte(uint8_t(0x80 + n));
14✔
68
    } else if (n < 0x10000) {
1✔
69
      writeByte(0xDE);
1✔
70
      writeInteger(uint16_t(n));
1✔
71
    } else {
72
      writeByte(0xDF);
×
73
      writeInteger(uint32_t(n));
×
74
    }
75
    for (const VariantSlot* slot = object.head(); slot; slot = slot->next()) {
55✔
76
      visitString(slot->key());
40✔
77
      slot->data()->accept(*this);
40✔
78
    }
79
    return bytesWritten();
15✔
80
  }
81

82
  size_t visitString(const char* value) {
40✔
83
    return visitString(value, strlen(value));
40✔
84
  }
85

86
  size_t visitString(const char* value, size_t n) {
59✔
87
    ARDUINOJSON_ASSERT(value != NULL);
88

89
    if (n < 0x20) {
59✔
90
      writeByte(uint8_t(0xA0 + n));
55✔
91
    } else if (n < 0x100) {
4✔
92
      writeByte(0xD9);
1✔
93
      writeInteger(uint8_t(n));
1✔
94
    } else if (n < 0x10000) {
3✔
95
      writeByte(0xDA);
2✔
96
      writeInteger(uint16_t(n));
2✔
97
    } else {
98
      writeByte(0xDB);
1✔
99
      writeInteger(uint32_t(n));
1✔
100
    }
101
    writeBytes(reinterpret_cast<const uint8_t*>(value), n);
59✔
102
    return bytesWritten();
59✔
103
  }
104

105
  size_t visitRawString(const char* data, size_t size) {
4✔
106
    writeBytes(reinterpret_cast<const uint8_t*>(data), size);
4✔
107
    return bytesWritten();
4✔
108
  }
109

110
  size_t visitSignedInteger(JsonInteger value) {
58✔
111
    if (value > 0) {
58✔
112
      visitUnsignedInteger(static_cast<JsonUInt>(value));
39✔
113
    } else if (value >= -0x20) {
19✔
114
      writeInteger(int8_t(value));
8✔
115
    } else if (value >= -0x80) {
11✔
116
      writeByte(0xD0);
4✔
117
      writeInteger(int8_t(value));
4✔
118
    } else if (value >= -0x8000) {
7✔
119
      writeByte(0xD1);
4✔
120
      writeInteger(int16_t(value));
4✔
121
    }
122
#if ARDUINOJSON_USE_LONG_LONG
123
    else if (value >= -0x80000000LL)
3✔
124
#else
125
    else
126
#endif
127
    {
128
      writeByte(0xD2);
2✔
129
      writeInteger(int32_t(value));
2✔
130
    }
131
#if ARDUINOJSON_USE_LONG_LONG
132
    else {
133
      writeByte(0xD3);
1✔
134
      writeInteger(int64_t(value));
1✔
135
    }
136
#endif
137
    return bytesWritten();
58✔
138
  }
139

140
  size_t visitUnsignedInteger(JsonUInt value) {
54✔
141
    if (value <= 0x7F) {
54✔
142
      writeInteger(uint8_t(value));
39✔
143
    } else if (value <= 0xFF) {
15✔
144
      writeByte(0xCC);
5✔
145
      writeInteger(uint8_t(value));
5✔
146
    } else if (value <= 0xFFFF) {
10✔
147
      writeByte(0xCD);
4✔
148
      writeInteger(uint16_t(value));
4✔
149
    }
150
#if ARDUINOJSON_USE_LONG_LONG
151
    else if (value <= 0xFFFFFFFF)
6✔
152
#else
153
    else
154
#endif
155
    {
156
      writeByte(0xCE);
3✔
157
      writeInteger(uint32_t(value));
3✔
158
    }
159
#if ARDUINOJSON_USE_LONG_LONG
160
    else {
161
      writeByte(0xCF);
3✔
162
      writeInteger(uint64_t(value));
3✔
163
    }
164
#endif
165
    return bytesWritten();
54✔
166
  }
167

168
  size_t visitBoolean(bool value) {
2✔
169
    writeByte(value ? 0xC3 : 0xC2);
2✔
170
    return bytesWritten();
2✔
171
  }
172

173
  size_t visitNull() {
65,538✔
174
    writeByte(0xC0);
65,538✔
175
    return bytesWritten();
65,538✔
176
  }
177

178
 private:
179
  size_t bytesWritten() const {
65,742✔
180
    return writer_.count();
65,742✔
181
  }
182

183
  void writeByte(uint8_t c) {
65,652✔
184
    writer_.write(c);
65,652✔
185
  }
65,652✔
186

187
  void writeBytes(const uint8_t* p, size_t n) {
151✔
188
    writer_.write(p, n);
151✔
189
  }
151✔
190

191
  template <typename T>
192
  void writeInteger(T value) {
88✔
193
    fixEndianess(value);
88✔
194
    writeBytes(reinterpret_cast<uint8_t*>(&value), sizeof(value));
88✔
195
  }
88✔
196

197
  CountingDecorator<TWriter> writer_;
198
};
199

200
ARDUINOJSON_END_PRIVATE_NAMESPACE
201

202
ARDUINOJSON_BEGIN_PUBLIC_NAMESPACE
203

204
// Produces a MessagePack document.
205
// https://arduinojson.org/v6/api/msgpack/serializemsgpack/
206
template <typename TDestination>
207
inline size_t serializeMsgPack(JsonVariantConst source, TDestination& output) {
66✔
208
  using namespace ArduinoJson::detail;
209
  return serialize<MsgPackSerializer>(source, output);
66✔
210
}
211

212
// Produces a MessagePack document.
213
// https://arduinojson.org/v6/api/msgpack/serializemsgpack/
214
inline size_t serializeMsgPack(JsonVariantConst source, void* output,
2✔
215
                               size_t size) {
216
  using namespace ArduinoJson::detail;
217
  return serialize<MsgPackSerializer>(source, output, size);
2✔
218
}
219

220
// Computes the length of the document that serializeMsgPack() produces.
221
// https://arduinojson.org/v6/api/msgpack/measuremsgpack/
222
inline size_t measureMsgPack(JsonVariantConst source) {
1✔
223
  using namespace ArduinoJson::detail;
224
  return measure<MsgPackSerializer>(source);
1✔
225
}
226

227
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