1 | #include <string.h> |
---|
2 | #include <stdio.h> |
---|
3 | #include <stdlib.h> |
---|
4 | #include <sys/stat.h> |
---|
5 | #include "service.h" |
---|
6 | #include "service_internal.h" |
---|
7 | #include <json/json.h> |
---|
8 | |
---|
9 | |
---|
10 | |
---|
11 | void maptojson(json_object ** obj,map * m){ |
---|
12 | json_object *jobj = json_object_new_object(); |
---|
13 | map *tmp = m; |
---|
14 | while(tmp != NULL){ |
---|
15 | json_object_object_add(jobj,tmp->name,json_object_new_string(tmp->value)); |
---|
16 | tmp = tmp->next; |
---|
17 | } |
---|
18 | *obj = jobj; |
---|
19 | } |
---|
20 | |
---|
21 | void mapstojson(json_object ** obj,maps * m){ |
---|
22 | json_object *jobj = json_object_new_object(); |
---|
23 | maps *tmp = m; |
---|
24 | while(tmp!=NULL){ |
---|
25 | json_object *map_obj; |
---|
26 | maptojson(&map_obj,tmp->content); |
---|
27 | json_object_object_add(jobj,tmp->name,map_obj); |
---|
28 | tmp = tmp->next; |
---|
29 | } |
---|
30 | *obj = jobj; |
---|
31 | } |
---|
32 | |
---|
33 | |
---|
34 | map* jsontomap(json_object * jobj){ |
---|
35 | map *m = (map *) malloc (MAP_SIZE); |
---|
36 | int i = 0; |
---|
37 | json_object_object_foreach(jobj, key, val){ |
---|
38 | if (json_object_is_type(val, json_type_string)){ |
---|
39 | if (i==0){ |
---|
40 | m->name = zStrdup(key); |
---|
41 | m->value = zStrdup(json_object_get_string(val)); |
---|
42 | m->next = NULL; |
---|
43 | i = 1; |
---|
44 | } |
---|
45 | else { |
---|
46 | map *tmp=createMap(key,json_object_get_string(val)); |
---|
47 | addMapToMap(&m,tmp); |
---|
48 | freeMap(&tmp); |
---|
49 | free(tmp); |
---|
50 | } |
---|
51 | } |
---|
52 | else { |
---|
53 | fprintf(stderr,"jsontomap not map :%s \n",key); |
---|
54 | /* Attention ce n'est pas une map */ |
---|
55 | return NULL; |
---|
56 | } |
---|
57 | } |
---|
58 | return m; |
---|
59 | } |
---|
60 | |
---|
61 | maps * jsontomaps(json_object * jobj){ |
---|
62 | maps *mm = (maps *) malloc (MAPS_SIZE); |
---|
63 | int i =0; |
---|
64 | json_object_object_foreach(jobj, key, val){ |
---|
65 | if (i ==0){ |
---|
66 | mm->content = jsontomap(val); |
---|
67 | mm->name = zStrdup(key); |
---|
68 | mm->next = NULL; |
---|
69 | i = 1; |
---|
70 | } |
---|
71 | else { |
---|
72 | maps *maps_tmp; |
---|
73 | maps_tmp = (maps *) malloc (MAPS_SIZE); |
---|
74 | maps_tmp->content = jsontomap(val); |
---|
75 | maps_tmp->name = zStrdup(key); |
---|
76 | maps_tmp->next = NULL; |
---|
77 | addMapsToMaps(&mm,maps_tmp); |
---|
78 | freeMaps(&maps_tmp); |
---|
79 | free(maps_tmp); |
---|
80 | maps_tmp = NULL; |
---|
81 | } |
---|
82 | } |
---|
83 | return mm; |
---|
84 | } |
---|
85 | |
---|
86 | |
---|