diff --git a/core/lib/PatternLab/JSON.php b/core/lib/PatternLab/JSON.php new file mode 100644 index 0000000..b824d3e --- /dev/null +++ b/core/lib/PatternLab/JSON.php @@ -0,0 +1,51 @@ + false, + JSON_ERROR_DEPTH => 'Maximum stack depth exceeded', + JSON_ERROR_STATE_MISMATCH => 'Underflow or the modes mismatch', + JSON_ERROR_CTRL_CHAR => 'Unexpected control character found', + JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON', + JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded' + ); + + /** + * Returns the last error message when building a JSON file. Mimics json_last_error_msg() from PHP 5.5 + * @param {String} the file that generated the error + */ + public static function hasError() { + $error = json_last_error(); + $errorMessage = array_key_exists($error, self::$errors) ? self::$errors[$error] : "Unknown error ({$error})"; + return $errorMessage; + } + + /** + * Returns the last error message when building a JSON file. Mimics json_last_error_msg() from PHP 5.5 + * @param {String} the file that generated the error + */ + public static function lastErrorMsg($file,$message,$data) { + print "\nThe JSON file, ".$file.", wasn't loaded. The error: ".$message."\n"; + if ($message == "Syntax error, malformed JSON") { + print "\n"; + $parser = new JsonLint\JsonParser(); + $error = $parser->lint($data); + print $error->getMessage(); + print "\n\n"; + } + } + +} \ No newline at end of file