[dss-commits] r8841 - dss/trunk/core

dss-commits at forum.digitalstrom.org dss-commits at forum.digitalstrom.org
Wed Oct 14 11:39:20 CEST 2009


Author: pstaehlin
Date: 2009-10-14 11:39:20 +0200 (Wed, 14 Oct 2009)
New Revision: 8841

Modified:
   dss/trunk/core/webserver.cpp
Log:
Return well-formed JSON even though the function called was invalid

Modified: dss/trunk/core/webserver.cpp
===================================================================
--- dss/trunk/core/webserver.cpp	2009-10-14 08:02:04 UTC (rev 8840)
+++ dss/trunk/core/webserver.cpp	2009-10-14 09:39:20 UTC (rev 8841)
@@ -566,28 +566,30 @@
     return sstream.str();
   } // toJSONValue(Group)
 
-  string ToJSONValue(Zone& _zone) {
+  string ToJSONValue(Zone& _zone, bool _includeDevices = true) {
     std::stringstream sstream;
     sstream << "{ \"id\": " << _zone.getZoneID() << ",";
     string name = _zone.getName();
     if(name.size() == 0) {
       name = string("Zone ") + intToString(_zone.getZoneID());
     }
-    sstream << "\"name\": " << ToJSONValue(name) << ", ";
-    sstream << "\"isPresent\": " << ToJSONValue(_zone.isPresent()) << ", ";
+    sstream << ToJSONValue("name") << ": " << ToJSONValue(name) << ", ";
+    sstream << ToJSONValue("isPresent") << ": " << ToJSONValue(_zone.isPresent()) << ", ";
 
-    Set devices = _zone.getDevices();
-    sstream << ToJSONValue(devices, "devices");
-    sstream << "," << ToJSONValue("groups") << ": [";
-    bool first = true;
-    foreach(Group* pGroup, _zone.getGroups()) {
-      if(!first) {
-        sstream << ",";
+    if(_includeDevices) {
+      Set devices = _zone.getDevices();
+      sstream << ToJSONValue(devices, "devices");
+      sstream << "," << ToJSONValue("groups") << ": [";
+      bool first = true;
+      foreach(Group* pGroup, _zone.getGroups()) {
+        if(!first) {
+          sstream << ",";
+        }
+        first = false;
+        sstream  << ToJSONValue(*pGroup);
       }
-      first = false;
-      sstream  << ToJSONValue(*pGroup);
+      sstream << "] ";
     }
-    sstream << "] ";
 
     sstream << "} ";
     return sstream.str();
@@ -1795,7 +1797,11 @@
 
     if(!handled) {
       emitHTTPHeader(404, _connection, "application/json");
-      result = "{ ok: " + ToJSONValue(false) + ", message: " + ToJSONValue("Call to unknown function") + " }";
+      std::ostringstream sstream;
+      sstream << "{" << ToJSONValue("ok") << ":" << ToJSONValue(false) << ",";
+      sstream << ToJSONValue("message") << ":" << ToJSONValue("Call to unknown function");
+      sstream << "}";
+      result = sstream.str();
       self.log("Unknown function '" + method + "'", lsError);
     } else {
       emitHTTPHeader(200, _connection, "application/json");



More information about the dss-commits mailing list