简体   繁体   English

当 json 值应该是 INT 但作为字符串类型发送时,rapidjson 崩溃了

[英]rapidjson crashed when json value should be INT but send as string type

I have a C++ code that parses incoming json message using rapidjson.我有一个 C++ 代码,它使用 Rapidjson 解析传入的 json 消息。

The json message received contains one key:value pair ("userID": 100), where the value is an integer.接收到的 json 消息包含一个 key:value 对 ("userID": 100),其中 value 是一个整数。

However, if the value is sent as a string '100', rapidjson crashed the whole program with the following error:但是,如果该值作为字符串“100”发送,rapidjson 会导致整个程序崩溃并出现以下错误:

Invalid response: { "error": "ERR_RATE_LIMIT"}
trading: ../../include/rapidjson/document.h:1737: int rapidjson::GenericValue<Encoding, Allocator>::GetInt() const [with Encoding = rapidjson::UTF8<>; Allocator = rapidjson::MemoryPoolAllocator<>]: Assertion `data_.f.flags & kIntFlag' failed.
/home/ray/dev/trading_execution/src/trading/trading.run.sh: line 39:  2518 Aborted                 (core dumped) ./trading 1234

I would expect rapidjson can handle this more gently than crashing the program.我希望 Rapidjson 可以比使程序崩溃更温和地处理这个问题。

Any suggestion how to deal with this situation?任何建议如何处理这种情况? For example, is there a better way to handle the error?例如,有没有更好的方法来处理错误?

Json message: JSON 消息:

{
   "ctRequestId":   "cfa5511f-8c1a-492b-b81a-1462d03bbe99",
    "requestType":  "generic",  
    "userID":       100,                        
}

Code:代码:

    userID          = getJSONInt(document,      "userID");      

    int getJSONInt(rapidjson::Document& document, const char* memberName)
    {
    int memberValue;

    try
    {
        if (document.HasMember(memberName))
        memberValue = document[memberName].GetInt();
    }
    catch(const std::exception& e)
    {
        std::cerr << e.what() << '\n';
    }

    return memberValue;
}

No expert of rapidjson, but according to the documentation ( http://rapidjson.org/md_doc_tutorial.html )没有rapidjson 专家,但根据文档( http://rapidjson.org/md_doc_tutorial.html

Note that, RapidJSON does not automatically convert values between JSON types.请注意,RapidJSON 不会自动在 JSON 类型之间转换值。 If a value is a string, it is invalid to call GetInt(), for example.例如,如果值是字符串,则调用 GetInt() 是无效的。 In debug mode it will fail an assertion.在调试模式下,它将使断言失败。 In release mode, the behavior is undefined.在发布模式下,行为未定义。 In the following sections we discuss details about querying individual types.在以下部分中,我们将讨论有关查询单个类型的详细信息。

If you look in the table in the section "Querying Number" of the linked document you can find some member function you can use to test the type before extracting it.如果您查看链接文档的“查询编号”部分中的表格,您可以找到一些成员函数,您可以在提取类型之前使用它来测试类型。 In your case you might want to try IsInt()在您的情况下,您可能想尝试IsInt()

Edit: for the particular use case IsUint/GetUint may be more appropriate as pointed out in the comments编辑:对于特定用例 IsUint/GetUint 可能更合适,如评论中所指出的

Try this:尝试这个:

{
...,
...,
"UserId" : "100"  // note double quotes for 100
}

And if the value of "UserId" is string, then query it using GetString() query and not GetInt() to pass assertion test.如果“UserId”的值是字符串,则使用GetString()查询而不是GetInt()来查询它以通过断言测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM