1. 问题陈述
首先, json报文语法格式是正确无误的, 使用json在线工具解析ok. 而且出现的问题现象是: 相同的报文格式, 有的解析成功; 而有的解析失败。比如报文:
[
{
"name": "lixiaogang5",
"sex": "男",
"address": "guizhousheng"
}
]
备注: 这个JSON仅用于本例说明, 并非实际项目中的JSON格式字符串数据.
概率性的某些同格式的json解析失败。解析过程:
document.Parse(pMsg);
if(document.HasParseError())
{
// 错误处理分支
}
因为没有增加RapidJSON解析失败的可视化字符串打印提示,所以无法查看具体报错原因。需要增加RapidJSON解析失败的错误码打印提示。
2. 问题排查
粗略扫描下RapidJSON源码, 共提供了以下几个与解析失败的提示信息有关的API, 现增加到项目工程中, 重新Makefile编译并替换服务器, 重启服务并复现该问题, 观察错误日志打印.
LOG_PRT("GetParseError:[%d], GetErrorOffset:[%ld], GetParseError_En[%s], HasParseError[%d]",
document.GetParseError(), document.GetErrorOffset(),
(const RAPIDJSON_ERROR_CHARTYPE*)GetParseError_En((ParseErrorCode)document.GetParseError()),
document.HasParseError());
得到RapidJSON可视化字符串提示信息如下:
![16d1ebdff73b39e07cbd37e32e4939be.png](https://i-blog.csdnimg.cn/blog_migrate/347a58ae2a4fc3c17f29aa86ca1342fd.jpeg)
在RapidJSON源码中, 共有以下几种可视化字符串提示信息,
//! Maps error code of parsing into error message.
/*!
ingroup RAPIDJSON_ERRORS
param parseErrorCode Error code obtained in parsing.
return the error message.
note User can mak