forked from armink/struct2json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
122 lines (105 loc) · 4.09 KB
/
main.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
/*
* This file is part of the struct2json Library.
*
* Copyright (c) 2015, Armink, <[email protected]>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* 'Software'), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* Function: demo for this library.
* Created on: 2015-10-14
*/
#include "s2j.h"
#include <stdint.h>
#include <stdio.h>
typedef struct {
char name[16];
} Hometown;
typedef struct {
uint8_t id;
double weight;
uint8_t score[8];
char name[10];
Hometown hometown;
} Student;
/**
* Student JSON object to structure object
*
* @param json_obj JSON object
*
* @return structure object
*/
static void *json_to_struct(cJSON* json_obj) {
/* create Student structure object */
s2j_create_struct_obj(struct_student, Student);
/* deserialize data to Student structure object. */
s2j_struct_get_basic_element(struct_student, json_obj, int, id);
s2j_struct_get_array_element(struct_student, json_obj, int, score);
s2j_struct_get_basic_element(struct_student, json_obj, string, name);
s2j_struct_get_basic_element(struct_student, json_obj, double, weight);
/* deserialize data to Student.Hometown structure object. */
s2j_struct_get_struct_element(struct_hometown, struct_student, json_hometown, json_obj, Hometown, hometown);
s2j_struct_get_basic_element(struct_hometown, json_hometown, string, name);
/* return Student structure object pointer */
return struct_student;
}
/**
* Student structure object to JSON object
*
* @param struct_obj structure object
*
* @param JSON object
*/
static cJSON *struct_to_json(void* struct_obj) {
Student *struct_student = (Student *)struct_obj;
/* create Student JSON object */
s2j_create_json_obj(json_student);
/* serialize data to Student JSON object. */
s2j_json_set_basic_element(json_student, struct_student, int, id);
s2j_json_set_basic_element(json_student, struct_student, double, weight);
s2j_json_set_array_element(json_student, struct_student, int, score, 8);
s2j_json_set_basic_element(json_student, struct_student, string, name);
/* serialize data to Student.Hometown JSON object. */
s2j_json_set_struct_element(json_hometown, json_student, struct_hometown, struct_student, Hometown, hometown);
s2j_json_set_basic_element(json_hometown, struct_hometown, string, name);
/* return Student JSON object pointer */
return json_student;
}
int main(void) {
static Student orignal_student_obj = {
.id = 24,
.weight = 71.2,
.score = {1, 2, 3, 4, 5, 6, 7, 8},
.name = "armink",
.hometown.name = "China",
};
/* serialize Student structure object */
cJSON *json_student = struct_to_json(&orignal_student_obj);
/* deserialize Student structure object */
Student *converted_student_obj = json_to_struct(json_student);
/* compare Student structure object */
if(memcmp(&orignal_student_obj, converted_student_obj, sizeof(Student))) {
printf("Converted failed!\n");
} else {
printf("Converted OK!\n");
}
s2j_delete_json_obj(json_student);
s2j_delete_struct_obj(converted_student_obj);
return 0;
}