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

bblanchon / ArduinoJson / 18653349747

20 Oct 2025 12:56PM UTC coverage: 99.34% (-0.08%) from 99.417%
18653349747

push

github

bblanchon
Return `bool` from all built-in `toJson` converters

104 of 108 new or added lines in 8 files covered. (96.3%)

3 existing lines in 2 files now uncovered.

3916 of 3942 relevant lines covered (99.34%)

10349.1 hits per line

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

98.7
/src/ArduinoJson/MsgPack/MsgPackExtension.hpp
1
#pragma once
2

3
#include <ArduinoJson/Variant/Converter.hpp>
4

5
ARDUINOJSON_BEGIN_PUBLIC_NAMESPACE
6

7
class MsgPackExtension {
8
 public:
9
  MsgPackExtension() : data_(nullptr), size_(0), type_(0) {}
13✔
10
  explicit MsgPackExtension(int8_t type, const void* data, size_t size)
37✔
11
      : data_(data), size_(size), type_(type) {}
37✔
12

13
  int8_t type() const {
25✔
14
    return type_;
25✔
15
  }
16

17
  const void* data() const {
65✔
18
    return data_;
65✔
19
  }
20

21
  size_t size() const {
152✔
22
    return size_;
152✔
23
  }
24

25
 private:
26
  const void* data_;
27
  size_t size_;
28
  int8_t type_;
29
};
30

31
template <>
32
struct Converter<MsgPackExtension> : private detail::VariantAttorney {
33
  static bool toJson(MsgPackExtension src, JsonVariant dst) {
19✔
34
    auto data = getData(dst);
19✔
35
    if (!data)
19✔
36
      return false;
1✔
37

38
    auto resources = getResourceManager(dst);
18✔
39
    detail::VariantImpl::clear(data, resources);
18✔
40

41
    if (!src.data())
18✔
NEW
42
      return true;
×
43

44
    uint8_t format, sizeBytes;
45
    if (src.size() >= 0x10000) {
18✔
46
      format = 0xc9;  // ext 32
1✔
47
      sizeBytes = 4;
1✔
48
    } else if (src.size() >= 0x100) {
17✔
49
      format = 0xc8;  // ext 16
4✔
50
      sizeBytes = 2;
4✔
51
    } else if (src.size() == 16) {
13✔
52
      format = 0xd8;  // fixext 16
1✔
53
      sizeBytes = 0;
1✔
54
    } else if (src.size() == 8) {
12✔
55
      format = 0xd7;  // fixext 8
1✔
56
      sizeBytes = 0;
1✔
57
    } else if (src.size() == 4) {
11✔
58
      format = 0xd6;  // fixext 4
1✔
59
      sizeBytes = 0;
1✔
60
    } else if (src.size() == 2) {
10✔
61
      format = 0xd5;  // fixext 2
1✔
62
      sizeBytes = 0;
1✔
63
    } else if (src.size() == 1) {
9✔
64
      format = 0xd4;  // fixext 1
1✔
65
      sizeBytes = 0;
1✔
66
    } else {
67
      format = 0xc7;  // ext 8
8✔
68
      sizeBytes = 1;
8✔
69
    }
70

71
    auto str = resources->createString(src.size() + 2 + sizeBytes);
18✔
72
    if (!str)
18✔
73
      return false;
2✔
74

75
    resources->saveString(str);
16✔
76
    auto ptr = reinterpret_cast<uint8_t*>(str->data);
16✔
77
    *ptr++ = uint8_t(format);
16✔
78
    for (uint8_t i = 0; i < sizeBytes; i++)
33✔
79
      *ptr++ = uint8_t(src.size() >> (sizeBytes - i - 1) * 8 & 0xff);
17✔
80
    *ptr++ = uint8_t(src.type());
16✔
81
    memcpy(ptr, src.data(), src.size());
16✔
82
    data->setRawString(str);
16✔
83
    return true;
16✔
84
  }
85

86
  static MsgPackExtension fromJson(JsonVariantConst src) {
31✔
87
    auto data = getData(src);
31✔
88
    if (!data)
31✔
89
      return {};
2✔
90
    auto rawstr = data->asRawString();
29✔
91
    if (rawstr.size() == 0)
29✔
92
      return {};
10✔
93
    auto p = reinterpret_cast<const uint8_t*>(rawstr.c_str());
19✔
94

95
    size_t payloadSize = 0;
19✔
96
    uint8_t headerSize = 0;
19✔
97

98
    const uint8_t& code = p[0];
19✔
99

100
    if (code >= 0xd4 && code <= 0xd8) {  // fixext 1
19✔
101
      headerSize = 2;
10✔
102
      payloadSize = size_t(1) << (code - 0xd4);
10✔
103
    }
104

105
    if (code >= 0xc7 && code <= 0xc9) {
19✔
106
      uint8_t sizeBytes = uint8_t(1 << (code - 0xc7));
8✔
107
      for (uint8_t i = 0; i < sizeBytes; i++)
30✔
108
        payloadSize = (payloadSize << 8) | p[1 + i];
22✔
109
      headerSize = uint8_t(2 + sizeBytes);
8✔
110
    }
111

112
    if (rawstr.size() == headerSize + payloadSize)
19✔
113
      return MsgPackExtension(int8_t(p[headerSize - 1]), p + headerSize,
18✔
114
                              payloadSize);
18✔
115

116
    return {};
1✔
117
  }
118

119
  static bool checkJson(JsonVariantConst src) {
9✔
120
    return fromJson(src).data() != nullptr;
9✔
121
  }
122
};
123

124
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