1. Support for JSON object in OCRepresentation.
2. Code refactor for JSON composition in various parts of the stack into one place (OCRepresenation).
3. Similar code refactoring for JSON parsing needs to be done. I will revisit when we look into
JSON parse library alternatives.
4. Implemented correct JSON composition. Earlier we were returning strings for all the json types.
Now strings are only used for string types and JSON objects.
5. garageserver.cpp and garageclient.cpp examples to explain the user of JSON object with in
the representation.
Change-Id: I9461db811c7b5e3b827d5704cde489aa2005e291
--- /dev/null
+//******************************************************************
+//
+// Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+// garageclient.cpp is the client program for garageserver.cpp, which
+// uses different representation in OCRepresention.
+
+#include <string>
+#include <cstdlib>
+#include <pthread.h>
+#include "OCPlatform.h"
+#include "OCApi.h"
+
+using namespace OC;
+
+const int SUCCESS_RESPONSE = 0;
+std::shared_ptr<OCResource> curResource;
+
+class Garage
+{
+public:
+
+ bool m_state;
+ std::string m_name;
+ bool m_lightState;
+ int m_lightPower;
+ OCRepresentation m_lightRep;
+
+ Garage() : m_state(false), m_name("")
+ {
+ }
+};
+
+Garage myGarage;
+
+// callback handler on PUT request
+void onPut(const OCRepresentation& rep, const int eCode)
+{
+ if(eCode == SUCCESS_RESPONSE)
+ {
+ std::cout << "PUT request was successful" << std::endl;
+
+ rep.getValue("state", myGarage.m_state);
+ rep.getValue("name", myGarage.m_name);
+ rep.getValue("light", myGarage.m_lightRep);
+
+ myGarage.m_lightRep.getValue("state", myGarage.m_lightState);
+ myGarage.m_lightRep.getValue("power", myGarage.m_lightPower);
+
+ std::cout << "\tstate: " << myGarage.m_state << std::endl;
+ std::cout << "\tname: " << myGarage.m_name << std::endl;
+ std::cout << "\tlightRep: state: " << myGarage.m_lightState << std::endl;
+ std::cout << "\tlightRep: power: " << myGarage.m_lightPower << std::endl;
+ }
+ else
+ {
+ std::cout << "onPut Response error: " << eCode << std::endl;
+ std::exit(-1);
+ }
+}
+
+// Local function to put a different state for this resource
+void putLightRepresentation(std::shared_ptr<OCResource> resource)
+{
+ if(resource)
+ {
+ OCRepresentation rep;
+
+ std::cout << "Putting light representation..."<<std::endl;
+
+ myGarage.m_state = true;
+
+ rep.setValue("state", myGarage.m_state);
+
+ // Create QueryParameters Map and add query params (if any)
+ QueryParamsMap queryParamsMap;
+
+ // Invoke resource's pit API with rep, query map and the callback parameter
+ resource->put(rep, queryParamsMap, &onPut);
+ }
+}
+
+// Callback handler on GET request
+void onGet(const OCRepresentation& rep, const int eCode)
+{
+ if(eCode == SUCCESS_RESPONSE)
+ {
+ std::cout << "GET request was successful" << std::endl;
+ std::cout << "Resource URI: " << rep.getUri() << std::endl;
+
+ rep.getValue("state", myGarage.m_state);
+ rep.getValue("name", myGarage.m_name);
+ rep.getValue("light", myGarage.m_lightRep);
+
+ myGarage.m_lightRep.getValue("state", myGarage.m_lightState);
+ myGarage.m_lightRep.getValue("power", myGarage.m_lightPower);
+
+ std::cout << "\tstate: " << myGarage.m_state << std::endl;
+ std::cout << "\tname: " << myGarage.m_name << std::endl;
+ std::cout << "\tlightRep: state: " << myGarage.m_lightState << std::endl;
+ std::cout << "\tlightRep: power: " << myGarage.m_lightPower << std::endl;
+
+ putLightRepresentation(curResource);
+ }
+ else
+ {
+ std::cout << "onGET Response error: " << eCode << std::endl;
+ std::exit(-1);
+ }
+}
+
+// Local function to get representation of light resource
+void getLightRepresentation(std::shared_ptr<OCResource> resource)
+{
+ if(resource)
+ {
+ std::cout << "Getting Light Representation..."<<std::endl;
+ // Invoke resource's get API with the callback parameter
+
+ QueryParamsMap test;
+ resource->get(test, &onGet);
+ }
+}
+
+// Callback to found resources
+void foundResource(std::shared_ptr<OCResource> resource)
+{
+ if(curResource)
+ {
+ std::cout << "Found another resource, ignoring"<<std::endl;
+ }
+
+ std::string resourceURI;
+ std::string hostAddress;
+ try
+ {
+ // Do some operations with resource object.
+ if(resource)
+ {
+ std::cout<<"DISCOVERED Resource:"<<std::endl;
+ // Get the resource URI
+ resourceURI = resource->uri();
+ std::cout << "\tURI of the resource: " << resourceURI << std::endl;
+
+ // Get the resource host address
+ hostAddress = resource->host();
+ std::cout << "\tHost address of the resource: " << hostAddress << std::endl;
+
+ // Get the resource types
+ std::cout << "\tList of resource types: " << std::endl;
+ for(auto &resourceTypes : resource->getResourceTypes())
+ {
+ std::cout << "\t\t" << resourceTypes << std::endl;
+ }
+
+ // Get the resource interfaces
+ std::cout << "\tList of resource interfaces: " << std::endl;
+ for(auto &resourceInterfaces : resource->getResourceInterfaces())
+ {
+ std::cout << "\t\t" << resourceInterfaces << std::endl;
+ }
+
+ if(resourceURI == "/a/garage")
+ {
+ curResource = resource;
+ // Call a local function which will internally invoke
+ // get API on the resource pointer
+ getLightRepresentation(resource);
+ }
+ }
+ else
+ {
+ // Resource is invalid
+ std::cout << "Resource is invalid" << std::endl;
+ }
+
+ }
+ catch(std::exception& e)
+ {
+ //log(e.what());
+ }
+}
+
+int main(int argc, char* argv[]) {
+
+ // Create PlatformConfig object
+ PlatformConfig cfg {
+ OC::ServiceType::InProc,
+ OC::ModeType::Client,
+ "192.168.1.10",
+ 5683,
+ OC::QualityOfService::NonConfirmable
+ };
+
+ // Create a OCPlatform instance.
+ // Note: Platform creation is synchronous call.
+
+ try
+ {
+ OCPlatform platform(cfg);
+ std::cout << "Created Platform..."<<std::endl;
+ // Find all resources
+ platform.findResource("", "coap://224.0.1.187/oc/core?rt=core.garage", &foundResource);
+ std::cout<< "Finding Resource... " <<std::endl;
+ while(true)
+ {
+ // some operations
+ }
+
+ }
+ catch(OCException& e)
+ {
+ //log(e.what());
+ }
+
+ return 0;
+}
+
--- /dev/null
+//******************************************************************
+//
+// Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
+///
+/// This sample provides using varous json types in the representation.
+///
+
+#include <functional>
+
+#include <pthread.h>
+
+#include "OCPlatform.h"
+#include "OCApi.h"
+
+using namespace OC;
+using namespace std;
+
+// Forward declaring the entityHandler
+void entityHandler(std::shared_ptr<OCResourceRequest> request,
+ std::shared_ptr<OCResourceResponse> response);
+
+/// This class represents a single resource named 'lightResource'. This resource has
+
+class GarageResource
+{
+public:
+ /// Access this property from a TB client
+ std::string m_name;
+ bool m_state;
+ std::string m_garageUri;
+ OCResourceHandle m_resourceHandle;
+ OCRepresentation m_garageRep;
+ ObservationIds m_interestedObservers;
+
+ // Light representation with in GarageResource
+ OCRepresentation m_lightRep;
+ bool m_lightState;
+ int m_lightPower;
+
+public:
+ /// Constructor
+ GarageResource(): m_name("John's Garage"), m_state(false), m_garageUri("/a/garage") {
+ // Initialize representation
+ m_garageRep.setUri(m_garageUri);
+
+ m_garageRep.setValue("state", m_state);
+ m_garageRep.setValue("name", m_name);
+
+ m_lightState = true;
+ m_lightPower = 10;
+ m_lightRep.setValue("state", m_lightState);
+ m_lightRep.setValue("power", m_lightPower);
+
+ // Storing another representation within a representation
+ m_garageRep.setValue("light", m_lightRep);
+ }
+
+ /* Note that this does not need to be a member function: for classes you do not have
+ access to, you can accomplish this with a free function: */
+
+ /// This function internally calls registerResource API.
+ void createResource(OC::OCPlatform& platform)
+ {
+ std::string resourceURI = m_garageUri; // URI of the resource
+ std::string resourceTypeName = "core.garage"; // resource type name.
+ std::string resourceInterface = DEFAULT_INTERFACE; // resource interface.
+
+ // OCResourceProperty is defined ocstack.h
+ uint8_t resourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
+
+ // This will internally create and register the resource.
+ OCStackResult result = platform.registerResource(
+ m_resourceHandle, resourceURI, resourceTypeName,
+ resourceInterface, &entityHandler, resourceProperty);
+
+ if (OC_STACK_OK != result)
+ {
+ cout << "Resource creation was unsuccessful\n";
+ }
+ }
+
+ OCResourceHandle getHandle()
+ {
+ return m_resourceHandle;
+ }
+
+ // Puts representation.
+ // Gets values from the representation and
+ // updates the internal state
+ void put(OCRepresentation& rep)
+ {
+ try {
+ if (rep.getValue("state", m_state))
+ {
+ cout << "\t\t\t\t" << "state: " << m_state << endl;
+ }
+ else
+ {
+ cout << "\t\t\t\t" << "state not found in the representation" << endl;
+ }
+ }
+ catch (exception& e)
+ {
+ cout << e.what() << endl;
+ }
+
+ }
+
+ // gets the updated representation.
+ // Updates the representation with latest internal state before
+ // sending out.
+ OCRepresentation get()
+ {
+ m_garageRep.setValue("state", m_state);
+
+ return m_garageRep;
+ }
+
+};
+
+// Create the instance of the resource class (in this case instance of class 'GarageResource').
+GarageResource myGarage;
+
+void entityHandler(std::shared_ptr<OCResourceRequest> request,
+ std::shared_ptr<OCResourceResponse> response)
+{
+ cout << "\tIn Server CPP entity handler:\n";
+
+ if(request)
+ {
+ // Get the request type and request flag
+ std::string requestType = request->getRequestType();
+ int requestFlag = request->getRequestHandlerFlag();
+
+ if(requestFlag & RequestHandlerFlag::InitFlag)
+ {
+ cout << "\t\trequestFlag : Init\n";
+
+ // entity handler to perform resource initialization operations
+ }
+ if(requestFlag & RequestHandlerFlag::RequestFlag)
+ {
+ cout << "\t\trequestFlag : Request\n";
+
+ // If the request type is GET
+ if(requestType == "GET")
+ {
+ cout << "\t\t\trequestType : GET\n";
+
+ if(response)
+ {
+ // TODO Error Code
+ response->setErrorCode(200);
+
+ response->setResourceRepresentation(myGarage.get());
+ }
+ }
+ else if(requestType == "PUT")
+ {
+ cout << "\t\t\trequestType : PUT\n";
+
+ OCRepresentation rep = request->getResourceRepresentation();
+
+ // Do related operations related to PUT request
+
+ // Update the lightResource
+ myGarage.put(rep);
+
+ if(response)
+ {
+ // TODO Error Code
+ response->setErrorCode(200);
+
+ response->setResourceRepresentation(myGarage.get());
+ }
+
+ }
+ else if(requestType == "POST")
+ {
+ // POST request operations
+ }
+ else if(requestType == "DELETE")
+ {
+ // DELETE request operations
+ }
+ }
+ if(requestFlag & RequestHandlerFlag::ObserverFlag)
+ {
+ // OBSERVE operations
+ }
+ }
+ else
+ {
+ std::cout << "Request invalid" << std::endl;
+ }
+}
+
+int main(int argc, char* argv[1])
+{
+ // Create PlatformConfig object
+ PlatformConfig cfg {
+ OC::ServiceType::InProc,
+ OC::ModeType::Server,
+ "134.134.161.33",
+ 56832,
+ OC::QualityOfService::NonConfirmable
+ };
+
+ // Create a OCPlatform instance.
+ // Note: Platform creation is synchronous call.
+ try
+ {
+ OCPlatform platform(cfg);
+
+ // Invoke createResource function of class light.
+ myGarage.createResource(platform);
+
+ // Perform app tasks
+ while(true)
+ {
+ // some tasks
+ }
+ }
+ catch(OCException e)
+ {
+ //log(e.what());
+ }
+
+ // No explicit call to stop the platform.
+ // When OCPlatform destructor is invoked, internally we do platform cleanup
+}
#CXX := clang
OUT_DIR := $(PWD)/$(BUILD)
-CXX_FLAGS.debug := -O0 -g3 -std=c++0x -Wall -pthread
+CXX_FLAGS.debug := -O0 -g3 -std=c++0x -Wall -pthread
-CXX_FLAGS.release := -O3 -std=c++0x -Wall -pthread
+CXX_FLAGS.release := -O3 -std=c++0x -Wall -pthread
CXX_INC := -I../include/
CXX_INC += -I../csdk/stack/include
CXX_INC += -I../csdk/libcoap
# Force metatargets to build:
-.PHONY: prep_dirs simpleserver simpleclient simpleclientserver roomserver roomclient presenceserver presenceclient ocicuc
+.PHONY: prep_dirs simpleserver simpleclient simpleclientserver roomserver roomclient presenceserver presenceclient garageserver garageclient ocicuc
all: .PHONY
roomclient: roomclient.cpp
$(CXX) $(CXX_FLAGS.$(BUILD)) -o $(OUT_DIR)/$@ roomclient.cpp $(CXX_INC) ../$(BUILD)/obj/liboc.a ../csdk/$(BUILD)/liboctbstack.a
+garageserver: garageserver.cpp
+ $(CXX) $(CXX_FLAGS.$(BUILD)) -o $(OUT_DIR)/$@ garageserver.cpp $(CXX_INC) ../$(BUILD)/obj/liboc.a ../csdk/$(BUILD)/liboctbstack.a
+
+garageclient: garageclient.cpp
+ $(CXX) $(CXX_FLAGS.$(BUILD)) -o $(OUT_DIR)/$@ garageclient.cpp $(CXX_INC) ../$(BUILD)/obj/liboc.a ../csdk/$(BUILD)/liboctbstack.a
+
+
ocicuc:
cd ocicuc && $(MAKE)
-clean:
- rm -rf $(OUT_DIR)/*
+clean:
+ rm -rf $(OUT_DIR)
cd ocicuc && $(MAKE) clean
return -1;
}
-
// Create PlatformConfig object
PlatformConfig cfg {
OC::ServiceType::InProc,
#include <map>
#include <memory>
+#include <boost/property_tree/ptree.hpp>
+#include <boost/property_tree/json_parser.hpp>
#include <boost/variant.hpp>
#include "ocstack.h"
ObserveAll
};
+ // Helper function to escape character in a string.
+ std::string escapeString(const std::string& value);
+
typedef std::map<std::string, std::string> AttributeMap;
+ class OCRepresentation
+ {
+ private:
+ std::string m_uri;
+ AttributeMap m_attributeMap;
+ std::vector<std::string> m_resourceTypes;
+ std::vector<std::string> m_resourceInterfaces;
+ int errorCode;
+
+ std::vector<OCRepresentation> m_children;
+
+ public:
+ OCRepresentation() {}
+
+ std::string getUri(void) const
+ {
+ return m_uri;
+ }
+
+ template <typename T>
+ void setValue(const std::string& str, const T& val);
+
+ template <typename T>
+ bool getValue(const std::string& str, T& val) const;
+
+ void setUri(std::string uri)
+ {
+ m_uri = uri;
+ }
+
+ std::vector<OCRepresentation> getChildren(void) const
+ {
+ return m_children;
+ }
+
+ void setChildren(std::vector<OCRepresentation> children)
+ {
+ m_children = children;
+ }
+
+ std::weak_ptr<OCResource> getResource() const
+ {
+ // TODO Needs to be implemented
+ std::weak_ptr<OCResource> wp;
+ return wp;
+ }
+
+ AttributeMap getAttributeMap() const
+ {
+ return m_attributeMap;
+ }
+
+ void setAttributeMap(const AttributeMap map)
+ {
+ m_attributeMap = map;
+ }
+
+ std::string getJSONRepresentation(void) const
+ {
+ std::ostringstream json;
+
+ json << "{";
+
+ for(auto itr = m_attributeMap.begin(); itr!= m_attributeMap.end(); ++ itr)
+ {
+ if(itr != m_attributeMap.begin())
+ {
+ json << ',';
+ }
+ json << "\""<<itr->first<<"\":"<< itr->second;
+ }
+ json << "}";
+
+ return json.str();
+ }
+
+ std::vector<std::string> getResourceTypes() const
+ {
+ return m_resourceTypes;
+ }
+
+ void setResourceTypes(std::vector<std::string> resourceTypes)
+ {
+ m_resourceTypes = resourceTypes;
+ }
+
+ std::vector<std::string> getResourceInterfaces(void) const
+ {
+ return m_resourceInterfaces;
+ }
+
+ void setResourceInterfaces(std::vector<std::string> resourceInterfaces)
+ {
+ m_resourceInterfaces = resourceInterfaces;
+ }
+ };
+
struct AttributeNull {};
// Forward declaration
bool,
std::string,
AttributeMapValue,
+ OCRepresentation,
AttributeValueVector> AttributeValue;
struct AttributeDataValue
std::string operator() (const std::string& str) const
{
- return str;
+ std::ostringstream json;
+ json << "\"";
+ json << str;
+ json << "\"";
+
+ return json.str();
}
std::string operator() (const bool b) const
return std::string();
std::ostringstream json;
}
+
+ std::string operator() (const OCRepresentation& rep) const
+ {
+ std::ostringstream json;
+
+ json << "\"";
+
+ json << escapeString(rep.getJSONRepresentation());
+
+ json << "\"";
+
+ return json.str();
+ }
+
};
class ParseVisitor : public boost::static_visitor<void>
// TODO Not Implemented
}
+ void operator() (OCRepresentation& rep) const
+ {
+ // TODO this will refactored
+ AttributeMap attributeMap;
+
+ std::stringstream requestStream;
+ requestStream << m_str;
+ boost::property_tree::ptree payload;
+ try
+ {
+ boost::property_tree::read_json(requestStream, payload);
+ }
+ catch(boost::property_tree::json_parser::json_parser_error &e)
+ {
+ //TODO: log this
+ std::cout << "Parse error\n";
+ return;
+ }
+
+ for(auto& item: payload)
+ {
+ std::string name = item.first.data();
+ std::string value = item.second.data();
+
+ attributeMap[name] = value;
+ }
+
+ rep.setAttributeMap(attributeMap);
+ }
+
private:
std::string m_str;
};
- typedef std::map<std::string, AttributeValue> AttributeMap1;
- std::string getJSON(const AttributeValue& v);
- void parseJSON(AttributeValue& v, std::string str);
+ inline std::string getJSON(const AttributeValue& v)
+ {
+ return boost::apply_visitor(ComposeVisitor(), v);
+ }
+
+ inline void parseJSON(AttributeValue& v, std::string str)
+ {
+ boost::apply_visitor(ParseVisitor(str), v);
+ }
+
+ template <typename T>
+ void OCRepresentation::setValue(const std::string& str, const T& val)
+ {
+ m_attributeMap[str] = getJSON(val);
+ }
+
+
+ template <typename T>
+ bool OCRepresentation::getValue(const std::string& str, T& val) const
+ {
+ auto x = m_attributeMap.find(str);
+
+ if(m_attributeMap.end() != x)
+ {
+ AttributeValue v = val;
+ parseJSON(v, x->second);
+ val = boost::get<T>(v);
+ return true;
+ }
+ else
+ {
+ return false;
+ }
+ }
+
// Typedef for query parameter map
typedef std::map<std::string, std::string> QueryParamsMap;
// 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
- {
-
- private:
- std::string m_uri;
- AttributeMap m_attributeMap;
- std::vector<std::string> m_resourceTypes;
- std::vector<std::string> m_resourceInterfaces;
- int errorCode;
-
- std::vector<OCRepresentation> m_children;
-
- public:
- OCRepresentation() {}
-
- std::string getUri(void) const
- {
- return m_uri;
- }
-
- template <typename T>
- void setValue(const std::string& str, const T& val)
- {
- m_attributeMap[str] = getJSON(val);
- }
-
- template <typename T>
- bool getValue(const std::string& str, T& val) const
- {
- auto x = m_attributeMap.find(str);
-
- if(m_attributeMap.end() != x)
- {
- AttributeValue v = val;
- parseJSON(v, x->second);
- val = boost::get<T>(v);
- return true;
- }
- else
- {
- return false;
- }
- }
-
- void setUri(std::string uri)
- {
- m_uri = uri;
- }
-
- std::vector<OCRepresentation> getChildren(void) const
- {
- return m_children;
- }
-
- void setChildren(std::vector<OCRepresentation> children)
- {
- m_children = children;
- }
-
- std::weak_ptr<OCResource> getResource() const
- {
- // TODO Needs to be implemented
- std::weak_ptr<OCResource> wp;
- return wp;
- }
-
- AttributeMap getAttributeMap() const
- {
- return m_attributeMap;
- }
-
- void setAttributeMap(const AttributeMap map)
- {
- m_attributeMap = map;
- }
-
- std::vector<std::string> getResourceTypes() const
- {
- return m_resourceTypes;
- }
-
- void setResourceTypes(std::vector<std::string> resourceTypes)
- {
- m_resourceTypes = resourceTypes;
- }
-
- std::vector<std::string> getResourceInterfaces(void) const
- {
- return m_resourceInterfaces;
- }
-
- void setResourceInterfaces(std::vector<std::string> resourceInterfaces)
- {
- m_resourceInterfaces = resourceInterfaces;
- }
- };
-
typedef std::function<void(std::shared_ptr<OCResource>)> FindCallback;
typedef std::function<void(const std::shared_ptr<OCResourceRequest>, const std::shared_ptr<OCResourceResponse>)> RegisterCallback;
typedef std::function<void(OCStackResult, const unsigned int)> SubscribeCallback;
/**
* API to set the entire resource attribute representation
- * @param attributeMap reference containing the name value pairs representing the resource's attributes
+ * @param attributeMap reference containing the name value pairs representing
+ * the resource's attributes
* @param interface specifies the interface
*/
void setResourceRepresentation(OCRepresentation& rep, std::string interface) {
/**
* API to set the entire resource attribute representation
- * @param attributeMap rvalue reference containing the name value pairs representing the resource's attributes
+ * @param attributeMap rvalue reference containing the name value pairs representing
+ * the resource's attributes
* @param interface specifies the interface
*/
void setResourceRepresentation(OCRepresentation&& rep, std::string interface) {
payload << rep.getUri();
payload << "\"" ;
- payload << ",\"rep\":{";
+ payload << ",\"rep\":";
- AttributeMap attributes = rep.getAttributeMap();
-
- for(auto itr = attributes.begin(); itr!= attributes.end(); ++ itr)
- {
- if(itr != attributes.begin())
- {
- payload << ',';
- }
- payload << "\""<<itr->first<<"\":\""<< itr->second <<"\"";
- }
+ payload << rep.getJSONRepresentation();
- payload << "}}";
+ payload << "}";
// Children stuff
std::vector<OCRepresentation> children = rep.getChildren();
payload << oitr->getUri();
payload << "\"" ;
- payload << ",\"rep\":{";
+ payload << ",\"rep\":";
- AttributeMap attributes = oitr->getAttributeMap();
+ payload << oitr->getJSONRepresentation();
- for(AttributeMap::const_iterator itr = attributes.begin(); itr!= attributes.end(); ++ itr)
- {
- if(itr != attributes.begin())
- {
- payload << ',';
- }
- payload << "\""<<itr->first<<"\":\""<< itr->second<<"\"";
- }
-
- payload << "}}";
+ payload << "}";
}
m_payload = payload.str();
std::string InProcClientWrapper::assembleSetResourcePayload(const OCRepresentation& rep)
{
- AttributeMap attributes = rep.getAttributeMap();
-
ostringstream payload;
// TODO need to change the format to "{"oc":[]}"
- payload << "{\"oc\":{";
+ payload << "{\"oc\":";
- for(AttributeMap::const_iterator itr = attributes.begin(); itr!= attributes.end(); ++ itr)
- {
- if(itr != attributes.begin())
- {
- payload << ',';
- }
+ payload << rep.getJSONRepresentation();
- payload << "\""<<itr->first<<"\":\""<< itr->second <<"\"";
- }
- payload << "}}";
+ payload << "}";
return payload.str();
}
namespace OC{
- std::string getJSON(const AttributeValue& v)
- {
- return boost::apply_visitor(ComposeVisitor(), v);
- }
-
- void parseJSON(AttributeValue& v, std::string str)
- {
- boost::apply_visitor(ParseVisitor(str), v);
- }
-
// Helper function to escape special character.
std::string escapeString(const std::string& value)
{