<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Event\Dispatcher\Runtime;

final class DispatcherSnapshotSerializer
{
    public function encode(DispatcherRuntimeState $state): string
    {
        return json_encode($state->toArray(), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) ?: '{}';
    }

    public function decode(string $json): DispatcherRuntimeState
    {
        $data = json_decode($json, true);
        return DispatcherRuntimeState::fromArray(is_array($data) ? $data : []);
    }
}
