PHP JSON parsing problem

Problem: json_encode() returns FALSE. This means that our crap is broken. Intially it looked like the depth could be set wrong, but that didn’t solve the issue. Fix: Use json_last_error_msg() which will output why it is broken. We could write an entire error handler so we would know about these things

Something like this would be sweet:

<?php
    if (!function_exists('json_last_error_msg')) {
        function json_last_error_msg() {
            static $ERRORS = array(
                JSON_ERROR_NONE => 'No error',
                JSON_ERROR_DEPTH => 'Maximum stack depth exceeded',
                JSON_ERROR_STATE_MISMATCH => 'State mismatch (invalid or malformed JSON)',
                JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
                JSON_ERROR_SYNTAX => 'Syntax error',
                JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded'
            );

            $error = json_last_error();
            return isset($ERRORS[$error]) ? $ERRORS[$error] : 'Unknown error';
        }
    }
?>

Also, in PHP 5.5.0+ we could just change our json_encode to:

echo json_encode($foo, JSON_PARTIAL_OUTPUT_ON_ERROR | JSON_PRETTY_PRINT);