[Django] #33974: Serializer incorrectly serializes Integer ArrayField as string

21 views
Skip to first unread message

Django

unread,
Sep 1, 2022, 5:33:27 PM9/1/22
to django-...@googlegroups.com
#33974: Serializer incorrectly serializes Integer ArrayField as string
-----------------------------------------+------------------------
Reporter: arthanson | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 4.0
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
If you define a model with an ArrayField of type Integer
(PositiveSmallInteger, etc..) and use the built in serializer to json it
will be serialized as an array of strings. Expected is that it will be
serialized as an array of integers. For example, given the model below:

{{{
from django.db import models

# Create your models here.
from django.contrib.postgres.fields import ArrayField
from django.db import models

class Board(models.Model):
pieces = ArrayField(
base_field=models.PositiveSmallIntegerField()
)

}}}

If you create an object with "pieces = [1, 3, 5, ]" and either create a
fixture or call serialize directly you will get an array of strings. The
following management command shows the issue:

{{{
from django.core.management.base import BaseCommand, CommandError
from serializertest.models import Board
from django.core.serializers import serialize
import json

class Command(BaseCommand):
help = ''

def handle(self, *args, **options):
obj = Board.objects.first()
if not obj:
pieces = [1, 3, 5]
obj = Board()
obj.pieces = pieces
obj.save()

print(serialize('json', [obj]))
print(json.dumps(obj.pieces))
}}}

Output will be:

[{"model": "serializertest.board", "pk": 1, "fields": {"pieces": "[\"1\",
\"3\", \"5\"]"}}]
[1, 3, 5]

Note: the serialize output an array of strings, while calling json
directly will give the expected array of integers.

--
Ticket URL: <https://code.djangoproject.com/ticket/33974>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Sep 1, 2022, 5:34:18 PM9/1/22
to django-...@googlegroups.com
#33974: Serializer incorrectly serializes Integer ArrayField as string
-------------------------------+--------------------------------------

Reporter: arthanson | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 4.0
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by arthanson):

* Attachment "models.py" added.

example model file with arrayfield

Django

unread,
Sep 1, 2022, 5:34:41 PM9/1/22
to django-...@googlegroups.com
#33974: Serializer incorrectly serializes Integer ArrayField as string
-------------------------------+--------------------------------------
Reporter: Arthur Hanson | Owner: nobody

Type: Uncategorized | Status: new
Component: Uncategorized | Version: 4.0
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by Arthur Hanson):

* Attachment "test_serializer.py" added.

management command that shows the issue

Django

unread,
Sep 2, 2022, 12:29:25 AM9/2/22
to django-...@googlegroups.com
#33974: Serializer incorrectly serializes Integer ArrayField as string
-------------------------------------+-------------------------------------

Reporter: Arthur Hanson | Owner: nobody
Type: New feature | Status: closed
Component: Core | Version: 4.0
(Serialization) |
Severity: Normal | Resolution: wontfix

Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => wontfix
* component: Uncategorized => Core (Serialization)
* type: Uncategorized => New feature


Comment:

Thanks for the report however (de-)serialization works for me with
`ArrayField(base_field=models.PositiveSmallIntegerField())`. Are there any
practical issues with the current approach (except for your preferences)?
Other complex fields (such as `JSONField`) are also serialized as strings.
I'm afraid that the proposed change is backward incompatible and you can
always use a custom serializer, if you need a different format.

--
Ticket URL: <https://code.djangoproject.com/ticket/33974#comment:1>

Django

unread,
Sep 2, 2022, 3:48:26 AM9/2/22
to django-...@googlegroups.com
#33974: Serializer incorrectly serializes Integer ArrayField as string
-------------------------------------+-------------------------------------
Reporter: Arthur Hanson | Owner: nobody
Type: New feature | Status: closed
Component: Core | Version: 4.0
(Serialization) |
Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Claude Paroz):

FWIW, I would be favorable to fix this, if not too complex to do.

--
Ticket URL: <https://code.djangoproject.com/ticket/33974#comment:2>

Django

unread,
Sep 2, 2022, 4:07:05 AM9/2/22
to django-...@googlegroups.com
#33974: Serializer incorrectly serializes Integer ArrayField as string
-------------------------------------+-------------------------------------
Reporter: Arthur Hanson | Owner: nobody
Type: New feature | Status: closed
Component: Core | Version: 4.0
(Serialization) |
Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak):

Replying to [comment:2 Claude Paroz]:


> FWIW, I would be favorable to fix this, if not too complex to do.

PoC is always welcome, I'm afraid that this will brake existing fixtures.

--
Ticket URL: <https://code.djangoproject.com/ticket/33974#comment:3>

Django

unread,
Sep 2, 2022, 5:20:23 AM9/2/22
to django-...@googlegroups.com
#33974: Serializer incorrectly serializes Integer ArrayField as string
-------------------------------------+-------------------------------------
Reporter: Arthur Hanson | Owner: nobody
Type: New feature | Status: closed
Component: Core | Version: 4.0
(Serialization) |
Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Claude Paroz):

Arthur, would you like to work on a possible fix? (with no guarantee it
will be accepted eventually)

--
Ticket URL: <https://code.djangoproject.com/ticket/33974#comment:4>

Reply all
Reply to author
Forward
0 new messages