<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\Event\Worker\Monitoring;

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

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