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

bblanchon / ArduinoJson / 4017712708

pending completion
4017712708

push

github

Benoit Blanchon
Deduce template argument of `pgm_read()`

15 of 15 new or added lines in 3 files covered. (100.0%)

3370 of 3386 relevant lines covered (99.53%)

6017.47 hits per line

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

98.17
/src/ArduinoJson/MsgPack/MsgPackSerializer.hpp
1
// ArduinoJson - https://arduinojson.org
2
// Copyright © 2014-2022, 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
namespace ARDUINOJSON_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) {}
65✔
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 visitRawJson(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) {
57✔
111
    if (value > 0) {
57✔
112
      visitUnsignedInteger(static_cast<JsonUInt>(value));
39✔
113
    } else if (value >= -0x20) {
18✔
114
      writeInteger(int8_t(value));
8✔
115
    } else if (value >= -0x80) {
10✔
116
      writeByte(0xD0);
4✔
117
      writeInteger(int8_t(value));
4✔
118
    } else if (value >= -0x8000) {
6✔
119
      writeByte(0xD1);
4✔
120
      writeInteger(int16_t(value));
4✔
121
    }
122
#if ARDUINOJSON_USE_LONG_LONG
123
    else if (value >= -0x80000000LL)
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);
134
      writeInteger(int64_t(value));
135
    }
136
#endif
137
    return bytesWritten();
57✔
138
  }
139

140
  size_t visitUnsignedInteger(JsonUInt value) {
51✔
141
    if (value <= 0x7F) {
51✔
142
      writeInteger(uint8_t(value));
39✔
143
    } else if (value <= 0xFF) {
12✔
144
      writeByte(0xCC);
5✔
145
      writeInteger(uint8_t(value));
5✔
146
    } else if (value <= 0xFFFF) {
7✔
147
      writeByte(0xCD);
4✔
148
      writeInteger(uint16_t(value));
4✔
149
    }
150
#if ARDUINOJSON_USE_LONG_LONG
151
    else if (value <= 0xFFFFFFFF)
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);
162
      writeInteger(uint64_t(value));
163
    }
164
#endif
165
    return bytesWritten();
51✔
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,738✔
180
    return _writer.count();
65,738✔
181
  }
182

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

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

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

197
  CountingDecorator<TWriter> _writer;
198
};
199

200
// Produces a MessagePack document.
201
// https://arduinojson.org/v6/api/msgpack/serializemsgpack/
202
template <typename TDestination>
203
inline size_t serializeMsgPack(JsonVariantConst source, TDestination& output) {
62✔
204
  return serialize<MsgPackSerializer>(source, output);
62✔
205
}
206

207
// Produces a MessagePack document.
208
// https://arduinojson.org/v6/api/msgpack/serializemsgpack/
209
inline size_t serializeMsgPack(JsonVariantConst source, void* output,
2✔
210
                               size_t size) {
211
  return serialize<MsgPackSerializer>(source, output, size);
2✔
212
}
213

214
// Computes the length of the document that serializeMsgPack() produces.
215
// https://arduinojson.org/v6/api/msgpack/measuremsgpack/
216
inline size_t measureMsgPack(JsonVariantConst source) {
1✔
217
  return measure<MsgPackSerializer>(source);
1✔
218
}
219

220
}  // namespace ARDUINOJSON_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