1
0
Fork 0

JSON: make the response configurable, set encoding

add request parameter 'i' and 'd'
if i=y, indent the output to make it human readable
if i is missing or anything else, send the output unformatted to save bandwith

'd' defines recursion depth with a default of 1 to show the requested node
and the immediate childs (if it has any)

call /json/?i=y&d=999 for a complete property tree backup (use with caution!)

Also set the encoding to UTF-8 as required per RFC 4627
This commit is contained in:
Torsten Dreyer 2014-03-07 21:53:51 +01:00
parent ef3f0a2738
commit ed8ec8f092

View file

@ -48,19 +48,26 @@ static const char * getPropertyTypeString( simgear::props::Type type )
}
}
static cJSON * PropToJson( SGPropertyNode_ptr n )
static cJSON * PropToJson( SGPropertyNode_ptr n, int depth )
{
cJSON * jsonProp = cJSON_CreateObject();
cJSON_AddItemToObject(jsonProp, "path", cJSON_CreateString(n->getPath(true).c_str()));
cJSON_AddItemToObject(jsonProp, "name", cJSON_CreateString(n->getName()));
cJSON_AddItemToObject(jsonProp, "value", cJSON_CreateString(n->getStringValue()));
cJSON_AddItemToObject(jsonProp, "type", cJSON_CreateString(getPropertyTypeString(n->getType())));
return jsonProp;
cJSON * json = cJSON_CreateObject();
cJSON_AddItemToObject(json, "path", cJSON_CreateString(n->getPath(true).c_str()));
cJSON_AddItemToObject(json, "name", cJSON_CreateString(n->getName()));
cJSON_AddItemToObject(json, "value", cJSON_CreateString(n->getStringValue()));
cJSON_AddItemToObject(json, "type", cJSON_CreateString(getPropertyTypeString(n->getType())));
if( depth > 0 && n->nChildren() > 0 ) {
cJSON * jsonArray = cJSON_CreateArray();
for( int i = 0; i < n->nChildren(); i++ )
cJSON_AddItemToArray( jsonArray, PropToJson( n->getChild(i), depth-1 ) );
cJSON_AddItemToObject( json, "children", jsonArray );
}
return json;
}
bool JsonUriHandler::handleGetRequest( const HTTPRequest & request, HTTPResponse & response )
{
response.Header["Content-Type"] = "application/json; charset=ISO-8859-1";
response.Header["Content-Type"] = "application/json; charset=UTF-8";
string propertyPath = request.Uri;
@ -77,6 +84,13 @@ bool JsonUriHandler::handleGetRequest( const HTTPRequest & request, HTTPResponse
while( false == propertyPath.empty() && propertyPath[ propertyPath.length()-1 ] == '/' )
propertyPath = propertyPath.substr(0,propertyPath.length()-1);
// max recursion depth
int depth = atoi(request.RequestVariables.get("d").c_str());
if( depth < 1 ) depth = 1; // at least one level
// pretty print (y) or compact print (default)
bool indent = request.RequestVariables.get("i") == "y";
SGPropertyNode_ptr node = fgGetNode( string("/") + propertyPath );
if( false == node.valid() ) {
response.StatusCode = 400;
@ -86,15 +100,9 @@ bool JsonUriHandler::handleGetRequest( const HTTPRequest & request, HTTPResponse
}
cJSON * json = PropToJson( node );
if( node->nChildren() > 0 ) {
cJSON * jsonArray = cJSON_CreateArray();
for( int i = 0; i < node->nChildren(); i++ )
cJSON_AddItemToArray( jsonArray, PropToJson( node->getChild(i) ) );
cJSON_AddItemToObject( json, "children", jsonArray );
}
cJSON * json = PropToJson( node, depth );
char * jsonString = cJSON_Print( json );
char * jsonString = indent ? cJSON_Print( json ) : cJSON_PrintUnformatted( json );
response.Content = jsonString;
free( jsonString );
cJSON_Delete( json );