<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Event\Streaming\Topic;

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

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