Newer
Older
/*
Copyright 2018-2020, Barcelona Supercomputing Center (BSC), Spain
Copyright 2015-2020, Johannes Gutenberg Universitaet Mainz, Germany
This software was partially supported by the
EC H2020 funded project NEXTGenIO (Project ID: 671951, www.nextgenio.eu).
This software was partially supported by the
ADA-FS project under the SPPEXA project funded by the DFG.
SPDX-License-Identifier: MIT
*/
#ifndef GKFS_IO_SERIALIZE_HPP
#define GKFS_IO_SERIALIZE_HPP
#include <nlohmann/json.hpp>
#include <reflection.hpp>
extern "C" {
#include <sys/stat.h>
}
serialize(const T& object) {
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
json j;
constexpr auto n = std::tuple_size<decltype(T::properties)>::value;
refl::for_sequence(std::make_index_sequence<n>{},
[&](auto i) {
constexpr auto p = std::get<i>(T::properties);
//j[p.name] = json {
// { "type" , p.type },
// { "value" , object.*(p.member) }
//};
j[p.name] = object.*(p.member);
}
);
return j;
}
namespace nlohmann {
// ADL specialization for pointers to complete types that
// we want serialized
template <typename T>
struct adl_serializer<T*> {
static void to_json(json& j, const T* opt) {
if (opt) {
j = *opt;
} else {
j = nullptr;
}
}
};
// ADL specialization for C strings
template <>
struct adl_serializer<const char*> {
static void to_json(json& j, const char* opt) {
if (opt) {
j = std::string{opt};
} else {
j = std::string{};
}
}
};
// base serializer for opaque pointers
template <typename T>
struct opaque_ptr_serializer {
static void to_json(json& j, const T opt) {
if (opt) {
j = reinterpret_cast<uint64_t>(opt);
} else {
j = nullptr;
}
}
};
// ADL specialization for opaque ::DIR* type
template <>
struct adl_serializer<::DIR*> : public opaque_ptr_serializer<::DIR*> {
using opaque_ptr_serializer<::DIR*>::to_json;
};
// ADL specialization for void* type
template <>
struct adl_serializer<void*> : public opaque_ptr_serializer<void*> {
using opaque_ptr_serializer<void*>::to_json;
};
// ADL specialization for struct ::timespec type
template <>
struct adl_serializer<struct ::timespec> {
static void to_json(json& j, const struct ::timespec opt) {
j = json {
{ "tv_sec", opt.tv_sec },
{ "tv_nsec", opt.tv_nsec }
};
}
};
// ADL specialization for struct ::statx_timestamp type
template <>
struct adl_serializer<struct ::statx_timestamp> {
static void to_json(json& j, const struct ::statx_timestamp opt) {
j = json {
{ "tv_sec", opt.tv_sec },
{ "tv_nsec", opt.tv_nsec }
};
}
};
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// ADL specialization for struct ::dirent type
template <>
struct adl_serializer<struct ::dirent> {
static void to_json(json& j, const struct ::dirent opt) {
j = json {
{ "d_ino", opt.d_ino },
{ "d_off", opt.d_off },
{ "d_reclen", opt.d_reclen },
{ "d_type", opt.d_type },
{ "d_name", opt.d_name },
};
}
};
//std::ostream&
//operator<<(std::ostream& os, const struct ::dirent& d) {
// return os << "hello there!\n";
//}
// ADL specialization for struct ::stat type
template <>
struct adl_serializer<struct ::stat> {
static void to_json(json& j, const struct ::stat opt) {
j = json {
{ "st_dev", opt.st_dev },
{ "st_ino", opt.st_ino },
{ "st_mode", opt.st_mode },
{ "st_nlink", opt.st_nlink },
{ "st_uid", opt.st_uid },
{ "st_gid", opt.st_gid },
{ "st_rdev", opt.st_rdev },
{ "st_size", opt.st_size },
{ "st_blksize", opt.st_blksize },
{ "st_blocks", opt.st_blocks },
{ "st_atim", opt.st_atim },
{ "st_mtim", opt.st_mtim },
{ "st_ctim", opt.st_ctim }
};
}
};
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// ADL specialization for struct ::statx type
template <>
struct adl_serializer<struct ::statx> {
static void to_json(json& j, const struct ::statx opt) {
j = json {
{ "stx_mask", opt.stx_mask },
{ "stx_blksize", opt.stx_blksize },
{ "stx_attributes", opt.stx_attributes},
{ "stx_nlink", opt.stx_nlink },
{ "stx_uid", opt.stx_uid },
{ "stx_gid", opt.stx_gid },
{ "stx_mode", opt.stx_mode },
{ "stx_ino", opt.stx_ino },
{ "stx_size", opt.stx_size },
{ "stx_blocks", opt.stx_blocks },
{ "stx_attributes_mask", opt.stx_attributes_mask},
{ "stx_atime", opt.stx_atime },
{ "stx_btime", opt.stx_btime },
{ "stx_ctime", opt.stx_ctime },
{ "stx_mtime", opt.stx_mtime },
{ "stx_rdev_major", opt.stx_rdev_major },
{ "stx_rdev_minor", opt.stx_rdev_minor },
{ "stx_dev_major", opt.stx_dev_major },
{ "stx_dev_minor", opt.stx_dev_minor }
};
}
};
} // namespace nlohmann
namespace fmt {
template <> struct formatter<struct ::dirent> {
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
constexpr auto parse(format_parse_context &ctx) {
// [ctx.begin(), ctx.end()) is a character range that contains a part of
// the format string starting from the format specifications to be parsed,
// e.g. in
//
// fmt::format("{:f} - point of interest", point{1, 2});
//
// the range will contain "f} - point of interest". The formatter should
// parse specifiers until '}' or the end of the range. In this example
// the formatter should parse the 'f' specifier and return an iterator
// pointing to '}'.
// Parse the presentation format and store it in the formatter:
auto it = ctx.begin(), end = ctx.end();
// Check if reached the end of the range:
if (it != end && *it != '}')
throw format_error("invalid format");
// Return an iterator past the end of the parsed range:
return it;
}
template <typename FormatContext>
auto format(const struct ::dirent &dirent, FormatContext &ctx) {
return format_to(ctx.out(),
"struct dirent {{\n"
" d_ino = {};\n"
" d_off = {};\n"
" d_reclen = {};\n"
" d_type = {};\n"
" d_name = {};\n"
"}}",
dirent.d_ino,
dirent.d_off,
dirent.d_reclen,
dirent.d_type,
dirent.d_name);
}
};
#endif // GKFS_IO_SERIALIZE_HPP