Skip to content

How to serialize from json to vector<customType>? #632

@ARMGAMES

Description

@ARMGAMES

Hey, i have such code

std::vector<ConfigStructures::MyConfigs> MyConfigs;
json JsonData;

struct MyConfigs
{
    bool selected = false;
    std::string Name;
};

inline void to_json(json& j, const MyConfigs& config) {
    j = json{
        { "selected", config.selected },
        { "name", config.Name }
    };
}

inline void from_json(const json& j, MyConfigs& config) {
    config.selected = j.at("selected").get < bool >();
    config.Name = j.at("name").get < int >();
}

i can serialize to json, as i set to_json function
JsonData = MyConfigs;

But i can't do this
MyConfigs = JsonData

Try do like this:
MyConfigs = JsonData.get < vector<ConfigStructures::MyConfigs> >();
and this

MyConfigs.clear();
for (auto& element : JsonData) {
    MyConfigs.push_back(element);
}

But it give me error:
JSON_THROW(type_error::create(302, "type must be number, but is " + j.type_name()));

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions