from pathlib import Path
import json
from typing import OrderedDict


def read_json(filename):
    """
    读取json

    :param filename: 文件路径
    :return: json.loads()
    """

    # Path()，PurePath subclass that can make system calls.
    filename = Path(filename)

    """
    ========= ===============================================================
    Character Meaning
    --------- ---------------------------------------------------------------
    'r'       open for reading (default)
    'w'       open for writing, truncating the file first
    'x'       create a new file and open it for writing
    'a'       open for writing, appending to the end of the file if it exists
    'b'       binary mode
    't'       text mode (default)
    '+'       open a disk file for updating (reading and writing)
    'U'       universal newline mode (deprecated)
    ========= ===============================================================
    """

    # rt模式下，r读取文本时，t会自动把\r\n转换成\n.（文本模式）
    with filename.open('rt') as handle:
        # 返回一个json.loads()，OrderedDict类型的，有序字典？
        # object_hook=，用object_hook这种类对象返回
        """
        json.load()，加载python json格式文件
        json.loads()，解码python json格式
        """
        return json.load(handle, object_hook=OrderedDict)