a
BgtH @ s d Z ddlZddlZddlZddlmZmZmZmZm Z m
Z
mZmZm
Z
ddlmZ ddlmZmZmZmZ eeZd/eeeeeegef eeegef eddd ZG d
d dZG dd
d
eZG dd dejZe
dddZG dd dej ej!edZ"e#edddZ$d0eee% ee#dddZ&e#edddZ'd1eee% ee#ddd Z(ej)ed!d"d#Z*eej)d$d%d&Z+ej)ed'd(d)Z,eej)d$d*d+Z-e
d,d-dZ.G d.d- d-e"Z/dS )2zJSON (de)serialization framework.
The framework presented here is somewhat based on `Go's "json" package`_
(especially the ``omitempty`` functionality).
.. _`Go's "json" package`: http://golang.org/pkg/encoding/json/
N) AnyCallableDictIterableListMappingOptionalTypeTypeVar)crypto)b64errors
interfacesutilF json_namedefault omitemptydecoderencoderreturnc C s t | ||||dS )a4 Convenient function to declare a :class:`Field` with proper type annotations.
This function allows to write the following code:
import josepy
class JSON(josepy.JSONObjectWithFields):
typ: str = josepy.field('type')
def other_type(self) -> str:
return self.typ
r r r r r )_TypedFieldr r 4/usr/lib/python3.9/site-packages/josepy/json_util.pyfield s
r c @ s e Zd ZdZdZdeeeee egef ee egef ddddZ
eeedd d
ZeedddZ
ed d
ddZe egef d dddZe egef d dddZeedddZeedddZeeedddZeeedddZdS )Fielda JSON object field.
:class:`Field` is meant to be used together with
:class:`JSONObjectWithFields`.
``encoder`` (``decoder``) is a callable that accepts a single
parameter, i.e. a value to be encoded (decoded), and returns the
serialized (deserialized) value. In case of errors it should raise
:class:`~josepy.errors.SerializationError`
(:class:`~josepy.errors.DeserializationError`).
Note, that ``decoder`` should perform partial serialization only.
:ivar str json_name: Name of the field when encoded to JSON.
:ivar default: Default value (used when not present in JSON object).
:ivar bool omitempty: If ``True`` and the field value is empty, then
it will not be included in the serialized JSON object, and
``default`` will be used for deserialization. Otherwise, if ``False``,
field is considered as required, value will always be included in the
serialized JSON objected, and it must also be present when
deserializing.
)r r r fdecfencNFr c C s> || _ || _|| _|d u r | jn|| _|d u r4| jn|| _d S N)r r r default_decoderr default_encoderr )selfr r r r r r r r __init__R s
zField.__init__valuer c C s t |t o| S )zIs the provided value considered "empty" for this field?
This is useful for subclasses that might want to override the
definition of being empty, e.g. for some more exotic data types.
)
isinstanceboolclsr% r r r _emptya s zField._emptyc C s | |o| jS )zOmit the value in output?)r* r r" r% r r r omitk s z
Field.omitkwargsr c K s0 | j | j| j| j| jd|}t| f i |S )Nr )r r r r r type)r" r. currentr r r _update_paramso s zField._update_params)r r c C s | j |dS )z6Descriptor to change the decoder on JSON object field.)r r1 )r" r r r r r z s z
Field.decoder)r r c C s | j |dS )z6Descriptor to change the encoder on JSON object field.)r r2 )r" r r r r r ~ s z
Field.encoderc C s
| |S )z4Decode a value, optionally with context JSON object.)r r+ r r r decode s zField.decodec C s
| |S )z4Encode a value, optionally with context JSON object.)r r+ r r r encode s zField.encodec sN t |tr t fdd|D S t |trFt fdd| D S |S dS )zDefault decoder.
Recursively deserialize into immutable types (
:class:`josepy.util.frozendict` instead of
:func:`dict`, :func:`tuple` instead of :func:`list`).
c 3 s | ]} |V qd S r r ).0Zsubvaluer) r r