Last active
September 14, 2017 20:02
-
-
Save DrPizza/a3e73db78a5af70d08b40232420bceec to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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