jsoncpp源码篇(二)

使用jsoncpp时主要使用到的类有 Json::ValueJson::Reader;

 1. Json::Value类

 1.1 提供的转换接口
const char* asCString() const;
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;
1.2 提供的测试接口
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;
1.3 提供的查找接口

//获取对应字段内容
Value get(const char* key, const Value& defaultValue) const;

//重载了[], 所以可以使用root["name"],root[0]类似操作;
const Value& operator[](int index) const;
Value& operator[](const char* key);

//上面接口本质都调用了find
Value const* find(char const* begin, char const* end) const;
1.4 重要成员变量

 其中成员变量 value_ 保存了Json::Value中实际存储的内容; 采用了联合体;

union ValueHolder {
  LargestInt int_;
  LargestUInt uint_;
  double real_;
  bool bool_;
  char* string_; // if allocated_, ptr to { unsigned, char[] }.
  ObjectValues* map_;
} value_;

其中的 ObjectValues* map_; 成员能够存储 objectValue或arrayValue 值;

定义: typedef std::map<CZString, Value> ObjectValues;

采用了std::map 存储 {"字段",内容};

举例说明:

{
	"name":"zhangsan", //存储为 char *string_ ; 
	"age":18,          //存储为 LargestInt int_;
	"score":{          //存储为 ObjectValues* map_;
	  "math":"prefect",
	  "english":90
	}
}

 2. Json::Reader类

2.1 提供的解析接口
bool parse(const std::string& document, Value& root,
             bool collectComments = true);
2.2 核心成员变量
using Nodes = std::stack<Value*>;
Nodes nodes_;

 存储Json::Value变量指针的栈;

2.3 重要函数
Value& OurReader::currentValue() { return *(nodes_.top()); }

其作用是获取栈顶指针;

该类中主要用到的就是parse()函数; 其内部通过循环查找 "Token" 类型(如'{','}','[',']','"','数字','bool类型'等) 去匹配 readObject,readArray,decodeNumber,decodeString等,通过如下:

currentValue().swapPayload(v);
currentValue().setOffsetStart(token.start_ - begin_);
currentValue().setOffsetLimit(token.end_ - begin_);

类型操作将内容增加到 parse()函数的root变量中;  其中涉及到nodes_入栈出栈操作,栈指针调整操作等; 具体细节怎么把解析的内容和root中的map联系到一起的?值得探索!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天未及海宽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值