JsonDecode example

#[Package('core')] class JsonApiDecoder implements DecoderInterface
{
    final public const FORMAT = 'jsonapi';

    /** * @return array|mixed */
    public function decode(string $data, string $format, array $context = [])
    {
        $decodedData = (new JsonDecode([JsonDecode::ASSOCIATIVE => true]))->decode($data, 'json');

        if (!\is_array($decodedData) || !\array_key_exists('data', $decodedData)) {
            throw new UnexpectedValueException('Input not a valid JSON:API data object.');
        }

        $includes = [];
        if (\array_key_exists('included', $decodedData)) {
            $includes = $this->resolveIncludes($decodedData['included']);
        }

        if ($this->isCollection($decodedData['data'])) {
            
private array $classes = [];

    /** * {@inheritdoc} * * @return array<string, mixed> */
    public function normalize(mixed $object, ?string $format = null, array $context = [])
    {
        $encoder = new JsonEncode();

        return (new JsonDecode([JsonDecode::ASSOCIATIVE => true]))->decode($encoder->encode($object, 'json'), 'json');
    }

    /** * {@inheritdoc} * * @param array<string, mixed> $context */
    public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
    {
        return $data instanceof Struct;
    }

    
Home | Imprint | This part of the site doesn't use cookies.