
    h                        S SK JrJrJrJrJrJr  S SKrS SKr	S SK
r
SSKJr  SSKJrJrJrJrJr   SS\S\\   S\S	\4S
 jjrS\\\4   S	\4S jrS\S	\4S jrS\S	\4S jrSS\S\S	\\   4S jjrSS\S\S\S	S4S jjrSS\S\S\S	S4S jjr  S S\S\\   S\S\S	S4
S jjrSS\S\S	\\   4S jjr  S S\S\\   S\S\S	S4
S jjr S\S	\4S jr! SS\\   S\S	\\   4S jjr"g)!    )UnionIterableSequenceAnyOptionalIteratorN   )ujson)
force_pathforce_stringFilePath	JSONInput
JSONOutputdataindent	sort_keysreturnc                     U(       a"  US:X  a  SOUn[         R                  " XSUS9nU$ [        R                  " XSS9nU$ )zSerialize 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       I/home/james-whalen/.local/lib/python3.13/site-packages/srsly/_json_api.py
json_dumpsr   
   sI     1&$$J)

 M TOM    c                 P    U S:X  a  [        S5      e[        R                  " U 5      $ )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                    U S:X  a4  [         R                  R                  5       n[        R                  " U5      $ [        U 5      nUR                  SSS9 n[        R                  " U5      sSSS5        $ ! , (       d  f       g= f)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   *   s]     s{yy~~{{4  4 I	f	-zz!} 
.	-	-s   A66
Bc                     [        U 5      n[        R                  " US5       n[        R                  " U5      sSSS5        $ ! , (       d  f       g= f)zsLoad 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   s5     T"I	9c	"azz!} 
#	"	"s   A
Askipc              #      #    [         R                  " [        U 5      S5       n[        X!S9 H  nUv   M	     SSS5        g! , (       d  f       g= f7f)zRead 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   s;      
:d#S	)Q%a3DJ 4 
*	)	)s   !AA	A
AAc                     [        XS9nU S:X  a  [        U5        g[        U SS9nUR                  SSS9 nUR	                  U5        SSS5        g! , (       d  f       g= f)	zCreate 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   sS     4/Is{itE:	^^C&^1QGGI 211s   A
Ac                     [        XS9n[        U 5      n[        R                  " US5       nUR	                  UR                  S5      5        SSS5        g! , (       d  f       g= f)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   sK     4/IT"I	9c	"a		  )* 
#	"	"s   !A
A$linesappendappend_new_linec           	      j   U(       a  SOSn[        U SS9n[        R                  " XTS9 nU(       a'  U(       a   UR                  SR	                  S5      5        UR                  U Vs/ s H   n[        U5      S-   R	                  S5      PM"     sn5        SSS5        gs  snf ! , (       d  f       g= f)	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	9	(AoGGDKK()	EREDz$'$.66w?ERS 
)	( S 
)	(s   >B$('B
B$B$$
B2c              #      #    U S:X  a$  [        [        R                  US9 H  nUv   M	     g[        U 5      nUR	                  SSS9 n[        XAS9 H  nUv   M	     SSS5        g! , (       d  f       g= f7f)a  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      se      s{%ciid;DJ < t$	^^C&^1Q)!7
 8 211s   AA7A&	A7&
A40A7c                 T   U S:X  a  U H  n[        [        U5      5        M     g	U(       a  SOSn[        U SS9nUR                  USS9 nU(       a  U(       a  UR	                  S5        U H   nUR	                  [        U5      S-   5        M"     S	S	S	5        g	! , (       d  f       g	= f)
ad  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^3q/
4(4/0  433s   
AB
B'objc                     [        U S5      (       a  g [        R                  " U 5        g! [        [        4 a     gf = f)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   + >>streamc              #      #    SnU  H9  nUR                  5       nUS:X  a  M   [        R                  " U5      v   US-  nM;     g ! [         a    U(       a   MR  [        SU SU 35      ef = f7f)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   