E
E
EVOSandru62021-09-29 17:06:09
PHP
EVOSandru6, 2021-09-29 17:06:09

How to convert to readable json stdObject with nested objects?

Hello!

class UserAuthDto
{
    public function __construct(
        private UserDto $user,
        private ApiTokenDto $auth,
    )
    {
    }
}

dd(json_encode((array)$userAuth));


But for some reason in the resulting JSON there are such dumb keys:

"\u0000App\\Model\\User\\Domains\\UserAuthDto\u0000user"

"\u0000App\\Model\\User\\Domains\\UserAuthDto\u0000auth

" them in user and auth ?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
EVOSandru6, 2021-09-29
@EVOSandru6

public

R
Rsa97, 2021-09-29
@Rsa97

Not automatically. Implement the JsonSerializable interface in your class , whose jsonSerialize method will return the data to be serialized to JSON.

D
Dmitry Gordinskiy, 2021-09-29
@DmitriyGordinskiy

public readonly

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question