Skip to content

Instantly share code, notes, and snippets.

@connormanning
Last active May 27, 2020 18:51
Show Gist options
  • Save connormanning/08462b92f1d9e29fe6454e07708c0d88 to your computer and use it in GitHub Desktop.
Save connormanning/08462b92f1d9e29fe6454e07708c0d88 to your computer and use it in GitHub Desktop.
Parsing map
using StringMap = std::map<std::string, std::string>;
// From null: [json.exception.type_error.302] type must be object, but is null
try { StringMap m = json(); }
catch (std::exception& e) { std::cout << "From null: " << e.what() << std::endl; }
// From non-object: [json.exception.type_error.302] type must be object, but is number
try { StringMap m = json(42); }
catch (std::exception& e) { std::cout << "From number: " << e.what() << std::endl; }
// From non-string: [json.exception.type_error.302] type must be string, but is number
try { StringMap m = json({ { "a", 42 } }); }
catch (std::exception& e) { std::cout << "From non-string: " << e.what() << std::endl; }
// Works
StringMap m = json({ { "a", "x" }, { "b", "y" }, { "c", "z" } });
for (const auto& p : m)
{
std::cout << p.first << " -> " << p.second << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment