jsoncpp.hpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #include <jsoncpp/json/json.h>
  2. /*json数据对象类*/
  3. class Json::Value
  4. {
  5. Value &operator=(const Value &other); // Value重载了[]和=,因此所有的赋值和获取数据都可以通过
  6. Value &operator[](const std::string &key); // 简单的⽅式完成 val["name"] ="xx";
  7. Value &operator[](const char *key);
  8. Value removeMember(const char *key); // 移除元素
  9. const Value &operator[](ArrayIndex index) const; // val["score"][0]
  10. Value &append(const Value &value); // 添加数组元素val["score"].append(88);
  11. ArrayIndex size() const; // 获取数组元素个数 val["score"].size();
  12. bool isNull(); // ⽤于判断是否存在某个字段
  13. std::string asString() const; // 转string string name =
  14. val["name"].asString();
  15. const char *asCString() const; // 转char* char *name =
  16. val["name"].asCString();
  17. Int asInt() const; // 转int int age = val["age"].asInt();
  18. float asFloat() const; // 转float float weight = val["weight"].asFloat();
  19. bool asBool() const; // 转 bool bool ok = val["ok"].asBool();
  20. };
  21. /*序列化接口*/
  22. class JSON_API StreamWriter
  23. {
  24. virtual int write(Value const &root, std::ostream *sout) = 0;
  25. };
  26. class JSON_API StreamWriterBuilder : public StreamWriter::Factory
  27. {
  28. virtual StreamWriter *newStreamWriter() const;
  29. };
  30. /*反序列化接口*/
  31. class JSON_API CharReader
  32. {
  33. virtual bool parse(char const *beginDoc, char const *endDoc,
  34. Value *root, std::string *errs) = 0;
  35. };
  36. class JSON_API CharReaderBuilder : public CharReader::Factory
  37. {
  38. virtual CharReader *newCharReader() const;
  39. };