c++validationconstantsstring-comparisonjson-spirit

comparing two std::string consts returned from functions (two json-spirit get_str()s) 1-liner


I'm using json-spirit to parse json for my ws++ server.

I'm comparing strings for validation of the signup process, confirmed logins, email, password, etc.

json-spirit's get_str() returns const std::string&.

I've tried ==, compare, even strcmp. All crash the program with terminate called after throwing an instance of 'std::runtime_error' what(): value type is 0 not 2 when I try to compare the function returns with a "1 liner" (all in the if comparison statement).

Can this function be returned using a 1-liner?


Solution

  • Just guessing here: I think your source json is not giving you what you think it's giving you in terms of the type of the value.

    I'm not familiar with json-spirit, but your link has this paragraph:

    You obtain the Value's type by calling Value::type(). You can then call the appropriate getter function. Generally, you will know a file's format, so you will know what type the JSON values should have. A std::runtime_error exception is thrown if you try to get a value of the wrong type, for example, if you try to extract a string from a value containing an integer.

    This sounds an awful lot like the error you're seeing. I'm guessing json-spirit thinks your strings aren't strings at all. When you try to call get_str() on something that's not a string (whatever is defined as type "0") it's throwing an exception.

    EDIT: poking around in json-spirit's source, type "0" is NULL_TYPE