描述JSON串
如何使用jsoncpp提供的數據結構來存儲如下JSON串?
// Configuration options
{
// Default encoding for text
"encoding" : "UTF-8",
// Plug-ins loaded at start-up
"plug-ins" : [
"python",
"c++", // trailing comment
"ruby"
],
// Tab indent size
// (multi-line comment)
"indent" : { /*embedded comment*/ "length" : 3, "use_space": true }
}
jsoncpp使用Json::Value對象來保存JSON串,Json::Value對象可以表示如下數據類型:
枚舉類型 | 說明 | 翻譯 |
---|---|---|
nullValue | 'null' value | 不表示任何數據,空值 |
intValue | signed integer value | 表示有符號整數 |
uintValue | unsigned integer value | 表示無符號整數 |
realValue | double value | 表示浮點數 |
stringValue | UTF-8 string value | 表示utf8格式的字符串 |
booleanValue | bool value | 表示布爾數 |
arrayValue | array value (ordered list) | 表示數組,即JSON串中的[] |
objectValue | object value (collection of name/value pairs) | 表示鍵值對,即JSON串中的{} |
整個JSON串可以看做一個Json::ValueType::objectValue類型的Json::Value對象,我們將其命名為`root`,`root`包含了許多子Json::Value對象,例如:
"encoding"是Json::ValueType::stringValue類型的Json::Value對象
"plug-ins"是Json::ValueType::arrayValue類型的Json::Value對象
"indent"是Json::ValueType::objectValue類型的Json::Value對象
讀取數據
在知道如何描述JSON串后,我們來看下Json::Value類提供了哪些方法來訪問保存的數據:
// 檢測保存的數據類型
bool isNull() const;
bool isBool() const;
bool isInt() const;
bool isInt64() const;
bool isUInt() const;
bool isUInt64() const;
bool isIntegral() const;
bool isDouble() const;
bool isNumeric() const;
bool isString() const;
bool isArray() const;
bool isObject() const;
// 基礎數據類型的訪問
const char* asCString() const;
JSONCPP_STRING asString() const;
Int asInt() const;
UInt asUInt() const;
Int64 asInt64() const;
UInt64 asUInt64() const;
LargestInt asLargestInt() const;
LargestUInt asLargestUInt() const;
float asFloat() const;
double asDouble() const;
bool asBool() const;
// ValueType::arrayValue和ValueType::objectValue數據類型的訪問,操作方式很類似C++的vector,可以使用數組風格或者迭代器風格來操作數據
ArrayIndex size() const;
Value& operator[](ArrayIndex index);
Value& operator[](int index);
const Value& operator[](ArrayIndex index) const;
const Value& operator[](int index) const;
Value get(ArrayIndex index, const Value& defaultValue) const;
const_iterator begin() const;
const_iterator end() const;
iterator begin();
iterator end();
// ValueType::objectValue數據類型的訪問,操作方式很類似C++的map
Value& operator[](const char* key);
const Value& operator[](const char* key) const;
Value& operator[](const JSONCPP_STRING& key);
const Value& operator[](const JSONCPP_STRING& key) const;
Value& operator[](const StaticString& key);
Value& operator[](const CppTL::ConstString& key);
const Value& operator[](const CppTL::ConstString& key) const;
Value get(const char* key, const Value& defaultValue) const;
Value get(const char* begin, const char* end, const Value& defaultValue) const;
Value get(const JSONCPP_STRING& key, const Value& defaultValue) const;
Value get(const CppTL::ConstString& key, const Value& defaultValue) const;
修改數據
知道如何獲取Json::Value對象或其子對象后,我們來看下如何修改Json::Value保存的數據:
// 直接使用=賦值就可以了
Value& operator=(Value other);
// 因為Json::Value已經實現了各種數據類型的構造函數
Value(ValueType type = nullValue);
Value(Int value);
Value(UInt value);
Value(Int64 value);
Value(UInt64 value);
Value(double value);
Value(const char* value);
Value(const char* begin, const char* end);
Value(const StaticString& value);
Value(const JSONCPP_STRING& value);
Value(const CppTL::ConstString& value);
Value(bool value);
Value(const Value& other);
Value(Value&& other);
解析JSON串
前面了解了Json::Value是如何存儲JSON串,但是如何解析JSON串並生成Json::Value對象呢?
bool Json::Reader::parse(const std::string& document, // JSON串
Value& root, // 這就時JSON串生成的Json::Value對象
bool collectComments = true); // 允許在反序列化的時候保存注釋,然后在序列化的時候寫回注釋
bool Json::Reader::parse(const char* beginDoc, // 除了傳入整個JSON串,還可以只指定其中的某一段進行解析
const char* endDoc,
Value& root,
bool collectComments = true);
bool Json::Reader::parse(JSONCPP_ISTREAM& is, // 當然還可以傳入標准輸入流,例如打開的文件
Value& root,
bool collectComments = true);
生成JSON串
現在我們知道怎么從JSON串中獲取Json::Value對象,現在讓我們來看下如何將一個Json::Value對象轉換成JSON串:
// 所有從Writer派生出來的類都可以直接將Json::Value對象轉換成JSON字符串,可使用的類有:FastWriter、StyledWriter
class JSON_API Writer {
public:
virtual ~Writer();
virtual JSONCPP_STRING write(const Value& root) = 0;
};
// 所有從StreamWriter派生出來的類都可以直接將Json::Value對象轉換成JSON字符串到輸出流中,可使用的類有:BuiltStyledStreamWriter,但是這個類無法直接訪問,只能通過StreamWriterBuilder::newStreamWriter方法直接獲取類實例。注:如果只想獲取字符串不想寫到流里面就Writer派生的類
class JSON_API StreamWriter {
protected:
JSONCPP_OSTREAM* sout_; // not owned; will not delete
public:
StreamWriter();
virtual ~StreamWriter();
virtual int write(Value const& root, JSONCPP_OSTREAM* sout) = 0;
class JSON_API Factory {
public:
virtual ~Factory();
virtual StreamWriter* newStreamWriter() const = 0;
}; // Factory
}; // StreamWriter
// 所有從StyledStreamWriter派生出來的類都可以直接將Json::Value對象轉換成JSON字符串到輸出流中,感覺和BuiltStyledStreamWriter實現類似。注:如果只想獲取字符串不想寫到流里面就Writer派生的類
class JSON_API StyledStreamWriter {
public:
StyledStreamWriter(JSONCPP_STRING indentation = "\t");
~StyledStreamWriter() {}
public:
void write(JSONCPP_OSTREAM& out, const Value& root);
}