From e8742e8850d838db8b4a3c8cfc6c99c6061a443c Mon Sep 17 00:00:00 2001 From: William Cheng Date: Sun, 1 May 2022 15:23:04 +0800 Subject: [PATCH] update samples --- .../include/CppRestPetstoreClient/ModelBase.h | 4 ++ .../cpp-restsdk/client/src/ModelBase.cpp | 47 +++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/samples/client/petstore/cpp-restsdk/client/include/CppRestPetstoreClient/ModelBase.h b/samples/client/petstore/cpp-restsdk/client/include/CppRestPetstoreClient/ModelBase.h index 8d005639e81..72191c60a9c 100644 --- a/samples/client/petstore/cpp-restsdk/client/include/CppRestPetstoreClient/ModelBase.h +++ b/samples/client/petstore/cpp-restsdk/client/include/CppRestPetstoreClient/ModelBase.h @@ -75,6 +75,7 @@ public: static web::json::value toJson( const std::shared_ptr& val ); template static web::json::value toJson( const std::shared_ptr& val ); + static web::json::value toJson( const std::shared_ptr& val ); template static web::json::value toJson( const std::vector& val ); template @@ -91,6 +92,7 @@ public: static bool fromString( const utility::string_t& val, std::shared_ptr & ); template static bool fromString( const utility::string_t& val, std::shared_ptr& ); + static bool fromString( const utility::string_t& val, std::shared_ptr& outVal ); template static bool fromString( const utility::string_t& val, std::vector & ); template @@ -107,6 +109,7 @@ public: static bool fromJson( const web::json::value& val, std::shared_ptr & ); template static bool fromJson( const web::json::value& val, std::shared_ptr& ); + static bool fromJson( const web::json::value& val, std::shared_ptr &outVal ); template static bool fromJson( const web::json::value& val, std::vector & ); template @@ -124,6 +127,7 @@ public: static std::shared_ptr toHttpContent( const utility::string_t& name, const std::shared_ptr& ); template static std::shared_ptr toHttpContent( const utility::string_t& name, const std::shared_ptr& , const utility::string_t& contentType = utility::conversions::to_string_t("application/json") ); + static std::shared_ptr toHttpContent(const utility::string_t& name, const std::shared_ptr& value , const utility::string_t& contentType = utility::conversions::to_string_t("application/json") ); template static std::shared_ptr toHttpContent( const utility::string_t& name, const std::vector& value, const utility::string_t& contentType = utility::conversions::to_string_t("") ); template diff --git a/samples/client/petstore/cpp-restsdk/client/src/ModelBase.cpp b/samples/client/petstore/cpp-restsdk/client/src/ModelBase.cpp index bfbdb963f33..ba5f63e53fe 100644 --- a/samples/client/petstore/cpp-restsdk/client/src/ModelBase.cpp +++ b/samples/client/petstore/cpp-restsdk/client/src/ModelBase.cpp @@ -123,6 +123,15 @@ web::json::value ModelBase::toJson( const std::shared_ptr& content } return value; } +web::json::value ModelBase::toJson( const std::shared_ptr& val ) +{ + web::json::value retVal; + if(val != nullptr) + { + retVal = toJson(*val); + } + return retVal; +} bool ModelBase::fromString( const utility::string_t& val, bool &outVal ) { utility::stringstream_t ss(val); @@ -253,6 +262,19 @@ bool ModelBase::fromString( const utility::string_t& val, std::shared_ptr& outVal ) +{ + bool ok = false; + if(outVal == nullptr) + { + outVal = std::shared_ptr(new utility::datetime()); + } + if( outVal != nullptr ) + { + ok = fromJson(web::json::value::parse(val), *outVal); + } + return ok; +} bool ModelBase::fromJson( const web::json::value& val, bool & outVal ) { outVal = !val.is_boolean() ? false : val.as_bool(); @@ -330,6 +352,19 @@ bool ModelBase::fromJson( const web::json::value& val, std::shared_ptr &outVal ) +{ + bool ok = false; + if(outVal == nullptr) + { + outVal = std::shared_ptr(new utility::datetime()); + } + if( outVal != nullptr ) + { + ok = fromJson(val, *outVal); + } + return ok; +} std::shared_ptr ModelBase::toHttpContent( const utility::string_t& name, bool value, const utility::string_t& contentType ) { std::shared_ptr content( new HttpContent ); @@ -425,6 +460,18 @@ std::shared_ptr ModelBase::toHttpContent( const utility::string_t& } return content; } +std::shared_ptr ModelBase::toHttpContent(const utility::string_t& name, const std::shared_ptr& value , const utility::string_t& contentType ) +{ + std::shared_ptr content( new HttpContent ); + if (value != nullptr ) + { + content->setName( name ); + content->setContentDisposition( utility::conversions::to_string_t("form-data") ); + content->setContentType( contentType ); + content->setData( std::shared_ptr( new std::stringstream( utility::conversions::to_utf8string( toJson(*value).serialize() ) ) ) ); + } + return content; +} bool ModelBase::fromHttpContent(std::shared_ptr val, bool & outVal ) { utility::string_t str;