Skip to content

Instantly share code, notes, and snippets.

@DrPizza
Last active September 14, 2017 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DrPizza/a3e73db78a5af70d08b40232420bceec to your computer and use it in GitHub Desktop.
Save DrPizza/a3e73db78a5af70d08b40232420bceec to your computer and use it in GitHub Desktop.
#include <variant>
#include <string_view>
#include <vector>
#include <unordered_map>
#include <utility>
namespace json {
struct Value;
using Null = std::nullptr_t;
using Bool = bool;
using Number = double;
using String = std::string_view;
using Array = std::vector<Value>;
using Object = std::unordered_map<String, Value>;
struct Value : public std::variant<Object, Array, String, Number, Bool, Null> {
using variant::variant;
};
} // end namespace json
#include <cassert>
int main() {
auto const Obj = json::Object{
{ "Hello", json::Value{ 1.5 } },
{ "World", json::Value{ true } },
{ "Foo", json::Value{ nullptr } },
{ "Bar", json::Value{ "Majestic" } } };
assert(std::get<json::Number>(Obj.at("Hello")) == 1.5);
assert(std::get<json::Bool>(Obj.at("World")) == true);
assert(std::get<json::Null>(Obj.at("Foo")) == nullptr);
assert(std::get<json::String>(Obj.at("Bar")) == json::String{ "Majestic" });
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment