Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#33974 closed New feature (wontfix)

Serializer incorrectly serializes Integer ArrayField as string

Reported by: Arthur Hanson Owned by: nobody
Component: Core (Serialization) Version: 4.0
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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.

Attachments (2)

models.py (253 bytes ) - added by Arthur Hanson 2 years ago.
example model file with arrayfield
test_serializer.py (495 bytes ) - added by Arthur Hanson 2 years ago.
management command that shows the issue

Download all attachments as: .zip

Change History (6)

by Arthur Hanson, 2 years ago

Attachment: models.py added

example model file with arrayfield

by Arthur Hanson, 2 years ago

Attachment: test_serializer.py added

management command that shows the issue

comment:1 by Mariusz Felisiak, 2 years ago

Component: UncategorizedCore (Serialization)
Resolution: wontfix
Status: newclosed
Type: UncategorizedNew feature

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.

comment:2 by Claude Paroz, 2 years ago

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

in reply to:  2 comment:3 by Mariusz Felisiak, 2 years ago

Replying to 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.

comment:4 by Claude Paroz, 2 years ago

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

Note: See TracTickets for help on using tickets.
Back to Top