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

bblanchon / ArduinoJson / 8738040343

18 Apr 2024 12:39PM CUT coverage: 99.589%. Remained the same
8738040343

push

github

bblanchon
Fix error "pasting X and Y does not give a valid preprocessing token"

3639 of 3654 relevant lines covered (99.59%)

13017.28 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)
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

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

70
    return bytesWritten();
4✔
71
  }
72

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

85
    auto slotId = object.head();
15✔
86
    while (slotId != NULL_SLOT) {
55✔
87
      auto slot = resources_->getSlot(slotId);
40✔
88
      visit(slot->key());
40✔
89
      slot->data()->accept(*this);
40✔
90
      slotId = slot->next();
40✔
91
    }
92

93
    return bytesWritten();
15✔
94
  }
95

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

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

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

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

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

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

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

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

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

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

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

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

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

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

217
ARDUINOJSON_END_PRIVATE_NAMESPACE
218

219
ARDUINOJSON_BEGIN_PUBLIC_NAMESPACE
220

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