<?php
namespace Root\Core\Helper;

class Json
{
    public static function encode(mixed $data, bool $pretty = false): string
    {
        $options = JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES;
        if ($pretty) $options |= JSON_PRETTY_PRINT;
        return json_encode($data, $options);
    }

    public static function decode(string $json, bool $assoc = true): mixed
    {
        $data = json_decode($json, $assoc);
        if (json_last_error() !== JSON_ERROR_NONE) {
            throw new \RuntimeException('Invalid JSON: ' . json_last_error_msg());
        }
        return $data;
    }
}
