#pragma once

#include "nlohmann/json.hpp"

#include "DebugLog.h"

template <typename T>
class CJsonParserText
{
public:
    bool Deserialize(const std::string& strText, T& myOption)
    {
        if (strText.empty())
        {
            // nothing to parse
            return false;
        }

        try
        {
            // parse string into json
            nlohmann::json jf = nlohmann::json::parse(strText);

            // deserialize json into object
            myOption = jf.template get<T>();

            return true;
        }
        catch (std::exception& e)
        {
            std::string ss = e.what();
            DebugLog("CJsonParserText::Deserialize(const std::string& strText, T& myOption) {} : {}", strText, ss);
            return false;
        }
    }

    bool Serialize(std::string& strText, const T& myOption)
    {
        // convert object into json
        nlohmann::json jf(myOption);

        strText = nlohmann::json::to_string(jf);

        return false;
    }

    bool Deserialize(const std::string& strText, std::vector<T>& myOption)
    {
        if (strText.empty())
        {
            // nothing to parse
            return false;
        }

        try
        {
            // load json file
            nlohmann::json jf = nlohmann::json::parse(strText);

            // deserialize json into object
            myOption = jf.template get<std::vector<T>>();

            return true;
        }
        catch (std::exception& e)
        {
            std::string ss = e.what();
            DebugLog("CJsonParserText::Deserialize(const std::string& strText, std::vector<T>& myOption) {} : {}", strText, ss);
            return false;
        }
    }

    bool Serialize(std::string& strText, const std::vector<T>& myOption)
    {
        // convert object into json
        nlohmann::json jf(myOption);
        strText = nlohmann::json::to_string(jf);
        return false;
    }
};