forked from trustwallet/wallet-core
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSerialization.h
More file actions
83 lines (66 loc) · 1.95 KB
/
Copy pathSerialization.h
File metadata and controls
83 lines (66 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#pragma once
#include <nlohmann/json.hpp>
#include "../Data.h"
#include "../BinaryCoding.h"
#include <vector>
#include <set>
#include <string>
namespace TW::Bravo {
inline void encodeVarInt64(uint64_t x, Data& os) {
// 64-bit int would take at most 10 bytes as a varint
static const int maxBytes = 10;
uint8_t bytes[maxBytes];
int lastNonZeroByte = 0;
for (int i = 0; i < maxBytes; ++i) {
bytes[i] = (x & 0x7F);
if (bytes[i]) {
lastNonZeroByte = i;
}
// set the first bit
bytes[i] |= 0x80;
x >>= 7;
}
// unset the first bit of the last byte
bytes[lastNonZeroByte] &= 0x7F;
os.insert(os.end(), bytes, bytes + lastNonZeroByte + 1);
}
inline void encodeVarInt32(uint32_t x, Data& os) {
encodeVarInt64(static_cast<uint64_t>(x), os);
}
inline void encodeString(const std::string& s, Data& os) {
size_t size = s.size();
encodeVarInt64(size, os);
os.insert(os.end(), s.data(), s.data() + size);
}
template<typename Collection>
inline void encodeCollection(const Collection& collection, Data& os) {
encodeVarInt64(std::size(collection), os);
for (const auto& item : collection) {
item.serialize(os);
}
}
template<typename Collection>
inline void encodePointerCollection(const Collection& collection, Data& os) {
encodeVarInt64(std::size(collection), os);
for (const auto& item : collection) {
item->serialize(os);
}
}
using json = nlohmann::json;
template<typename Collection>
inline json encodeCollection(const Collection& collection) {
json array = json::array();
for (const auto& item : collection) {
array.push_back(item.serialize());
}
return array;
}
template<typename Collection>
inline json encodePointerCollection(const Collection& collection) {
json array = json::array();
for (const auto& item : collection) {
array.push_back(item->serialize());
}
return array;
}
} // namespace