
    :Qg
                        d Z ddlmZ ddlZddlmZmZmZ ddlm	Z	 ddl
mZmZ ddlmZmZmZ ddlmZ dd	lmZ dd
lmZ  e        eej.                        e		 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 dd                     Zy)a  Provides `partition_json()`.

Note this does not partition arbitrary JSON. Its only use-case is to "rehydrate" unstructured
document elements serialized to JSON, essentially the same function as `elements_from_json()`, but
this allows a document of already-partitioned elements to be combined transparently with other
documents in a partitioning run. It also allows multiple (low-cost) chunking runs to be performed on
a document while only incurring partitioning cost once.
    )annotationsN)IOAnyOptional)add_chunking_strategy)Elementprocess_metadata)FileTypeadd_metadata_with_filetypeis_json_processable)exactly_one)get_last_modified_date)elements_from_dictsc                v   ||j                         dk(  r|s| sg S t        | ||       | rt        |       nd}d}| 't        | d      5 }|j	                         }ddd       nS|D|j	                         }t        |t              r|n|j                         }|j                  d       n|t        |      }t        |      st        d      	 t        j                  |      }	t        |	      }
|
D ]  }|xs ||j                  _         |
S # 1 sw Y   _xY w# t        j                  $ r t        d	      w xY w)
a  Partitions serialized Unstructured output into its constituent elements.

    Parameters
    ----------
    filename
        A string defining the target filename path.
    file
        A file-like object as bytes --> open(filename, "rb").
    text
        The string representation of the .json document.
    metadata_last_modified
        The last modified date for the document.
    N )filenamefiletextutf8)encodingr   )	file_textzJJSON cannot be partitioned. Schema does not match the Unstructured schema.zNot a valid json)stripr   r   openread
isinstancestrdecodeseekr   
ValueErrorjsonloadsr   JSONDecodeErrormetadatalast_modified)r   r   r   metadata_last_modifiedkwargsr$   r   ffile_contentelement_dictselementselements               X/var/www/html/answerous/venv/lib/python3.12/site-packages/unstructured/partition/json.pypartition_jsonr-      sA   . DJJLB.tH	488@*84dMI(V, 	!I	! 	! 
	yy{$.|S$AL|GZGZG\			!		I	3X
 	
-

9-&}5  Q)?)P=&Q O3	! 	!&  -+,,-s   
D D DD8)NNNN)r   Optional[str]r   zOptional[IO[bytes]]r   r.   r%   r.   r&   r   returnzlist[Element])__doc__
__future__r   r    typingr   r   r   unstructured.chunkingr   unstructured.documents.elementsr   r	    unstructured.file_utils.filetyper
   r   r   $unstructured.partition.common.commonr   &unstructured.partition.common.metadatar   unstructured.staging.baser   JSONr-        r,   <module>r<      s    #  $ $ 7 E 
 = I 9 HMM*" $,0	55
5 5 *	5
 5 5  + 5r;   