Skip to content
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

Support multi-byte characters (Unicode) #69

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

bluedasher13
Copy link

RAW JSON Data (errorData.json):

{"DAMAGE":"圓圓圓圓圓圓圓"

Test Code:

$parser = new \Seld\JsonLint\JsonParser();
$exception = $parser->lint(file_get_contents('errorData.json'));
echo $exception->getMessage();

【Before fix】:

Result:

Parse error on line 1:
...�圓圓圓圓圓圓"
----------------------^
Expected one of: '}', ','

Contains incorrect characters, and fails to json_encode($message):

var_export(json_encode($exception->getMessage()) === false);
var_export(json_last_error_msg() === 'Malformed UTF-8 characters, possibly incorrectly encoded');

【After fix】:

Result (after fix):

Parse error on line 1:
{"DAMAGE":"圓圓圓圓圓圓圓"
------------------^
Expected one of: '}', ','

Not contains incorrect characters, and succeed in json_encode($message):

var_export(json_encode($exception->getMessage()) === '"Parse error on line 1:\\n{\\"DAMAGE\\":\\"\\u5713\\u5713\\u5713\\u5713\\u5713\\u5713\\u5713\\"\\n------------------^\\nExpected one of: \'}\', \',\'"');
var_export(json_last_error_msg() === 'No error');

RAW JSON Data (errorData.json):
```json
{"DAMAGE":"圓圓圓圓圓圓圓"
```

Test Code:
```php
$parser = new \Seld\JsonLint\JsonParser();
$exception = $parser->lint(file_get_contents('errorData.json'));
echo $exception->getMessage();
```


【Before fix】:

Result:
```
Parse error on line 1:
...�圓圓圓圓圓圓"
----------------------^
Expected one of: '}', ','
```

Contains incorrect characters, and fails to `json_encode($message)`:
```php
var_export(json_encode($exception->getMessage()) === false);
var_export(json_last_error_msg() === 'Malformed UTF-8 characters, possibly incorrectly encoded');
```

【After fix】:

Result (after fix):
```
Parse error on line 1:
{"DAMAGE":"圓圓圓圓圓圓圓"
------------------^
Expected one of: '}', ','
```

Not contains incorrect characters, and succeed in `json_encode($message)`:
```php
var_export(json_encode($exception->getMessage()) === '"Parse error on line 1:\\n{\\"DAMAGE\\":\\"\\u5713\\u5713\\u5713\\u5713\\u5713\\u5713\\u5713\\"\\n------------------^\\nExpected one of: \'}\', \',\'"');
var_export(json_last_error_msg() === 'No error');
```
RAW JSON Data (errorData.json):
```json
{"DAMAGE":"圓圓圓圓圓圓圓"
```

Test Code:
```php
$parser = new \Seld\JsonLint\JsonParser();
$exception = $parser->lint(file_get_contents('errorData.json'));
echo $exception->getMessage();
```


【Before fix】:

Result:
```
Parse error on line 1:
...�圓圓圓圓圓圓"
----------------------^
Expected one of: '}', ','
```

Contains incorrect characters, and fails to `json_encode($message)`:
```php
var_export(json_encode($exception->getMessage()) === false);
var_export(json_last_error_msg() === 'Malformed UTF-8 characters, possibly incorrectly encoded');
```

【After fix】:

Result (after fix):
```
Parse error on line 1:
{"DAMAGE":"圓圓圓圓圓圓圓"
------------------^
Expected one of: '}', ','
```

Not contains incorrect characters, and succeed in `json_encode($message)`:
```php
var_export(json_encode($exception->getMessage()) === '"Parse error on line 1:\\n{\\"DAMAGE\\":\\"\\u5713\\u5713\\u5713\\u5713\\u5713\\u5713\\u5713\\"\\n------------------^\\nExpected one of: \'}\', \',\'"');
var_export(json_last_error_msg() === 'No error');
```
@Seldaek
Copy link
Owner

Seldaek commented May 28, 2020

Thanks but this changes way too much code for what is needed IMO.

I think it should be enough to fix this in getPastInput, getUpcomingInput and getFullUpcomingInput as those are used to generate exceptions. The rest works fine as is and does not need changing.

And for those 3 functions, it is ok to use mb_* functions if they are present, but we need to check with function_exists first, otherwise call the regular str* functions. mbstring cannot be a requirement of this package.

@Seldaek Seldaek changed the base branch from master to main December 9, 2020 11:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants