Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ARMGAMES opened this issue Jun 21, 2017 · 1 comment
Closed

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

ARMGAMES opened this issue Jun 21, 2017 · 1 comment

Comments

@ARMGAMES
Copy link

ARMGAMES commented Jun 21, 2017

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()));

@nlohmann
Copy link
Owner

Does it work now?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants