1 // jsonprops.cxx -- convert properties from/to json
3 // Written by Torsten Dreyer, started April 2014.
5 // Copyright (C) 2014 Torsten Dreyer
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.
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.
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.
21 #include "jsonprops.hxx"
22 #include <simgear/misc/strutils.hxx>
23 #include <simgear/math/SGMath.hxx>
24 namespace flightgear {
29 static const char * getPropertyTypeString(simgear::props::Type type)
32 case simgear::props::NONE:
35 case simgear::props::ALIAS:
38 case simgear::props::BOOL:
41 case simgear::props::INT:
44 case simgear::props::LONG:
47 case simgear::props::FLOAT:
50 case simgear::props::DOUBLE:
53 case simgear::props::STRING:
56 case simgear::props::UNSPECIFIED:
59 case simgear::props::EXTENDED:
62 case simgear::props::VEC3D:
65 case simgear::props::VEC4D:
73 cJSON * JSON::toJson(SGPropertyNode_ptr n, int depth, double timestamp )
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()));
79 switch( n->getType() ) {
80 case simgear::props::BOOL:
81 cJSON_AddItemToObject(json, "value", cJSON_CreateBool(n->getBoolValue()));
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));
92 cJSON_AddItemToObject(json, "value", cJSON_CreateString(n->getStringValue()));
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()));
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);
111 void JSON::toProp(cJSON * json, SGPropertyNode_ptr base)
113 if (NULL == json) return;
115 SGPropertyNode_ptr n = base;
117 // check if name is set. If so, update child with given name
119 cJSON * cj = cJSON_GetObjectItem(json, "name");
121 const char * name = cj->valuestring;
122 if (NULL == name) name = "";
124 //TODO: better check for valid name
125 string namestr = simgear::strutils::strip(string(name));
126 if( false == namestr.empty() ) {
128 cj = cJSON_GetObjectItem(json, "index");
129 if (NULL != cj) index = cj->valueint;
130 if (index < 0) return;
132 n = base->getNode(namestr, index, true);
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);
142 cj = cJSON_GetObjectItem(json, "value");
144 switch ( cj->type ) {
146 n->setStringValue(cj->valuestring);
150 n->setDoubleValue(cj->valuedouble);
154 n->setBoolValue(true);
158 n->setBoolValue(false);
168 void JSON::addChildrenToProp(cJSON * json, SGPropertyNode_ptr n)
170 if (NULL == json) return;
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);
181 string JSON::toJsonString(bool indent, SGPropertyNode_ptr n, int depth, double timestamp )
183 cJSON * json = toJson( n, depth, timestamp );
184 char * jsonString = indent ? cJSON_Print( json ) : cJSON_PrintUnformatted( json );
185 string reply(jsonString);
187 cJSON_Delete( json );
192 } // namespace flightgear