o
    
i                     @   s  d dl mZmZmZmZmZmZ d dlZd dlZ	d dl
Z
ddlmZ ddlmZmZmZmZmZ 	d,dedee d	ed
efddZdeeef d
efddZded
efddZded
efddZd-deded
ee fddZd.dededed
dfddZd.dededed
dfddZ		d/dedee deded
df
d d!Zd-deded
ee fd"d#Z		d/dedee deded
df
d$d%Z d&ed
efd'd(Z!	d-d)ee ded
ee fd*d+Z"dS )0    )UnionIterableSequenceAnyOptionalIteratorN   )ujson)
force_pathforce_stringFilePath	JSONInput
JSONOutputFdataindent	sort_keysreturnc                 C   s>   |r|dkrdn|}t j| |d|d}|S tj| |dd}|S )a  Serialize an object to a JSON string.

    data: The JSON-serializable data.
    indent (int): Number of spaces used to indent JSON.
    sort_keys (bool): Sort dictionary keys. Falls back to json module for now.
    RETURNS (str): The serialized string.
    r   N),:)r   
separatorsr   F)r   escape_forward_slashes)_builtin_jsondumpsr	   )r   r   r   result r   C/home/ubuntu/.local/lib/python3.10/site-packages/srsly/_json_api.py
json_dumps
   s   
r   c                 C   s   | dkrt dt| S )zDeserialize unicode or bytes to a Python object.

    data (str / bytes): The data to deserialize.
    RETURNS: The deserialized Python object.
    -zExpected object or value)
ValueErrorr	   loads)r   r   r   r   
json_loads   s   
r    pathc                 C   s`   | dkrt j }t|S t| }|jddd}t|W  d   S 1 s)w   Y  dS )zLoad JSON from file or standard input.

    path (FilePath): The file path. "-" for reading from stdin.
    RETURNS (JSONOutput): The loaded JSON content.
    r   rutf8encodingN)sysstdinreadr	   r   r
   openload)r!   r   	file_pathfr   r   r   	read_json*   s   

$r-   c                 C   sB   t | }t|d}t|W  d   S 1 sw   Y  dS )zLoad JSON from a gzipped file.

    location (FilePath): The file path.
    RETURNS (JSONOutput): The loaded JSON content.
    r"   N)r   gzipr)   r	   r*   )r!   r+   r,   r   r   r   read_gzip_json8   s   $r/   skipc                 c   sP    t t| d}t||dD ]}|V  qW d   dS 1 s!w   Y  dS )a  Read a gzipped .jsonl file and yield contents line by line.
    Blank lines will always be skipped.

    path (FilePath): The file path.
    skip (bool): Skip broken lines and don't raise ValueError.
    YIELDS (JSONOutput): The unpacked, deserialized Python objects.
    r"   r0   N)r.   r)   r
   _yield_json_lines)r!   r0   r,   liner   r   r   read_gzip_jsonlC   s   "r4      c                 C   sj   t ||d}| dkrt| dS t| dd}|jddd}|| W d   dS 1 s.w   Y  dS )	a  Create a .json file and dump contents or write to standard
    output.

    location (FilePath): The file path. "-" for writing to stdout.
    data (JSONInput): The JSON-serializable data to output.
    indent (int): Number of spaces used to indent JSON.
    r   r   Frequire_existswr#   r$   N)r   printr
   r)   writer!   r   r   	json_datar+   r,   r   r   r   
write_jsonP   s   "r>   c                 C   sV   t ||d}t| }t|d}||d W d   dS 1 s$w   Y  dS )zCreate a .json.gz file and dump contents.

    path (FilePath): The file path.
    data (JSONInput): The JSON-serializable data to output.
    indent (int): Number of spaces used to indent JSON.
    r6   r9   utf-8N)r   r   r.   r)   r;   encoder<   r   r   r   write_gzip_jsona   s
   "rA   Tlinesappendappend_new_linec                 C   sx   |rdnd}t | dd}tj||d}|r |r |dd |dd	 |D  W d
   d
S 1 s5w   Y  d
S )a  Create a .jsonl.gz file and dump contents.

    location (FilePath): The file path.
    lines (Sequence[JSONInput]): The JSON-serializable contents of each line.
    append (bool): Whether or not to append to the location. Appending to .gz files is generally not recommended, as it
        doesn't allow the algorithm to take advantage of all data when compressing - files may hence be poorly
        compressed.
    append_new_line (bool): Whether or not to write a new line before appending
        to the file.
    ar9   Fr7   )mode
r?   c                 S   s   g | ]}t |d  dqS )rG   r?   )r   r@   ).0r3   r   r   r   
<listcomp>   s    z$write_gzip_jsonl.<locals>.<listcomp>N)r
   r.   r)   r;   r@   
writelines)r!   rB   rC   rD   rF   r+   r,   r   r   r   write_gzip_jsonln   s   "rK   c                 c   s|    | dkrt tj|dD ]}|V  qdS t| }|jddd}t ||dD ]}|V  q&W d   dS 1 s7w   Y  dS )a2  Read a .jsonl file or standard input and yield contents line by line.
    Blank lines will always be skipped.

    path (FilePath): The file path. "-" for reading from stdin.
    skip (bool): Skip broken lines and don't raise ValueError.
    YIELDS (JSONOutput): The loaded JSON contents of each line.
    r   r1   r"   r#   r$   N)r2   r&   r'   r
   r)   )r!   r0   r3   r+   r,   r   r   r   
read_jsonl   s   "rL   c                 C   s   | dkr|D ]}t t| qd	S |rdnd}t| dd}|j|dd }|r.|r.|d |D ]}|t|d  q0W d	   d	S 1 sGw   Y  d	S )
a|  Create a .jsonl file and dump contents or write to standard output.

    location (FilePath): The file path. "-" for writing to stdout.
    lines (Sequence[JSONInput]): The JSON-serializable contents of each line.
    append (bool): Whether or not to append to the location.
    append_new_line (bool): Whether or not to write a new line before appending
        to the file.
    r   rE   r9   Fr7   r?   r$   rG   N)r:   r   r
   r)   r;   )r!   rB   rC   rD   r3   rF   r+   r,   r   r   r   write_jsonl   s   
"rM   objc              	   C   s8   t | drdS zt|  W dS  ttfy   Y dS w )zCheck if a Python object is JSON-serializable.

    obj: The object to check.
    RETURNS (bool): Whether the object is JSON-serializable.
    __call__FT)hasattrr	   r   	TypeErrorOverflowError)rN   r   r   r   is_json_serializable   s   

rS   streamc              	   c   sj    d}| D ]-}|  }|dkrqzt|V  W n ty-   |r#Y qtd| d| w |d7 }qd S )Nr    zInvalid JSON on line z: )stripr	   r   r   )rT   r0   line_nor3   r   r   r   r2      s   
r2   )r   F)F)r5   )FT)#typingr   r   r   r   r   r   r&   jsonr   r.   rU   r	   utilr
   r   r   r   r   intboolstrr   bytesr    r-   r/   r4   r>   rA   rK   rL   rM   rS   r2   r   r   r   r   <module>   st     


