
    h                     D   d Z ddlZddlZddlZddlZddlZddlZddlmZ ddl	Z	ddl	m
Z
mZmZ ddlmZmZmZmZ ddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddl m!Z! ddl"m#Z#m$Z$m%Z%m&Z&m'Z' ddl(m)Z) ddl*m+Z+ ddl,m-Z- ej\                  ej^                  ej`                  ejb                  ejd                  ejf                  dZ4 e5g d      Z6 G d d      Z7 G d d      Z8 G d de      Z9 e9e8jt                        Z;	 e;Z<	  e9e8jz                        Z>	  e9e8j~                        Z@	  e9de7j                  d      ZB	 d ZCd ZDe<fdZEe<fd ZFe<fd!ZGd" ZHd# ZId$ ZJd% ZKd& ZLd' ZMd( ZNd) ZOd* ZPd+ ZQd, ZRd- ZSd. ZTd/ ZUd0 ZVd1 ZWd2 ZXd3 ZYd4 ZZe<fd5Z[y)6a  Tools for using Python's :mod:`json` module with BSON documents.

This module provides two helper methods `dumps` and `loads` that wrap the
native :mod:`json` methods and provide explicit BSON conversion to and from
JSON. :class:`~bson.json_util.JSONOptions` provides a way to control how JSON
is emitted and parsed, with the default being the legacy PyMongo format.
:mod:`~bson.json_util` can also generate Canonical or Relaxed `Extended JSON`_
when :const:`CANONICAL_JSON_OPTIONS` or :const:`RELAXED_JSON_OPTIONS` is
provided, respectively.

.. _Extended JSON: https://github.com/mongodb/specifications/blob/master/source/extended-json.rst

Example usage (deserialization):

.. doctest::

   >>> from bson.json_util import loads
   >>> loads('[{"foo": [1, 2]}, {"bar": {"hello": "world"}}, {"code": {"$scope": {}, "$code": "function x() { return 1; }"}}, {"bin": {"$type": "80", "$binary": "AQIDBA=="}}]')
   [{u'foo': [1, 2]}, {u'bar': {u'hello': u'world'}}, {u'code': Code('function x() { return 1; }', {})}, {u'bin': Binary('...', 128)}]

Example usage (serialization):

.. doctest::

   >>> from bson import Binary, Code
   >>> from bson.json_util import dumps
   >>> dumps([{'foo': [1, 2]},
   ...        {'bar': {'hello': 'world'}},
   ...        {'code': Code("function x() { return 1; }", {})},
   ...        {'bin': Binary(b"")}])
   '[{"foo": [1, 2]}, {"bar": {"hello": "world"}}, {"code": {"$code": "function x() { return 1; }", "$scope": {}}}, {"bin": {"$binary": "AQIDBA==", "$type": "00"}}]'

Example usage (with :const:`CANONICAL_JSON_OPTIONS`):

.. doctest::

   >>> from bson import Binary, Code
   >>> from bson.json_util import dumps, CANONICAL_JSON_OPTIONS
   >>> dumps([{'foo': [1, 2]},
   ...        {'bar': {'hello': 'world'}},
   ...        {'code': Code("function x() { return 1; }")},
   ...        {'bin': Binary(b"")}],
   ...       json_options=CANONICAL_JSON_OPTIONS)
   '[{"foo": [{"$numberInt": "1"}, {"$numberInt": "2"}]}, {"bar": {"hello": "world"}}, {"code": {"$code": "function x() { return 1; }"}}, {"bin": {"$binary": {"base64": "AQIDBA==", "subType": "00"}}}]'

Example usage (with :const:`RELAXED_JSON_OPTIONS`):

.. doctest::

   >>> from bson import Binary, Code
   >>> from bson.json_util import dumps, RELAXED_JSON_OPTIONS
   >>> dumps([{'foo': [1, 2]},
   ...        {'bar': {'hello': 'world'}},
   ...        {'code': Code("function x() { return 1; }")},
   ...        {'bin': Binary(b"")}],
   ...       json_options=RELAXED_JSON_OPTIONS)
   '[{"foo": [1, 2]}, {"bar": {"hello": "world"}}, {"code": {"$code": "function x() { return 1; }"}}, {"bin": {"$binary": {"base64": "AQIDBA==", "subType": "00"}}}]'

Alternatively, you can manually pass the `default` to :func:`json.dumps`.
It won't handle :class:`~bson.binary.Binary` and :class:`~bson.code.Code`
instances (as they are extended strings you can't provide custom defaults),
but it will be faster as there is less recursion.

.. note::
   If your application does not need the flexibility offered by
   :class:`JSONOptions` and spends a large amount of time in the `json_util`
   module, look to
   `python-bsonjs <https://pypi.python.org/pypi/python-bsonjs>`_ for a nice
   performance improvement. `python-bsonjs` is a fast BSON to MongoDB
   Extended JSON converter for Python built on top of
   `libbson <https://github.com/mongodb/libbson>`_. `python-bsonjs` works best
   with PyMongo when using :class:`~bson.raw_bson.RawBSONDocument`.

.. versionchanged:: 2.8
   The output format for :class:`~bson.timestamp.Timestamp` has changed from
   '{"t": <int>, "i": <int>}' to '{"$timestamp": {"t": <int>, "i": <int>}}'.
   This new format will be decoded to an instance of
   :class:`~bson.timestamp.Timestamp`. The old format will continue to be
   decoded to a python dict as before. Encoding to the old format is no longer
   supported as it was never correct and loses type information.
   Added support for $numberLong and $undefined - new in MongoDB 2.6 - and
   parsing $date in ISO-8601 format.

.. versionchanged:: 2.7
   Preserves order when rendering SON, Timestamp, Code, Binary, and DBRef
   instances.

.. versionchanged:: 2.3
   Added dumps and loads helpers to automatically handle conversion to and
   from json and supports :class:`~bson.binary.Binary` and
   :class:`~bson.code.Code`
    N)ConfigurationError)EPOCH_AWARERE_TYPESON)BinaryUuidRepresentationALL_UUID_SUBTYPESUUID_SUBTYPE)Code)CodecOptions)DBRef)
Decimal128)Int64)MaxKey)MinKey)ObjectId)PY3	iteritemsinteger_typesstring_type	text_type)Regex)	Timestamp)utc)ilmsux)$id$ref$dbc                       e Zd ZdZ	 dZ	 dZy)DatetimeRepresentationr         N)__name__
__module____qualname__LEGACY
NUMBERLONGISO8601     Q/var/www/html/ranktracker/api/venv/lib/python3.12/site-packages/bson/json_util.pyr%   r%      s$    F J G	r/   r%   c                       e Zd ZdZ	 dZ	 dZy)JSONModer   r&   r'   N)r(   r)   r*   r+   RELAXED	CANONICALr.   r/   r0   r2   r2      s$    F G Ir/   r2   c                   n     e Zd ZdZdej
                  dej
                  f fd	Z fdZ fdZ	d Z
 xZS )JSONOptionsaR  Encapsulates JSON options for :func:`dumps` and :func:`loads`.

    :Parameters:
      - `strict_number_long`: If ``True``, :class:`~bson.int64.Int64` objects
        are encoded to MongoDB Extended JSON's *Strict mode* type
        `NumberLong`, ie ``'{"$numberLong": "<number>" }'``. Otherwise they
        will be encoded as an `int`. Defaults to ``False``.
      - `datetime_representation`: The representation to use when encoding
        instances of :class:`datetime.datetime`. Defaults to
        :const:`~DatetimeRepresentation.LEGACY`.
      - `strict_uuid`: If ``True``, :class:`uuid.UUID` object are encoded to
        MongoDB Extended JSON's *Strict mode* type `Binary`. Otherwise it
        will be encoded as ``'{"$uuid": "<hex>" }'``. Defaults to ``False``.
      - `json_mode`: The :class:`JSONMode` to use when encoding BSON types to
        Extended JSON. Defaults to :const:`~JSONMode.LEGACY`.
      - `document_class`: BSON documents returned by :func:`loads` will be
        decoded to an instance of this class. Must be a subclass of
        :class:`collections.MutableMapping`. Defaults to :class:`dict`.
      - `uuid_representation`: The :class:`~bson.binary.UuidRepresentation`
        to use when encoding and decoding instances of :class:`uuid.UUID`.
        Defaults to :const:`~bson.binary.UuidRepresentation.PYTHON_LEGACY`.
      - `tz_aware`: If ``True``, MongoDB Extended JSON's *Strict mode* type
        `Date` will be decoded to timezone aware instances of
        :class:`datetime.datetime`. Otherwise they will be naive. Defaults
        to ``True``.
      - `tzinfo`: A :class:`datetime.tzinfo` subclass that specifies the
        timezone from which :class:`~datetime.datetime` objects should be
        decoded. Defaults to :const:`~bson.tz_util.utc`.
      - `args`: arguments to :class:`~bson.codec_options.CodecOptions`
      - `kwargs`: arguments to :class:`~bson.codec_options.CodecOptions`

    .. seealso:: The specification for Relaxed and Canonical `Extended JSON`_.

    .. versionadded:: 3.4

    .. versionchanged:: 3.5
       Accepts the optional parameter `json_mode`.

    Fc                    |j                  dd      |d<   |d   r|j                  dt              |d<   |t        j                  t        j                  t        j
                  fvrt        d      t        t        | &  | g|i |}|t        j                  t        j                  t        j                  fvrt        d      ||_        |j                  t        j                  k(  r%d|_        t        j
                  |_        d|_        |S |j                  t        j                  k(  r%d|_        t        j                  |_        d|_        |S ||_        ||_        ||_        |S )Ntz_awareTtzinfoznJSONOptions.datetime_representation must be one of LEGACY, NUMBERLONG, or ISO8601 from DatetimeRepresentation.zQJSONOptions.json_mode must be one of LEGACY, RELAXED, or CANONICAL from JSONMode.F)getr   r%   r+   r,   r-   r   superr6   __new__r2   r3   r4   	json_modestrict_number_longdatetime_representationstrict_uuid)	clsr>   r?   r@   r=   argskwargsself	__class__s	           r0   r<   zJSONOptions.__new__  sf    $ZZ
D9z*%zz(C8F8"+A+H+H+A+L+L+A+I+I+K K %FG G [#.sDTDVDX__%--%//1 1 %./ / #>>X---&+D#+A+I+ID(#D  ^^x111&*D#+A+L+LD(#D
  '9D#+BD(*Dr/   c                     d| j                   d| j                  d| j                  d| j                  dt        t
        |          
S )Nzstrict_number_long=z, datetime_representation=z, strict_uuid=z, json_mode=z, )r>   r?   r@   r=   r;   r6   _arguments_repr)rD   rE   s    r0   rG   zJSONOptions._arguments_repr0  s@     ++00$$NN+t<>@ 	Ar/   c                     t         t        |          }|j                  | j                  | j
                  | j                  | j                  d       |S )Nr>   r?   r@   r=   )r;   r6   _options_dictupdater>   r?   r@   r=   )rD   options_dictrE   s     r0   rJ   zJSONOptions._options_dict:  sO    [$=?"&"9"9'+'C'C++	) 	*
 r/   c                     | j                         }dD ]!  }|j                  |t        | |            ||<   # |j                  |       t	        di |S )a{  
        Make a copy of this JSONOptions, overriding some options::

            >>> from bson.json_util import CANONICAL_JSON_OPTIONS
            >>> CANONICAL_JSON_OPTIONS.tz_aware
            True
            >>> json_options = CANONICAL_JSON_OPTIONS.with_options(tz_aware=False)
            >>> json_options.tz_aware
            False

        .. versionadded:: 3.12
        rI   r.   )rJ   r:   getattrrK   r6   )rD   rC   optsopts       r0   with_optionszJSONOptions.with_optionsD  sZ     !!#0 	<C

3c(:;DI	< 	F"T""r/   )r(   r)   r*   __doc__r%   r+   r2   r<   rG   rJ   rQ   __classcell__)rE   s   @r0   r6   r6      s6    &P ).(>(E(E!X__!FA#r/   r6   )r=   T)r>   r?   r@   c                 t    |j                  dt              }t        j                  t	        | |      g|i |S )aW  Helper function that wraps :func:`json.dumps`.

    Recursive function that handles all BSON types including
    :class:`~bson.binary.Binary` and :class:`~bson.code.Code`.

    :Parameters:
      - `json_options`: A :class:`JSONOptions` instance used to modify the
        encoding of MongoDB Extended JSON types. Defaults to
        :const:`DEFAULT_JSON_OPTIONS`.

    .. versionchanged:: 3.4
       Accepts optional parameter `json_options`. See :class:`JSONOptions`.

    .. versionchanged:: 2.7
       Preserves order when rendering SON, Timestamp, Code, Binary, and DBRef
       instances.
    json_options)popDEFAULT_JSON_OPTIONSjsondumps_json_convert)objrB   rC   rU   s       r0   rY   rY     s5    $ ::n.BCL::mC6HHHHr/   c                 r    |j                  dt              fd|d<   t        j                  | g|i |S )a  Helper function that wraps :func:`json.loads`.

    Automatically passes the object_hook for BSON type conversion.

    Raises ``TypeError``, ``ValueError``, ``KeyError``, or
    :exc:`~bson.errors.InvalidId` on invalid MongoDB Extended JSON.

    :Parameters:
      - `json_options`: A :class:`JSONOptions` instance used to modify the
        decoding of MongoDB Extended JSON types. Defaults to
        :const:`DEFAULT_JSON_OPTIONS`.

    .. versionchanged:: 3.5
       Parses Relaxed and Canonical Extended JSON as well as PyMongo's legacy
       format. Now raises ``TypeError`` or ``ValueError`` when parsing JSON
       type wrappers with values of the wrong type or any extra keys.

    .. versionchanged:: 3.4
       Accepts optional parameter `json_options`. See :class:`JSONOptions`.
    rU   c                     t        |       S N)object_pairs_hookpairsrU   s    r0   <lambda>zloads.<locals>.<lambda>  s    0A|1 r/   r_   )rV   rW   rX   loads)r   rB   rC   rU   s      @r0   rc   rc     s<    * ::n.BCL#F::a)$)&))r/   c                    t        | d      st        | d      rt        fdt        |       D              S t        | d      r*t        | t        t
        f      st        fd| D              S 	 t        |       S # t        $ r | cY S w xY w)z]Recursive helper method that converts BSON types so they can be
    converted into json.
    r   itemsc              3   @   K   | ]  \  }}|t        |      f  y wr^   rZ   ).0kvrU   s      r0   	<genexpr>z _json_convert.<locals>.<genexpr>  s)      01 a67 0s   __iter__c              3   6   K   | ]  }t        |        y wr^   rg   )rh   rj   rU   s     r0   rk   z _json_convert.<locals>.<genexpr>  s     A]1l3As   )	hasattrr   r   
isinstancer   byteslistdefault	TypeError)r[   rU   s    `r0   rZ   rZ     s     sK GC$9 0 )#0 1 	1	j	!*S9e:L*MASABBsL)) 
s   .A: :BBc                 8    t        |j                  |       |      S r^   )object_hookdocument_classr`   s     r0   r_   r_     s    |2259<HHr/   c                 L   d| v rt        |       S d| v rt        |       S d| v rt        | |      S d| v rt        |       S d| v rt	        |       S d| v rt        |       S d| v rd| v rt        | |      S t        | |      S d	| v rt        |       S d
| v rt        | |      S d| v ry d| v rt        |       S d| v r| d   }t        |d   |d         S d| v rt        |       S d| v rt        |       S d| v rt        |       S d| v rt        |       S d| v rt!        |       S d| v rt#        |       S | S )N$oidr"   $date$regex$minKey$maxKey$binary$type$code$uuidz
$undefined$numberLong
$timestamptr   $numberDecimal
$dbPointer$regularExpression$symbol
$numberInt$numberDouble)_parse_canonical_oid_parse_canonical_dbref_parse_canonical_datetime_parse_legacy_regex_parse_canonical_minkey_parse_canonical_maxkey_parse_legacy_binary_parse_canonical_binary_parse_canonical_code_parse_legacy_uuid_parse_canonical_int64r   _parse_canonical_decimal128_parse_canonical_dbpointer_parse_canonical_regex_parse_canonical_symbol_parse_canonical_int32_parse_canonical_double)dctrU   tsps      r0   ru   ru     s}   }#C((}%c**#~(l;;3"3''C&s++C&s++Cc>'\::*3==#~$S))#~!#|44s%c**s,S3s8,,3*3//s)#..s"%c**C&s++s%c**#&s++Jr/   c                     | d   }t        |t              r| S d}| j                  dd      D ]  }|t        j                  |d      z  } t        ||      S )Nrz   r   $options )ro   r   r:   _RE_OPT_TABLE)docpatternflagsrP   s       r0   r   r     s]    (mG'5!
Ewwz2& +""3**+%  r/   c                 <   t        |       dk7  rt        d|       t        | d   t              st        d|       |j                  t
        j                  k(  r+t        j                  t        j                  | d               S t        j                  | d         S )z*Decode a JSON legacy $uuid to Python UUID.r&   zBad $uuid, extra field(s): r   z$uuid must be a string: )lenrs   ro   r   uuid_representationr   UNSPECIFIEDr   	from_uuiduuidUUID)r   rU   s     r0   r   r     s}    
3x1}3@AAc'lI.=>>''+=+I+II		#g, 788yyW&&r/   c                 :   |t         v r{|j                  }t        | |      }|t        j                  k(  r|S |t
        k(  rt        j                  }n#|t        j                  k(  rt        j                  }|j                  |      S t        r|dk(  r| S t        | |      S )Nr   )
r	   r   r   r   r   r
   STANDARDPYTHON_LEGACYas_uuidr   )datasubtyperU   r   binary_values        r0   _binary_or_uuidr     s    ##*>>dG,"4"@"@@l""4"="= $6$?$?? #5"B"B##$788
w!|$  r/   c                     t        | d   t              rd| d   z  | d<   t        | d   d      }|dk\  rt        | d   dd  d      }t        j                  | d   j	                               }t        |||      S )Nr~   %02x   l       r}   )ro   intbase64	b64decodeencoder   )r   rU   r   r   s       r0   r   r   &  s}    #g,$G,G#g,#G*c'l12&+C	N1134D4,77r/   c                 `   | d   }|d   }|d   }t        |t              st        d|       t        |t              rt        |      dkD  rt        d|       t        |      dk7  rt        d|       t	        j
                  |j                               }t        |t        |d      |      S )	Nr}   r   subTypez!$binary base64 must be a string: r'   z7$binary subType must be a string at most 2 characters: z=$binary must include only "base64" and "subType" components: r   )	ro   r   rs   r   r   r   r   r   r   )r   rU   binaryb64r   r   s         r0   r   r   0  s    ^F

CYGc;'FGGg{+s7|a/?,/2 3 	3
6{a,/2 3 	3 CJJL)D4Wb!1<@@r/   c                 0   | d   }t        |       dk7  rt        d|       t        |t              r|d   dk(  r|dd }d}nB|d   dv r|d	   d
k(  r|dd }|dd }n(|d   dv r|dd }|dd }n|d	   dv r|dd	 }|d	d }n|}d}|j	                  d      }d}|dk7  rt        t        ||d       dz        }|d| }t        j                  j                  |d      j                  |t              }|r|dk7  rt        |      dk(  r5|dd j                  d
      \  }}	t        |      dz  t        |	      dz  z   }
nQt        |      dk(  r$t        |dd       dz  t        |dd       dz  z   }
nt        |      dk(  rt        |dd       dz  }
|d   dk(  r
dz  }
|t        j                  
      z
  }|j                  r)|j                  r|j                  |j                        }|S |j                  d      S t!        j"                  t        |      |      S )z3Decode a JSON datetime to python datetime.datetime.ry   r&   zBad $date, extra field(s): ZNi)+-:r   .r   i@B %Y-%m-%dT%H:%M:%S)microsecondr9   r   i  <         r   )secondsr9   )r   rs   ro   r   rfindr   floatdatetimestrptimereplacer   split	timedeltar8   r9   
astimezonebson_millis_to_datetime)r   rU   dtmdtoffset	dot_indexr   awarehoursminutessecss              r0   r   r   A  sX   
g,C
3x1}3@AA#{#r7c>SbBFW
"s2w#~SbBXFW
"SbBXFW
"SbBXFBF HHSM	?eByzN3g=>KJYB!!**#%%,W47 &- &9 	 fm6{a!'!1!1#!6wE
T)CL2,==V!F1QK(4/#fQRj/B2FFV!6!A;'$.ayC
H..t<<E  ""(()<)<=L===--##CHl;;r/   c                 V    t        |       dk7  rt        d|       t        | d         S )z1Decode a JSON ObjectId to bson.objectid.ObjectId.r&   zBad $oid, extra field(s): rx   )r   rs   r   r   s    r0   r   r   |  s*    
3x1}#?@@CK  r/   c                 Z    | d   }t        |       dk7  rt        d|       t        |      S )z&Decode a JSON symbol to Python string.r   r&   zBad $symbol, extra field(s): )r   rs   r   )r   symbols     r0   r   r     s/    ^F
3x1}SBCCVr/   c                 r    | D ]  }|dvst        d|        t        | d   | j                  d            S )z%Decode a JSON code to bson.code.Code.)r   $scopezBad $code, extra field(s): r   r   )scope)rs   r   r:   r   keys     r0   r   r     sF     F))sDEEF GCGGH$566r/   c                     | d   }t        |       dk7  rt        d|       t        |      dk7  rt        d|       t        |d   |d         S )z(Decode a JSON regex to bson.regex.Regex.r   r&   z(Bad $regularExpression, extra field(s): r'   zLBad $regularExpression must include only "pattern"and "options" components: r   options)r   rs   r   )r   regexs     r0   r   r     s`    $%E
3x1}MNN
5zQ:=@ A 	Ay!5#344r/   c                     | D ]   }|j                  d      s|t        vs| c S  t        | j                  d      | j                  d      fd| j                  dd      i| S )z(Decode a JSON DBRef to bson.dbref.DBRef.$r"   r!   databaser#   N)
startswith_DBREF_KEYSr   rV   r   s     r0   r   r     sg     >>#3k#9J #''%. 7''%.7257 7r/   c                 `   | d   }t        |       dk7  rt        d|       t        |t              rp|j	                         }|j
                  t        d|      t        |j                  t              st        d|      t        |      dk7  rt        d|      |S t        d|       )	z9Decode a JSON (deprecated) DBPointer to bson.dbref.DBRef.r   r&   z Bad $dbPointer, extra field(s): z!Bad $dbPointer, extra field $db: z)Bad $dbPointer, $id must be an ObjectId: r'   z)Bad $dbPointer, extra field(s) in DBRef: z"Bad $dbPointer, expected a DBRef: )r   rs   ro   r   as_docr   idr   )r   dbref	dbref_docs      r0   r   r     s    E
3x1}EFF%LLN	>>%9BDF F%((H-AJLN Ny>QAJLN N#GHHr/   c                     | d   }t        |       dk7  rt        d|       t        |t              st        d|       t	        |      S )z"Decode a JSON int32 to python int.r   r&   z Bad $numberInt, extra field(s): z$numberInt must be string: )r   rs   ro   r   r   )r   i_strs     r0   r   r     sG    E
3x1}EFFe[)3@AAu:r/   c                 Z    | d   }t        |       dk7  rt        d|       t        |      S )z(Decode a JSON int64 to bson.int64.Int64.r   r&   z!Bad $numberLong, extra field(s): )r   rs   r   )r   l_strs     r0   r   r     s/    E
3x1}FGG<r/   c                     | d   }t        |       dk7  rt        d|       t        |t              st        d|       t	        |      S )z%Decode a JSON double to python float.r   r&   z#Bad $numberDouble, extra field(s): z$numberDouble must be string: )r   rs   ro   r   r   r   d_strs     r0   r   r     sG     E
3x1}3HIIe[)cCDD<r/   c                     | d   }t        |       dk7  rt        d|       t        |t              st        d|       t	        |      S )z7Decode a JSON decimal128 to bson.decimal128.Decimal128.r   r&   z$Bad $numberDecimal, extra field(s): z$numberDecimal must be string: )r   rs   ro   r   r   r   s     r0   r   r     sI     !E
3x1}CIJJe[)sDEEer/   c                     t        | d         t        us| d   dk7  rt        d|       t        |       dk7  rt        d|       t	               S )z,Decode a JSON MinKey to bson.min_key.MinKey.r{   r&   z$minKey value must be 1: Bad $minKey, extra field(s): )typer   rs   r   r   r   s    r0   r   r     sL    C	N3&#i.A*=>??
3x1}SBCC8Or/   c                     t        | d         t        us| d   dk7  rt        d| f      t        |       dk7  rt        d|       t	               S )z,Decode a JSON MaxKey to bson.max_key.MaxKey.r|   r&   z$maxKey value must be 1: %sr   )r  r   rs   r   r   r   s    r0   r   r     sN    C	N3&#i.A*=5v>>
3x1}SBCC8Or/   c                    |j                   t        j                  k(  r5t        dt	        j
                  |       j                         fdd|z  fg      S dt        dt	        j
                  |       j                         fdd|z  fg      iS )Nr}   r~   r   r   r   )r=   r2   r+   r   r   	b64encodedecode)r   r   rU   s      r0   _encode_binaryr    s    0((.5578fw&') * 	* s	6##D)0023	FW$%' ( ) )r/   c                 V   t        | t              rdt        |       iS t        | t              rt	        | j                         |      S t        | t        j                        r|j                  t        j                  k(  r| j                  s| j                  t              } | t        k\  r| j                  j                  |       }|j                  |j                   |j"                  fdk(  rd}n| j%                  d      }t'        | j(                  dz        }|rd|fz  nd	}d
| j%                  d      ||iS t+        j,                  |       }|j                  t        j.                  k(  rd
|iS d
dt        |      iiS |j0                  rt        | t2              rdt        |       iS t        | t4        t6        f      rQd	}| j8                  t:        j<                  z  r|dz  }| j8                  t:        j>                  z  r|dz  }| j8                  t:        j@                  z  r|dz  }| j8                  t:        jB                  z  r|dz  }| j8                  t:        jD                  z  r|dz  }| j8                  t:        jF                  z  r|dz  }t        | jH                  tJ              r| jH                  }n| jH                  jM                  d      }|jN                  tP        j.                  k(  rtS        d|fd|fg      S dtS        d|fd|fg      iS t        | tT              rddiS t        | tV              rddiS t        | tX              r'dtS        d| jZ                  fd| j\                  fg      iS t        | t^              rG| j`                  dt        |       iS tS        dt        |       fdt	        | j`                  |      fg      S t        | tb              rte        | | jf                  |      S th        rt        | tj              rte        | d |      S t        | tl        jn                        rR|jp                  r8tc        jr                  | |jt                  !      }te        ||jf                  |      S d"| jv                  iS t        | tx              rd#t        |       iS t        | tz              r| S |jN                  tP        j|                  k(  r8t        | t~              r(d$| cxk  rd%k  rn nd&tK        |       iS dtK        |       iS |jN                  tP        j.                  k7  r~t        | t              rnt        j                  |       rd'd(iS t        j                  |       r| d kD  rd)nd*}	d'|	iS |jN                  tP        j|                  k(  rd'tK        t        |             iS t        d+| z        ),Nrx   )rU   r   )r   r   r   r   z%zi  z.%03dr   ry   r   r   r   r   r   r   r   r    zutf-8rz   r   r   r   r   r{   r&   r|   r   r   r   r   r   )r   r   r   i   l        r   r   NaNInfinityz	-Infinityz%r is not JSON serializable)Fro   r   strr   rZ   r   r   r?   r%   r-   r9   r   r   r   	utcoffsetdaysr   microsecondsstrftimer   r   r   _datetime_to_millisr+   r>   r   r   r   r   re
IGNORECASELOCALE	MULTILINEDOTALLUNICODEVERBOSEr   r   r  r=   r2   r   r   r   r   timeincr   r   r   r  r   r   rp   r   r   r@   r   r   hexr   boolr4   r   r   mathisnanisinfreprrs   )
r[   rU   off	tz_stringmillisfracsecsr   r   binvalrepresentations
             r0   rr   rr      s    #x C!!#uSZZ\EE#x(()00&../::kkk-k!jj**3/HHckk3+;+;<	I #I #T 2IS__t34287fY.bLL!45x"L M M ))#.00&--.V$$-V566&&:c5+As3x((#'(99r}}$SLE99ryy SLE99r||#SLE99ryy SLE99rzz!SLE99rzz!SLEckk9-kkGkk((1G!!X__47+j%-@ABB$cIw+?,5u+=+? '@ A 	A#v1~#v1~#y!cC?S#''N"CDEE#t99SX&&c#h}SYY=>@ A 	A #vc3;;==
z#u%c1l33#tyy!##%%)I)IKF!&&..,GGSWW%%#z" #c(++#t
("4"44sM*s$W$ )C.11y~..0ZU5K::c?#U++ZZ_+.7ZN#^44##x'9'99 $YtCy%9::
1C7
88r/   )\rR   r   r   rX   r  r  r   pymongo.errorsr   r   r   r   r   bson.binaryr   r   r	   r
   	bson.coder   bson.codec_optionsr   
bson.dbrefr   bson.decimal128r   
bson.int64r   bson.max_keyr   bson.min_keyr   bson.objectidr   bson.py3compatr   r   r   r   r   
bson.regexr   bson.timestampr   bson.tz_utilr   ILMSUXr   	frozensetr   r%   r2   r6   r+   LEGACY_JSON_OPTIONSrW   r4   CANONICAL_JSON_OPTIONSr3   RELAXED_JSON_OPTIONSr-   STRICT_JSON_OPTIONSrY   rc   rZ   r_   ru   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  rr   r.   r/   r0   <module>r>     s  [z     	  -  * *' '  +  &    "' '  $  
					 ./! !H( (Vr#, r#j "HOO<  +  %x/A/AB  #X-=-=>  "2:: I,*6 %9  +? I #7 )X	!	'!*8A"8<v!757I,) 3 _9r/   