<?php
declare(strict_types=1); namespace App\Storage; use App\Interfaces\FileSystemInterface; use App\Exceptions\JsonParseException; class JsonStorage { private FileSystemInterface $fileSystem; public function __construct(FileSystemInterface $fileSystem) { $this->fileSystem = $fileSystem; } public function readJson(string $path): array { $content = $this->fileSystem->read($path); if (trim($content) === '') { return []; } $data = json_decode($content, true); if (json_last_error() !== JSON_ERROR_NONE) { throw new JsonParseException("Failed to parse JSON in file {$path}: " . json_last_error_msg()); } return is_array($data) ? $data : []; } public function writeJson(string $path, array $data): void { $jsonString = json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); if ($jsonString === false) { throw new JsonParseException("Failed to encode data to JSON: " . json_last_error_msg()); } $this->fileSystem->write($path, $jsonString); } } 