55
66from odin import ResourceAdapter , bases , resources , serializers
77from odin .exceptions import CodecDecodeError , CodecEncodeError
8- from odin .utils import getmeta
98
109LIST_TYPES = (bases .ResourceIterable , typing .ValuesView , typing .KeysView )
1110JSON_TYPES = {
1817
1918
2019class OdinEncoder (json .JSONEncoder ):
21- """
22- Encoder for Odin resources.
23- """
20+ """Encoder for Odin resources."""
2421
2522 def __init__ (
2623 self , include_virtual_fields = True , include_type_field = True , * args , ** kwargs
@@ -30,12 +27,8 @@ def __init__(
3027 self .include_type_field = include_type_field
3128
3229 def default (self , o ):
33- if isinstance (o , (resources .ResourceBase , ResourceAdapter )):
34- meta = getmeta (o )
35- obj = o .to_dict (self .include_virtual_fields )
36- if self .include_type_field :
37- obj [meta .type_field ] = meta .resource_name
38- return obj
30+ if isinstance (o , resources .ResourceBase | ResourceAdapter ):
31+ return o .to_dict (self .include_virtual_fields , self .include_type_field )
3932
4033 elif isinstance (o , LIST_TYPES ):
4134 return list (o )
@@ -53,11 +46,12 @@ def load(fp, resource=None, full_clean=True, default_to_not_supplied=False):
5346 See :py:meth:`loads` for more details of the loading operation.
5447
5548 :param fp: a file pointer to read JSON data from.
56- :param resource: A resource type, resource name or list of resources and names to use as the base for creating a
57- resource. If a list is supplied the first item will be used if a resource type is not supplied.
49+ :param resource: A resource type, resource name or list of resources and names to
50+ use as the base for creating a resource. If a list is supplied the first item
51+ will be used if a resource type is not supplied.
5852 :param full_clean: Do a full clean of the object as part of the loading process.
59- :param default_to_not_supplied: Used for loading partial resources. Any fields not supplied are replaced with
60- NOT_SUPPLIED.
53+ :param default_to_not_supplied: Used for loading partial resources. Any fields not
54+ supplied are replaced with NOT_SUPPLIED.
6155 :returns: A resource object or object graph of resources loaded from file.
6256
6357 """
@@ -68,18 +62,21 @@ def loads(s, resource=None, full_clean=True, default_to_not_supplied=False):
6862 """
6963 Load from a JSON encoded string.
7064
71- If a ``resource`` value is supplied it is used as the base resource for the supplied JSON. I one is not supplied a
72- resource type field ``$`` is used to obtain the type represented by the dictionary. A ``ValidationError`` will be
73- raised if either of these values are supplied and not compatible. It is valid for a type to be supplied in the file
74- to be a child object from within the inheritance tree.
65+ If a ``resource`` value is supplied it is used as the base resource for the
66+ supplied JSON. I one is not supplied a resource type field ``$`` is used to obtain
67+ the type represented by the dictionary. A ``ValidationError`` will be raised if
68+ either of these values are supplied and not compatible. It is valid for a type to
69+ be supplied in the file to be a child object from within the inheritance tree.
7570
7671 :param s: String to load and parse.
77- :param resource: A resource type, resource name or list of resources and names to use as the base for creating a
78- resource. If a list is supplied the first item will be used if a resource type is not supplied.
72+ :param resource: A resource type, resource name or list of resources and names to
73+ use as the base for creating a resource. If a list is supplied the first item
74+ will be used if a resource type is not supplied.
7975 :param full_clean: Do a full clean of the object as part of the loading process.
80- :param default_to_not_supplied: Used for loading partial resources. Any fields not supplied are replaced with
81- NOT_SUPPLIED.
82- :returns: A resource object or object graph of resources parsed from supplied string.
76+ :param default_to_not_supplied: Used for loading partial resources. Any fields not
77+ supplied are replaced with NOT_SUPPLIED.
78+ :returns: A resource object or object graph of resources parsed from supplied
79+ string.
8380
8481 """
8582 try :
@@ -95,7 +92,8 @@ def dump(resource, fp, cls=OdinEncoder, **kwargs):
9592 Dump to a JSON encoded file.
9693
9794 :param resource: The root resource to dump to a JSON encoded file.
98- :param cls: Encoder to use serializing to a string; default is the :py:class:`OdinEncoder`.
95+ :param cls: Encoder to use serializing to a string; default is the
96+ :py:class:`OdinEncoder`.
9997 :param fp: The file pointer that represents the output file.
10098
10199 """
@@ -110,7 +108,8 @@ def dumps(resource, cls=OdinEncoder, **kwargs):
110108 Dump to a JSON encoded string.
111109
112110 :param resource: The root resource to dump to a JSON encoded file.
113- :param cls: Encoder to use serializing to a string; default is the :py:class:`OdinEncoder`.
111+ :param cls: Encoder to use serializing to a string; default is the
112+ :py:class:`OdinEncoder`.
114113 :returns: JSON encoded string.
115114
116115 """
0 commit comments