]> git.mxchange.org Git - flightgear.git/blob - src/Network/http/jsonprops.cxx
Interim windows build fix
[flightgear.git] / src / Network / http / jsonprops.cxx
1 // jsonprops.cxx -- convert properties from/to json
2 //
3 // Written by Torsten Dreyer, started April 2014.
4 //
5 // Copyright (C) 2014  Torsten Dreyer
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20
21 #include "jsonprops.hxx"
22 #include <simgear/misc/strutils.hxx>
23 #include <simgear/math/SGMath.hxx>
24 namespace flightgear {
25 namespace http {
26
27 using std::string;
28
29 static const char * getPropertyTypeString(simgear::props::Type type)
30 {
31   switch (type) {
32     case simgear::props::NONE:
33       return "-";
34
35     case simgear::props::ALIAS:
36       return "alias";
37
38     case simgear::props::BOOL:
39       return "bool";
40
41     case simgear::props::INT:
42       return "int";
43
44     case simgear::props::LONG:
45       return "long";
46
47     case simgear::props::FLOAT:
48       return "float";
49
50     case simgear::props::DOUBLE:
51       return "double";
52
53     case simgear::props::STRING:
54       return "string";
55
56     case simgear::props::UNSPECIFIED:
57       return "unspecified";
58
59     case simgear::props::EXTENDED:
60       return "extended";
61
62     case simgear::props::VEC3D:
63       return "vec3d";
64
65     case simgear::props::VEC4D:
66       return "vec4d";
67
68     default:
69       return "?";
70   }
71 }
72
73 cJSON * JSON::toJson(SGPropertyNode_ptr n, int depth, double timestamp )
74 {
75   cJSON * json = cJSON_CreateObject();
76   cJSON_AddItemToObject(json, "path", cJSON_CreateString(n->getPath(true).c_str()));
77   cJSON_AddItemToObject(json, "name", cJSON_CreateString(n->getName()));
78   if( n->hasValue() ) {
79     switch( n->getType() ) {
80       case simgear::props::BOOL:
81         cJSON_AddItemToObject(json, "value", cJSON_CreateBool(n->getBoolValue()));
82         break;
83       case simgear::props::INT:
84       case simgear::props::LONG:
85       case simgear::props::FLOAT:
86       case simgear::props::DOUBLE: {
87         double val = n->getDoubleValue();
88         cJSON_AddItemToObject(json, "value", SGMiscd::isNaN(val) ? cJSON_CreateNull() : cJSON_CreateNumber(val));
89         break;
90       }
91       default:
92         cJSON_AddItemToObject(json, "value", cJSON_CreateString(n->getStringValue()));
93         break;
94     }
95   }
96   cJSON_AddItemToObject(json, "type", cJSON_CreateString(getPropertyTypeString(n->getType())));
97   cJSON_AddItemToObject(json, "index", cJSON_CreateNumber(n->getIndex()));
98   if( timestamp >= 0.0 )
99     cJSON_AddItemToObject(json, "ts", cJSON_CreateNumber(timestamp));
100   cJSON_AddItemToObject(json, "nChildren", cJSON_CreateNumber(n->nChildren()));
101
102   if (depth > 0 && n->nChildren() > 0) {
103     cJSON * jsonArray = cJSON_CreateArray();
104     for (int i = 0; i < n->nChildren(); i++)
105       cJSON_AddItemToArray(jsonArray, toJson(n->getChild(i), depth - 1, timestamp ));
106     cJSON_AddItemToObject(json, "children", jsonArray);
107   }
108   return json;
109 }
110
111 void JSON::toProp(cJSON * json, SGPropertyNode_ptr base)
112 {
113   if (NULL == json) return;
114
115   SGPropertyNode_ptr n = base;
116
117   // check if name is set. If so, update child with given name
118   // else update base
119   cJSON * cj = cJSON_GetObjectItem(json, "name");
120   if ( cj ) {
121     const char * name = cj->valuestring;
122     if (NULL == name) name = "";
123
124     //TODO: better check for valid name
125     string namestr = simgear::strutils::strip(string(name));
126     if( false == namestr.empty() ) {
127       int index = 0;
128       cj = cJSON_GetObjectItem(json, "index");
129       if (NULL != cj) index = cj->valueint;
130       if (index < 0) return;
131
132       n = base->getNode(namestr, index, true);
133     }
134   }
135
136   cJSON * children = cJSON_GetObjectItem(json, "children");
137   if (NULL != children) {
138     for (int i = 0; i < cJSON_GetArraySize(children); i++) {
139       toProp(cJSON_GetArrayItem(children, i), n);
140     }
141   } else {
142     cj = cJSON_GetObjectItem(json, "value");
143     if (NULL != cj) {
144       switch ( cj->type ) {
145       case cJSON_String:
146         n->setStringValue(cj->valuestring);
147         break;
148         
149       case cJSON_Number:
150         n->setDoubleValue(cj->valuedouble);
151         break;
152         
153       case cJSON_True:
154         n->setBoolValue(true);
155         break;
156         
157       case cJSON_False:
158         n->setBoolValue(false);
159         break;
160           
161       default:
162         break;
163       }
164     } // of have value
165   } // of no children
166 }
167
168 void JSON::addChildrenToProp(cJSON * json, SGPropertyNode_ptr n)
169 {
170   if (NULL == json) return;
171   if (!n) return;
172   
173   cJSON * children = cJSON_GetObjectItem(json, "children");
174   if (NULL != children) {
175     for (int i = 0; i < cJSON_GetArraySize(children); i++) {
176       toProp(cJSON_GetArrayItem(children, i), n);
177     }
178   }
179 }
180
181 string JSON::toJsonString(bool indent, SGPropertyNode_ptr n, int depth, double timestamp )
182 {
183   cJSON * json = toJson( n, depth, timestamp );
184   char * jsonString = indent ? cJSON_Print( json ) : cJSON_PrintUnformatted( json );
185   string reply(jsonString);
186   free( jsonString );
187   cJSON_Delete( json );
188   return reply;
189 }
190
191 }  // namespace http
192 }  // namespace flightgear
193