Skip to content

json_last_error_msg - better message - error position near by #18866

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

juan-morales
Copy link
Contributor

@juan-morales juan-morales commented Jun 16, 2025

Working POC giving a better error message when the json_last_error_msg function is called (only for this function).

Putting aside my bad C coding skill, I would like to know your opinions on this PR, and kindly ask for reviews in order to improve it.

Thx in advance.

(@bukka I know you have been working on a new parser, but ... until that reaches the surface, could we go this way? (with the proper adjustments for sure)

@juan-morales juan-morales changed the title WIP - Json last error msg - better message - error position near by json_last_error_msg - better message - error position near by Jun 17, 2025
Copy link
Member

@DanielEScherzer DanielEScherzer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few code style issues

case PHP_JSON_ERROR_UNSUPPORTED_TYPE:
case PHP_JSON_ERROR_INVALID_PROPERTY_NAME:
case PHP_JSON_ERROR_UTF16:
spprintf(&final_message, 0, "%s near character %zu",error_message, JSON_G(error_pos));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing space

Suggested change
spprintf(&final_message, 0, "%s near character %zu",error_message, JSON_G(error_pos));
spprintf(&final_message, 0, "%s near character %zu", error_message, JSON_G(error_pos));

@@ -340,6 +350,7 @@ PHP_JSON_API void php_json_parser_init_ex(php_json_parser *parser,
int max_depth,
const php_json_parser_methods *parser_methods)
{

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated whitespace addition, please remove

@@ -26,6 +26,7 @@ struct _php_json_encoder {
int depth;
int max_depth;
php_json_error_code error_code;
size_t error_pos;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please indent to match the other lines

@@ -29,12 +29,14 @@ typedef struct _php_json_scanner {
php_json_ctype *marker; /* marker position for backtracking */
php_json_ctype *ctxmarker; /* marker position for context backtracking */
php_json_ctype *str_start; /* start position of the string */
php_json_ctype *input_start; /* start position of the string */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please document the difference between str_start and input_start rather than just repeating the same documentation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants