Skip to content
JasonPP::

IsJsonValid

bool JasonPP::IsJsonValid(const std::string code, JsonData* out_parsedJson = 0x0)

A global function for checking whether or not json code is valid.

Because this function literally just tries to parse the code in a try-catch block, you can also provide a pointer to your own JsonData/Json object to parse into.
This way you avoid parsing the same code twice.

The bad way
if (IsJsonValid(myCode)) // <-- Parsing once here to see if its valid
{
    Json j;
    j.Parse(myCode); // <-- Parsing again
}
The good way
Json j;
if (IsJsonValid(myCode, &j)) // <-- Parsing once here to see if its valid
{
    // use j
}
else
{
    // don't use j here. It might be partly parsed. Depending on when and why the parsing aborted.
}

Returns bool:

true: if the json code supplied is valid
false: if the json code supplied is invalid


Arguments:

std::string code: The json code to validate
JsonData* out_parsedJson: Optional parameter. Directly parse into this JsonData struct.