#pragma once #include #include #include class JsonValue { public: enum class Type { Null, Boolean, Number, String, Array, Object }; JsonValue(); explicit JsonValue(bool value); explicit JsonValue(double value); explicit JsonValue(const char* value); explicit JsonValue(const std::string& value); static JsonValue MakeArray(); static JsonValue MakeObject(); Type type() const { return mType; } bool isNull() const { return mType == Type::Null; } bool isBoolean() const { return mType == Type::Boolean; } bool isNumber() const { return mType == Type::Number; } bool isString() const { return mType == Type::String; } bool isArray() const { return mType == Type::Array; } bool isObject() const { return mType == Type::Object; } bool asBoolean(bool fallback = false) const; double asNumber(double fallback = 0.0) const; const std::string& asString() const; const std::vector& asArray() const; const std::map& asObject() const; std::vector& array(); std::map& object(); void pushBack(const JsonValue& value); void set(const std::string& key, const JsonValue& value); const JsonValue* find(const std::string& key) const; private: Type mType; bool mBooleanValue; double mNumberValue; std::string mStringValue; std::vector mArrayValue; std::map mObjectValue; }; bool ParseJson(const std::string& text, JsonValue& value, std::string& error); std::string SerializeJson(const JsonValue& value, bool pretty = false);