Added helper function to escape a special character in a string.
authorSakthivel Samidurai <sakthivel.samidurai@intel.com>
Mon, 25 Aug 2014 05:15:39 +0000 (22:15 -0700)
committerSakthivel Samidurai <sakthivel.samidurai@intel.com>
Mon, 25 Aug 2014 05:31:43 +0000 (22:31 -0700)
Change-Id: Icdca0fe4fb1c959b374cd6afea57272576e86061

OCLib/InProcClientWrapper.cpp
OCLib/OCUtilities.cpp
include/OCApi.h
include/OCResourceRequest.h
include/OCResourceResponse.h

index 8b7bd32..8ba56c3 100644 (file)
@@ -244,7 +244,14 @@ namespace OC
         }
 
         boost::property_tree::ptree root;
-        boost::property_tree::read_json(requestStream, root);
+        try
+        {
+            boost::property_tree::read_json(requestStream, root);
+        }
+        catch(boost::property_tree::json_parser::json_parser_error &e)
+        {
+            return OCRepresentation();
+        }
         boost::property_tree::ptree payload = root.get_child("oc", boost::property_tree::ptree());
         OCRepresentation root_resource;
         std::vector<OCRepresentation> children;
index 53c8dc7..33f9f72 100644 (file)
@@ -27,7 +27,38 @@ extern "C" {
 #include <uri.h>    // libcoap
 #include <option.h> // libcoap
 }
-
+namespace OC{
+    // Helper function to escape special character.
+    std::string escapeString(const std::string& value) 
+    {
+        std::ostringstream stringStream;
+        for (auto iter = value.cbegin(); iter != value.cend(); iter++) 
+        {
+            switch (*iter) 
+            {
+                case '\\': stringStream << "\\\\"; 
+                    break;
+                case '"': stringStream << "\\\""; 
+                    break;
+                case '/': stringStream << "\\/"; 
+                    break;
+                case '\b': stringStream << "\\b"; 
+                    break;
+                case '\f': stringStream << "\\f"; 
+                    break;
+                case '\n': stringStream << "\\n"; 
+                    break;
+                case '\r': stringStream << "\\r"; 
+                    break;
+                case '\t': stringStream << "\\t"; 
+                    break;
+                default: stringStream << *iter; 
+                    break;
+           }
+        }
+        return stringStream.str();
+    }
+}
 // [TODO] remove this function
 // this function is just a temporary patch for Sudarshan
 // it seems that the C stack is parsing and giving out the query separately.
index 6735cb6..c86ed15 100644 (file)
  #define __INTEL_OCAPI_H_2014_07_10
 
 #include <string>
+#include <sstream>
 #include <vector>
 #include <map>
 
 namespace OC {
 
+
 class OCResource;
 
 } // namespace OC
@@ -103,6 +105,9 @@ namespace OC {
  // Used in GET, PUT, POST, DELETE methods on links to other resources of a collection.
  const std::string BATCH_INTERFACE = "oc.mi.b";
 
+ // Helper function to escape character in a string.
+ std::string escapeString(const std::string& value);
+
  class OCRepresentation
     {
 
@@ -179,7 +184,6 @@ namespace OC {
             m_resourceInterfaces = resourceInterfaces;
         }
     };
-
 } // namespace OC
 
 #endif  
index e5da5ce..8805f74 100644 (file)
@@ -105,7 +105,15 @@ namespace OC
             std::stringstream requestStream;
             requestStream << requestPayload;
             boost::property_tree::ptree root;
-            boost::property_tree::read_json(requestStream, root);
+            try
+            {
+                boost::property_tree::read_json(requestStream, root);
+            }
+            catch(boost::property_tree::json_parser::json_parser_error &e)
+            {
+                //TOD: log this
+                return;
+            }
 
             // TODO this expects the representation oc:{} and not oc:[{}]
             //      this representation is fine when setting for simple resource.
index b9a32bb..8c3195b 100644 (file)
@@ -169,7 +169,7 @@ namespace OC
                 {
                     payload << ',';
                 }
-                payload << "\""<<itr->first<<"\":" << itr->second.front();
+                payload << "\""<<itr->first<<"\":\""<< itr->second.front()<<"\"";
             }
 
             payload << "}}";
@@ -258,7 +258,7 @@ namespace OC
                     {
                         payload << ',';
                     }
-                    payload << "\""<<itr->first<<"\":" << itr->second.front();
+                    payload << "\""<<itr->first<<"\":\""<< itr->second.front()<<"\"";
                 }
 
                 payload << "}}";
@@ -291,7 +291,8 @@ namespace OC
                     payload << ',';
                 }
                 // cout << itr->first << ":" <, itr->second.front() << endl;
-                payload << "\""<<itr->first<<"\":" << itr->second.front();
+                payload << "\""<<itr->first<<"\":\""<< itr->second.front()<<"\"";
+
             }
 
             payload << "}}";