1 | /** |
---|
2 | * Author : David Saggiorato |
---|
3 | * |
---|
4 | * Copyright 2008-2009 GeoLabs SARL. All rights reserved. |
---|
5 | * |
---|
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
7 | * of this software and associated documentation files (the "Software"), to deal |
---|
8 | * in the Software without restriction, including without limitation the rights |
---|
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
---|
10 | * copies of the Software, and to permit persons to whom the Software is |
---|
11 | * furnished to do so, subject to the following conditions: |
---|
12 | * |
---|
13 | * The above copyright notice and this permission notice shall be included in |
---|
14 | * all copies or substantial portions of the Software. |
---|
15 | * |
---|
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
---|
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
---|
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
---|
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
---|
22 | * THE SOFTWARE. |
---|
23 | */ |
---|
24 | |
---|
25 | |
---|
26 | #include <string.h> |
---|
27 | #include <stdio.h> |
---|
28 | #include <stdlib.h> |
---|
29 | #include <sys/stat.h> |
---|
30 | #include "service.h" |
---|
31 | #include "service_internal.h" |
---|
32 | #include <json/json.h> |
---|
33 | |
---|
34 | |
---|
35 | |
---|
36 | void maptojson(json_object ** obj,map * m){ |
---|
37 | json_object *jobj = json_object_new_object(); |
---|
38 | map *tmp = m; |
---|
39 | while(tmp != NULL){ |
---|
40 | json_object_object_add(jobj,tmp->name,json_object_new_string(tmp->value)); |
---|
41 | tmp = tmp->next; |
---|
42 | } |
---|
43 | *obj = jobj; |
---|
44 | } |
---|
45 | |
---|
46 | void mapstojson(json_object ** obj,maps * m){ |
---|
47 | json_object *jobj = json_object_new_object(); |
---|
48 | maps *tmp = m; |
---|
49 | while(tmp!=NULL){ |
---|
50 | json_object *map_obj; |
---|
51 | maptojson(&map_obj,tmp->content); |
---|
52 | json_object_object_add(jobj,tmp->name,map_obj); |
---|
53 | tmp = tmp->next; |
---|
54 | } |
---|
55 | *obj = jobj; |
---|
56 | } |
---|
57 | |
---|
58 | |
---|
59 | map* jsontomap(json_object * jobj){ |
---|
60 | map *m = (map *) malloc (MAP_SIZE); |
---|
61 | int i = 0; |
---|
62 | json_object_object_foreach(jobj, key, val){ |
---|
63 | if (json_object_is_type(val, json_type_string)){ |
---|
64 | if (i==0){ |
---|
65 | m->name = zStrdup(key); |
---|
66 | m->value = zStrdup(json_object_get_string(val)); |
---|
67 | m->next = NULL; |
---|
68 | i = 1; |
---|
69 | } |
---|
70 | else { |
---|
71 | map *tmp=createMap(key,json_object_get_string(val)); |
---|
72 | addMapToMap(&m,tmp); |
---|
73 | freeMap(&tmp); |
---|
74 | free(tmp); |
---|
75 | } |
---|
76 | } |
---|
77 | else { |
---|
78 | fprintf(stderr,"jsontomap not map :%s \n",key); |
---|
79 | /* Attention ce n'est pas une map */ |
---|
80 | return NULL; |
---|
81 | } |
---|
82 | } |
---|
83 | return m; |
---|
84 | } |
---|
85 | |
---|
86 | maps * jsontomaps(json_object * jobj){ |
---|
87 | maps *mm = (maps *) malloc (MAPS_SIZE); |
---|
88 | int i =0; |
---|
89 | json_object_object_foreach(jobj, key, val){ |
---|
90 | if (i ==0){ |
---|
91 | mm->content = jsontomap(val); |
---|
92 | mm->name = zStrdup(key); |
---|
93 | mm->next = NULL; |
---|
94 | i = 1; |
---|
95 | } |
---|
96 | else { |
---|
97 | maps *maps_tmp; |
---|
98 | maps_tmp = (maps *) malloc (MAPS_SIZE); |
---|
99 | maps_tmp->content = jsontomap(val); |
---|
100 | maps_tmp->name = zStrdup(key); |
---|
101 | maps_tmp->next = NULL; |
---|
102 | addMapsToMaps(&mm,maps_tmp); |
---|
103 | freeMaps(&maps_tmp); |
---|
104 | free(maps_tmp); |
---|
105 | maps_tmp = NULL; |
---|
106 | } |
---|
107 | } |
---|
108 | return mm; |
---|
109 | } |
---|
110 | |
---|
111 | |
---|