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 | #ifdef __cplusplus |
---|
92 | } |
---|
93 | #endif |
---|