Assert\Assertion::isJsonString PHP Method

isJsonString() public static method

NOTICE: Since this does a json_decode to determine its validity you probably should consider, when using the variable content afterwards, just to decode and check for yourself instead of using this assertion.
public static isJsonString ( mixed $value, string | null $message = null, string | null $propertyPath = null ) : boolean
$value mixed
$message string | null
$propertyPath string | null
return boolean
    public static function isJsonString($value, $message = null, $propertyPath = null)
    {
        if (null === json_decode($value) && JSON_ERROR_NONE !== json_last_error()) {
            $message = sprintf($message ?: 'Value "%s" is not a valid JSON string.', static::stringify($value));
            throw static::createException($value, $message, static::INVALID_JSON_STRING, $propertyPath);
        }
        return true;
    }

Usage Example

 /**
  * @param string $jsonString
  * @return Result
  */
 public static function fromJSON($jsonString)
 {
     Assertion::isJsonString($jsonString);
     $itemJsonStrings = json_decode($jsonString, true);
     $items = array();
     foreach ($itemJsonStrings as $anItemJsonString) {
         $items[] = Item::fromJSON($anItemJsonString);
     }
     return new ItemCollection($items);
     $p = new Paginator();
 }
All Usage Examples Of Assert\Assertion::isJsonString