-
Notifications
You must be signed in to change notification settings - Fork 0
/
decodeTest.c
172 lines (140 loc) · 4.75 KB
/
decodeTest.c
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
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
170
171
172
#include "decoders.h"
#include "stdio.h"
#include <stdlib.h>
char *pointStr =
"{\n\
\"x\": 69,\n\
\"y\": 420,\n\
}";
char *personStr = "{\n \"firstName\": \"Walter\",\n \"lastName\" : \"White\",\n \"age\": 52\n}";
char *numbersStr = "[1, 2, 3, 4, 5]";
char *pointListStr = "[{ \"x\": 19, \"y\": 95 }, { \"x\": 4, \"y\": 20 }, { \"x\": 18, \"y\": 99 }]";
char *familyStr = "{\"father\":{\"firstName\":\"Walter\",\"lastName\":\"White\",\"age\":52},\"mother\":{\"firstName\":\"Skyler\",\"lastName\":\"White\",\"age\":40},\"children\":[{\"firstName\":\"Walter Jr.\",\"lastName\":\"White\",\"age\":17},{\"firstName\":\"Holly\",\"lastName\":\"White\",\"age\":1}]}";
char *familyStrWrong = "{\"father\":{\"firstName\":\"Walter\",\"lastName\":\"White\",\"age\":52},\"mother\":{\"firstName\":\"Skyler\",\"lastName\":\"White\",\"age\":40},\"children\":[{\"firstName\":\"Walter Jr.\",\"lastName\":\"White\",\"age\":17},{\"firstName\":\"Holly\",\"lastName\":\"White\",\"age\": \"hello\"}]}";
typedef struct Point {
int x;
int y;
} Point;
void printPoint(Point point) {
printf("Point { .x = %d, .y = %d }\n", point.x, point.y);
}
bool decodePoint(DecoderState *state, void *dest) {
Point *point = (Point*)dest;
return decodeFields(state, 2,
makeField("x", &point->x, decodeInt),
makeField("y", &point->y, decodeInt)
);
}
typedef struct NumberList {
int *numbers;
int length;
} NumberList;
bool decodeNumberList(DecoderState *state, void *dest) {
NumberList *numberList = (NumberList*)dest;
return decodeList(state, &numberList->numbers, &numberList->length, sizeof(int), decodeInt);
}
void printNumberList(NumberList list) {
for (int i = 0; i < list.length; i++) {
printf("%d\n", list.numbers[i]);
}
}
typedef struct PointList {
Point *points;
int len;
} PointList;
bool decodePointList(DecoderState *state, void *dest) {
PointList *pointList = (PointList*)dest;
return decodeList(state, &pointList->points, &pointList->len, sizeof(Point), decodePoint);
}
typedef struct Person {
char *firstName;
char *lastName;
int age;
} Person;
void printPerson(Person person) {
printf("%s %s, %d\n", person.firstName, person.lastName, person.age);
}
typedef struct Family {
Person father;
Person mother;
Person *children;
int childCount;
} Family;
bool decodePerson(DecoderState *state, void *dest) {
Person *person = (Person*)dest;
return decodeFields(state, 3,
makeField("firstName", &person->firstName, decodeString),
makeField("lastName", &person->lastName, decodeString),
makeField("age", &person->age, decodeInt)
);
}
bool decodeFamily(DecoderState *state, void *dest) {
Family *family = (Family*)dest;
return decodeFields(state, 3,
makeField("father", &family->father, decodePerson),
makeField("mother", &family->mother, decodePerson),
makeListField("children", &family->children, &family->childCount, sizeof(Person), decodePerson)
);
}
int main() {
Point decodedPoint;
DecodeResult pointRes = decode(pointStr, &decodedPoint, decodePoint);
printf("Decoded point: \n");
printf("----------------------------\n");
if (pointRes.success) {
printPoint(decodedPoint);
printf("\n");
} else {
printDecoderError(pointRes.error);
DecodeError_free(pointRes.error);
}
// ------------------
Person decodedPerson;
decode(personStr, &decodedPerson, decodePerson);
printf("Decoded person: \n");
printf("----------------------------\n");
printPerson(decodedPerson);
printf("\n");
// --------------
NumberList numberList;
decode(numbersStr, &numberList, decodeNumberList);
printf("Decoded list: \n");
printf("----------------------------\n");
printNumberList(numberList);
printf("\n");
// --------------
PointList pointList;
decode(pointListStr, &pointList, decodePointList);
printf("Decoded list of points: \n");
printf("----------------------------\n");
for (int i = 0; i < pointList.len; i++) {
printPoint(pointList.points[i]);
}
printf("\n");
// --------------
Family family;
DecodeResult res = decode(familyStr, &family, decodeFamily);
if(!res.success) {
printDecoderError(res.error);
DecodeError_free(res.error);
}
printf("Decoded family: \n");
printf("----------------------------\n");
printf("Father: "); printPerson(family.father);
printf("Mother: "); printPerson(family.mother);
printf("Children: \n");
for (int i = 0; i < family.childCount; i++) {
printf(" "); printPerson(family.children[i]);
}
printf("\n");
// --------------
printf("Error message example: \n");
printf("----------------------------\n");
Family wrongFam;
DecodeResult wrongFamRes = decode(familyStrWrong, &wrongFam, decodeFamily);
if (!wrongFamRes.success) {
printDecoderError(wrongFamRes.error);
DecodeError_free(wrongFamRes.error);
}
return 1;
}