Chương trình schema validator dùng quicktype trên VS

Như tiêu đề ạ, em đang làm schema validator dùng quicktype. Em đã thực hiện một vài step sau:

  • Generate json schema bằng quicktype.
  • add library nlohman/json.hpp https://github.com/nlohmann/json
  • download và run 2 file BAT boost
  • Trong VS, chọn Project->Properties->
    • C/C+±>General->Add link boost_1_75 vào Additional Include Directory
    • Linker->General->Add link boost_1_75/stage/lib vào Additional Library Directories
  • Run
    Và kết quả là nó bị lỗi này
//  To parse this JSON data, first install
//
//      Boost     http://www.boost.org
//      json.hpp  https://github.com/nlohmann/json
//
//  Then include this file, and then do
//
//     Models data = nlohmann::json::parse(jsonString);

#pragma once

#include "json.hpp"

#include <boost/optional.hpp>
#include <stdexcept>
#include <regex>


#include <iostream>
#include <fstream>

#ifndef NLOHMANN_OPT_HELPER
#define NLOHMANN_OPT_HELPER
namespace nlohmann {
    template <typename T>
    struct adl_serializer<std::shared_ptr<T>> {
        static void to_json(json& j, const std::shared_ptr<T>& opt) {
            if (!opt) j = nullptr; else j = *opt;
        }

        static std::shared_ptr<T> from_json(const json& j) {
            if (j.is_null()) return std::unique_ptr<T>(); else return std::unique_ptr<T>(new T(j.get<T>()));
        }
    };
}
#endif

namespace quicktype {
    using nlohmann::json;

    class ClassMemberConstraints {
    private:
        boost::optional<int> min_value;
        boost::optional<int> max_value;
        boost::optional<size_t> min_length;
        boost::optional<size_t> max_length;
        boost::optional<std::string> pattern;

    public:
        ClassMemberConstraints(
            boost::optional<int> min_value,
            boost::optional<int> max_value,
            boost::optional<size_t> min_length,
            boost::optional<size_t> max_length,
            boost::optional<std::string> pattern
        ) : min_value(min_value), max_value(max_value), min_length(min_length), max_length(max_length), pattern(pattern) {}
        ClassMemberConstraints() = default;
        virtual ~ClassMemberConstraints() = default;

        void set_min_value(int min_value) { this->min_value = min_value; }
        auto get_min_value() const { return min_value; }

        void set_max_value(int max_value) { this->max_value = max_value; }
        auto get_max_value() const { return max_value; }

        void set_min_length(size_t min_length) { this->min_length = min_length; }
        auto get_min_length() const { return min_length; }

        void set_max_length(size_t max_length) { this->max_length = max_length; }
        auto get_max_length() const { return max_length; }

        void set_pattern(const std::string& pattern) { this->pattern = pattern; }
        auto get_pattern() const { return pattern; }
    };

    class ClassMemberConstraintException : public std::runtime_error {
    public:
        ClassMemberConstraintException(const std::string& msg) : std::runtime_error(msg) {}
    };

    class ValueTooLowException : public ClassMemberConstraintException {
    public:
        ValueTooLowException(const std::string& msg) : ClassMemberConstraintException(msg) {}
    };

    class ValueTooHighException : public ClassMemberConstraintException {
    public:
        ValueTooHighException(const std::string& msg) : ClassMemberConstraintException(msg) {}
    };

    class ValueTooShortException : public ClassMemberConstraintException {
    public:
        ValueTooShortException(const std::string& msg) : ClassMemberConstraintException(msg) {}
    };

    class ValueTooLongException : public ClassMemberConstraintException {
    public:
        ValueTooLongException(const std::string& msg) : ClassMemberConstraintException(msg) {}
    };

    class InvalidPatternException : public ClassMemberConstraintException {
    public:
        InvalidPatternException(const std::string& msg) : ClassMemberConstraintException(msg) {}
    };

    inline void CheckConstraint(const std::string& name, const ClassMemberConstraints& c, int64_t value) {
        if (c.get_min_value() != boost::none && value < *c.get_min_value()) {
            throw ValueTooLowException("Value too low for " + name + " (" + std::to_string(value) + "<" + std::to_string(*c.get_min_value()) + ")");
        }

        if (c.get_max_value() != boost::none && value > *c.get_max_value()) {
            throw ValueTooHighException("Value too high for " + name + " (" + std::to_string(value) + ">" + std::to_string(*c.get_max_value()) + ")");
        }
    }

    inline void CheckConstraint(const std::string& name, const ClassMemberConstraints& c, const std::string& value) {
        if (c.get_min_length() != boost::none && value.length() < *c.get_min_length()) {
            throw ValueTooShortException("Value too short for " + name + " (" + std::to_string(value.length()) + "<" + std::to_string(*c.get_min_length()) + ")");
        }

        if (c.get_max_length() != boost::none && value.length() > *c.get_max_length()) {
            throw ValueTooLongException("Value too long for " + name + " (" + std::to_string(value.length()) + ">" + std::to_string(*c.get_max_length()) + ")");
        }

        if (c.get_pattern() != boost::none) {
            std::smatch result;
            std::regex_search(value, result, std::regex(*c.get_pattern()));
            if (result.empty()) {
                throw InvalidPatternException("Value doesn't match pattern for " + name + " (" + value + " != " + *c.get_pattern() + ")");
            }
        }
    }

    inline json get_untyped(const json& j, const char* property) {
        if (j.find(property) != j.end()) {
            return j.at(property).get<json>();
        }
        return json();
    }

    inline json get_untyped(const json& j, std::string property) {
        return get_untyped(j, property.data());
    }

    template <typename T>
    inline std::shared_ptr<T> get_optional(const json& j, const char* property) {
        if (j.find(property) != j.end()) {
            return j.at(property).get<std::shared_ptr<T>>();
        }
        return std::shared_ptr<T>();
    }

    template <typename T>
    inline std::shared_ptr<T> get_optional(const json& j, std::string property) {
        return get_optional<T>(j, property.data());
    }

    class Datum {
    public:
        Datum() :
            password_constraint(boost::none, boost::none, boost::none, boost::none, std::string("^[a-zA-Z0-9._]{8,16}$")),
            username_constraint(boost::none, boost::none, boost::none, boost::none, std::string("^[a-zA-Z0-9._]+$"))
        {}
        virtual ~Datum() = default;

    private:
        std::shared_ptr<std::string> password;
        ClassMemberConstraints password_constraint;
        std::shared_ptr<std::string> username;
        ClassMemberConstraints username_constraint;

    public:
        std::shared_ptr<std::string> get_password() const { return password; }
        void set_password(std::shared_ptr<std::string> value) { if (value) CheckConstraint("password", password_constraint, *value); this->password = value; }

        std::shared_ptr<std::string> get_username() const { return username; }
        void set_username(std::shared_ptr<std::string> value) { if (value) CheckConstraint("username", username_constraint, *value); this->username = value; }
    };

    class Models {
    public:
        Models() = default;
        virtual ~Models() = default;

    private:
        std::shared_ptr<std::vector<Datum>> data;

    public:
        std::shared_ptr<std::vector<Datum>> get_data() const { return data; }
        void set_data(std::shared_ptr<std::vector<Datum>> value) { this->data = value; }
    };
}

namespace nlohmann {
    void from_json(const json& j, quicktype::Datum& x);
    void to_json(json& j, const quicktype::Datum& x);

    void from_json(const json& j, quicktype::Models& x);
    void to_json(json& j, const quicktype::Models& x);

    inline void from_json(const json& j, quicktype::Datum& x) {
        x.set_password(quicktype::get_optional<std::string>(j, "password"));
        x.set_username(quicktype::get_optional<std::string>(j, "username"));
    }

    inline void to_json(json& j, const quicktype::Datum& x) {
        j = json::object();
        j["password"] = x.get_password();
        j["username"] = x.get_username();
    }

    inline void from_json(const json& j, quicktype::Models& x) {
        x.set_data(quicktype::get_optional<std::vector<quicktype::Datum>>(j, "data"));
    }

    inline void to_json(json& j, const quicktype::Models& x) {
        j = json::object();
        j["data"] = x.get_data();
    }
}


//using json = nlohmann::json;

int main()
{
    std::ifstream i("account.json");
    quicktype::Models data = nlohmann::json::parse(i);
    std::cout << data.get_data();
}

Các anh chỉ giúp em với ạ, em xin cảm ơn trước :smile:

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?