U ã€C^nã@s ddlmZddlZddlZddlZddlmZddlm Z m Z ddd„Z d d „Z d d „Z d d„Zddd„Zddd„Zd dd„Zd!dd„Zdd„Zd"dd„ZdS)#é)Úunicode_literalsNé)Úujson)Ú force_pathÚ force_stringFcCsT|r(|dkrdn|}tj||d|d�}ntj||dd�}tjddkrP| d¡S|S) aSerialize 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 (unicode): The serialized string. rN)ú,ú:)ÚindentÚ separatorsÚ sort_keysF)r Zescape_forward_slasheséÚutf8)Ú _builtin_jsonÚdumpsrÚsysÚ version_infoÚdecode)Údatar r Úresult©rú2/tmp/pip-install-6_kvzl1k/srsly/srsly/_json_api.pyÚ json_dumps sÿ rcCs t |¡S)z˜Deserialize unicode or bytes to a Python object. data (unicode / bytes): The data to deserialize. RETURNS: The deserialized Python object. )rÚloads)rrrrÚ json_loads src CsT|dkrtj ¡}t |¡St|ƒ}|jddd��}t |¡W5QR£SQRXdS)zªLoad JSON from file or standard input. location (unicode / Path): The file path. "-" for reading from stdin. RETURNS (dict / list): The loaded JSON content. ú-Úrr ©ÚencodingN)rÚstdinÚreadrrrÚopenÚload)ÚlocationrÚ file_pathÚfrrrÚ read_json)s   r%c Cs6t|ƒ}t |d¡�}t |¡W5QR£SQRXdS)zŽLoad JSON from a gzipped file. location (unicode / Path): The file path. RETURNS (dict / list): The loaded JSON content. rN)rÚgzipr rr!)r"r#r$rrrÚread_gzip_json7sr'r c CsRt||d�}|dkrt|ƒn0t|dd�}|jddd��}| |¡W5QRXdS) zýCreate a .json file and dump contents or write to standard output. location (unicode / Path): The file path. "-" for writing to stdout. data: The JSON-serializable data to output. indent (int): Number of spaces used to indent JSON. ©r rF©Zrequire_existsÚwr rN)rÚprintrr Úwrite©r"rr Z json_datar#r$rrrÚ write_jsonBs    r.c Cs@t||d�}t|ƒ}t |d¡�}| | d¡¡W5QRXdS)zÅCreate a .json.gz file and dump contents. location (unicode / Path): The file path. data: The JSON-serializable data to output. indent (int): Number of spaces used to indent JSON. r(r*úutf-8N)rrr&r r,Úencoder-rrrÚwrite_gzip_jsonSs r1c csb|dkr$ttj|d�D] }|Vqn:t|ƒ}|jddd��}t||d�D] }|VqHW5QRXdS)a/Read a .jsonl file or standard input and yield contents line by line. Blank lines will always be skipped. location (unicode / Path): The file path. "-" for reading from stdin. skip (bool): Skip broken lines and don't raise ValueError. YIELDS: The loaded JSON contents of each line. r)Úskiprr rN)Ú_yield_json_linesrrrr )r"r2Úliner#r$rrrÚ read_jsonl`s r5Tc Cs„|dkr |D]}tt|ƒƒq n`|r(dnd}t|dd�}|j|dd��4}|rZ|rZ| d¡|D]}| t|ƒd¡q^W5QRXd S) a‡Create a .jsonl file and dump contents or write to standard output. location (unicode / Path): The file path. "-" for writing to stdout. lines (list): 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Úar*Fr)r/rÚ N)r+rrr r,)r"ÚlinesÚappendZappend_new_liner4Úmoder#r$rrrÚ write_jsonlrs    r;c Cs>t|dƒrdSzt |¡WdSttfk r8YdSXdS)z�Check if a Python object is JSON-serializable. obj: The object to check. RETURNS (bool): Whether the object is JSON-serializable. Ú__call__FTN)ÚhasattrrrÚ TypeErrorÚ OverflowError)ÚobjrrrÚis_json_serializableˆs  rAc csjd}|D]\}| ¡}|dkrqzt |¡VWn,tk rZ|rFYqtd ||¡ƒ‚YnX|d7}qdS)NrÚzInvalid JSON on line {}: {})ÚstriprrÚ ValueErrorÚformat)Ústreamr2Zline_nor4rrrr3˜sr3)rF)r )r )F)FT)F)Ú __future__rrÚjsonrr&rBrÚutilrrrrr%r'r.r1r5r;rAr3rrrrÚs