1
0
Fork 0

JsonUriHandler: allow POST updates without child

updates to properties used to be
POST /json/some/property/path
{
  name: 'somechild',
  value: 'somevalue'
}

which required some ugly path hacking when directly updating a node.
now, this works too (and in a probably more intuitive way)
POST /json/some/property/path/somechild
{
  value: 'somevalue'
}
This commit is contained in:
Torsten Dreyer 2015-02-27 11:07:25 +01:00
parent 4b5dec2cae
commit 146efcafa4

View file

@ -19,7 +19,7 @@
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "jsonprops.hxx"
#include <simgear/misc/strutils.hxx>
namespace flightgear {
namespace http {
@ -109,19 +109,29 @@ void JSON::toProp(cJSON * json, SGPropertyNode_ptr base)
{
if (NULL == json) return;
SGPropertyNode_ptr n = base;
// check if name is set. If so, update child with given name
// else update base
cJSON * cj = cJSON_GetObjectItem(json, "name");
if (NULL == cj) return; // a node with no name?
char * name = cj->valuestring;
if (NULL == name) return; // still no name?
if ( cj ) {
char * name = cj->valuestring;
if (NULL == name) return; // no name?
//TODO: check valid name
string namestr = simgear::strutils::strip(string(name));
if( namestr.empty() )
return;
int index = 0;
cj = cJSON_GetObjectItem(json, "index");
if (NULL != cj) index = cj->valueint;
if (index < 0) return;
//TODO: better check for valid name
int index = 0;
cj = cJSON_GetObjectItem(json, "index");
if (NULL != cj) index = cj->valueint;
if (index < 0) return;
n = base->getNode(namestr, index, true);
}
SGPropertyNode_ptr n = base->getNode(name, index, true);
cJSON * children = cJSON_GetObjectItem(json, "children");
if (NULL != children) {
for (int i = 0; i < cJSON_GetArraySize(children); i++) {