U C^n@sddlmZddlZddlZddlZddlmZddlm Z m Z dddZ d d Z d d Z d dZdddZdddZd ddZd!ddZddZd"ddZdS)#)unicode_literalsN)ujson) force_path force_stringFcCsT|r(|dkrdn|}tj||d|d}ntj||dd}tjddkrP|dS|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_slashesutf8) _builtin_jsondumpsrsys version_infodecode)datar r resultr2/tmp/pip-install-6_kvzl1k/srsly/srsly/_json_api.py json_dumps s rcCs t|S)zDeserialize unicode or bytes to a Python object. data (unicode / bytes): The data to deserialize. RETURNS: The deserialized Python object. )rloads)rrrr json_loads src CsT|dkrtj}t|St|}|jddd}t|W5QRSQRXdS)zLoad 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)rstdinreadrrropenload)locationr file_pathfrrr read_json)s   r%c Cs6t|}t|d}t|W5QRSQRXdS)zLoad JSON from a gzipped file. location (unicode / Path): The file path. RETURNS (dict / list): The loaded JSON content. rN)rgzipr rr!)r"r#r$rrrread_gzip_json7sr'r c CsRt||d}|dkrt|n0t|dd}|jddd}||W5QRXdS) zCreate 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 rFZrequire_existswr rN)rprintrr writer"rr Z json_datar#r$rrr write_jsonBs    r.c Cs@t||d}t|}t|d}||dW5QRXdS)zCreate 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-rrrwrite_gzip_jsonSs r1c csb|dkr$ttj|dD] }|Vqn:t|}|jddd}t||dD] }|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"r2liner#r$rrr read_jsonl`s r5Tc Cs|dkr |D]}tt|q n`|r(dnd}t|dd}|j|dd4}|rZ|rZ|d|D]}|t|dq^W5QRXd S) aCreate 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. rar*Fr)r/r N)r+rrr r,)r"linesappendZappend_new_liner4moder#r$rrr write_jsonlrs    r;c Cs>t|drdSzt|WdSttfk r8YdSXdS)zCheck 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)objrrris_json_serializables  rAc csjd}|D]\}|}|dkrqzt|VWn,tk rZ|rFYqtd||YnX|d7}qdS)NrzInvalid JSON on line {}: {})striprr ValueErrorformat)streamr2Zline_nor4rrrr3sr3)rF)r )r )F)FT)F) __future__rrjsonrr&rBrutilrrrrr%r'r.r1r5r;rAr3rrrrs