1

I know that std::string cannot be null, but I can't figure out the problem here, let me explain. This is my function:

void HandleResponse(Mod::PlayerEntry player, std::string response)

So response usually has a json value, I parse it with nlohmann json:

auto value = json::parse(response);

In certain occasions, it gives "null", I debugged using:

std::cout << "response: " << response << ", value: " << value << std::endl;
// outputs: response: null, value: null

Now the problem is that I can't figure out how to compare if it's null, here's all the different checks I've tried:

if(response == "null"){}
if(response == ""){}
if(response.empty()){}
if(response == 0){}
if(response == std::string("null")){}
if(response.c_str() == "null"){}
if(response.c_str() == NULL){}
if(response.c_str() == '\0'){}
if(value == "null"){}

None of these have worked.

9
  • 1
    Depends on what you mean by "null". If you mean a string with length zero, try if (response.length() == 0) or if (!response.empty()). Commented Jun 27, 2020 at 0:50
  • Can you post a simple example code? Commented Jun 27, 2020 at 0:56
  • Please elaborate on "none of these have worked." What happens when you try these checks? What is the code that uses them? Commented Jun 27, 2020 at 1:01
  • This is the main variable unsigned char **pk, then these are defined auto id = *(unsigned int *) (*pk + 40); auto data = *(std::string *) (*pk + 48);, the data variable is passed to HandleResponse as response. Commented Jun 27, 2020 at 1:02
  • What is the type of response? Does the json library not allow you to check if the value is null? Looking at nlohmann it looks like json::parse() returns an object of json. Note this is not a std::string. The json object has a convenient type checker: value.is_null(). Commented Jun 27, 2020 at 1:19

3 Answers 3

4

The problem is that response can potentially contain white space. So comparing it exactly for null might be harder than you think. You need to check if response contains null but that anything else is simple white space.

But looking at nlohmann library the json::parse() function returns an object of type json.

json value = json::parse(response);

You can simply check the type of the value:

id (value.is_null())    {std::cout << "is null\n";}
id (value.is_boolean()) {std::cout << "is bool\n";}
id (value.is_number())  {std::cout << "is numb\n";}
id (value.is_object())  {std::cout << "is obj\n";}
id (value.is_array())   {std::cout << "is array\n";}
id (value.is_string())  {std::cout << "is string\n";}
Sign up to request clarification or add additional context in comments.

1 Comment

This is the correct answer to the question. It's also described in the library's readme in its Github repo and the doc. Also the tip that the response might contain white space is very helpful.
1

If response is a std::string, and inserting it into an output stream produces "null", then a correct way to compare it is response == "null".

Comments

0

Apparently the cause is really stupid. I trimmed the string with:

boost::trim(response);

Now I checked if the response is "null":

if (response == "null") return;

and boom, it works!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.