{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "dd93cc43",
   "metadata": {},
   "source": [
    "# 15-25 · JSON: сохраняем структуры данных\n",
    "\n",
    "Практика к разделу [«JSON»](../../site/chapters/glava-15/15-25-json-serializatsiya.html)."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4dedf1e7",
   "metadata": {},
   "source": [
    "## Рабочий пример — dump/load"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "c805c3da",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-18T15:53:07.676267Z",
     "iopub.status.busy": "2026-08-18T15:53:07.676156Z",
     "iopub.status.idle": "2026-08-18T15:53:07.700498Z",
     "shell.execute_reply": "2026-08-18T15:53:07.700107Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'Anna', 'score': 1200}\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "\n",
    "data = {\"name\": \"Anna\", \"score\": 1200}\n",
    "with open(\"igrok2.json\", \"w\", encoding=\"utf-8\") as f:\n",
    "    json.dump(data, f, ensure_ascii=False, indent=2)\n",
    "\n",
    "with open(\"igrok2.json\", \"r\", encoding=\"utf-8\") as f:\n",
    "    loaded = json.load(f)\n",
    "print(loaded)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6b290229",
   "metadata": {},
   "source": [
    "## Задание ★★ Самостоятельная задача — мини-проект: менеджер настроек"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4d1ed082",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2026-08-18T15:53:07.701607Z",
     "iopub.status.busy": "2026-08-18T15:53:07.701471Z",
     "iopub.status.idle": "2026-08-18T15:53:07.706677Z",
     "shell.execute_reply": "2026-08-18T15:53:07.706072Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'theme': 'light', 'language': 'ru'}\n",
      "{'theme': 'dark', 'language': 'ru'}\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "from pathlib import Path\n",
    "\n",
    "DEFAULT_SETTINGS = {\"theme\": \"light\", \"language\": \"ru\"}\n",
    "\n",
    "def load_settings(path):\n",
    "    if not path.exists():\n",
    "        return dict(DEFAULT_SETTINGS)\n",
    "    with path.open(\"r\", encoding=\"utf-8\") as f:\n",
    "        return json.load(f)\n",
    "\n",
    "def save_settings(path, settings):\n",
    "    with path.open(\"w\", encoding=\"utf-8\") as f:\n",
    "        json.dump(settings, f, ensure_ascii=False, indent=2)\n",
    "\n",
    "settings_path = Path(\"nastroyki2.json\")\n",
    "settings_path.unlink(missing_ok=True)   # для повторяемости примера при повторном запуске\n",
    "settings_missing = load_settings(settings_path)\n",
    "save_settings(settings_path, {\"theme\": \"dark\", \"language\": \"ru\"})\n",
    "settings_loaded = load_settings(settings_path)\n",
    "print(settings_missing)\n",
    "print(settings_loaded)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Cartesian Python 3.14",
   "language": "python",
   "name": "cartesian-python314"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.14.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
