Skip to content

LL: add class InvalidEncodingException and flag VALIDATE_UTF8_ENCODING#91

Merged
Seldaek merged 18 commits into
Seldaek:mainfrom
LLyaudet:issue_52_check_UTF8_encoding
Jun 11, 2026
Merged

LL: add class InvalidEncodingException and flag VALIDATE_UTF8_ENCODING#91
Seldaek merged 18 commits into
Seldaek:mainfrom
LLyaudet:issue_52_check_UTF8_encoding

Conversation

@LLyaudet

Copy link
Copy Markdown
Contributor

to call validateUTF8Encoding() function.

@LLyaudet

Copy link
Copy Markdown
Contributor Author

See #52

@LLyaudet

LLyaudet commented Feb 13, 2026

Copy link
Copy Markdown
Contributor Author

This is a first draft.
Please let me know if the information given in the thrown exceptions is OK for you.
If you have any other remarks, I'll be happy to improve the code.
Currently, I think I could also add some testing,
but we need to be OK on the information given in the thrown exceptions first.

Comment thread src/Seld/JsonLint/JsonParser.php Outdated
Comment thread src/Seld/JsonLint/JsonParser.php Outdated
Comment thread src/Seld/JsonLint/InvalidEncodingException.php Outdated
@Seldaek

Seldaek commented Jun 4, 2026

Copy link
Copy Markdown
Owner

I think it looks ok (the exception message). Having some unit tests definitely would help tho.

@LLyaudet LLyaudet force-pushed the issue_52_check_UTF8_encoding branch from c359e25 to a9c2a52 Compare June 4, 2026 17:46
Comment thread src/Seld/JsonLint/JsonParser.php Outdated
Comment thread src/Seld/JsonLint/JsonParser.php
Comment thread src/Seld/JsonLint/JsonParser.php
@LLyaudet LLyaudet force-pushed the issue_52_check_UTF8_encoding branch 5 times, most recently from b66acf9 to 82ca084 Compare June 10, 2026 15:14
LLyaudet added 2 commits June 10, 2026 17:18
… repository, and re?correct bug when some continuation octet is still needed at end of string as suggested by Jordi Boggiano.
@LLyaudet LLyaudet force-pushed the issue_52_check_UTF8_encoding branch from 82ca084 to 4f3f03b Compare June 10, 2026 15:18
@LLyaudet LLyaudet force-pushed the issue_52_check_UTF8_encoding branch from 4f3f03b to 04efacd Compare June 10, 2026 15:34
LLyaudet added 2 commits June 11, 2026 10:24
…629 :

   The definition of UTF-8 prohibits encoding character numbers between
   U+D800 and U+DFFF, which are reserved for use with the UTF-16
   encoding form (as surrogate pairs) and do not directly represent
   characters.
@LLyaudet LLyaudet force-pushed the issue_52_check_UTF8_encoding branch from 9ade89f to 202ff29 Compare June 11, 2026 11:29
@Seldaek Seldaek merged commit 703485b into Seldaek:main Jun 11, 2026
16 checks passed
/**
* @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 :).

Comment thread src/Seld/JsonLint/InvalidEncodingException.php
Comment thread tests/JsonParserTest.php
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.

3 participants