3
}:ah= @ s d Z ddlZddlZddlZddlmZmZ ddlZddlm Z m
Z
mZmZ ej
eZG dd dZG dd dejZG d d
d
ejejedZeedd
dZd ddZdd Zd!ddZdd Zdd Zdd Zdd ZG dd deZ dS )"zJSON (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)DictType)b64errors
interfacesutilc @ sr e Zd ZdZdZdd d
Zedd Zd
d Zdd Z dd Z
dd Zdd Zdd Z
edd Zedd ZdS )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.
json_namedefault omitemptyfdecfencNFc C s> || _ || _|| _|d kr | jn|| _|d kr4| jn|| _d S )N)r r
r default_decoderr default_encoderr
)selfr r
r decoderencoder r /usr/lib/python3.6/json_util.py__init__/ s
zField.__init__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.
)
isinstancebool)clsvaluer r r _empty9 s zField._emptyc C s | j |o| jS )zOmit the value in output?)r r )r r r r r omitC s z
Field.omitc K s, | j | j| j| j| jd|}t| f |S )N)r r
r r r )r r
r r r
type)r kwargsZcurrentr r r _update_paramsG s
zField._update_paramsc C s | j |dS )z6Descriptor to change the decoder on JSON object field.)r )r )r r r r r r P s z
Field.decoderc C s | j |dS )z6Descriptor to change the encoder on JSON object field.)r )r )r r
r r r r T s z
Field.encoderc C s
| j |S )z4Decode a value, optionally with context JSON object.)r )r r r r r decodeX s zField.decodec C s
| j |S )z4Encode a value, optionally with context JSON object.)r
)r r r r r encode\ s zField.encodec sN t |tr t fdd|D S t |trFtj fdd|j 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 | ]} j |V qd S )N)r ).0Zsubvalue)r r r