<?php

namespace Kai\Banner\Model\Serialize\Serializer;

use Magento\Framework\Serialize\Serializer\Json as SerializerJson;

class Json extends SerializerJson
{
    /**
     * @inheritDoc
     * @since 101.0.0
     */
    public function unserialize($string)
    {
        if ($string === null) {
            throw new \InvalidArgumentException(
                'Unable to unserialize value. Error: Parameter must be a string type, null given.'
            );
        }

        $result = json_decode($string, true);

        if (json_last_error() !== JSON_ERROR_NONE) {
            parse_str($string, $result);
            if (is_array($result)) {
                return $result;
            } else {
                throw new \InvalidArgumentException(
                    $string . " - Unable to unserialize value. Error: " . json_last_error_msg()
                );
            }
        }

        return $result;
    }
}
