How to throw an exception if a certain class object is serialized via json_encode?

Due to a refactoring I have the use case to throw a LogicException if, and only if, a certain class instance is serialized via json_encode. (Currently, a lot of serialization happens silently under the hood, generating broken/empty data structures, yet the code runs fine as no error is thrown. I prefer Exceptions to really catch all the places.)

E.g.:

class ValueObject
{
    public function __construct(public string $someProperty)
    {
    }
}
$an_object = new ValueObject('example');
$result = json_encode($an_object, JSON_PRETTY_PRINT); // this should throw as an instance of ValueObject is passed
echo $result;

This should also work if the instances appear nested, e.g. in an array but also as part of other object’s properties:

$nested_array = [
    [
        'foo',
        new ValueObject('foo'),
        new ValueObject('GNARF'),
    ],
    'gnarf' => new ValueObject('poit'),
];
$nested_result = json_encode($nested_array, JSON_PRETTY_PRINT); // should fail too
echo $nested_result;