Skip to content

Update value_class_encoder.py #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 52 additions & 55 deletions appwrite/encoders/value_class_encoder.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
"""
This module defines a custom JSON encoder that handles various enum classes.

It provides a convenient way to serialize enum values to their corresponding string representations
when encoding objects to JSON.

Usage:
import json
from your_module import ValueClassEncoder

# Create an instance of the encoder
encoder = ValueClassEncoder()

# Serialize an object containing enum values
json_data = json.dumps(your_object, cls=ValueClassEncoder)
"""
import json
from ..enums.authenticator_type import AuthenticatorType
from ..enums.authentication_factor import AuthenticationFactor
Expand All @@ -18,60 +34,41 @@
from ..enums.password_hash import PasswordHash
from ..enums.messaging_provider_type import MessagingProviderType

class ValueClassEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, AuthenticatorType):
return o.value

if isinstance(o, AuthenticationFactor):
return o.value

if isinstance(o, OAuthProvider):
return o.value

if isinstance(o, Browser):
return o.value

if isinstance(o, CreditCard):
return o.value

if isinstance(o, Flag):
return o.value

if isinstance(o, RelationshipType):
return o.value

if isinstance(o, RelationMutate):
return o.value

if isinstance(o, IndexType):
return o.value

if isinstance(o, Runtime):
return o.value

if isinstance(o, ExecutionMethod):
return o.value

if isinstance(o, Name):
return o.value

if isinstance(o, SmtpEncryption):
return o.value

if isinstance(o, Compression):
return o.value

if isinstance(o, ImageGravity):
return o.value

if isinstance(o, ImageFormat):
return o.value

if isinstance(o, PasswordHash):
return o.value

if isinstance(o, MessagingProviderType):
return o.value
class ValueClassEncoder(json.JSONEncoder):
"""Custom JSON encoder for handling enum classes."""

VALUE_CLASSES = (
AuthenticatorType,
AuthenticationFactor,
OAuthProvider,
Browser,
CreditCard,
Flag,
RelationshipType,
RelationMutate,
IndexType,
Runtime,
ExecutionMethod,
Name,
SmtpEncryption,
Compression,
ImageGravity,
ImageFormat,
PasswordHash,
MessagingProviderType,
)

return super().default(o)
def default(self, o):
"""Encodes the given object to a JSON representation.

Args:
o: The object to be encoded.

Returns:
The JSON representation of the object.
"""
for enum_class in self.VALUE_CLASSES:
if isinstance(o, enum_class):
return o.value
return super().default(o)