| 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 | const char * JSON::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::valueToJson(SGPropertyNode_ptr n) |
| 74 | { |
| 75 | if( !n->hasValue() ) |
| 76 | return cJSON_CreateNull(); |
| 77 | |
| 78 | switch( n->getType() ) { |
| 79 | case simgear::props::BOOL: |
| 80 | return cJSON_CreateBool(n->getBoolValue()); |
| 81 | case simgear::props::INT: |
| 82 | case simgear::props::LONG: |
| 83 | case simgear::props::FLOAT: |
| 84 | case simgear::props::DOUBLE: { |
| 85 | double val = n->getDoubleValue(); |
| 86 | return SGMiscd::isNaN(val) ? cJSON_CreateNull() : cJSON_CreateNumber(val); |
| 87 | } |
| 88 | |
| 89 | default: |
| 90 | return cJSON_CreateString(n->getStringValue().c_str()); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | |
| 95 | cJSON * JSON::toJson(SGPropertyNode_ptr n, int depth, double timestamp ) |
| 96 | { |
| 97 | cJSON * json = cJSON_CreateObject(); |
| 98 | cJSON_AddItemToObject(json, "path", cJSON_CreateString(n->getPath(true).c_str())); |
| 99 | cJSON_AddItemToObject(json, "name", cJSON_CreateString(n->getNameString().c_str())); |
| 100 | if( n->hasValue() ) { |
| 101 | switch( n->getType() ) { |
| 102 | case simgear::props::BOOL: |
| 103 | cJSON_AddItemToObject(json, "value", cJSON_CreateBool(n->getBoolValue())); |
| 104 | break; |
| 105 | case simgear::props::INT: |
| 106 | case simgear::props::LONG: |
| 107 | case simgear::props::FLOAT: |
| 108 | case simgear::props::DOUBLE: { |
| 109 | double val = n->getDoubleValue(); |
| 110 | cJSON_AddItemToObject(json, "value", SGMiscd::isNaN(val) ? cJSON_CreateNull() : cJSON_CreateNumber(val)); |
| 111 | break; |
| 112 | } |
| 113 | default: |
| 114 | cJSON_AddItemToObject(json, "value", cJSON_CreateString(n->getStringValue().c_str())); |
| 115 | break; |
| 116 | } |
| 117 | } |
| 118 | cJSON_AddItemToObject(json, "type", cJSON_CreateString(getPropertyTypeString(n->getType()))); |
| 119 | cJSON_AddItemToObject(json, "index", cJSON_CreateNumber(n->getIndex())); |
| 120 | if( timestamp >= 0.0 ) |
| 121 | cJSON_AddItemToObject(json, "ts", cJSON_CreateNumber(timestamp)); |
| 122 | cJSON_AddItemToObject(json, "nChildren", cJSON_CreateNumber(n->nChildren())); |
| 123 | |
| 124 | if (depth > 0 && n->nChildren() > 0) { |
| 125 | cJSON * jsonArray = cJSON_CreateArray(); |
| 126 | for (int i = 0; i < n->nChildren(); i++) |
| 127 | cJSON_AddItemToArray(jsonArray, toJson(n->getChild(i), depth - 1, timestamp )); |
| 128 | cJSON_AddItemToObject(json, "children", jsonArray); |
| 129 | } |
| 130 | return json; |
| 131 | } |
| 132 | |
| 133 | void JSON::toProp(cJSON * json, SGPropertyNode_ptr base) |
| 134 | { |
| 135 | if (NULL == json) return; |
| 136 | |
| 137 | SGPropertyNode_ptr n = base; |
| 138 | |
| 139 | // check if name is set. If so, update child with given name |
| 140 | // else update base |
| 141 | cJSON * cj = cJSON_GetObjectItem(json, "name"); |
| 142 | if ( cj ) { |
| 143 | const char * name = cj->valuestring; |
| 144 | if (NULL == name) name = ""; |
| 145 | |
| 146 | // TODO: better check for valid name |
| 147 | string namestr = simgear::strutils::strip(string(name)); |
| 148 | if( !namestr.empty() ) { |
| 149 | int index = 0; |
| 150 | cj = cJSON_GetObjectItem(json, "index"); |
| 151 | if (NULL != cj) index = cj->valueint; |
| 152 | if (index < 0) return; |
| 153 | |
| 154 | n = base->getNode(namestr, index, true); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | cJSON * children = cJSON_GetObjectItem(json, "children"); |
| 159 | if (NULL != children) { |
| 160 | for (int i = 0; i < cJSON_GetArraySize(children); i++) { |
| 161 | toProp(cJSON_GetArrayItem(children, i), n); |
| 162 | } |
| 163 | } else { |
| 164 | cj = cJSON_GetObjectItem(json, "value"); |
| 165 | if (NULL != cj) { |
| 166 | switch ( cj->type ) { |
| 167 | case cJSON_String: |
| 168 | n->setStringValue(cj->valuestring); |
| 169 | break; |
| 170 | |
| 171 | case cJSON_Number: |
| 172 | n->setDoubleValue(cj->valuedouble); |
| 173 | break; |
| 174 | |
| 175 | case cJSON_True: |
| 176 | n->setBoolValue(true); |
| 177 | break; |
| 178 | |
| 179 | case cJSON_False: |
| 180 | n->setBoolValue(false); |
| 181 | break; |
| 182 | |
| 183 | default: |
| 184 | break; |
| 185 | } |
| 186 | } // of have value |
| 187 | } // of no children |
| 188 | } |
| 189 | |
| 190 | void JSON::addChildrenToProp(cJSON * json, SGPropertyNode_ptr n) |
| 191 | { |
| 192 | if (NULL == json) return; |
| 193 | if (!n) return; |
| 194 | |
| 195 | cJSON * children = cJSON_GetObjectItem(json, "children"); |
| 196 | if (NULL != children) { |
| 197 | for (int i = 0; i < cJSON_GetArraySize(children); i++) { |
| 198 | toProp(cJSON_GetArrayItem(children, i), n); |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | string JSON::toJsonString(bool indent, SGPropertyNode_ptr n, int depth, double timestamp ) |
| 204 | { |
| 205 | cJSON * json = toJson( n, depth, timestamp ); |
| 206 | char * jsonString = indent ? cJSON_Print( json ) : cJSON_PrintUnformatted( json ); |
| 207 | string reply(jsonString); |
| 208 | free( jsonString ); |
| 209 | cJSON_Delete( json ); |
| 210 | return reply; |
| 211 | } |
| 212 | |
| 213 | } // namespace http |
| 214 | } // namespace flightgear |
| 215 |