
    h#                    6   S SK Jr  S SKrS SKJrJrJr  S SKJrJ	r	  S SK
Jr  S SKJr  S SKJr  \(       a  S SKJr  S S	KJr            SS
 jr              SS jr          SS jr\" 5       SSSS\SS.               SS jj5       rg)    )annotationsN)IterableMappingSequence)TYPE_CHECKINGAny)unstable)	DataFrame)N_INFER_DEFAULT)JSONEncoder)Schemac           
         US:  aZ  0 n[        U [        5      (       a  [        U UUUS9nU$ [        U [        5      (       a  U  Vs/ s H  n[	        UUUUS9PM     nnU$ U$ U $ s  snf )Nr   )data	separator	max_levelencoderr   r   r   )
isinstancedict_normalize_json_orderedlist_simple_json_normalize)r   r   r   r   normalized_json_objectrownormalized_json_lists          R/home/james-whalen/.local/lib/python3.13/site-packages/polars/convert/normalize.pyr   r      s     1}!#dD!!%<##	&"" &% d##  $  C '''#	   ! $ ('%%$s   A#c           
         [        U [        5      (       a^  US:  aL  U(       a  U U 3OSnUS-
  nU R                  5        H"  u  pU(       a  U U 3OUn
[        U	U
UUUUS9  M$     U$ U" U 5      X!'   U$ XU'   U$ )aT  
Main recursive function.

Designed for the most basic use case of `pl.json_normalize(data)`,
intended as a performance improvement.

Parameters
----------
data : Any
    Type dependent on types contained within nested Json
key_string : str
    New key (with separator(s) in) for data
normalized_dict : dict
    The new normalized/flattened Json dict
separator : str, default '.'
    Nested records will generate names separated by sep,
    e.g., for sep='.', { 'foo' : { 'bar' : 0 } } -> foo.bar
max_level
    recursion depth
encoder
    Custom JSON encoder; if not given, `json.dumps` is used.
r       r   
key_stringnormalized_dictr   r   r   )r   r   items_normalize_json)r   r!   r"   r   r   r   key_rootnested_max_levelkeyvaluenew_keys              r   r$   r$   2   s    < $q=5?*i[1RH(1}"jjl
08XJse,c&$3'.# + 	 +2$-O'""&*
#    c           	         0 0 pTU R                  5        H$  u  pg[        U[        5      (       a  XuU'   M   XtU'   M&     [        US0 UUUS9n0 UEUE$ )a  
Order the top level keys and then recursively go to depth.

Parameters
----------
data
    Deserialized JSON objects (dict or list of dicts)
separator
    Nested records will generate names separated by sep. e.g.,
    for `separator=".", {"foo": {"bar": 0}}` -> foo.bar.
max_level
    Max number of levels(depth of dict) to normalize.
encoder
    Custom JSON encoder; if not given, `json.dumps` is used.

Returns
-------
dict or list of dicts, matching `normalized_json_object`
r   r    )r#   r   r   r$   )	r   r   r   r   top_nested_datakvnested_s	            r   r   r   g   sh    2 B+

aNG	  G dgr*   .T)r   r   schemastrictinfer_schema_lengthr   c          	     z   Uc  SnUS-  n[        U [        5      (       a  [        U 5      S:X  a	  [        US9$ [        U [        5      (       a  U /n OC[        U [
        5      (       a!  [        U [        5      (       d  [        U 5      n OSn[        U5      eUc  [        R                  n[        [        U UUUS9UUUS9$ )u  
Normalize semi-structured deserialized JSON data into a flat table.

Dictionary objects that will not be unnested/normalized are encoded
as json string data. Unlike it pandas' counterpart, this function will
not encode dictionaries as objects at any level.

.. warning::
    This functionality is considered **unstable**. It may be changed
    at any point without it being considered a breaking change.

Parameters
----------
data
    Deserialized JSON objects.
separator
    Nested records will generate names separated by sep. e.g.,
    for `separator=".", {"foo": {"bar": 0}}` -> foo.bar.
max_level
    Max number of levels(depth of dict) to normalize.
    If None, normalizes all levels.
schema
    Overwrite the `Schema` when the normalized data is passed to
    the `DataFrame` constructor.
strict
    Whether Polars should be strict when constructing the DataFrame.
infer_schema_length
    Number of rows to take into consideration to determine the schema.
encoder
    Custom JSON encoder function; if not given, `json.dumps` is used.

Examples
--------
>>> data = [
...     {
...         "id": 1,
...         "name": "Cole Volk",
...         "fitness": {"height": 180, "weight": 85},
...     },
...     {
...         "id": 2,
...         "name": "Faye Raker",
...         "fitness": {"height": 155, "weight": 58},
...     },
...     {
...         "name": "Mark Reg",
...         "fitness": {"height": 170, "weight": 78},
...     },
... ]
>>> pl.json_normalize(data, max_level=1)
shape: (3, 4)
┌──────┬────────────┬────────────────┬────────────────┐
│ id   ┆ name       ┆ fitness.height ┆ fitness.weight │
│ ---  ┆ ---        ┆ ---            ┆ ---            │
│ i64  ┆ str        ┆ i64            ┆ i64            │
╞══════╪════════════╪════════════════╪════════════════╡
│ 1    ┆ Cole Volk  ┆ 180            ┆ 85             │
│ 2    ┆ Faye Raker ┆ 155            ┆ 58             │
│ null ┆ Mark Reg   ┆ 170            ┆ 78             │
└──────┴────────────┴────────────────┴────────────────┘

Normalize to a specific depth, using a custom JSON encoder
(note that `orson.dumps` encodes to bytes, not str).

>>> import orjson
>>> pl.json_normalize(data, max_level=0, encoder=orjson.dumps)
shape: (3, 3)
┌──────┬────────────┬───────────────────────────────┐
│ id   ┆ name       ┆ fitness                       │
│ ---  ┆ ---        ┆ ---                           │
│ i64  ┆ str        ┆ binary                        │
╞══════╪════════════╪═══════════════════════════════╡
│ 1    ┆ Cole Volk  ┆ b"{"height":180,"weight":85}" │
│ 2    ┆ Faye Raker ┆ b"{"height":155,"weight":58}" │
│ null ┆ Mark Reg   ┆ b"{"height":170,"weight":78}" │
└──────┴────────────┴───────────────────────────────┘
l        r   r   )r2   z expected list or dict of objectsr   )r2   r3   r4   )r   r   lenr
   r   r   strr   
ValueErrorjsondumpsr   )r   r   r   r2   r3   r4   r   msgs           r   json_normalizer<      s    p 	NI$!!c$i1n''	D'	"	"v	D(	#	#JtS,A,ADz0o**		
 /
 
r*   )
r   /dict[Any, Any] | Sequence[dict[Any, Any] | Any]r   r7   r   intr   r   returnz+dict[Any, Any] | list[dict[Any, Any]] | Any)r   r   r!   r7   r"   dict[str, Any]r   r7   r   r>   r   r   r?   r@   )
r   r@   r   r7   r   r>   r   r   r?   r@   )r   r=   r   r7   r   
int | Noner2   zSchema | Noner3   boolr4   rA   r   zJSONEncoder | Noner?   r
   )
__future__r   r9   collections.abcr   r   r   typingr   r   polars._utils.unstabler	   polars.dataframer
   polars.datatypes.constantsr   polars._typingr   polars.schemar   r   r$   r   r<    r*   r   <module>rL      sZ   #  7 7 % + & 6*$
9  	
 1@2
22 $2 	2
 2 2 2j(
(( ( 	(
 (V 
   &5"&r
9r r 	r
 r r $r  r r rr*   