Skip to content

Commit 77b7cfe

Browse files
authored
Update value_class_encoder.py
Refactored the multiple if statements into single for loop and provided proper documentation to the module
1 parent 4c9856c commit 77b7cfe

File tree

1 file changed

+52
-55
lines changed

1 file changed

+52
-55
lines changed
Lines changed: 52 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
"""
2+
This module defines a custom JSON encoder that handles various enum classes.
3+
4+
It provides a convenient way to serialize enum values to their corresponding string representations
5+
when encoding objects to JSON.
6+
7+
Usage:
8+
import json
9+
from your_module import ValueClassEncoder
10+
11+
# Create an instance of the encoder
12+
encoder = ValueClassEncoder()
13+
14+
# Serialize an object containing enum values
15+
json_data = json.dumps(your_object, cls=ValueClassEncoder)
16+
"""
117
import json
218
from ..enums.authenticator_type import AuthenticatorType
319
from ..enums.authentication_factor import AuthenticationFactor
@@ -18,60 +34,41 @@
1834
from ..enums.password_hash import PasswordHash
1935
from ..enums.messaging_provider_type import MessagingProviderType
2036

21-
class ValueClassEncoder(json.JSONEncoder):
22-
def default(self, o):
23-
if isinstance(o, AuthenticatorType):
24-
return o.value
25-
26-
if isinstance(o, AuthenticationFactor):
27-
return o.value
28-
29-
if isinstance(o, OAuthProvider):
30-
return o.value
31-
32-
if isinstance(o, Browser):
33-
return o.value
34-
35-
if isinstance(o, CreditCard):
36-
return o.value
37-
38-
if isinstance(o, Flag):
39-
return o.value
40-
41-
if isinstance(o, RelationshipType):
42-
return o.value
4337

44-
if isinstance(o, RelationMutate):
45-
return o.value
46-
47-
if isinstance(o, IndexType):
48-
return o.value
49-
50-
if isinstance(o, Runtime):
51-
return o.value
52-
53-
if isinstance(o, ExecutionMethod):
54-
return o.value
55-
56-
if isinstance(o, Name):
57-
return o.value
58-
59-
if isinstance(o, SmtpEncryption):
60-
return o.value
61-
62-
if isinstance(o, Compression):
63-
return o.value
64-
65-
if isinstance(o, ImageGravity):
66-
return o.value
67-
68-
if isinstance(o, ImageFormat):
69-
return o.value
70-
71-
if isinstance(o, PasswordHash):
72-
return o.value
73-
74-
if isinstance(o, MessagingProviderType):
75-
return o.value
38+
class ValueClassEncoder(json.JSONEncoder):
39+
"""Custom JSON encoder for handling enum classes."""
40+
41+
VALUE_CLASSES = (
42+
AuthenticatorType,
43+
AuthenticationFactor,
44+
OAuthProvider,
45+
Browser,
46+
CreditCard,
47+
Flag,
48+
RelationshipType,
49+
RelationMutate,
50+
IndexType,
51+
Runtime,
52+
ExecutionMethod,
53+
Name,
54+
SmtpEncryption,
55+
Compression,
56+
ImageGravity,
57+
ImageFormat,
58+
PasswordHash,
59+
MessagingProviderType,
60+
)
7661

77-
return super().default(o)
62+
def default(self, o):
63+
"""Encodes the given object to a JSON representation.
64+
65+
Args:
66+
o: The object to be encoded.
67+
68+
Returns:
69+
The JSON representation of the object.
70+
"""
71+
for enum_class in self.VALUE_CLASSES:
72+
if isinstance(o, enum_class):
73+
return o.value
74+
return super().default(o)

0 commit comments

Comments
 (0)