
    i                        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
Zdeeef   d	efdZded	efdZded	efdZddeded	ee   fdZddededed	dfdZddededed	dfdZ	 	 d dedee   deded	df
dZddeded	ee   fdZ	 	 d dedee   deded	df
dZ ded	efdZ!	 ddee   ded	ee   fdZ"y)!    )UnionIterableSequenceAnyOptionalIteratorN   )ujson)
force_pathforce_stringFilePath	JSONInput
JSONOutputdataindent	sort_keysreturnc                     |r$|dk(  rdn|}t        j                  | |d|      }|S t        j                  | |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   results       `/var/www/vps2.regionflexible.com/Desarrollo/venv/lib/python3.12/site-packages/srsly/_json_api.py
json_dumpsr   
   sM     1&$$J)

 M T&OM    c                 L    | dk(  rt        d      t        j                  |       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   s    r   
json_loadsr#      s&     s{344;;tr   pathc                    | dk(  r3t         j                  j                         }t        j                  |      S t        |       }|j                  dd      5 }t        j                  |      cddd       S # 1 sw Y   yxY w)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fs       r   	read_jsonr1   *   sf     s{yy~~{{4  4 I	f	- zz!}  s   A66A?c                     t        |       }t        j                  |d      5 }t        j                  |      cddd       S # 1 sw Y   yxY w)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/   r0   s      r   read_gzip_jsonr4   8   s>     T"I	9c	" azz!}  s   AA
skipc              #      K   t        j                  t        |       d      5 }t        ||      D ]  }|  	 ddd       y# 1 sw Y   yxY ww)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&   r5   N)r3   r-   r   _yield_json_lines)r$   r5   r0   lines       r   read_gzip_jsonlr:   C   sL      
:d#S	) Q%ad3 	DJ	  s    AA	AAAc                     t        ||      }| dk(  rt        |       yt        | d      }|j                  dd      5 }|j	                  |       ddd       y# 1 sw Y   yxY w)	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/   r0   s         r   
write_jsonrD   P   s^     4/Is{itE:	^^C&^1 	QGGI	 	 	s   AA#c                     t        ||      }t        |       }t        j                  |d      5 }|j	                  |j                  d             ddd       y# 1 sw Y   yxY w)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.
    r<   r?   utf-8N)r   r   r3   r-   rA   encoderB   s         r   write_gzip_jsonrH   a   sV     4/IT"I	9c	" +a		  )*+ + +s   !AA"linesappendappend_new_linec           	      H   |rdnd}t        | d      }t        j                  ||      5 }|r"|r |j                  dj	                  d             |j                  |D cg c]  }t        |      dz   j	                  d      ! c}       ddd       yc c}w # 1 sw Y   yxY w)	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.
    ar?   Fr=   )mode
rF   N)r   r3   r-   rA   rG   
writelinesr   )r$   rI   rJ   rK   rN   r/   r0   r9   s           r   write_gzip_jsonlrQ   n   s      3cD46I	94	( TAoGGDKK()	ERDz$'$.66w?RST T ST Ts   4B$B
BBB!c              #      K   | dk(  r%t        t        j                  |      D ]  }|  yt        |       }|j	                  dd      5 }t        ||      D ]  }|  	 ddd       y# 1 sw Y   yxY ww)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    r7   r&   r'   r(   N)r8   r*   r+   r   r-   )r$   r5   r9   r/   r0   s        r   
read_jsonlrS      s{      s{%ciid; 	DJ	 t$	^^C&^1 	Q)!$7 
	 	 	s   A	A8A,#	A8,A51A8c                 0   | dk(  r|D ]  }t        t        |              y	|rdnd}t        | d      }|j                  |d      5 }|r|r|j	                  d       |D ]  }|j	                  t        |      dz          ! 	 d	d	d	       y	# 1 sw Y   y	xY w)
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    rM   r?   Fr=   rF   r(   rO   N)r@   r   r   r-   rA   )r$   rI   rJ   rK   r9   rN   r/   r0   s           r   write_jsonlrU      s     s{ 	$D*T"#	$ s#tE:	^^D7^3 	1q/ 1
4(4/01	1 	1 	1s   :BBobjc                 t    t        | d      ry	 t        j                  |        y# t        t        f$ r Y yw xY 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)rV   s    r   is_json_serializabler\      s<     sJC}% s   % 77streamc              #      K   d}| D ]5  }|j                         }|dk(  r	 t        j                  |       |dz  }7 y # t        $ r |rY Ft        d| d|       w xY ww)Nr	    zInvalid JSON on line z: )stripr
   r"   r!   )r]   r5   line_nor9   s       r   r8   r8      s      G 
zz|2:	H++d##
 	1
  	H4WIRvFGG	Hs'   A"A A" AA"AA")r   F)F)   )FT)#typingr   r   r   r   r   r   r*   jsonr   r3   r_   r
   utilr   r   r   r   r   intboolstrr   bytesr#   r1   r4   r:   rD   rH   rQ   rS   rU   r\   r8    r   r   <module>rk      s   E E 
    K K CH
%c];?(	U3:& 	: 	H   j 
( 
$ 
8J;O 
X Y  D "
+( 
+) 
+S 
+ 
+   	T
TIT T 	T
 
T0X T hz6J *  	1
1I1 1 	1
 
16c d " ).SM!%jr   