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

bblanchon / ArduinoJson / 3619649488

pending completion
3619649488

push

github

Benoit Blanchon
CI: use `ubuntu-20.04` for GCC

3373 of 3389 relevant lines covered (99.53%)

5926.06 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) {}
23

24
  template <typename T>
25
  typename enable_if<sizeof(T) == 4, size_t>::type visitFloat(T value32) {
26
    if (canConvertNumber<Integer>(value32)) {
27
      Integer truncatedValue = Integer(value32);
28
      if (value32 == T(truncatedValue))
29
        return visitSignedInteger(truncatedValue);
30
    }
31
    writeByte(0xCA);
32
    writeInteger(value32);
33
    return bytesWritten();
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) {
39
    float value32 = float(value64);
40
    if (value32 == value64)
41
      return visitFloat(value32);
42
    writeByte(0xCB);
43
    writeInteger(value64);
44
    return bytesWritten();
45
  }
46

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

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

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

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

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

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

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

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

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

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

178
 private:
179
  size_t bytesWritten() const {
180
    return _writer.count();
181
  }
182

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

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

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

197
  CountingDecorator<TWriter> _writer;
198
};
199

200
template <typename TDestination>
201
inline size_t serializeMsgPack(VariantConstRef source, TDestination& output) {
202
  return serialize<MsgPackSerializer>(source, output);
203
}
204

205
inline size_t serializeMsgPack(VariantConstRef source, void* output,
206
                               size_t size) {
207
  return serialize<MsgPackSerializer>(source, output, size);
208
}
209

210
inline size_t measureMsgPack(VariantConstRef source) {
211
  return measure<MsgPackSerializer>(source);
212
}
213

214
}  // 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