locked
C++/CX template to JsonObject^

    Question

  • Hi C++/CX experts,

    Here's seeking expertise on C++/CX JSON serialization and templates in C++ /CX.

    How do you convert template type to JSON object?

    template <typename T>
    ref class APIResponse sealed
    {
        //...    

        internal:
        property T Data
        {
            T get();
            void set(T value);
        }

        //...
    }

    In the ConvertToJSON() Method:

    //Please help
    JsonObject^ jsonObject = ref new JsonObject();
    auto var = safe_cast<T>(this->Data); // Compilation Error
    jsonObject->Insert(L"data", var);

    Please refer APIResponse.cpp in the following code snippet.

    Here's the code snippet:

    // APIError.h
    #pragma once
    using namespace Platform;
    using namespace Windows::Foundation::Collections;
    
    namespace PP 
    {
    	namespace Checkout 
    	{
    		namespace Transaction
    		{
    			ref class APIError sealed
    			{
    				private:
    					String^ name;
    					String^ message;
    					String^ displayMessage;
    					String^ debugID;
    					IMap<String^, Object^>^ details;
    
    				public:
    					APIError();
    					virtual ~APIError(void);
    
    					property String^ Name
    					{
    						String^ get();					
    						void set(String^ value);
    					}
    
    					property String^ DeveloperMessage
    					{
    						String^ get();					
    						void set(String^ value);
    					}
    
    					property String^ DisplayMessage
    					{
    						String^ get();					
    						void set(String^ value);
    					}
    
    					property String^ DebugID
    					{
    						String^ get();					
    						void set(String^ value);
    					}
    
    					property IMap<String^, Object^>^ Details
    					{
    						IMap<String^, Object^>^ get();					
    						void set(IMap<String^, Object^>^ value);
    					}
    
    					String^ ConvertToJSON();
    			};
    		}
    	}
    }
    
    // APIError.cpp
    #include "pch.h"
    #include "APIError.h"
    #include "collection.h"
    
    using namespace Platform::Collections;
    using namespace Windows::Foundation::Collections;
    using namespace Windows::Data::Json;
    using namespace PP::Checkout::Transaction;
    
    APIError::APIError()
    {
    }
    
    APIError::~APIError(void)
    {
    }
    
    String^ APIError::Name::get()
    {
    	return name;
    }
    
    void APIError::Name::set(String^ value)
    {
    	name = value;
    }
    
    String^ APIError::DeveloperMessage::get()
    {
    	return message;
    }
    
    void APIError::DeveloperMessage::set(String^ value)
    {
    	message = value;
    }
    
    String^ APIError::DisplayMessage::get()
    {
    	return displayMessage;
    }
    
    void APIError::DisplayMessage::set(String^ value)
    {
    	displayMessage = value;
    }
    
    String^ APIError::DebugID::get()
    {
    	return debugID;
    }
    
    void APIError::DebugID::set(String^ value)
    {
    	debugID = value;
    }
    
    IMap<String^, Object^>^ APIError::Details::get()
    {
    	return details;
    }
    
    void APIError::Details::set(IMap<String^, Object^>^ value)
    {
    	details = value;
    }
    
    String^ APIError::ConvertToJSON()
    {
    	String^ jsonString = "";
    	JsonObject^ jsonObject = ref new JsonObject();
    
    	jsonObject->Insert(L"name", JsonValue::CreateStringValue(this->Name));
    	jsonObject->Insert(L"developerMessage", JsonValue::CreateStringValue(this->DeveloperMessage));
    	jsonObject->Insert(L"displayMessage", JsonValue::CreateStringValue(this->DisplayMessage));
    	jsonObject->Insert(L"debugID", JsonValue::CreateStringValue(this->DebugID));
    	
    	if(this->Details->Size > 0)
    	{
    		JsonObject^ jsonObjectDetails = ref new JsonObject();
    		JsonArray^ jsonArrayDetails = ref new JsonArray();
    		for each (IKeyValuePair<Platform::String^, Platform::Object^>^ pair in this->Details)
    		{
    			jsonObjectDetails->Insert(pair->Key, JsonValue::CreateStringValue(dynamic_cast<String^>(pair->Value)));	
    			jsonArrayDetails->Append(jsonObjectDetails);
    		}
    		jsonObject->Insert(L"details", jsonArrayDetails);
    	}
    
    	jsonString = jsonObject->Stringify();
    	return jsonString;
    }
    
    // APIResponse.h
    #pragma once
    #include "APIError.h"
    
    using namespace Platform;
    
    namespace PP 
    {
    	namespace Checkout 
    	{
    		namespace Transaction
    		{
    			template <typename T>
    			ref class APIResponse sealed
    			{
    				internal:					
    					String^ apiVersion;	
    					APIError^ error;	
    
    					property T Data
    					{
    						T get();
    						void set(T value);
    					}
    
    				public:
    					APIResponse(void);	
    					virtual ~APIResponse(void);	
    
    					property String^ ApiVersion
    					{
    						String^ get();						
    						void set(String^ value);
    					}				
    
    					property APIError^ Error
    					{
    						APIError^ get();
    						void set(APIError^ value);
    					}
    
    					String^ ConvertToJSON();
    			};
    
    		}
    	}
    }
    
    // APIResponse.cpp
    #include "pch.h"
    #include "APIResponse.h"
    #include "collection.h"
    
    using namespace Platform;
    using namespace Platform::Collections;
    using namespace Windows::Foundation::Collections;
    using namespace Windows::Data::Json;
    using namespace PP::Checkout::Transaction;
    
    APIResponse<class T>::APIResponse(void)
    {
    	
    }
    
    template<> APIResponse<class T>::~APIResponse(void)
    {
    
    }
    
    String^ APIResponse<typename  T>::ApiVersion::get()
    {
    	return apiVersion;
    }
    
    void APIResponse<typename  T>::ApiVersion::set(String^ value)
    {
    	apiVersion = value;
    }
    
    APIError^ APIResponse<typename  T>::Error::get()
    {
    	return error;
    }
    
    void APIResponse<typename  T>::Error::set(APIError^ value)
    {
    	error = value;
    }
    
    String^ APIResponse<typename  T>::ConvertToJSON()
    {
    	String^ jsonString = "";
    	JsonObject^ jsonObject = ref new JsonObject();
    	jsonObject->Insert(L"apiVersion", JsonValue::CreateStringValue(this->ApiVersion));
    	
    	//Please help 
    	//auto var = safe_cast<T>(this->Data);
    	//jsonObject->Insert(L"data", var);
    
    	JsonObject^ jsonObjectAPIError = ref new JsonObject();
    	jsonObjectAPIError->Insert(L"name", JsonValue::CreateStringValue(this->Error->Name));
    	jsonObjectAPIError->Insert(L"developerMessage", JsonValue::CreateStringValue(this->Error->DeveloperMessage));
    	jsonObjectAPIError->Insert(L"displayMessage", JsonValue::CreateStringValue(this->Error->DisplayMessage));
    	jsonObjectAPIError->Insert(L"debugID", JsonValue::CreateStringValue(this->Error->DebugID));	
    	if(this->Error->Details->Size > 0)
    	{
    		JsonObject^ jsonObjectAPIErrorDetails = ref new JsonObject();
    		JsonArray^ jsonArrayAPIErrorDetails = ref new JsonArray();
    		for each (IKeyValuePair<Platform::String^, Platform::Object^>^ pair in this->Error->Details)
    		{
    			jsonObjectAPIErrorDetails->Insert(pair->Key, JsonValue::CreateStringValue(dynamic_cast<String^>(pair->Value)));	
    			jsonArrayAPIErrorDetails->Append(jsonObjectAPIErrorDetails);
    		}
    		jsonObjectAPIError->Insert(L"details", jsonArrayAPIErrorDetails);
    	}
    	jsonObject->Insert(L"error", jsonObjectAPIError);
    
    	jsonString = jsonObject->Stringify();
    	return jsonString;
    }

     



    • Edited by recherche Thursday, May 23, 2013 4:59 AM typo
    Wednesday, May 22, 2013 12:14 PM

Answers

All replies