diff options
Diffstat (limited to 'SDL_Core/src/components/formatters/include')
7 files changed, 1604 insertions, 0 deletions
diff --git a/SDL_Core/src/components/formatters/include/formatters/CFormatterJsonBase.hpp b/SDL_Core/src/components/formatters/include/formatters/CFormatterJsonBase.hpp new file mode 100644 index 000000000..ef57d9b1b --- /dev/null +++ b/SDL_Core/src/components/formatters/include/formatters/CFormatterJsonBase.hpp @@ -0,0 +1,122 @@ +/** + * @file CFormatterJsonBase.hpp + * @brief CFormatterJsonBase header 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. + +#ifndef __CFORMATTERJSONBASE_HPP__ +#define __CFORMATTERJSONBASE_HPP__ + +#include "smart_objects/smart_object.h" +#include "json/json.h" + +namespace NsSmartDeviceLink { +namespace NsJSONHandler { +namespace Formatters { + +namespace meta_formatter_error_code { + /** + * @brief Error codes of MetaFormatter represented as bitmask + **/ + typedef long tMetaFormatterErrorCode ; + + /** + * @brief OK, no error + */ + static const tMetaFormatterErrorCode kErrorOk = 0x0; + + /** + * @brief origin smart object is not function + */ + static const tMetaFormatterErrorCode kErrorObjectIsNotFunction = 0x01; + + /** + * @brief smart shema describes object which is not function + */ + static const tMetaFormatterErrorCode kErrorSchemaIsNotFunction = 0x02; + + /** + * @brief result smart object has invalid type (SmartType_Invalid) + * before passing to MetaFormatter, i.e. result object can not + * be changed, i.e. result object can not be built + * + */ + static const tMetaFormatterErrorCode kErrorFailedCreateObjectBySchema = 0x04; +} + + /** + * @brief The base class for all JSON based formatters. + */ + class CFormatterJsonBase + { + private: + + /** + * @brief Constructor. + */ + CFormatterJsonBase(); + + /** + * @brief Copy constructor. + * + * @param obj Object to copy. + */ + CFormatterJsonBase(const CFormatterJsonBase &obj); + + protected: + + + public: + + /** + * @brief The method constructs a SmartObject from the input JSON object + * + * @param value Input JSON object. + * @param obj The resulting SmartObject. + */ + static void jsonValueToObj(const Json::Value &value, + NsSmartDeviceLink::NsSmartObjects::SmartObject &obj); + + /** + * @brief The method constructs a JSON object from the input SmartObject + * + * @param obj Input SmartObject. Can contain a complex structure of objects. + * @param value The resulting JSON object. It has the same structure as the input SmartObject. + */ + static void objToJsonValue(const NsSmartDeviceLink::NsSmartObjects::SmartObject &obj, + Json::Value &value); + }; + +} +} +} // namespace NsSmartDeviceLink::NsJSONHandler::Formatters + +#endif // __CFORMATTERJSONBASE_HPP__ diff --git a/SDL_Core/src/components/formatters/include/formatters/CFormatterJsonSDLRPCv1.hpp b/SDL_Core/src/components/formatters/include/formatters/CFormatterJsonSDLRPCv1.hpp new file mode 100644 index 000000000..3b24a8a71 --- /dev/null +++ b/SDL_Core/src/components/formatters/include/formatters/CFormatterJsonSDLRPCv1.hpp @@ -0,0 +1,238 @@ +// 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. +#ifndef __SMARTDEVICELINKCORE_JSONHANDLER_FORMATTERS__CFORMATTERJSONSDLRPCV1_HPP__ +#define __SMARTDEVICELINKCORE_JSONHANDLER_FORMATTERS__CFORMATTERJSONSDLRPCV1_HPP__ + +#include "json/json.h" + +#include "smart_objects/smart_object.h" +#include "smart_objects/enum_schema_item.h" + +#include "CFormatterJsonBase.hpp" + +#include "formatters/CSmartFactory.hpp" +#include "formatters/meta_formatter.h" + +namespace NsSmartDeviceLink { +namespace NsJSONHandler { +namespace Formatters { + +/** + * @brief Class is used to convert SmartObjects to JSON string and vice versa. + * + * JSON strings is in SDLRPCv1 format. + */ +class CFormatterJsonSDLRPCv1 : public CFormatterJsonBase { + private: + /** + * @brief Hidden constructor. + * + * The class contains only static methods. Should not be instantiated. + */ + CFormatterJsonSDLRPCv1(); + + /** + * @brief Hidden copy constructor. + * + * The class contains only static methods. Should not be instantiated. + */ + CFormatterJsonSDLRPCv1(const CFormatterJsonSDLRPCv1&); + + /** + * @brief Extracts a message type from the SmartObject + */ + static const std::string getMessageType( + const NsSmartDeviceLink::NsSmartObjects::SmartObject& obj); + + /** + * @brief Extracts a message type from the root JSON object. + * + * @return Type or empty string if there's no type in the JSON object. + */ + static const std::string getMessageType(const Json::Value& root); + + // SDLRPCv1 string consts + + /** + * @brief String constant for REQUEST. + */ + static const std::string S_REQUEST; + + /** + * @brief String constant for RESPONSE. + */ + static const std::string S_RESPONSE; + + /** + * @brief String constant for NOTIFICATION. + */ + static const std::string S_NOTIFICATION; + + /** + * @brief String constant for PARAMETERS. + */ + static const std::string S_PARAMETERS; + + /** + * @brief String constant for NAME. + */ + static const std::string S_NAME; + + /** + * @brief String constant for CORRELATION_ID. + */ + static const std::string S_CORRELATION_ID; + + public: + + static const int32_t kSuccess; + static const int32_t kParsingError; + static const int32_t kFunctionIdNotFound; + static const int32_t kMessageTypeNotFound; + static const int32_t kCorrelationIdNotFound; + + typedef NsSmartDeviceLink::NsJSONHandler::Formatters::meta_formatter_error_code::tMetaFormatterErrorCode tMetaFormatterErrorCode; + + /** + * @brief Creates a JSON string from a SmartObject. + * + * @param obj input SmartObject + * @param outStr resulting JSON string + * @return true if success, false otherwise + */ + static bool toString( + const NsSmartDeviceLink::NsSmartObjects::SmartObject &obj, + std::string& outStr); + + /** + * @brief Creates a SmartObject from a JSON string. + * + * @param str input JSON string + * @param out The resulting SmartObject + * @return true if success, otherwise - false + */ + template<typename FunctionId, typename MessageType> + static int32_t fromString(const std::string &str, + NsSmartDeviceLink::NsSmartObjects::SmartObject &out); + + /** + * @brief Converts to string the smart object against the given schema + * + * @param object Original smart object + * @param schema Smart schema which describes 'fake' smart object to be formatted + * @param outStr Resulting JSON string + * @return formatting error code + */ + static tMetaFormatterErrorCode MetaFormatToString( + const NsSmartDeviceLink::NsSmartObjects::SmartObject& object, + const NsSmartDeviceLink::NsSmartObjects::CSmartSchema& schema, + std::string& outStr); + +}; + +// ---------------------------------------------------------------------------- + +template<typename FunctionId, typename MessageType> +int32_t Formatters::CFormatterJsonSDLRPCv1::fromString( + const std::string& str, + NsSmartDeviceLink::NsSmartObjects::SmartObject& out) { + int32_t result = kSuccess; + + try { + Json::Value root; + Json::Reader reader; + std::string type; + + if (false == reader.parse(str, root)) { + result = kParsingError | kMessageTypeNotFound | kFunctionIdNotFound + | kCorrelationIdNotFound; + } + + if (kSuccess == result) { + type = getMessageType(root); + if (true == type.empty()) { + result = kMessageTypeNotFound | kFunctionIdNotFound + | kCorrelationIdNotFound; + } + } + + FunctionId functionId; + MessageType messageType; + + if (kSuccess == result) { + typedef NsSmartDeviceLink::NsSmartObjects::TEnumSchemaItem<MessageType> MessageTypeEnum; + if (false == MessageTypeEnum::stringToEnum(type, messageType)) { + // If MessageType is not found than FunctionId and CorrelationId can not be found either + result = kMessageTypeNotFound | kFunctionIdNotFound + | kCorrelationIdNotFound; + } + } + + if (kSuccess == result) { + typedef NsSmartDeviceLink::NsSmartObjects::TEnumSchemaItem<FunctionId> FunctionIdEnum; + if (false + == FunctionIdEnum::stringToEnum(root[type][S_NAME].asString(), + functionId)) { + result = kFunctionIdNotFound; + functionId = FunctionId::INVALID_ENUM; + } + } + + namespace S = NsSmartDeviceLink::NsJSONHandler::strings; + + if (!(result & kMessageTypeNotFound)) { + jsonValueToObj(root[type][S_PARAMETERS], out[S::S_MSG_PARAMS]); + + out[S::S_PARAMS][S::S_MESSAGE_TYPE] = messageType; + out[S::S_PARAMS][S::S_FUNCTION_ID] = functionId; + if (true == root[type][S_CORRELATION_ID].empty()) { + if (type != S_NOTIFICATION) { // Notification may not have CorrelationId + result |= kCorrelationIdNotFound; + out[S::S_PARAMS][S::S_CORRELATION_ID] = -1; + } + } else { + out[S::S_PARAMS][S::S_CORRELATION_ID] = root[type][S_CORRELATION_ID] + .asInt(); + } + out[S::S_PARAMS][S::S_PROTOCOL_TYPE] = 0; + out[S::S_PARAMS][S::S_PROTOCOL_VERSION] = 1; + } + } catch (...) { + result = kParsingError; + } + + return result; +} + +} +} +} // namespace NsSmartDeviceLink::NsJSONHandler::Formatters + +#endif // __SMARTDEVICELINKCORE_JSONHANDLER_FORMATTERS__CFORMATTERJSONSDLRPCV1_HPP__ diff --git a/SDL_Core/src/components/formatters/include/formatters/CFormatterJsonSDLRPCv2.hpp b/SDL_Core/src/components/formatters/include/formatters/CFormatterJsonSDLRPCv2.hpp new file mode 100644 index 000000000..3b4e20651 --- /dev/null +++ b/SDL_Core/src/components/formatters/include/formatters/CFormatterJsonSDLRPCv2.hpp @@ -0,0 +1,176 @@ +// 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. + +#ifndef __SMARTDEVICELINKCORE_JSONHANDLER_FORMATTERS__CFORMATTERJSONSDLRPCV2_HPP__ +#define __SMARTDEVICELINKCORE_JSONHANDLER_FORMATTERS__CFORMATTERJSONSDLRPCV2_HPP__ + +#include "json/json.h" + +#include "smart_objects/smart_object.h" + +#include "CFormatterJsonBase.hpp" +#include "formatters/CSmartFactory.hpp" + +namespace NsSmartDeviceLink { +namespace NsJSONHandler { +namespace Formatters { + +/** + * @brief Class is used to convert SmartObjects to JSON string and vice versa. + * + * JSON strings is in SDLRPCv2 format. + */ +class CFormatterJsonSDLRPCv2 : public CFormatterJsonBase { + private: + + /** + * @brief Hidden constructor. + * + * The class contains only static methods. Should not be instantiated. + */ + CFormatterJsonSDLRPCv2(); + + /** + * @brief Hidden copy constructor. + * + * The class contains only static methods. Should not be instantiated. + */ + CFormatterJsonSDLRPCv2(const CFormatterJsonSDLRPCv2&); + + public: + + typedef NsSmartDeviceLink::NsJSONHandler::Formatters::meta_formatter_error_code::tMetaFormatterErrorCode tMetaFormatterErrorCode; + + /** + * @brief Creates a JSON string from a SmartObject. + * + * @param obj input SmartObject + * @param outStr resulting JSON string + * @return true if success, false otherwise + */ + static bool toString( + const NsSmartDeviceLink::NsSmartObjects::SmartObject &obj, + std::string& outStr); + + /** + * @brief Creates a SmartObject from a JSON string. + * + * @param str Input JSON string in SDLRPCv2 format + * @param out Output SmartObject + * @param functionId The corresponding field in SmartObject is filled with this param. + * @param messageType The corresponding field in SmartObject is filled with this param. + * @return true if success, otherwise - false + */ + template<typename FunctionId, typename MessageType> + static bool fromString(const std::string &str, + NsSmartDeviceLink::NsSmartObjects::SmartObject &out, + FunctionId functionId, MessageType messageType); + + /** + * @brief Creates a SmartObject from a JSON string. + * + * Version with CorrelationID. + * + * @param str Input JSON string in SDLRPCv2 format + * @param out Output SmartObject + * @param functionId The corresponding field in SmartObject is filled with this param. + * @param messageType The corresponding field in SmartObject is filled with this param. + * @param correlatioId It's like sequence number. The corresponding field in SmartObject + * is filled with this param. + * @return true if success, otherwise - false + */ + template<typename FunctionId, typename MessageType> + static bool fromString(const std::string &str, + NsSmartDeviceLink::NsSmartObjects::SmartObject &out, + FunctionId functionId, MessageType messageType, + int32_t correlationId); + + /** + * @brief Converts to string the smart object against the given schema + * + * @param object Original smart object + * @param schema Smart schema which describes 'fake' smart object to be formatted + * @param outStr Resulting JSON string + * @return formatting error code + */ + static tMetaFormatterErrorCode MetaFormatToString( + const NsSmartDeviceLink::NsSmartObjects::SmartObject& object, + const NsSmartDeviceLink::NsSmartObjects::CSmartSchema& schema, + std::string& outStr); +}; + +template<typename FunctionId, typename MessageType> +inline bool CFormatterJsonSDLRPCv2::fromString( + const std::string& str, NsSmartDeviceLink::NsSmartObjects::SmartObject& out, + FunctionId functionId, MessageType messageType) { + bool result = true; + + try { + Json::Value root; + Json::Reader reader; + + namespace strings = NsSmartDeviceLink::NsJSONHandler::strings; + bool result = reader.parse(str, root); + + if (true == result) { + out[strings::S_PARAMS][strings::S_MESSAGE_TYPE] = messageType; + out[strings::S_PARAMS][strings::S_FUNCTION_ID] = functionId; + out[strings::S_PARAMS][strings::S_PROTOCOL_TYPE] = 0; + out[strings::S_PARAMS][strings::S_PROTOCOL_VERSION] = 2; + + jsonValueToObj(root, out[strings::S_MSG_PARAMS]); + } + } catch (...) { + result = false; + } + + return result; +} + +template<typename FunctionId, typename MessageType> +inline bool CFormatterJsonSDLRPCv2::fromString( + const std::string& str, NsSmartDeviceLink::NsSmartObjects::SmartObject& out, + FunctionId functionId, MessageType messageType, int32_t correlationId) { + + bool result = fromString(str, out, functionId, messageType); + namespace strings = NsSmartDeviceLink::NsJSONHandler::strings; + + if (true == result) { + out[strings::S_PARAMS][strings::S_CORRELATION_ID] = correlationId; + } + + return result; +} + +} +} +} // namespace NsSmartDeviceLink::NsJSONHandler::Formatters + +#endif // __SMARTDEVICELINKCORE_JSONHANDLER_FORMATTERS__CFORMATTERJSONSDLRPCV2_HPP__ diff --git a/SDL_Core/src/components/formatters/include/formatters/CSmartFactory.hpp b/SDL_Core/src/components/formatters/include/formatters/CSmartFactory.hpp new file mode 100644 index 000000000..7cefabaa7 --- /dev/null +++ b/SDL_Core/src/components/formatters/include/formatters/CSmartFactory.hpp @@ -0,0 +1,422 @@ +/** + * @file CSmartFactory.hpp + * @brief CSmartFactory header 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. + +#ifndef __CSMARTFACTORY_HPP__ +#define __CSMARTFACTORY_HPP__ + +#include "smart_objects/smart_object.h" +#include "smart_objects/smart_schema.h" +#include <map> +#include <string> + +namespace NsSmartDeviceLink +{ + namespace NsJSONHandler + { + /** + * @brief String constants used by SmartFactory. + */ + namespace strings + { + /** + * @brief String constant for MSG_PARAMS. + */ + extern const std::string S_MSG_PARAMS; + + /** + * @brief String constant for PARAMS. + */ + extern const std::string S_PARAMS; + + /** + * @brief String constant for FUNCTION_ID. + */ + extern const std::string S_FUNCTION_ID; + + /** + * @brief String constant for MESSAGE_TYPE. + */ + extern const std::string S_MESSAGE_TYPE; + + /** + * @brief String constant for PROTOCOL_VERSION. + */ + extern const std::string S_PROTOCOL_VERSION; + + /** + * @brief String constant for PROTOCOL_TYPE. + */ + extern const std::string S_PROTOCOL_TYPE; + + /** + * @brief String constant for CORRELATION_ID. + */ + extern const std::string S_CORRELATION_ID; + + /** + * @brief String constant for "code" param name. + */ + extern const std::string kCode; + + /** + * @brief String constant for "message" param name. + */ + extern const std::string kMessage; + } + + /** + * @brief Smart Schema key. + * + * @tparam FunctionIdEnum Type of function ID enum. + * @tparam MessageTypeEnum Type of messageType enum. + */ + template <class FunctionIdEnum, class MessageTypeEnum> + struct SmartSchemaKey + { + /** + * @brief Value of function ID for the key. + */ + FunctionIdEnum functionId; + + /** + * @brief Value of messageType for the key. + */ + MessageTypeEnum messageType; + + /** + * @brief Constructor. + * + * @param functionIdParam Value of function ID. + * @param messageTypeParam Value of message type. + */ + SmartSchemaKey(FunctionIdEnum functionIdParam, MessageTypeEnum messageTypeParam); + }; + + /** + * @brief Smart Factory. + * + * This class is used as base class for generated factories. + * Clients should use methods of this class to access all + * SmartSchema validation features. + * + * @tparam FunctionIdEnum Type of function ID enum. + * @tparam MessageTypeEnum Type of messageType enum. + * @tparam StructIdEnum Type of StructId enum. + */ + template <class FunctionIdEnum, class MessageTypeEnum, class StructIdEnum> + class CSmartFactory + { + public: + + /** + * @brief Constructor. + */ + CSmartFactory(void); + + /** + * @brief Attach schema to the function SmartObject. + * + * @param object SmartObject to attach schema for. + * + * @return True if operation was successful or false otherwise. + */ + bool attachSchema(NsSmartDeviceLink::NsSmartObjects::SmartObject& object); + + /** + * @brief Attach schema to the struct SmartObject. + * + * @param struct_id Identifier of the struct. + * @param object SmartObject to attach schema for. + * + * @return True if operation was successful of false otherwise. + */ + bool AttachSchema(const StructIdEnum struct_id, + NsSmartDeviceLink::NsSmartObjects::SmartObject &object); + + + /** + * @brief Create new SmartObject with attached function SmartSchema. + * + * @param function_id FunctionID of the function. + * @param message_type messageType of the function. + * + * @return If function succeeded it returns new SmartObject with + * map type and attached SmartSchema. Client can use such + * object to store specific function and perform validation. + * Otherwise (if SmartSchema was not attached to the + * SmartObject) function returns empty SmartObject with + * null type. + */ + NsSmartDeviceLink::NsSmartObjects::SmartObject CreateSmartObject( + const FunctionIdEnum function_id, + const MessageTypeEnum message_type); + + /** + * @brief Create new SmartObject with attached struct SmartSchema. + * + * @param struct_id Identifier of the struct. + * + * @return If function succeeded it returns new SmartObject with + * map type and attached SmartSchema. Client can use such + * object to store specific struct and perform validation. + * Otherwise (if SmartSchema was not attached to the + * SmartObject) function returns empty SmartObject with + * null type. + */ + NsSmartDeviceLink::NsSmartObjects::SmartObject CreateSmartObject( + const StructIdEnum struct_id); + + /** + * @brief Get SmartSchema for specific function. + * + * @param function_id FunctionID of the function. + * @param message_type messageType of the function. + * @param[out] result This value will be copy of the desired + * function SmartSchema if it found (this + * function returns true) or unmodified if + * SmartSchema is not found (this function + * returns false). + * + * @return True if function schema for specified input parameters + * is found or false otherwise. + */ + bool GetSchema( + const FunctionIdEnum function_id, + const MessageTypeEnum message_type, + NsSmartDeviceLink::NsSmartObjects::CSmartSchema &result); + + /** + * @brief Get SmartSchema for specific struct. + * + * @param struct_id Identifier of the struct. + * + * @param[out] result This value will be copy of the desired + * struct SmartSchema if it found (this + * function returns true) or unmodified if + * SmartSchema is not found (this function + * returns false). + * + * @return True if struct schema for specified input parameter is + * found or false otherwise. + */ + bool GetSchema( + const StructIdEnum struct_id, + NsSmartDeviceLink::NsSmartObjects::CSmartSchema &result); + + protected: + + /** + * @brief Defines map of SmartSchemaKeys to the SmartSchemes. + * + * This container type should be used to store SmartSchemes of + * functions. + */ + typedef std::map<SmartSchemaKey<FunctionIdEnum, MessageTypeEnum>, + NsSmartDeviceLink::NsSmartObjects::CSmartSchema> + FuncionsSchemesMap; + + /** + * @brief Defines map of StructIdEnum to the SmartSchemes. + * + * This container type should be used to store SmartSchemes of + * structs. + */ + typedef std::map<StructIdEnum, + NsSmartDeviceLink::NsSmartObjects::CSmartSchema> + StructsSchemesMap; + + /** + * @brief Map of all function schemes for this factory. + */ + FuncionsSchemesMap functions_schemes_; + + /** + * @brief Map of all struct shemes for this factory. + */ + StructsSchemesMap structs_schemes_; + }; + + template <class FunctionIdEnum, class MessageTypeEnum, class StructIdEnum> + CSmartFactory<FunctionIdEnum, MessageTypeEnum, StructIdEnum>::CSmartFactory(void) + : functions_schemes_(), + structs_schemes_() + { + } + + template <class FunctionIdEnum, class MessageTypeEnum, class StructIdEnum> + bool CSmartFactory<FunctionIdEnum, MessageTypeEnum, StructIdEnum>::attachSchema(NsSmartDeviceLink::NsSmartObjects::SmartObject &object) + { + if(false == object.keyExists(strings::S_PARAMS)) return false; + if(false == object[strings::S_PARAMS].keyExists(strings::S_MESSAGE_TYPE)) return false; + if(false == object[strings::S_PARAMS].keyExists(strings::S_FUNCTION_ID)) return false; + + MessageTypeEnum msgtype((MessageTypeEnum)object[strings::S_PARAMS][strings::S_MESSAGE_TYPE].asInt()); + FunctionIdEnum fid((FunctionIdEnum)object[strings::S_PARAMS][strings::S_FUNCTION_ID].asInt()); + + SmartSchemaKey<FunctionIdEnum, MessageTypeEnum> key(fid, msgtype); + + typename FuncionsSchemesMap::iterator schemaIterator = functions_schemes_.find(key); + + if(schemaIterator == functions_schemes_.end()) + { + // Schema was not found + return false; + } + + object.setSchema(schemaIterator->second); + schemaIterator->second.applySchema(object); + + return true; + } + + template <class FunctionIdEnum, + class MessageTypeEnum, + class StructIdEnum> + bool CSmartFactory<FunctionIdEnum, MessageTypeEnum, StructIdEnum>:: + AttachSchema( + const StructIdEnum struct_id, + NsSmartDeviceLink::NsSmartObjects::SmartObject &object) { + typename StructsSchemesMap::iterator structs_iterator = + structs_schemes_.find(struct_id); + + if (structs_iterator == structs_schemes_.end()) { + return false; + } + + object.setSchema(structs_iterator->second); + structs_iterator->second.applySchema(object); + + return true; + } + + template <class FunctionIdEnum, + class MessageTypeEnum, + class StructIdEnum> + NsSmartDeviceLink::NsSmartObjects::SmartObject + CSmartFactory<FunctionIdEnum, MessageTypeEnum, StructIdEnum>:: + CreateSmartObject( + const FunctionIdEnum function_id, + const MessageTypeEnum message_type) { + SmartSchemaKey<FunctionIdEnum, MessageTypeEnum> key( + function_id, message_type); + + typename FuncionsSchemesMap::iterator schema_iterator = + functions_schemes_.find(key); + + if(schema_iterator != functions_schemes_.end()) { + NsSmartDeviceLink::NsSmartObjects::SmartObject function_object( + NsSmartDeviceLink::NsSmartObjects::SmartType_Map); + function_object.setSchema(schema_iterator->second); + schema_iterator->second.applySchema(function_object); + return function_object; + } + + return NsSmartDeviceLink::NsSmartObjects::SmartObject(); + } + + template <class FunctionIdEnum, + class MessageTypeEnum, + class StructIdEnum> + NsSmartDeviceLink::NsSmartObjects::SmartObject + CSmartFactory<FunctionIdEnum, MessageTypeEnum, StructIdEnum>:: + CreateSmartObject(const StructIdEnum struct_id) { + NsSmartDeviceLink::NsSmartObjects::SmartObject struct_object( + NsSmartDeviceLink::NsSmartObjects::SmartType_Map); + if (AttachSchema(struct_id, struct_object)) { + return struct_object; + } + + return NsSmartDeviceLink::NsSmartObjects::SmartObject(); + } + + template <class FunctionIdEnum, + class MessageTypeEnum, + class StructIdEnum> + bool CSmartFactory<FunctionIdEnum, MessageTypeEnum, StructIdEnum>:: + GetSchema(const FunctionIdEnum function_id, + const MessageTypeEnum message_type, + NsSmartDeviceLink::NsSmartObjects::CSmartSchema &result) { + SmartSchemaKey<FunctionIdEnum, MessageTypeEnum> key(function_id, + message_type); + + typename FuncionsSchemesMap::iterator schema_iterator = + functions_schemes_.find(key); + + if(schema_iterator != functions_schemes_.end()) { + result = schema_iterator->second; + return true; + } + + return false; + } + + template <class FunctionIdEnum, + class MessageTypeEnum, + class StructIdEnum> + bool CSmartFactory<FunctionIdEnum, MessageTypeEnum, StructIdEnum>:: + GetSchema(const StructIdEnum struct_id, + NsSmartDeviceLink::NsSmartObjects::CSmartSchema &result) { + typename StructsSchemesMap::iterator structs_iterator = + structs_schemes_.find(struct_id); + + if(structs_iterator != structs_schemes_.end()) { + result = structs_iterator->second; + return true; + } + + return false; + } + + template <class FunctionIdEnum, class MessageTypeEnum> + SmartSchemaKey<FunctionIdEnum, MessageTypeEnum>::SmartSchemaKey(FunctionIdEnum functionIdParam, MessageTypeEnum messageTypeParam) + : functionId(functionIdParam) + , messageType(messageTypeParam) + { + + } + + template <class FunctionIdEnum, class MessageTypeEnum> + bool operator<(const SmartSchemaKey< FunctionIdEnum, MessageTypeEnum >& l, const SmartSchemaKey< FunctionIdEnum, MessageTypeEnum >& r) + { + if (l.functionId < r.functionId) return true; + if (l.functionId > r.functionId) return false; + + if (l.messageType < r.messageType) return true; + if (l.messageType > r.messageType) return false; + + return false; + } + } +} +#endif //__CSMARTFACTORY_HPP__ diff --git a/SDL_Core/src/components/formatters/include/formatters/formatter_json_rpc.h b/SDL_Core/src/components/formatters/include/formatters/formatter_json_rpc.h new file mode 100644 index 000000000..196cab9c2 --- /dev/null +++ b/SDL_Core/src/components/formatters/include/formatters/formatter_json_rpc.h @@ -0,0 +1,497 @@ +/** + * @file formatter_json_rpc.h + * @brief FormatterJsonRpc header 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. + +#ifndef SMARTDEVICELINK_COMPONENTS_JSONHANDLER_INCLUDE_FORMATTERS_FORMATTER_JSON_RPC_H_ +#define SMARTDEVICELINK_COMPONENTS_JSONHANDLER_INCLUDE_FORMATTERS_FORMATTER_JSON_RPC_H_ + +#include <string> +#include <sys/stat.h> + +#include "smart_objects/smart_object.h" +#include "smart_objects/enum_schema_item.h" +#include "json/json.h" + +#include "CFormatterJsonBase.hpp" +#include "formatters/CSmartFactory.hpp" + +namespace NsSmartDeviceLink { +namespace NsJSONHandler { +namespace Formatters { + +/** + * @brief Formatter for JSON RPC format. + */ +class FormatterJsonRpc: public CFormatterJsonBase { + public: + /** + * @brief No errors occured during the parsing of the input JSON string. + */ + static const int32_t kSuccess = 0; + + /** + * @brief Input JSON string cannot be parsed. + */ + static const int32_t kParsingError = 1; + + /** + * @brief Input JSON string has invalid format. + */ + static const int32_t kInvalidFormat = 2; + + /** + * @brief Method in input JSON string is not + * specified or specified incorrectly. + */ + static const int32_t kMethodNotSpecified = 4; + + /** + * @brief Method is unknown. + */ + static const int32_t kUnknownMethod = 8; + + /** + * @brief Message type is unknown. + */ + static const int32_t kUnknownMessageType = 16; + + /** + * @brief Id of request or response is invalid. + */ + static const int32_t kInvalidId = 32; + + /** + * @brief Response code is not available. + */ + static const int32_t kResponseCodeNotAvailable = 64; + + /** + * @brief Message for error response is not available. + */ + static const int32_t kErrorResponseMessageNotAvailable = 128; + + /** + * @brief Creates a JSON string from a SmartObject. + * + * @param obj Input SmartObject. + * @param out_str Resulting JSON string. + * + * @return true if success, false otherwise. + */ + static bool ToString(const NsSmartObjects::SmartObject& obj, + std::string& out_str); + + /** + * @brief Creates a SmartObject from a JSON string. + * + * @tparam FunctionId Type of function id enumeration. + * @tparam MessageType Type of message type enumeration. + * + * @param str input JSON string. + * @param out The resulting SmartObject. + * + * @return An integer that is a bitwise-or of all error codes occured + * during the parsing of the input string. 0 if no errors occured. + */ + template <typename FunctionId, typename MessageType> + static int32_t FromString(const std::string& str, + NsSmartObjects::SmartObject& out); + + private: + /** + * @brief Request. + */ + static const char* kRequest; + + /** + * @brief Response. + */ + static const char* kResponse; + + /** + * @brief Notification. + */ + static const char* kNotification; + + /** + * @brief Error reponse. + */ + static const char* kErrorResponse; + + /** + * @brief Name of "jsonrpc" message field. + */ + static const char* kJsonRpc; + + /** + * @brief Expected value of "jsonrpc" message field ("2.0"). + */ + static const char* kJsonRpcExpectedValue; + + /** + * @brief Name of "id" message field. + */ + static const char* kId; + + /** + * @brief Name of "method" field. + */ + static const char* kMethod; + + /** + * @brief Name of "params" field. + */ + static const char* kParams; + + /** + * @brief Name of "result" field. + */ + static const char* kResult; + + /** + * @brief Name of "error" field. + */ + static const char* kError; + + /** + * @brief Name of "code" field. + */ + static const char* kCode; + + /** + * @brief Name of "data" field. + */ + static const char* kData; + + /** + * @brief Name of "message" field. + */ + static const char* kMessage; + + /** + * @brief Constructor. + */ + FormatterJsonRpc(); + + /** + * @brief Copy constructor. + * + * @param unused Unused parameter. + */ + FormatterJsonRpc(const FormatterJsonRpc& unused); + + /** + * @brief Parse function id. + * + * @tparam FunctionId Type of function id enumeration. + * + * @param method_value JSON value with function id. + * @param out The resulting SmartObject. + * + * @return An integer that is a bitwise-or of all error codes occured + * during the parsing of the function id. 0 if no errors occured. + */ + template <typename FunctionId> + static int32_t ParseFunctionId(const Json::Value& method_value, + NsSmartObjects::SmartObject& out); + + /** + * @brief Set method. + * + * Try to extract function id from the message object and set the "method" + * field of the container. + * + * @param params Message parameters object. + * @param method_container Container for the "method" field. + * + * @return true if function id was extracted successfully and set as a + * value of "method" field. + */ + static bool SetMethod(const NsSmartObjects::SmartObject& params, + Json::Value& method_container); + + /** + * @brief Set id. + * + * Try to extract request/response id from the message object and set the + * "id" field of the container. + * + * @param params Message parameters object. + * @param id_container Container for the "id" field. + * + * @return true if request/response id was extracted successfully and set + * as a value of "id" field. + */ + static bool SetId(const NsSmartObjects::SmartObject& params, + Json::Value& id_container); + + /** + * @brief Set message + * + * Try to extract message from response error object and set "message" field for the container + * + * @param params Message parameters object. + * @param id_container Container of the "message" field + * + * @return true if message string was extracted successfully and set + * as a value of "message" field. + */ + static bool SetMessage(const NsSmartObjects::SmartObject& params, + Json::Value& id_container); +}; + +template <typename FunctionId, typename MessageType> +int32_t FormatterJsonRpc::FromString(const std::string& str, + NsSmartObjects::SmartObject& out) { + int32_t result = kSuccess; + try { + Json::Value root; + Json::Reader reader; + namespace strings = NsSmartDeviceLink::NsJSONHandler::strings; + + if (false == reader.parse(str, root)) { + result = kParsingError | kMethodNotSpecified | kUnknownMethod | + kUnknownMessageType; + } else { + if (false == root.isMember(kJsonRpc)) { + result |= kInvalidFormat; + } else { + const Json::Value& jsonRpcValue = root[kJsonRpc]; + + if ((false == jsonRpcValue.isString()) || + (jsonRpcValue.asString() != kJsonRpcExpectedValue)) { + result |= kInvalidFormat; + } + } + + std::string message_type_string; + Json::Value response_value; + bool response_value_found = false; + bool is_error_response = false; + + if (false == root.isMember(kId)) { + message_type_string = kNotification; + + if (false == root.isMember(kMethod)) { + result |= kMethodNotSpecified | kUnknownMethod; + } else { + result |= ParseFunctionId<FunctionId>(root[kMethod], out); + } + out[strings::S_MSG_PARAMS] + = NsSmartObjects::SmartObject(NsSmartObjects::SmartType_Map); + } else { + const Json::Value& id_value = root[kId]; + + if (true == id_value.isString()) { + out[strings::S_PARAMS][strings::S_CORRELATION_ID] = + id_value.asString(); + } else if (true == id_value.isInt()) { + out[strings::S_PARAMS][strings::S_CORRELATION_ID] = + id_value.asInt(); + } else if (true == id_value.isDouble()) { + out[strings::S_PARAMS][strings::S_CORRELATION_ID] = + id_value.asDouble(); + } else if (true == id_value.isNull()) { + out[strings::S_PARAMS][strings::S_CORRELATION_ID] = + NsSmartObjects::SmartObject(NsSmartObjects::SmartType_Null); + } else { + result |= kInvalidFormat | kInvalidId; + } + + if (true == root.isMember(kMethod)) { + message_type_string = kRequest; + result |= ParseFunctionId<FunctionId>(root[kMethod], out); + out[strings::S_MSG_PARAMS] + = NsSmartObjects::SmartObject(NsSmartObjects::SmartType_Map); + } else { + Json::Value method_container; + bool method_container_found = false; + + if (true == root.isMember(kResult)) { + out[strings::S_MSG_PARAMS] + = NsSmartObjects::SmartObject(NsSmartObjects::SmartType_Map); + + message_type_string = kResponse; + response_value = root[kResult]; + response_value_found = true; + method_container = root[kResult]; + method_container_found = true; + } else if (true == root.isMember(kError)) { + out[strings::S_MSG_PARAMS] + = NsSmartObjects::SmartObject(NsSmartObjects::SmartType_Map); + message_type_string = kErrorResponse; + response_value = root[kError]; + response_value_found = true; + is_error_response = true; + + if (true == response_value.isObject()) { + if (true == response_value.isMember(kData)) { + method_container = response_value[kData]; + method_container_found = true; + } + } + } else { + result |= kUnknownMessageType; + } + + if (false == method_container_found) { + result |= kMethodNotSpecified | kUnknownMethod; + } else if (false == method_container.isObject()) { + result |= kInvalidFormat | kMethodNotSpecified | kUnknownMethod; + } else { + if (false == method_container.isMember(kMethod)) { + result |= kMethodNotSpecified | kUnknownMethod; + } else { + result |= ParseFunctionId<FunctionId>(method_container[kMethod], + out); + } + } + } + } + + if (0 == (result & kUnknownMessageType)) { + MessageType message_type; + + if (false == NsSmartObjects::TEnumSchemaItem<MessageType>::stringToEnum( + message_type_string, message_type)) { + result |= kUnknownMessageType; + } else { + out[strings::S_PARAMS][strings::S_MESSAGE_TYPE] = message_type; + } + } + + if (true == root.isMember(kParams)) { + const Json::Value& params_value = root[kParams]; + + if (false == params_value.isObject()) { + result |= kInvalidFormat; + } else { + jsonValueToObj(root[kParams], out[strings::S_MSG_PARAMS]); + } + } else if (true == root.isMember(kResult)) { + const Json::Value& result_value = root[kResult]; + + if (false == result_value.isObject()) { + result |= kInvalidFormat; + } else { + jsonValueToObj(root[kResult], out[strings::S_MSG_PARAMS]); + } + } else if (true == is_error_response) { + jsonValueToObj(response_value[kData], out[strings::S_PARAMS][kData]); + } + + if ((kResponse == message_type_string) || + (kErrorResponse == message_type_string)) { + if (true == out.keyExists(strings::S_MSG_PARAMS)) { + out[strings::S_MSG_PARAMS].erase(kMethod); + out[strings::S_MSG_PARAMS].erase(kCode); + } + + if (false == response_value_found) { + result |= kResponseCodeNotAvailable; + } else { + if (false == response_value.isObject()) { + result |= kInvalidFormat | kResponseCodeNotAvailable; + + if (true == is_error_response) { + result |= kErrorResponseMessageNotAvailable; + } + } else { + if (false == response_value.isMember(kCode)) { + result |= kResponseCodeNotAvailable; + } else { + const Json::Value& code_value = response_value[kCode]; + + if (false == code_value.isInt()) { + result |= kInvalidFormat | kResponseCodeNotAvailable; + } else { + out[strings::S_PARAMS][strings::kCode] = code_value.asInt(); + } + } + + if (true == is_error_response) { + if (false == response_value.isMember(kMessage)) { + result |= kErrorResponseMessageNotAvailable; + } else { + const Json::Value& message_value = response_value[kMessage]; + + if (false == message_value.isString()) { + result |= kErrorResponseMessageNotAvailable; + } else { + out[strings::S_PARAMS][strings::kMessage] = + message_value.asString(); + } + } + } + } + } + } + } + + out[strings::S_PARAMS][strings::S_PROTOCOL_TYPE] = 1; + out[strings::S_PARAMS][strings::S_PROTOCOL_VERSION] = 2; + } catch(...) { + result = kParsingError; + } + + return result; +} + +template <typename FunctionId> +int32_t FormatterJsonRpc::ParseFunctionId(const Json::Value& method_value, + NsSmartObjects::SmartObject& out) { + int32_t result = kSuccess; + + if (false == method_value.isString()) { + result |= kInvalidFormat | kUnknownMethod; + } else { + FunctionId function_id; + + if (false == NsSmartObjects::TEnumSchemaItem<FunctionId>::stringToEnum( + method_value.asString(), function_id)) { + result |= kUnknownMethod; + } else { + namespace strings = NsSmartDeviceLink::NsJSONHandler::strings; + + out[strings::S_PARAMS][strings::S_FUNCTION_ID] = function_id; + } + } + + return result; +} + +} // namespace Formatters +} // namespace NsJSONHandler +} // namespace NsSmartDeviceLink + +#endif // SMARTDEVICELINK_COMPONENTS_JSONHANDLER_INCLUDE_FORMATTERS_FORMATTER_JSON_RPC_H_ diff --git a/SDL_Core/src/components/formatters/include/formatters/generic_json_formatter.h b/SDL_Core/src/components/formatters/include/formatters/generic_json_formatter.h new file mode 100644 index 000000000..3eaee0b36 --- /dev/null +++ b/SDL_Core/src/components/formatters/include/formatters/generic_json_formatter.h @@ -0,0 +1,71 @@ +/** + * @file generic_json_formatter.h + * @brief Generic JSON formatter header 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. + +#ifndef SMARTDEVICELINK_COMPONENTS_FORMATTERS_INCLUDE_FORMATTERS_GENERIC_JSON_FORMATTER_H_ +#define SMARTDEVICELINK_COMPONENTS_FORMATTERS_INCLUDE_FORMATTERS_GENERIC_JSON_FORMATTER_H_ + +#include "CFormatterJsonBase.hpp" + +namespace NsSmartDeviceLink { +namespace NsJSONHandler { +namespace Formatters { + +class GenericJsonFormatter: public CFormatterJsonBase { +public: + /** + * @brief Creates a JSON string from a SmartObject. + * + * @param obj Input SmartObject. + * @param out_str Resulting JSON string. + */ + static void ToString(const NsSmartObjects::SmartObject& obj, + std::string& out_str); + + /** + * @brief Creates a SmartObject from a JSON string. + * + * @param str input JSON string. + * @param out The resulting SmartObject. + * + * @return true if success, false otherwise. + */ + static bool FromString(const std::string& str, + NsSmartObjects::SmartObject& out); +}; + +} // namespace Formatters +} // namespace NsJSONHandler +} // namespace NsSmartDeviceLink + +#endif // SMARTDEVICELINK_COMPONENTS_FORMATTERS_INCLUDE_FORMATTERS_GENERIC_JSON_FORMATTER_H_ diff --git a/SDL_Core/src/components/formatters/include/formatters/meta_formatter.h b/SDL_Core/src/components/formatters/include/formatters/meta_formatter.h new file mode 100644 index 000000000..c1b767d7c --- /dev/null +++ b/SDL_Core/src/components/formatters/include/formatters/meta_formatter.h @@ -0,0 +1,78 @@ +/** + * @file meta_fromatter.h + * @brief file describes 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. + +#ifndef __SMARTDEVICELINKCORE_JSONHANDLER_FORMATTERS_METAFORMATTER_H__ +#define __SMARTDEVICELINKCORE_JSONHANDLER_FORMATTERS_METAFORMATTER_H__ + +#include "smart_objects/smart_object.h" +#include "smart_objects/smart_schema.h" + +namespace NsSmartDeviceLink { +namespace NsJSONHandler { +namespace Formatters { + +/** + * @brief Formats to string the smart object against given schema for given formatter + * + * Sample usage: + * CSmartFactory factory; + * SmartObject object; + * CSmartSchmema schema; + * + */ +class CMetaFormatter { + public: + + /** + * @brief Creates smart object by the given schema having copied + * matched tree elements from original object. + * + * @param object Original smart object which macthed tree elements + * will be copied from + * @param schema Smart schema which describes result smart object + * @param result_object createdsmart object + * @return true if successful, false - otherwise + */ + static bool CreateObjectByPattern( + const NsSmartDeviceLink::NsSmartObjects::SmartObject& object, + const NsSmartDeviceLink::NsSmartObjects::CSmartSchema& schema, + NsSmartDeviceLink::NsSmartObjects::SmartObject& result_object); +}; + +}// namespace NsSmartDeviceLink +}// namespace NsSmartDeviceLink::NsJSONHandler +}// namespace NsSmartDeviceLink::NsJSONHandler::Formatters + +#endif // __SMARTDEVICELINKCORE_JSONHANDLER_FORMATTERS_METAFORMATTER_H__ |