65 lines
1.6 KiB
C++
65 lines
1.6 KiB
C++
#pragma once
|
|
|
|
#include <map>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
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<JsonValue>& asArray() const;
|
|
const std::map<std::string, JsonValue>& asObject() const;
|
|
|
|
std::vector<JsonValue>& array();
|
|
std::map<std::string, JsonValue>& object();
|
|
|
|
void pushBack(const JsonValue& value);
|
|
void set(const std::string& key, const JsonValue& value);
|
|
|
|
const JsonValue* find(const std::string& key) const;
|
|
|
|
private:
|
|
void reset(Type type);
|
|
|
|
Type mType;
|
|
bool mBooleanValue;
|
|
double mNumberValue;
|
|
std::string mStringValue;
|
|
std::vector<JsonValue> mArrayValue;
|
|
std::map<std::string, JsonValue> mObjectValue;
|
|
};
|
|
|
|
bool ParseJson(const std::string& text, JsonValue& value, std::string& error);
|
|
std::string SerializeJson(const JsonValue& value, bool pretty = false);
|