From a8a86ad006f1c89c857f2f90d4cc7d084e4396c0 Mon Sep 17 00:00:00 2001 From: Gyuri Date: Fri, 2 Feb 2024 11:48:57 +0100 Subject: [PATCH 1/3] fix Decimal support on ArrayFormField --- django_jsonform/forms/fields.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/django_jsonform/forms/fields.py b/django_jsonform/forms/fields.py index 65983f5..6af511c 100644 --- a/django_jsonform/forms/fields.py +++ b/django_jsonform/forms/fields.py @@ -79,6 +79,8 @@ class UUIDCompatibleEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, UUID): return str(obj) + if isinstance(obj, Decimal): + return float(obj) return json.JSONEncoder.default(self, obj) From b3c9d694bd6a689c21c0eeeabd332234f20adacb Mon Sep 17 00:00:00 2001 From: Gyuri Date: Fri, 2 Feb 2024 13:49:44 +0100 Subject: [PATCH 2/3] optimise if --- django_jsonform/forms/fields.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/django_jsonform/forms/fields.py b/django_jsonform/forms/fields.py index 6af511c..e32d124 100644 --- a/django_jsonform/forms/fields.py +++ b/django_jsonform/forms/fields.py @@ -79,7 +79,7 @@ class UUIDCompatibleEncoder(json.JSONEncoder): def default(self, obj): if isinstance(obj, UUID): return str(obj) - if isinstance(obj, Decimal): + elif isinstance(obj, Decimal): return float(obj) return json.JSONEncoder.default(self, obj) From 5299f236951b13f9ac867ad0ee7497f3af6cfb63 Mon Sep 17 00:00:00 2001 From: Gyuri Date: Sun, 4 Feb 2024 10:08:36 +0100 Subject: [PATCH 3/3] use DjangoJSONEncoder for json dumps --- django_jsonform/forms/fields.py | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/django_jsonform/forms/fields.py b/django_jsonform/forms/fields.py index e32d124..5846195 100644 --- a/django_jsonform/forms/fields.py +++ b/django_jsonform/forms/fields.py @@ -1,9 +1,9 @@ import json -from uuid import UUID import django from django.db import models from django.conf import settings from django.core.exceptions import ImproperlyConfigured, ValidationError +from django.core.serializers.json import DjangoJSONEncoder from django_jsonform.utils import _get_django_version django_major, django_minor = _get_django_version() @@ -75,15 +75,6 @@ def add_error(self, error_map): self.widget.add_error(error_map) -class UUIDCompatibleEncoder(json.JSONEncoder): - def default(self, obj): - if isinstance(obj, UUID): - return str(obj) - elif isinstance(obj, Decimal): - return float(obj) - return json.JSONEncoder.default(self, obj) - - class ArrayFormField(SimpleArrayField): def __init__(self, base_field, **kwargs): if hasattr(SimpleArrayField, 'mock_field'): @@ -105,7 +96,7 @@ def __init__(self, base_field, **kwargs): def prepare_value(self, value): if isinstance(value, list): - return json.dumps(value, cls=UUIDCompatibleEncoder) + return json.dumps(value, cls=DjangoJSONEncoder) return value def to_python(self, value):