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

bblanchon / ArduinoJson / 16354972779

17 Jul 2025 08:11PM UTC coverage: 99.215% (-0.05%) from 99.267%
16354972779

push

github

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

116 of 120 new or added lines in 8 files covered. (96.67%)

3919 of 3950 relevant lines covered (99.22%)

11026.5 hits per line

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

98.36
/src/ArduinoJson/MsgPack/MsgPackBinary.hpp
1
#pragma once
2

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

5
ARDUINOJSON_BEGIN_PUBLIC_NAMESPACE
6

7
class MsgPackBinary {
8
 public:
9
  MsgPackBinary() : data_(nullptr), size_(0) {}
13✔
10
  explicit MsgPackBinary(const void* c, size_t size) : data_(c), size_(size) {}
21✔
11

12
  const void* data() const {
47✔
13
    return data_;
47✔
14
  }
15

16
  size_t size() const {
78✔
17
    return size_;
78✔
18
  }
19

20
 private:
21
  const void* data_;
22
  size_t size_;
23
};
24

25
template <>
26
struct Converter<MsgPackBinary> : private detail::VariantAttorney {
27
  static bool toJson(MsgPackBinary src, JsonVariant dst) {
15✔
28
    auto impl = getImpl(dst);
15✔
29
    if (impl.isUnbound())
15✔
30
      return false;
1✔
31

32
    if (!src.data())
14✔
NEW
33
      return true;
×
34

35
    size_t headerSize = src.size() >= 0x10000 ? 5 : src.size() >= 0x100 ? 3 : 2;
14✔
36

37
    auto resources = impl.resources();
14✔
38
    auto str = resources->createString(src.size() + headerSize);
14✔
39
    if (!str)
14✔
40
      return false;
2✔
41
    resources->saveString(str);
12✔
42

43
    auto ptr = reinterpret_cast<uint8_t*>(str->data);
12✔
44
    switch (headerSize) {
12✔
45
      case 2:
8✔
46
        ptr[0] = uint8_t(0xc4);
8✔
47
        ptr[1] = uint8_t(src.size() & 0xff);
8✔
48
        break;
8✔
49
      case 3:
2✔
50
        ptr[0] = uint8_t(0xc5);
2✔
51
        ptr[1] = uint8_t(src.size() >> 8 & 0xff);
2✔
52
        ptr[2] = uint8_t(src.size() & 0xff);
2✔
53
        break;
2✔
54
      case 5:
2✔
55
        ptr[0] = uint8_t(0xc6);
2✔
56
        ptr[1] = uint8_t(src.size() >> 24 & 0xff);
2✔
57
        ptr[2] = uint8_t(src.size() >> 16 & 0xff);
2✔
58
        ptr[3] = uint8_t(src.size() >> 8 & 0xff);
2✔
59
        ptr[4] = uint8_t(src.size() & 0xff);
2✔
60
        break;
2✔
61
      default:
12✔
62
        ARDUINOJSON_ASSERT(false);
63
    }
64
    memcpy(ptr + headerSize, src.data(), src.size());
12✔
65
    impl.data()->setRawString(str);
12✔
66
    return true;
12✔
67
  }
68

69
  static MsgPackBinary fromJson(JsonVariantConst src) {
19✔
70
    auto variant = getImpl(src);
19✔
71
    auto rawstr = variant.asRawString();
19✔
72
    auto p = reinterpret_cast<const uint8_t*>(rawstr.c_str());
19✔
73
    auto n = rawstr.size();
19✔
74
    if (n >= 2 && p[0] == 0xc4) {  // bin 8
19✔
75
      size_t size = p[1];
2✔
76
      if (size + 2 == n)
2✔
77
        return MsgPackBinary(p + 2, size);
2✔
78
    } else if (n >= 3 && p[0] == 0xc5) {  // bin 16
17✔
79
      size_t size = size_t(p[1] << 8) | p[2];
2✔
80
      if (size + 3 == n)
2✔
81
        return MsgPackBinary(p + 3, size);
2✔
82
    } else if (n >= 5 && p[0] == 0xc6) {  // bin 32
15✔
83
      size_t size =
2✔
84
          size_t(p[1] << 24) | size_t(p[2] << 16) | size_t(p[3] << 8) | p[4];
2✔
85
      if (size + 5 == n)
2✔
86
        return MsgPackBinary(p + 5, size);
2✔
87
    }
88
    return {};
13✔
89
  }
90

91
  static bool checkJson(JsonVariantConst src) {
3✔
92
    return fromJson(src).data() != nullptr;
3✔
93
  }
94
};
95

96
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

© 2026 Coveralls, Inc