summaryrefslogtreecommitdiff
path: root/src/components/formatters/src
diff options
context:
space:
mode:
Diffstat (limited to 'src/components/formatters/src')
-rw-r--r--src/components/formatters/src/CFormatterJsonBase.cpp118
-rw-r--r--src/components/formatters/src/CFormatterJsonSDLRPCv1.cpp157
-rw-r--r--src/components/formatters/src/CFormatterJsonSDLRPCv2.cpp106
-rw-r--r--src/components/formatters/src/CSmartFactory.cpp46
-rw-r--r--src/components/formatters/src/formatter_json_rpc.cc204
-rw-r--r--src/components/formatters/src/generic_json_formatter.cc63
-rw-r--r--src/components/formatters/src/meta_formatter.cc60
7 files changed, 754 insertions, 0 deletions
diff --git a/src/components/formatters/src/CFormatterJsonBase.cpp b/src/components/formatters/src/CFormatterJsonBase.cpp
new file mode 100644
index 0000000000..64a60e4f8f
--- /dev/null
+++ b/src/components/formatters/src/CFormatterJsonBase.cpp
@@ -0,0 +1,118 @@
+/**
+ * @file CFormatterJsonBase.cpp
+ * @brief CFormatterJsonBase source file.
+ */
+// Copyright (c) 2013, Ford Motor Company
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+// Redistributions of source code must retain the above copyright notice, this
+// list of conditions and the following disclaimer.
+//
+// Redistributions in binary form must reproduce the above copyright notice,
+// this list of conditions and the following
+// disclaimer in the documentation and/or other materials provided with the
+// distribution.
+//
+// Neither the name of the Ford Motor Company nor the names of its contributors
+// may be used to endorse or promote products derived from this software
+// without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 'A PARTICULAR PURPOSE
+// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+// POSSIBILITY OF SUCH DAMAGE.
+#include "json/json.h"
+
+#include "formatters/CFormatterJsonBase.hpp"
+
+void NsSmartDeviceLink::NsJSONHandler::Formatters::CFormatterJsonBase::jsonValueToObj(
+ const Json::Value& value,
+ NsSmartDeviceLink::NsSmartObjects::SmartObject& obj) {
+ try {
+ if (value.type() == Json::objectValue) {
+ obj = NsSmartDeviceLink::NsSmartObjects::SmartObject(
+ NsSmartDeviceLink::NsSmartObjects::SmartType_Map);
+
+ Json::Value::Members members = value.getMemberNames();
+
+ for (uint32_t i = 0; i < members.size(); i++) {
+ jsonValueToObj(value[members[i]], obj[members[i]]);
+ }
+ } else if (value.type() == Json::arrayValue) {
+ obj = NsSmartDeviceLink::NsSmartObjects::SmartObject(
+ NsSmartDeviceLink::NsSmartObjects::SmartType_Array);
+
+ for (uint32_t i = 0; i < value.size(); i++) {
+ jsonValueToObj(value[i], obj[i]);
+ }
+ } else if (value.type() == Json::intValue
+ || value.type() == Json::uintValue) {
+ obj = value.asInt();
+ } else if (value.type() == Json::realValue) {
+ obj = value.asDouble();
+ } else if (value.type() == Json::booleanValue) {
+ obj = value.asBool();
+ } else if (value.type() == Json::stringValue) {
+ obj = value.asString();
+ }
+ } catch (...) {
+ }
+}
+
+// ----------------------------------------------------------------------------
+
+void NsSmartDeviceLink::NsJSONHandler::Formatters::CFormatterJsonBase::objToJsonValue(
+ const NsSmartDeviceLink::NsSmartObjects::SmartObject &obj,
+ Json::Value &item) {
+ try {
+ if (NsSmartDeviceLink::NsSmartObjects::SmartType_Array == obj.getType()) {
+ item = Json::arrayValue;
+
+ for (uint32_t i = 0; i < obj.length(); i++) {
+ Json::Value value(Json::nullValue);
+
+ objToJsonValue(obj.getElement(i), value);
+
+ item.append(value);
+ }
+ } else if (NsSmartDeviceLink::NsSmartObjects::SmartType_Map
+ == obj.getType()) {
+ item = Json::objectValue;
+ std::set<std::string> keys = obj.enumerate();
+
+ for (std::set<std::string>::const_iterator key = keys.begin();
+ key != keys.end(); key++) {
+ Json::Value value(Json::nullValue);
+
+ objToJsonValue(obj.getElement(*key), value);
+
+ item[*key] = value;
+ }
+ } else if (NsSmartDeviceLink::NsSmartObjects::SmartType_Boolean
+ == obj.getType()) {
+ item = obj.asBool();
+ } else if (NsSmartDeviceLink::NsSmartObjects::SmartType_Integer
+ == obj.getType()) {
+ item = obj.asInt();
+ } else if (NsSmartDeviceLink::NsSmartObjects::SmartType_Double
+ == obj.getType()) {
+ item = obj.asDouble();
+ } else if (NsSmartDeviceLink::NsSmartObjects::SmartType_Null
+ == obj.getType()) {
+ item = Json::nullValue;
+ } else {
+ item = obj.asString();
+ }
+ } catch (...) {
+ }
+}
diff --git a/src/components/formatters/src/CFormatterJsonSDLRPCv1.cpp b/src/components/formatters/src/CFormatterJsonSDLRPCv1.cpp
new file mode 100644
index 0000000000..407a001883
--- /dev/null
+++ b/src/components/formatters/src/CFormatterJsonSDLRPCv1.cpp
@@ -0,0 +1,157 @@
+// Copyright (c) 2013, Ford Motor Company
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+// Redistributions of source code must retain the above copyright notice, this
+// list of conditions and the following disclaimer.
+//
+// Redistributions in binary form must reproduce the above copyright notice,
+// this list of conditions and the following
+// disclaimer in the documentation and/or other materials provided with the
+// distribution.
+//
+// Neither the name of the Ford Motor Company nor the names of its contributors
+// may be used to endorse or promote products derived from this software
+// without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 'A PARTICULAR PURPOSE
+// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+// POSSIBILITY OF SUCH DAMAGE.
+#include "formatters/CFormatterJsonSDLRPCv1.hpp"
+#include "formatters/meta_formatter.h"
+
+namespace strings = NsSmartDeviceLink::NsJSONHandler::strings;
+namespace smart_objects_ns = NsSmartDeviceLink::NsSmartObjects;
+
+namespace NsSmartDeviceLink {
+namespace NsJSONHandler {
+namespace Formatters {
+
+// ----------------------------------------------------------------------------
+
+const std::string CFormatterJsonSDLRPCv1::S_REQUEST("request");
+const std::string CFormatterJsonSDLRPCv1::S_RESPONSE("response");
+const std::string CFormatterJsonSDLRPCv1::S_NOTIFICATION("notification");
+const std::string CFormatterJsonSDLRPCv1::S_PARAMETERS("parameters");
+const std::string CFormatterJsonSDLRPCv1::S_NAME("name");
+const std::string CFormatterJsonSDLRPCv1::S_CORRELATION_ID("correlationID");
+
+const int32_t CFormatterJsonSDLRPCv1::kSuccess = 0;
+const int32_t CFormatterJsonSDLRPCv1::kParsingError = 1 << 0;
+const int32_t CFormatterJsonSDLRPCv1::kFunctionIdNotFound = 1 << 1;
+const int32_t CFormatterJsonSDLRPCv1::kMessageTypeNotFound = 1 << 2;
+const int32_t CFormatterJsonSDLRPCv1::kCorrelationIdNotFound = 1 << 3;
+// ----------------------------------------------------------------------------
+
+const std::string CFormatterJsonSDLRPCv1::getMessageType(
+ const smart_objects_ns::SmartObject& obj) {
+ return obj.getElement(strings::S_PARAMS).getElement(strings::S_MESSAGE_TYPE)
+ .asString();
+}
+
+// ----------------------------------------------------------------------------
+
+const std::string CFormatterJsonSDLRPCv1::getMessageType(
+ const Json::Value& root) {
+ std::string type;
+
+ if (true == root.isMember(S_REQUEST)) {
+ type = S_REQUEST;
+ } else if (true == root.isMember(S_RESPONSE)) {
+ type = S_RESPONSE;
+ } else if (true == root.isMember(S_NOTIFICATION)) {
+ type = S_NOTIFICATION;
+ } else {
+ }
+
+ return type;
+}
+
+// ----------------------------------------------------------------------------
+
+bool CFormatterJsonSDLRPCv1::toString(const smart_objects_ns::SmartObject& obj,
+ std::string& outStr) {
+ bool result = false;
+ try {
+ Json::Value root(Json::objectValue);
+ Json::Value params(Json::objectValue);
+
+ smart_objects_ns::SmartObject formattedObj(obj);
+ formattedObj.getSchema().unapplySchema(formattedObj); // converts enums(as int32_t) to strings
+
+ objToJsonValue(formattedObj.getElement(strings::S_MSG_PARAMS), params);
+
+ std::string type = getMessageType(formattedObj);
+ root[type] = Json::Value(Json::objectValue);
+ root[type][S_PARAMETERS] = params;
+
+ if (formattedObj[strings::S_PARAMS].keyExists(strings::S_CORRELATION_ID)) {
+ root[type][S_CORRELATION_ID] =
+ formattedObj[strings::S_PARAMS][strings::S_CORRELATION_ID].asInt();
+ }
+
+ root[type][S_NAME] = formattedObj[strings::S_PARAMS][strings::S_FUNCTION_ID]
+ .asString();
+
+ outStr = root.toStyledString();
+
+ result = true;
+ } catch (...) {
+ result = false;
+ }
+
+ return result;
+}
+
+// ----------------------------------------------------------------------------
+
+CFormatterJsonSDLRPCv1::tMetaFormatterErrorCode CFormatterJsonSDLRPCv1::MetaFormatToString(
+ const smart_objects_ns::SmartObject& object,
+ const smart_objects_ns::CSmartSchema& schema, std::string& outStr) {
+ meta_formatter_error_code::tMetaFormatterErrorCode result_code =
+ meta_formatter_error_code::kErrorOk;
+
+ smart_objects_ns::SmartObject tmp_object;
+
+ if (false
+ == CMetaFormatter::CreateObjectByPattern(object, schema, tmp_object)) {
+ result_code |= meta_formatter_error_code::kErrorFailedCreateObjectBySchema;
+ return result_code;
+ }
+
+ // determine whether smart objects are functions
+ // (in terms of SDLRPC communication)
+ bool is_root_object_created_by_schema = ((tmp_object.getType()
+ == smart_objects_ns::SmartType_Map)
+ && tmp_object.keyExists(strings::S_PARAMS)
+ && tmp_object.keyExists(strings::S_MSG_PARAMS));
+
+ bool is_root_object = ((object.getType() == smart_objects_ns::SmartType_Map)
+ && object.keyExists(strings::S_PARAMS)
+ && object.keyExists(strings::S_MSG_PARAMS));
+
+ if (false == is_root_object) {
+ result_code |= meta_formatter_error_code::kErrorObjectIsNotFunction;
+ }
+ if (false == is_root_object_created_by_schema) {
+ result_code |= meta_formatter_error_code::kErrorSchemaIsNotFunction;
+ }
+
+ CFormatterJsonSDLRPCv1::toString(tmp_object, outStr);
+
+ return result_code;
+}
+
+}
+}
+}
diff --git a/src/components/formatters/src/CFormatterJsonSDLRPCv2.cpp b/src/components/formatters/src/CFormatterJsonSDLRPCv2.cpp
new file mode 100644
index 0000000000..d76e28911c
--- /dev/null
+++ b/src/components/formatters/src/CFormatterJsonSDLRPCv2.cpp
@@ -0,0 +1,106 @@
+// Copyright (c) 2013, Ford Motor Company
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+// Redistributions of source code must retain the above copyright notice, this
+// list of conditions and the following disclaimer.
+//
+// Redistributions in binary form must reproduce the above copyright notice,
+// this list of conditions and the following
+// disclaimer in the documentation and/or other materials provided with the
+// distribution.
+//
+// Neither the name of the Ford Motor Company nor the names of its contributors
+// may be used to endorse or promote products derived from this software
+// without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 'A PARTICULAR PURPOSE
+// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+// POSSIBILITY OF SUCH DAMAGE.
+
+#include "formatters/CFormatterJsonSDLRPCv2.hpp"
+#include "formatters/meta_formatter.h"
+
+namespace smart_objects_ns = NsSmartDeviceLink::NsSmartObjects;
+namespace strings = NsSmartDeviceLink::NsJSONHandler::strings;
+
+namespace NsSmartDeviceLink {
+namespace NsJSONHandler {
+namespace Formatters {
+
+// ----------------------------------------------------------------------------
+
+bool CFormatterJsonSDLRPCv2::toString(const smart_objects_ns::SmartObject& obj,
+ std::string& outStr) {
+ bool result = true;
+ try {
+ Json::Value root(Json::objectValue);
+
+ smart_objects_ns::SmartObject formattedObj(obj);
+ formattedObj.getSchema().unapplySchema(formattedObj); // converts enums(as int32_t) to strings
+
+ objToJsonValue(formattedObj.getElement(strings::S_MSG_PARAMS), root);
+
+ outStr = root.toStyledString();
+
+ result = true;
+ } catch (...) {
+ result = false;
+ }
+
+ return result;
+}
+
+// ----------------------------------------------------------------------------
+
+CFormatterJsonSDLRPCv2::tMetaFormatterErrorCode CFormatterJsonSDLRPCv2::MetaFormatToString(
+ const smart_objects_ns::SmartObject& object,
+ const smart_objects_ns::CSmartSchema& schema, std::string& outStr) {
+
+ meta_formatter_error_code::tMetaFormatterErrorCode result_code =
+ meta_formatter_error_code::kErrorOk;
+
+ smart_objects_ns::SmartObject tmp_object;
+
+ if (false
+ == CMetaFormatter::CreateObjectByPattern(object, schema, tmp_object)) {
+ result_code |= meta_formatter_error_code::kErrorFailedCreateObjectBySchema;
+ return result_code;
+ }
+
+ // determine whether smart objects are functions
+ // (in terms of SDLRPC communication)
+ bool is_root_object_created_by_schema = ((tmp_object.getType()
+ == smart_objects_ns::SmartType_Map)
+ && tmp_object.keyExists(strings::S_PARAMS)
+ && tmp_object.keyExists(strings::S_MSG_PARAMS));
+
+ bool is_root_object = ((object.getType() == smart_objects_ns::SmartType_Map)
+ && object.keyExists(strings::S_PARAMS)
+ && object.keyExists(strings::S_MSG_PARAMS));
+
+ if (false == is_root_object) {
+ result_code |= meta_formatter_error_code::kErrorObjectIsNotFunction;
+ }
+ if (false == is_root_object_created_by_schema) {
+ result_code |= meta_formatter_error_code::kErrorSchemaIsNotFunction;
+ }
+
+ CFormatterJsonSDLRPCv2::toString(tmp_object, outStr);
+
+ return result_code;
+}
+
+}
+}
+}
diff --git a/src/components/formatters/src/CSmartFactory.cpp b/src/components/formatters/src/CSmartFactory.cpp
new file mode 100644
index 0000000000..363db7696e
--- /dev/null
+++ b/src/components/formatters/src/CSmartFactory.cpp
@@ -0,0 +1,46 @@
+/**
+ * @file CSmartFactory.cpp
+ * @brief CSmartFactory source file.
+ */
+// Copyright (c) 2013, Ford Motor Company
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+// Redistributions of source code must retain the above copyright notice, this
+// list of conditions and the following disclaimer.
+//
+// Redistributions in binary form must reproduce the above copyright notice,
+// this list of conditions and the following
+// disclaimer in the documentation and/or other materials provided with the
+// distribution.
+//
+// Neither the name of the Ford Motor Company nor the names of its contributors
+// may be used to endorse or promote products derived from this software
+// without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 'A PARTICULAR PURPOSE
+// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+// POSSIBILITY OF SUCH DAMAGE.
+
+#include "formatters/CSmartFactory.hpp"
+
+const std::string NsSmartDeviceLink::NsJSONHandler::strings::S_MSG_PARAMS("msg_params");
+const std::string NsSmartDeviceLink::NsJSONHandler::strings::S_PARAMS("params");
+const std::string NsSmartDeviceLink::NsJSONHandler::strings::S_FUNCTION_ID("function_id");
+const std::string NsSmartDeviceLink::NsJSONHandler::strings::S_MESSAGE_TYPE("message_type");
+const std::string NsSmartDeviceLink::NsJSONHandler::strings::S_PROTOCOL_VERSION("protocol_version");
+const std::string NsSmartDeviceLink::NsJSONHandler::strings::S_PROTOCOL_TYPE("protocol_type");
+const std::string NsSmartDeviceLink::NsJSONHandler::strings::S_CORRELATION_ID("correlation_id");
+const std::string NsSmartDeviceLink::NsJSONHandler::strings::kCode("code");
+const std::string NsSmartDeviceLink::NsJSONHandler::strings::kMessage(
+ "message");
diff --git a/src/components/formatters/src/formatter_json_rpc.cc b/src/components/formatters/src/formatter_json_rpc.cc
new file mode 100644
index 0000000000..8722df46df
--- /dev/null
+++ b/src/components/formatters/src/formatter_json_rpc.cc
@@ -0,0 +1,204 @@
+/**
+ * @file formatter_json_rpc.cc
+ * @brief formatter_json_rpc source file.
+ */
+// Copyright (c) 2013, Ford Motor Company
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+// Redistributions of source code must retain the above copyright notice, this
+// list of conditions and the following disclaimer.
+//
+// Redistributions in binary form must reproduce the above copyright notice,
+// this list of conditions and the following
+// disclaimer in the documentation and/or other materials provided with the
+// distribution.
+//
+// Neither the name of the Ford Motor Company nor the names of its contributors
+// may be used to endorse or promote products derived from this software
+// without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 'A PARTICULAR PURPOSE
+// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+// POSSIBILITY OF SUCH DAMAGE.
+
+#include "formatters/formatter_json_rpc.h"
+
+namespace NsSmartDeviceLink {
+namespace NsJSONHandler {
+namespace Formatters {
+
+namespace strings = NsJSONHandler::strings;
+
+const char *FormatterJsonRpc::kRequest = "request";
+const char *FormatterJsonRpc::kResponse = "response";
+const char *FormatterJsonRpc::kNotification = "notification";
+const char *FormatterJsonRpc::kErrorResponse = "error_response";
+const char *FormatterJsonRpc::kJsonRpc = "jsonrpc";
+const char *FormatterJsonRpc::kJsonRpcExpectedValue = "2.0";
+const char *FormatterJsonRpc::kId = "id";
+const char *FormatterJsonRpc::kMethod = "method";
+const char *FormatterJsonRpc::kParams = "params";
+const char *FormatterJsonRpc::kResult = "result";
+const char *FormatterJsonRpc::kError = "error";
+const char *FormatterJsonRpc::kCode = "code";
+const char *FormatterJsonRpc::kData = "data";
+const char *FormatterJsonRpc::kMessage = "message";
+
+bool FormatterJsonRpc::ToString(const NsSmartObjects::SmartObject &obj,
+ std::string &out_str) {
+ bool result = true;
+ try {
+ Json::Value root(Json::objectValue);
+
+ root[kJsonRpc] = kJsonRpcExpectedValue;
+
+ NsSmartObjects::SmartObject formatted_object(obj);
+ Json::Value msg_params_json(Json::objectValue);
+ formatted_object.getSchema().unapplySchema(formatted_object);
+
+ bool is_message_params = formatted_object.keyExists(strings::S_MSG_PARAMS);
+ bool empty_message_params = true;
+ if (true == is_message_params) {
+ const NsSmartObjects::SmartObject &msg_params = formatted_object
+ .getElement(strings::S_MSG_PARAMS);
+
+ result = (NsSmartObjects::SmartType_Map == msg_params.getType());
+ if (true == result) {
+ objToJsonValue(msg_params, msg_params_json);
+ }
+ if (0 < msg_params.length()) {
+ empty_message_params = false;
+ }
+ }
+
+ if (false == formatted_object.keyExists(strings::S_PARAMS)) {
+ result = false;
+ } else {
+ const NsSmartObjects::SmartObject &params = formatted_object.getElement(
+ strings::S_PARAMS);
+ if (NsSmartObjects::SmartType_Map != params.getType()) {
+ result = false;
+ } else {
+ const NsSmartObjects::SmartObject &message_type_object = params
+ .getElement(strings::S_MESSAGE_TYPE);
+
+ if (NsSmartObjects::SmartType_String != message_type_object.getType()) {
+ result = false;
+ } else {
+ const std::string message_type = message_type_object.asString();
+
+ if (kRequest == message_type) {
+ if (false == empty_message_params) {
+ root[kParams] = msg_params_json;
+ }
+ result = result && SetMethod(params, root);
+ result = result && SetId(params, root);
+ } else if (kResponse == message_type) {
+ root[kResult] = msg_params_json;
+ result = result && SetMethod(params, root[kResult]);
+ result = result && SetId(params, root);
+
+ if (false == params.keyExists(strings::kCode)) {
+ result = false;
+ } else {
+ const NsSmartObjects::SmartObject &code = params.getElement(
+ strings::kCode);
+
+ if (NsSmartObjects::SmartType_Integer != code.getType()) {
+ result = false;
+ } else {
+ root[kResult][kCode] = code.asInt();
+ }
+ }
+ } else if (kNotification == message_type) {
+ root[kParams] = msg_params_json;
+ result = result && SetMethod(params, root);
+ } else if (kErrorResponse == message_type) {
+ result = result && SetId(params, root);
+ result = result && SetMethod(params, root[kError][kData]);
+ result = result && SetMessage(params, root[kError]);
+
+ const NsSmartObjects::SmartObject &code = params.getElement(
+ strings::kCode);
+ if (NsSmartObjects::SmartType_Integer != code.getType()) {
+ result = false;
+ } else {
+ root[kError][kCode] = code.asInt();
+ }
+ }
+ }
+ }
+ }
+ out_str = root.toStyledString();
+ } catch (...) {
+ result = false;
+ }
+
+ return result;
+}
+
+bool FormatterJsonRpc::SetMethod(const NsSmartObjects::SmartObject &params,
+ Json::Value &method_container) {
+ bool result = false;
+
+ if (true == params.keyExists(strings::S_FUNCTION_ID)) {
+ const NsSmartObjects::SmartObject &function_id =
+ params.getElement(strings::S_FUNCTION_ID);
+
+ if (NsSmartObjects::SmartType_String == function_id.getType()) {
+ method_container[kMethod] = function_id.asString();
+ result = true;
+ }
+ }
+
+ return result;
+}
+
+bool FormatterJsonRpc::SetId(const NsSmartObjects::SmartObject &params,
+ Json::Value &id_container) {
+ bool result = false;
+
+ if (true == params.keyExists(strings::S_CORRELATION_ID)) {
+ const NsSmartObjects::SmartObject &id = params.getElement(
+ strings::S_CORRELATION_ID);
+
+ if (NsSmartObjects::SmartType_Integer == id.getType()) {
+ id_container[kId] = id.asUInt();
+ result = true;
+ }
+ }
+
+ return result;
+}
+
+bool FormatterJsonRpc::SetMessage(const NsSmartObjects::SmartObject &params,
+ Json::Value &message_container) {
+ bool result = false;
+
+ if (true == params.keyExists(strings::kMessage)) {
+ const NsSmartObjects::SmartObject &message = params.getElement(
+ strings::kMessage);
+
+ if (NsSmartObjects::SmartType_String == message.getType()) {
+ message_container[kMessage] = message.asString();
+ result = true;
+ }
+ }
+
+ return result;
+}
+
+} // namespace Formatters
+} // namespace NsJSONHandler
+} // namespace NsSmartDeviceLink
diff --git a/src/components/formatters/src/generic_json_formatter.cc b/src/components/formatters/src/generic_json_formatter.cc
new file mode 100644
index 0000000000..ce1aa03807
--- /dev/null
+++ b/src/components/formatters/src/generic_json_formatter.cc
@@ -0,0 +1,63 @@
+/**
+ * @file generic_json_formatter.cc
+ * @brief Generic JSON formatter source file.
+ */
+// Copyright (c) 2013, Ford Motor Company
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+// Redistributions of source code must retain the above copyright notice, this
+// list of conditions and the following disclaimer.
+//
+// Redistributions in binary form must reproduce the above copyright notice,
+// this list of conditions and the following
+// disclaimer in the documentation and/or other materials provided with the
+// distribution.
+//
+// Neither the name of the Ford Motor Company nor the names of its contributors
+// may be used to endorse or promote products derived from this software
+// without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 'A PARTICULAR PURPOSE
+// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+// POSSIBILITY OF SUCH DAMAGE.
+
+#include "formatters/generic_json_formatter.h"
+
+namespace NsSmartDeviceLink {
+namespace NsJSONHandler {
+namespace Formatters {
+
+void GenericJsonFormatter::ToString(const NsSmartObjects::SmartObject& obj,
+ std::string& out_str) {
+ Json::Value json_root;
+ objToJsonValue(obj, json_root);
+ out_str = json_root.toStyledString();
+}
+
+bool GenericJsonFormatter::FromString(const std::string& str,
+ NsSmartObjects::SmartObject& out) {
+ Json::Value json_root;
+ Json::Reader reader;
+ bool result = reader.parse(str, json_root);
+
+ if (true == result) {
+ jsonValueToObj(json_root, out);
+ }
+
+ return result;
+}
+
+} // namespace Formatters
+} // namespace NsJSONHandler
+} // namespace NsSmartDeviceLink
diff --git a/src/components/formatters/src/meta_formatter.cc b/src/components/formatters/src/meta_formatter.cc
new file mode 100644
index 0000000000..145020a5e2
--- /dev/null
+++ b/src/components/formatters/src/meta_formatter.cc
@@ -0,0 +1,60 @@
+/**
+ * @file meta_fromatter.cc
+ * @brief implementation of class CMetaFormatter which is designed to format
+ * the smart object against given schema for given formatter
+ */
+// Copyright (c) 2013, Ford Motor Company
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+// Redistributions of source code must retain the above copyright notice, this
+// list of conditions and the following disclaimer.
+//
+// Redistributions in binary form must reproduce the above copyright notice,
+// this list of conditions and the following
+// disclaimer in the documentation and/or other materials provided with the
+// distribution.
+//
+// Neither the name of the Ford Motor Company nor the names of its contributors
+// may be used to endorse or promote products derived from this software
+// without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 'A PARTICULAR PURPOSE
+// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+// POSSIBILITY OF SUCH DAMAGE.auto
+
+#include <set>
+#include <string>
+
+#include "formatters/meta_formatter.h"
+
+namespace formatter_ns = NsSmartDeviceLink::NsJSONHandler::Formatters;
+namespace smart_objects_ns = NsSmartDeviceLink::NsSmartObjects;
+
+//---------------------------------------------------------------
+
+bool formatter_ns::CMetaFormatter::CreateObjectByPattern(
+ const NsSmartDeviceLink::NsSmartObjects::SmartObject& object,
+ const NsSmartDeviceLink::NsSmartObjects::CSmartSchema& schema,
+ NsSmartDeviceLink::NsSmartObjects::SmartObject& result_object) {
+
+ if (smart_objects_ns::SmartType_Invalid == result_object.getType()) {
+ return false;
+ }
+
+ schema.BuildObjectBySchema(object, result_object);
+
+ result_object.setSchema(schema);
+
+ return true;
+}