-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
127 lines (99 loc) · 3.01 KB
/
main.cpp
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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include "MsgpackWrapper.h"
// int main(const int argc, const char *argv[]) {
// fprintf(stdout, "json to class object\n");
// return EXIT_SUCCESS;
// }
// #include <msgpack.h>
// #include <stdio.h>
void print(char const *buf, size_t len) {
size_t i = 0;
for (; i < len; ++i) printf("%02x ", 0xff & buf[i]);
printf("\n");
}
typedef enum { uAdmin, uViewer } TUserRole;
class UserDto : MsgpackObject {
public:
MsgpackValue<char *> Name;
MsgpackValue<uint32_t> Role;
UserDto(const char *name = {}, const TUserRole role = {})
: Name(this, 0, name), //
Role(this, 1, role){};
size_t WriteTo(char *outBuffer, size_t outBufferSize) {
msgpack_sbuffer sbuf;
msgpack_packer packer;
msgpack_sbuffer_init(&sbuf);
msgpack_packer_init(&packer, &sbuf, msgpack_sbuffer_write);
for (const auto &field : Fields) {
if (!field->Write(&packer)) {
msgpack_sbuffer_destroy(&sbuf);
return 0;
};
}
size_t size;
if (sbuf.size > outBufferSize) {
size = outBufferSize;
} else {
size = sbuf.size;
}
memcpy(outBuffer, sbuf.data, size);
msgpack_sbuffer_destroy(&sbuf);
return size;
}
msgpack_unpacker *BeginTryParse(const char *buffer, size_t length) {
if (buffer == NULL) { return NULL; }
auto unpacker = msgpack_unpacker_new(length);
if (unpacker == NULL) { return NULL; }
msgpack_unpacked unpacked;
msgpack_unpack_return ret;
msgpack_unpacked_init(&unpacked);
ret = msgpack_unpacker_next(unpacker, &unpacked);
switch (ret) {
case MSGPACK_UNPACK_SUCCESS:
if (TryParse(&unpacked.data)) { break; }
msgpack_unpacked_destroy(&unpacked);
EndTryParse(unpacker);
return NULL;
case MSGPACK_UNPACK_CONTINUE:
case MSGPACK_UNPACK_PARSE_ERROR:
default:
msgpack_unpacked_destroy(&unpacked);
EndTryParse(unpacker);
return NULL;
}
msgpack_unpacked_destroy(&unpacked);
return unpacker;
}
void EndTryParse(msgpack_unpacker *unpacker) { msgpack_unpacker_free(unpacker); }
};
int main(void) {
UserDto userDto;
auto unpacker = msgpack_unpacker_new(1234);
userDto.EndTryParse(unpacker);
msgpack_sbuffer sbuf;
msgpack_packer pk;
/* msgpack::sbuffer is a simple buffer implementation. */
msgpack_sbuffer_init(&sbuf);
/* serialize values into the buffer using msgpack_sbuffer_write callback function. */
msgpack_packer_init(&pk, &sbuf, msgpack_sbuffer_write);
msgpack_pack_array(&pk, 3);
msgpack_pack_int(&pk, 1);
msgpack_pack_true(&pk);
msgpack_pack_str(&pk, 7);
msgpack_pack_str_body(&pk, "example", 7);
print(sbuf.data, sbuf.size);
msgpack_zone mempool;
msgpack_object deserialized;
/* deserialize the buffer into msgpack_object instance. */
/* deserialized object is valid during the msgpack_zone instance alive. */
msgpack_zone_init(&mempool, 2048);
msgpack_unpack(sbuf.data, sbuf.size, NULL, &mempool, &deserialized);
/* print the deserialized object. */
msgpack_object_print(stdout, deserialized);
puts("");
msgpack_zone_destroy(&mempool);
msgpack_sbuffer_destroy(&sbuf);
return 0;
}