سریالایز کردن cv::Rect با nlohmann::json - هفت خط کد انجمن پرسش و پاسخ برنامه نویسی

سریالایز کردن cv::Rect با nlohmann::json

+1 امتیاز

سلام. خدمت دوستان عزیز

من به این روش کلاس cv::Rect را سریالایز می کنم و تو یک کلاس دیگر که اون هم باید سریالایز بشه ازش استفاده می کنم ولی خطا میده کسی راه حلی داره برای این موضوع 


void to_json(json& j, const cv::Rect& rect)
{
    j =
        nlohmann::json
    {
        {"x", rect.x},
        {"y", rect.y},
        {"w", rect.width},
        {"h", rect.height}
    };
}

 void from_json(const json& j, cv::Rect& rect)
{
    j.at("x").get_to(rect.x);
    j.at("y").get_to(rect.y);
    j.at("w").get_to(rect.width);
    j.at("h").get_to(rect.height);
}


class ConstR {
public:
    ConstR() {}
    ConstR(int id, const cv::Rect& region) :
        id_{ id },
        region_{ region } {

    }
    int id_;
    cv::Rect region_;
    void print() {
        cout << "id:" << id_ << endl;
        cout << "region:" << region_ << endl;
    }
};

void to_json(json& j, const ConstR& p)
{
    j = { {"id", p.id_}, {"region", p.region_}};
}
void from_json(const json& j, ConstR& p)
{

    j.at("id").get_to(p.id_);
    j.at("region").get_to(p.region_);
}

 

سوال شده آبان 21, 1400  بوسیله ی Oscar (امتیاز 127)   8 25 29

1 پاسخ

+1 امتیاز
 
بهترین پاسخ

از تکنیک ADL برای سریالایز کلاس cv::Rect استفاده کنید:

namespace nlohmann
{

    template<>
    struct adl_serializer<cv::Rect>
    {
        static inline void to_json(json& j, const cv::Rect& rect)
        {
            j =
                nlohmann::json
            {
                {"x", rect.x},
                {"y", rect.y},
                {"w", rect.width},
                {"h", rect.height}
            };
        }

        static inline void from_json(const json& j, cv::Rect& rect)
        {
            j.at("x").get_to(rect.x);
            j.at("y").get_to(rect.y);
            j.at("w").get_to(rect.width);
            j.at("h").get_to(rect.height);
        }
    };
}

 

پاسخ داده شده آبان 21, 1400 بوسیله ی farnoosh (امتیاز 8,362)   20 44 59
انتخاب شد بهمن 6, 1401 بوسیله ی مصطفی ساتکی
...