
    <i                         S r SSKrSSKrSSKrSSKrSSKJ	r	   " S S\R                  5      r " S S\R                  5      rSS	 jrSS
 jrS rS rS rg)z!STIX2 core serialization methods.    N   )format_datetimec                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )STIXJSONEncoder   a+  Custom JSONEncoder subclass for serializing Python ``stix2`` objects.

If an optional property with a default value specified in the STIX 2 spec
is set to that default value, it will be left out of the serialized output.

An example of this type of property include the ``revoked`` common property.
c                 4  > [        U[        R                  [        R                  45      (       a  [	        U5      $ [        U[
        R                  R                  5      (       a"  [        U5      nUR                   H  nX#	 M     U$ [        [        U ]3  U5      $ N)
isinstancedtdatedatetimer   stix2base	_STIXBasedict_defaulted_optional_propertiessuperr   default)selfobjtmp_obj	prop_name	__class__s       M/home/james-whalen/.local/lib/python3.13/site-packages/stix2/serialization.pyr   STIXJSONEncoder.default   sv    cBGGR[[122"3''UZZ11223iG ??	& @N$7<<     __name__
__module____qualname____firstlineno____doc__r   __static_attributes____classcell__r   s   @r   r   r      s    	= 	=r   r   c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )&STIXJSONIncludeOptionalDefaultsEncoder"   a  Custom JSONEncoder subclass for serializing Python ``stix2`` objects.

Differs from ``STIXJSONEncoder`` in that if an optional property with a default
value specified in the STIX 2 spec is set to that default value, it will be
included in the serialized output.
c                   > [        U[        R                  [        R                  45      (       a  [	        U5      $ [        U[
        R                  R                  5      (       a  [        U5      $ [        [        U ]/  U5      $ r	   )r
   r   r   r   r   r   r   r   r   r   r(   r   )r   r   r   s     r   r   .STIXJSONIncludeOptionalDefaultsEncoder.default*   s\    cBGGR[[122"3''UZZ11229?NsSSr   r   r   r&   s   @r   r(   r(   "   s    T Tr   r(   c                     [         R                  " 5        n[        XX40 UD6  UR                  5       sSSS5        $ ! , (       d  f       g= f)a  
Serialize a STIX object.

Args:
    obj: The STIX object to be serialized.
    pretty (bool): If True, output properties following the STIX specs
        formatting. This includes indentation. Refer to notes for more
        details. (Default: ``False``)
    include_optional_defaults (bool): Determines whether to include
        optional properties set to the default value defined in the spec.
    **kwargs: The arguments for a json.dumps() call.

Returns:
    str: The serialized JSON object.

Note:
    The argument ``pretty=True`` will output the STIX object following
    spec order. Using this argument greatly impacts object serialization
    performance. If your use case is centered across machine-to-machine
    operation it is recommended to set ``pretty=False``.

    When ``pretty=True`` the following key-value pairs will be added or
    overridden: indent=4, separators=(",", ": "), item_sort_key=sort_by.
N)ioStringIOfp_serializegetvalue)r   prettyinclude_optional_defaultskwargsfps        r   	serializer5   3   s1    2 
"SfJ6J{{} 
s	   =
Ac                    ^  U(       a  U 4S jnUR                  SSUS.5        U(       a  [        R                  " T U4S[        0UD6  g[        R                  " T U4S[        0UD6  g)a  
Serialize a STIX object to ``fp`` (a text stream file-like supporting object).

Args:
    obj: The STIX object to be serialized.
    fp: A text stream file-like object supporting ``.write()``.
    pretty (bool): If True, output properties following the STIX specs
        formatting. This includes indentation. Refer to notes for more
        details. (Default: ``False``)
    include_optional_defaults (bool): Determines whether to include
        optional properties set to the default value defined in the spec.
    **kwargs: The arguments for a json.dumps() call.

Returns:
    None

Note:
    The argument ``pretty=True`` will output the STIX object following
    spec order. Using this argument greatly impacts object serialization
    performance. If your use case is centered across machine-to-machine
    operation it is recommended to set ``pretty=False``.

    When ``pretty=True`` the following key-value pairs will be added or
    overridden: indent=4, separators=(",", ": "), item_sort_key=sort_by.
c                    > [        T/U Q76 $ r	   find_property_index)elementr   s    r   sort_byfp_serialize.<locals>.sort_byl   s    &s5W55r      ),z: )indent
separatorsitem_sort_keyclsN)updatejsondumpr(   r   )r   r4   r1   r2   r3   r;   s   `     r   r/   r/   Q   sW    4 	6 	+PWXY 		#rPEPP		#r99&9r   c                 F     U R                  U5      $ ! [         a     gf = f)a  
Search sequence 'seq' for val.  This behaves like str.find(): if not found,
-1 is returned instead of throwing an exception.

Args:
    seq: The sequence to search
    val: The value to search for

Returns:
    int: The index of the value if found, or -1 if not found
)index
ValueError)seqvals     r   _findrL   w   s'    yy~ s    
  c                 F    SnU  H  n[        XAU5      nUS:  d  M    U$    U$ )a  
Helper for find_property_index(): search for the property in all elements
of the given sequence.

Args:
    seq: The sequence
    search_key: Property name to find
    search_value: Property value to find

Returns:
    int: A property index, or -1 if the property was not found
rG   r   r8   )rJ   
search_keysearch_valueidxelems        r   _find_property_in_seqrR      s5     C!$LA!8J 
 Jr   c                    UR                  5       (       a  [        U5      $ [        U [        R                  R
                  5      (       aA  X;   a  X   U:X  a  [        [        U 5      U5      nU$ [        U R                  5       X5      n U$ [        U [        5      (       aA  X;   a  X   U:X  a  [        [        U 5      U5      nU$ [        U R                  5       X5      n U$ [        U [        5      (       a  [        XU5      nU$ SnU$ )aQ  
Search (recursively) for the given key and value in the given object.
Return an index for the key, relative to whatever object it's found in.

Args:
    obj: The object to search (list, dict, or stix object)
    search_key: A search key
    search_value: A search value

Returns:
    int: An index; -1 if the key and value aren't found
rG   )isdigitintr
   r   r   r   rL   listrR   valuesr   sorted)r   rN   rO   rP   s       r   r9   r9      s     :#uzz++,,L!@S	:.C J (

jOC J 
C		L!@sZ0C J (

jOC J 
C		#C\B
 J Jr   )FF)r#   r   r   r-   
simplejsonrD   
stix2.baser   utilsr   JSONEncoderr   r(   r5   r/   rL   rR   r9   r   r   r   <module>r]      sW    '  	   "=d&& =*TT-=-= T"<#:L$,"r   