All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cpp-rest-sdk-client.modelbase-header.mustache Maven / Gradle / Ivy

There is a newer version: 7.9.0
Show newest version
{{>licenseInfo}}
/*
 * ModelBase.h
 *
 * This is the base class for all model classes
 */

#ifndef {{modelHeaderGuardPrefix}}_ModelBase_H_
#define {{modelHeaderGuardPrefix}}_ModelBase_H_

{{{defaultInclude}}}
#include 
#include 

#include "HttpContent.h"
#include "MultipartFormData.h"

#include 
#include 

{{#modelNamespaceDeclarations}}
namespace {{this}} {
{{/modelNamespaceDeclarations}}

class {{declspec}} ModelBase
{
public:
    ModelBase();
    virtual ~ModelBase();

    virtual void validate() = 0;

    virtual web::json::value toJson() const = 0;
    virtual bool fromJson( const web::json::value& json ) = 0;

    virtual void toMultipart( std::shared_ptr multipart, const utility::string_t& namePrefix ) const = 0;
    virtual bool fromMultiPart( std::shared_ptr multipart, const utility::string_t& namePrefix ) = 0;

    virtual bool isSet() const;

    static utility::string_t toString( const bool val );
    static utility::string_t toString( const float val );
    static utility::string_t toString( const double val );
    static utility::string_t toString( const int32_t val );
    static utility::string_t toString( const int64_t val );
    static utility::string_t toString( const utility::string_t &val );
    static utility::string_t toString( const utility::datetime &val );
    static utility::string_t toString( const web::json::value &val );
    static utility::string_t toString( const std::shared_ptr& val );
    template 
    static utility::string_t toString( const std::shared_ptr& val );
    template 
    static utility::string_t toString( const std::vector & val );

    static web::json::value toJson( bool val );
    static web::json::value toJson( float val );
    static web::json::value toJson( double val );
    static web::json::value toJson( int32_t val );
    static web::json::value toJson( int64_t val );
    static web::json::value toJson( const utility::string_t& val );
    static web::json::value toJson( const utility::datetime& val );
    static web::json::value toJson( const web::json::value& val );
    static web::json::value toJson( const std::shared_ptr& val );
    template
    static web::json::value toJson( const std::shared_ptr& val );
    template
    static web::json::value toJson( const std::vector& val );
    template
    static web::json::value toJson( const std::map& val );

    static bool fromString( const utility::string_t& val, bool & );
    static bool fromString( const utility::string_t& val, float & );
    static bool fromString( const utility::string_t& val, double & );
    static bool fromString( const utility::string_t& val, int32_t & );
    static bool fromString( const utility::string_t& val, int64_t & );
    static bool fromString( const utility::string_t& val, utility::string_t & );
    static bool fromString( const utility::string_t& val, utility::datetime & );
    static bool fromString( const utility::string_t& val, web::json::value & );
    static bool fromString( const utility::string_t& val, std::shared_ptr & );
    template
    static bool fromString( const utility::string_t& val, std::shared_ptr& );
    template
    static bool fromString( const utility::string_t& val, std::vector & );
    template
    static bool fromString( const utility::string_t& val, std::map & );

    static bool fromJson( const web::json::value& val, bool & );
    static bool fromJson( const web::json::value& val, float & );
    static bool fromJson( const web::json::value& val, double & );
    static bool fromJson( const web::json::value& val, int32_t & );
    static bool fromJson( const web::json::value& val, int64_t & );
    static bool fromJson( const web::json::value& val, utility::string_t & );
    static bool fromJson( const web::json::value& val, utility::datetime & );
    static bool fromJson( const web::json::value& val, web::json::value & );
    static bool fromJson( const web::json::value& val, std::shared_ptr & );
    template
    static bool fromJson( const web::json::value& val, std::shared_ptr& );
    template
    static bool fromJson( const web::json::value& val, std::vector & );
    template
    static bool fromJson( const web::json::value& val, std::map & );


    static std::shared_ptr toHttpContent( const utility::string_t& name, bool value, const utility::string_t& contentType = utility::conversions::to_string_t("") );
    static std::shared_ptr toHttpContent( const utility::string_t& name, float value, const utility::string_t& contentType = utility::conversions::to_string_t("") );
    static std::shared_ptr toHttpContent( const utility::string_t& name, double value, const utility::string_t& contentType = utility::conversions::to_string_t("") );
    static std::shared_ptr toHttpContent( const utility::string_t& name, int32_t value, const utility::string_t& contentType = utility::conversions::to_string_t("") );
    static std::shared_ptr toHttpContent( const utility::string_t& name, int64_t value, const utility::string_t& contentType = utility::conversions::to_string_t("") );
    static std::shared_ptr toHttpContent( const utility::string_t& name, const utility::string_t& value, const utility::string_t& contentType = utility::conversions::to_string_t(""));
    static std::shared_ptr toHttpContent( const utility::string_t& name, const utility::datetime& value, const utility::string_t& contentType = utility::conversions::to_string_t(""));
    static std::shared_ptr toHttpContent( const utility::string_t& name, const web::json::value& value, 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& );
    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") );
    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 
    static std::shared_ptr toHttpContent( const utility::string_t& name, const std::map& value, const utility::string_t& contentType = utility::conversions::to_string_t("") );

    static bool fromHttpContent( std::shared_ptr val, bool & );
    static bool fromHttpContent( std::shared_ptr val, float & );
    static bool fromHttpContent( std::shared_ptr val, double & );
    static bool fromHttpContent( std::shared_ptr val, int64_t & );
    static bool fromHttpContent( std::shared_ptr val, int32_t & );
    static bool fromHttpContent( std::shared_ptr val, utility::string_t & );
    static bool fromHttpContent( std::shared_ptr val, utility::datetime & );
    static bool fromHttpContent( std::shared_ptr val, web::json::value & );
    static bool fromHttpContent( std::shared_ptr val, std::shared_ptr& );
    template 
    static bool fromHttpContent( std::shared_ptr val, std::shared_ptr& );
    template 
    static bool fromHttpContent( std::shared_ptr val, std::vector & );
    template 
    static bool fromHttpContent( std::shared_ptr val, std::map & );

    static utility::string_t toBase64( utility::string_t value );
    static utility::string_t toBase64( std::shared_ptr value );
    static std::shared_ptr fromBase64( const utility::string_t& encoded );
protected:
    bool m_IsSet;
};

template 
utility::string_t ModelBase::toString( const std::shared_ptr& val )
{
    utility::stringstream_t ss;
    if( val != nullptr )
    {
        val->toJson().serialize(ss);
    }
    return  utility::string_t(ss.str());
}
template
utility::string_t ModelBase::toString( const std::vector & val )
{
    utility::string_t strArray;
    for ( const auto &item : val )
    {
        strArray.append( toString(item) + "," );
    }
    if (val.count() > 0)
    {
        strArray.pop_back();
    }
    return strArray;
}
template
web::json::value ModelBase::toJson( const std::shared_ptr& val )
{
    web::json::value retVal;
    if(val != nullptr)
    {
        retVal = val->toJson();
    }
    return retVal;
}
template
web::json::value ModelBase::toJson( const std::vector& value )
{
    std::vector ret;
    for ( const auto& x : value )
    {
        ret.push_back( toJson(x) );
    }
    return web::json::value::array(ret);
}
template
web::json::value ModelBase::toJson( const std::map& val )
{
    web::json::value obj;
    for ( const auto &itemkey : val )
    {
        obj[itemkey.first] = toJson( itemkey.second );
    }
    return obj;
}
template
bool ModelBase::fromString( const utility::string_t& val, std::shared_ptr& outVal )
{
    bool ok = false;
    if(outVal == nullptr)
    {
        outVal = std::shared_ptr(new T());
    }    
    if( outVal != nullptr )
    {
        ok = outVal->fromJson(web::json::value::parse(val));
    }
    return ok;
}
template
bool ModelBase::fromJson( const web::json::value& val, std::shared_ptr &outVal )
{
    bool ok = false;
    if(outVal == nullptr)
    {
        outVal = std::shared_ptr(new T());
    }
    if( outVal != nullptr )
    {
        ok = outVal->fromJson(val);
    }
    return ok;
}
template
bool ModelBase::fromJson( const web::json::value& val, std::vector &outVal )
{
    bool ok = true;
    if (val.is_array())
    {
        for (const auto jitem : val.as_array())
        {
            T item;
            ok &= fromJson(jitem, item);
            outVal.push_back(item);
        }
    }
    else
    {
        ok = false;
    }
    return ok;
}
template
bool ModelBase::fromJson( const web::json::value& jval, std::map &outVal )
{
    bool ok = true;
    if ( jval.is_object() )
    {
        auto obj = jval.as_object();
        for( auto objItr = obj.begin() ; objItr != obj.end() ; objItr++ )
        {
            T itemVal;
            ok &= fromJson(objItr->second, itemVal);
            outVal.insert(std::pair(objItr->first, itemVal));
        }
    }
    else
    {
        ok = false;
    }
    return ok;
}
template 
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( value->toJson().serialize() ) ) ) );
    }
    return content;
}
template 
std::shared_ptr ModelBase::toHttpContent( const utility::string_t& name, const std::vector& value, const utility::string_t& contentType )
{
    web::json::value json_array = ModelBase::toJson(value);
    std::shared_ptr content( new HttpContent );
    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(json_array.serialize()) ) ) );
    return content;
}
template 
std::shared_ptr ModelBase::toHttpContent( const utility::string_t& name, const std::map& value, const utility::string_t& contentType )
{
    web::json::value jobj = ModelBase::toJson(value);
    std::shared_ptr content( new HttpContent );
    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(jobj.serialize()) ) ) );
    return content;
}
template 
bool ModelBase::fromHttpContent( std::shared_ptr val,  std::shared_ptr& outVal )
{
    utility::string_t str;
    if(val == nullptr) return false;
    if( outVal == nullptr )
    {
        outVal = std::shared_ptr(new T());
    }
    ModelBase::fromHttpContent(val, str);
    return fromString(str, outVal);
}
template 
bool ModelBase::fromHttpContent( std::shared_ptr val, std::vector & )
{
    return true;
}
template 
bool ModelBase::fromHttpContent( std::shared_ptr val, std::map & )
{
    return true;
}
{{#modelNamespaceDeclarations}}
}
{{/modelNamespaceDeclarations}}

#endif /* {{modelHeaderGuardPrefix}}_ModelBase_H_ */




© 2015 - 2024 Weber Informatics LLC | Privacy Policy