Skip to content

.get() throws error if used with userdefined structs in unordered_map #1448

Closed
@MagicTripz

Description

  • What is the issue you have?
    I'm trying to save a unordered_map<int,struct> to a json and after that convert it back to the same unordred_map. Saving it works fine, the output is as expected but when i try to load the map from the given json this error is thrown : "JSON_THROW(type_error::create(304, "cannot use at() with " + std::string(type_name())));" (line 15402)."
    If i simply overlooked something then im very sorry to bother.

  • Please describe the steps to reproduce the issue. Can you provide a small but working code example?

struct Data
{
	bool bool1;
	bool bool2;
};

inline void from_json(const json& j, Data& config)
{
	config.bool1 = j.at("bool1").get < bool >();
	config.bool2 = j.at("bool2").get < bool >();
}
inline void to_json(json& j, const Data& config)
{
	j = json{
		{ "bool1", config.bool1 },
		{ "bool2", config.bool2 }
	};
}
void codeexample()
{
	//oversimplified example
	std::string key1, key2;//give them some random value 
	key1 = "fine1";
	key2 = "fine2";
	int integerofchoice = 1;
	std::unordered_map<int, Data> map =
	{
		{1,{true,false}},
		{2,{true,true}},
	};
	//saving it

	json Cfg;
	Cfg[key1][key2].push_back(map[integerofchoice]);
	//output is fine

       map[integerofchoice] = Cfg[key1][key2].get<Data>();//crashes 
        //step by step for debugging
	auto fine = Cfg.at(key1);
	auto fine2 = fine.at(key2);
	auto crash = fine2.at("bool1").get<bool>();



}
  • What is the expected behavior?
    .get<>() function should return bool1 and bool2 as from__json() is defined

  • And what is the actual behavior instead?
    Throws the error and crashes

  • Which compiler and operating system are you using? Is it a supported compiler?
    Msvc win10 newest build

  • Did you use a released version of the library or the version from the develop branch?
    release version 3.5.0

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions