1 | /* |
---|
2 | * Author : Gérald FENOY |
---|
3 | * |
---|
4 | * Copyright 2017 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 | #include "service_json.h" |
---|
26 | |
---|
27 | #ifdef __cplusplus |
---|
28 | extern "C" { |
---|
29 | #endif |
---|
30 | |
---|
31 | json_object* mapToJson(map* myMap){ |
---|
32 | json_object *res=json_object_new_object(); |
---|
33 | map* cursor=myMap; |
---|
34 | map* sizeMap=getMap(myMap,"size"); |
---|
35 | map* length=getMap(myMap,"length"); |
---|
36 | while(cursor!=NULL){ |
---|
37 | json_object *val=NULL; |
---|
38 | if(length==NULL && sizeMap==NULL) |
---|
39 | val=json_object_new_string(cursor->value); |
---|
40 | else{ |
---|
41 | if(length==NULL && sizeMap!=NULL){ |
---|
42 | if(strncasecmp(cursor->name,"value",5)==0) |
---|
43 | val=json_object_new_string_len(cursor->value,atoi(sizeMap->value)); |
---|
44 | else |
---|
45 | val=json_object_new_string(cursor->value); |
---|
46 | } |
---|
47 | else{ |
---|
48 | // In other case we should consider array with potential sizes |
---|
49 | int limit=atoi(length->value); |
---|
50 | int i=0; |
---|
51 | val=json_object_new_array(); |
---|
52 | for(i=0;i<limit;i++){ |
---|
53 | map* lsizeMap=getMapArray(myMap,"size",i); |
---|
54 | if(lsizeMap!=NULL && strncasecmp(cursor->name,"value",5)==0){ |
---|
55 | json_object_array_add(val,json_object_new_string_len(cursor->value,atoi(sizeMap->value))); |
---|
56 | }else{ |
---|
57 | json_object_array_add(val,json_object_new_string(cursor->value)); |
---|
58 | } |
---|
59 | } |
---|
60 | } |
---|
61 | } |
---|
62 | if(val!=NULL) |
---|
63 | json_object_object_add(res,cursor->name,val); |
---|
64 | cursor=cursor->next; |
---|
65 | } |
---|
66 | return res; |
---|
67 | } |
---|
68 | |
---|
69 | json_object* mapsToJson(maps* myMap){ |
---|
70 | json_object *res=json_object_new_object(); |
---|
71 | maps* cursor=myMap; |
---|
72 | while(cursor!=NULL){ |
---|
73 | json_object *obj=NULL; |
---|
74 | if(cursor->content!=NULL){ |
---|
75 | //json_object *content=NULL; |
---|
76 | obj=mapToJson(cursor->content); |
---|
77 | //json_object_object_add(obj,"content",content); |
---|
78 | }else |
---|
79 | obj=json_object_new_object(); |
---|
80 | if(cursor->child!=NULL){ |
---|
81 | json_object *child=NULL; |
---|
82 | child=mapsToJson(cursor->child); |
---|
83 | json_object_object_add(obj,"child",child); |
---|
84 | } |
---|
85 | json_object_object_add(res,cursor->name,obj); |
---|
86 | cursor=cursor->next; |
---|
87 | } |
---|
88 | return res; |
---|
89 | } |
---|
90 | |
---|
91 | json_object* elementsToJson(elements* myElements){ |
---|
92 | json_object *res=json_object_new_object(); |
---|
93 | elements* cur=myElements; |
---|
94 | while(cur!=NULL){ |
---|
95 | json_object *cres=json_object_new_object(); |
---|
96 | json_object_object_add(cres,"content",mapToJson(cur->content)); |
---|
97 | json_object_object_add(cres,"metadata",mapToJson(cur->metadata)); |
---|
98 | json_object_object_add(cres,"additional_parameters",mapToJson(cur->additional_parameters)); |
---|
99 | if(cur->format!=NULL){ |
---|
100 | json_object_object_add(cres,"format",json_object_new_string(cur->format)); |
---|
101 | } |
---|
102 | if(cur->child==NULL){ |
---|
103 | if(cur->defaults!=NULL) |
---|
104 | json_object_object_add(cres,"defaults",mapToJson(cur->defaults->content)); |
---|
105 | else |
---|
106 | json_object_object_add(cres,"defaults",mapToJson(NULL)); |
---|
107 | iotype* scur=cur->supported; |
---|
108 | json_object *resi=json_object_new_array(); |
---|
109 | while(scur!=NULL){ |
---|
110 | json_object_array_add(resi,mapToJson(scur->content)); |
---|
111 | scur=scur->next; |
---|
112 | } |
---|
113 | json_object_object_add(cres,"supported",resi); |
---|
114 | fprintf(stderr,"%s %d\n",__FILE__,__LINE__); |
---|
115 | fflush(stderr); |
---|
116 | } |
---|
117 | |
---|
118 | dumpElements(cur->child); |
---|
119 | json_object_object_add(cres,"child",elementsToJson(cur->child)); |
---|
120 | fprintf(stderr,"%s %d\n",__FILE__,__LINE__); |
---|
121 | fflush(stderr); |
---|
122 | |
---|
123 | json_object_object_add(res,cur->name,cres); |
---|
124 | cur=cur->next; |
---|
125 | } |
---|
126 | return res; |
---|
127 | } |
---|
128 | |
---|
129 | json_object* serviceToJson(service* myService){ |
---|
130 | json_object *res=json_object_new_object(); |
---|
131 | json_object_object_add(res,"name",json_object_new_string(myService->name)); |
---|
132 | json_object_object_add(res,"content",mapToJson(myService->content)); |
---|
133 | json_object_object_add(res,"metadata",mapToJson(myService->metadata)); |
---|
134 | json_object_object_add(res,"additional_parameters",mapToJson(myService->additional_parameters)); |
---|
135 | json_object_object_add(res,"inputs",elementsToJson(myService->inputs)); |
---|
136 | json_object_object_add(res,"outputs",elementsToJson(myService->outputs)); |
---|
137 | return res; |
---|
138 | } |
---|
139 | |
---|
140 | |
---|
141 | #ifdef __cplusplus |
---|
142 | } |
---|
143 | #endif |
---|