mardi 18 juin 2019

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.

The json message received contains one key:value pair ("userID": 100), where the value is an integer.

However, if the value is sent as a string '100', rapidjson crashed the whole program with the following error:

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.

Any suggestion how to deal with this situation? For example, is there a better way to handle the error?

Json message:

{
   "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;
}

Aucun commentaire:

Enregistrer un commentaire