Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
b668843
LL: add class InvalidEncodingException and flag VALIDATE_UTF8_ENCODIN…
LLyaudet Feb 13, 2026
dd6e82e
JsonParser.php correction Octetss -> Octets
LLyaudet Mar 18, 2026
a9c2a52
Keep same copyright as other files, and add author tag in PHPDoc-stri…
LLyaudet Jun 4, 2026
b62f4a0
add corrections to warnings detected by phpstan.
LLyaudet Jun 6, 2026
dce19ad
Merge branch 'main' into issue_52_check_UTF8_encoding
LLyaudet Jun 10, 2026
4b27710
activate long option --validate-utf8-encodin in bin/jsonlint (remove …
LLyaudet Jun 10, 2026
485836e
move UTF-8 validation into parse.
LLyaudet Jun 10, 2026
086fb2f
improve data for exceptions in validateUTF8Encoding similar to my own…
LLyaudet Jun 10, 2026
f69273e
add tests in JsonParserTest.php with files nonvalidutf8.json and val…
LLyaudet Jun 10, 2026
07b84fc
add fast-path for mainly valid-UTF8 files using preg_match.
LLyaudet Jun 10, 2026
fbadc85
do not allow octets C0, C1, F5, FF in UTF8 validation.
LLyaudet Jun 10, 2026
4d6fd5c
UTF8 validation must forbid range D800 to DFFF
LLyaudet Jun 10, 2026
04efacd
It should be the last non-UTF-8 correction, F5 to F7 are too high val…
LLyaudet Jun 10, 2026
d2a194b
add further tests to check all InvalidEncodingException messages.
LLyaudet Jun 11, 2026
7170180
disable fast path for UTF8-validation since it doesn't check for RFC3…
LLyaudet Jun 11, 2026
38be61d
add PHP issue link explaining why it is buggy.
LLyaudet Jun 11, 2026
728b4eb
correction of my bug on surrogate pairs that were checked in another …
LLyaudet Jun 11, 2026
202ff29
deal with bugs in Unicode support of PHP before 5.4.
LLyaudet Jun 11, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bin/jsonlint
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function getFlags(
$flags = 0;
if($allowComments){ $flags |= JsonParser::ALLOW_COMMENTS; }
if($detectKeyConflicts){ $flags |= JsonParser::DETECT_KEY_CONFLICTS; }
// TODO if($validateUTF8Encoding){ $flags |= JsonParser::VALIDATE_UTF8_ENCODING; }
if($validateUTF8Encoding){ $flags |= JsonParser::VALIDATE_UTF8_ENCODING; }
return $flags;
}

Expand Down Expand Up @@ -142,6 +142,6 @@ function showUsage($programPath)
echo ' -h, --help Show this message'.PHP_EOL;
echo ' --allow-comments Cause jsonlint to enable syntax for comments similar to JSON5.'.PHP_EOL;
echo ' --detect-key-conflicts Cause jsonlint to detect key conflicts in objects.'.PHP_EOL;
// TODO echo ' --validate-utf8-encoding Cause jsonlint to validate that the whole file is valid UTF-8 encoding.'.PHP_EOL;
echo ' --validate-utf8-encoding Cause jsonlint to validate that the whole file is valid UTF-8 encoding.'.PHP_EOL;
exit(0);
}
50 changes: 50 additions & 0 deletions src/Seld/JsonLint/InvalidEncodingException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/*
* This file is part of the JSON Lint package.
*
* (c) Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Seld\JsonLint;

/**
* @author Laurent Lyaudet <laurent.lyaudet@gmail.com>
*/
class InvalidEncodingException extends ParsingException
{
/**
* @var array{key: string, line: int}
*/
protected $details;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

should be private IMO

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yep, probably no further specialization of this exception will be done in the code.
Since the PR is already merged, I will prepare another PR to address your remarks that are not already dealed with.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

even if specialization is done, the child class can use the getter if it needs to read it.

Btw, I'm not even sure whether those details are useful. I fail to see what they bring compared to the message.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

even if specialization is done, the child class can use the getter if it needs to read it.

Ok, but you can't overwrite the PHPDoc in that case, if you want distinct keys in the details array after specialization.

Btw, I'm not even sure whether those details are useful. I fail to see what they bring compared to the message.

Noob ;). You always need the strongly typed data instead of a hotchpotch string if you want to do further error handling of the exception and some automatized recovery after failure instead of just printing the message of the exception :).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ok, but you can't overwrite the PHPDoc in that case, if you want distinct keys in the details array after specialization.

Changing the type of a property in a child class does not respect variance rules anyway.
And I don't see the use case anyway for specializing the class: the library will not use your special child class as it won't know about it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Then you need to talk with @Seldaek about variance rules, because currently it already overrides ParsingException:

class ParsingException extends \Exception
{
    /**
     * @var array{text?: string, token?: string|int, line?: int, loc?: array{first_line: int, first_column: int, last_line: int, last_column: int}, expected?: string[]}
     */
    protected $details;

I assumed array type hint was enough.
Feel free to propose a PR for "the details" ;).
I will mess up with the tests in the meantime :).


/**
* @param string $message
* @param string $key
* @phpstan-param array{line: int} $details
Comment thread
LLyaudet marked this conversation as resolved.
*/
public function __construct($message, $key, array $details)
{
$details['key'] = $key;
parent::__construct($message, $details);
}

/**
* @return string
*/
public function getKey()
{
return $this->details['key'];
}

/**
* @phpstan-return array{key: string, line: int}
*/
public function getDetails()
{
return $this->details;
}
}
Loading