Skip to content

Instantly share code, notes, and snippets.

@connormanning
Created May 27, 2020 18:37
Show Gist options
  • Save connormanning/c73647f01235fd8cbf25f26adc0b1882 to your computer and use it in GitHub Desktop.
Save connormanning/c73647f01235fd8cbf25f26adc0b1882 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