Opened 18 years ago

Closed 17 years ago

Last modified 17 years ago

#3466 closed (fixed)

'fields' and 'stream' options not working for json serializer

Reported by: Parand Darugar <tdarugar@…> Owned by: Jacob
Component: Core (Serialization) Version: dev
Severity: Keywords:
Cc: egmanoj@…, ferringb@… Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I'm attempting to use the 'fields' option with the json serializer without any luck. The same call with the xml serializer works fine:

serializers.serialize("json", (Something.objects.get(pk=1), ), fields='myfield' )

breaks with:
Traceback (most recent call last):

File "<console>", line 1, in ?
File "C:\Python24\lib\site-packages\django\core\serializers\init.py", line 55, in serialize s.serialize(queryset, options)
File "C:\Python24\lib\site-packages\django\core\serializers\base.py", line 49, in serialize self.end_serialization()
File "C:\Python24\lib\site-packages\django\core\serializers\json.py", line 19, in end_serialization
simplejson.dump(self.objects, self.stream, cls=DateTimeAwareJSONEncoder,
self.options)
File "C:\Python24\lib\site-packages\django\utils\simplejson\init.py", line 134, in dump check_circular=check_circular, allow_nan=allow_nan, indent=indent,

TypeError: init() got an unexpected keyword argument 'fields'

Trying:
serializers.serialize("xml", (Something.objects.get(pk=1), ), fields='myfield' )

works fine however.

Attachments (4)

tests.py (1.8 KB ) - added by Simon G. <dev@…> 18 years ago.
core.serializers.json.diff (456 bytes ) - added by egmanoj@… 18 years ago.
Patch for core.serializers.json.Serializer
modeltests.serializers.models.diff (1.0 KB ) - added by egmanoj@… 18 years ago.
Added new tests to tests.modeltests.serializers.models
core.serializers.json.2.diff (676 bytes ) - added by Chris Beaven 18 years ago.

Download all attachments as: .zip

Change History (13)

comment:1 by Simon G. <dev@…>, 18 years ago

Triage Stage: UnreviewedAccepted

I've confirmed this bug with the latest svn, just working on some tests for it.

comment:2 by Simon G. <dev@…>, 18 years ago

I've attached tests which show this problem in action. It's just the json serializer that's affected. Unfortunately, I'm not entirely sure what to do to fix it.

by Simon G. <dev@…>, 18 years ago

Attachment: tests.py added

comment:3 by egmanoj@…, 18 years ago

Has patch: set

I have added a couple of tests and a patch.

The patch affects the end_serialization method of core.serializers.json.Serializer. The rationale for removing fields from self.options is that by the time this method gets called, fields would already have served its purpose.

by egmanoj@…, 18 years ago

Attachment: core.serializers.json.diff added

Patch for core.serializers.json.Serializer

by egmanoj@…, 18 years ago

Added new tests to tests.modeltests.serializers.models

comment:4 by egmanoj@…>, 18 years ago

Cc: egmanoj@… added

comment:5 by Chris Beaven, 18 years ago

Summary: 'fields' option not working for json serializer'fields' and 'stream' options not working for json serializer

This is an issue for stream too.

by Chris Beaven, 18 years ago

comment:6 by Russell Keith-Magee, 17 years ago

Resolution: fixed
Status: newclosed

(In [5409]) Fixed #3466 -- Fixed problem with specifyin a 'fields' argument to a JSON serializer. Also added documenation for the 'fields' argument.

comment:7 by (removed), 17 years ago

Cc: ferringb@… added
Resolution: fixed
Status: closedreopened

Reopening (proper route, or...?) since rev 3331 (this patch) fails tests, upon commiting horked the build.

======================================================================
ERROR: test_json_serializer_fields (regressiontests.serializers_regress.tests.SerializerTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/lib/python2.4/site-packages/django/utils/functional.py", line 3, in _curried
    return _curried_func(*(args+moreargs), **dict(kwargs, **morekwargs))
  File "/home/bharring/archives/django/svn-django/tests/regressiontests/serializers_regress/tests.py", line 290, in fieldsTest
    serialized_data = serializers.serialize(format, [obj], indent=2, fields=('field1','field3'))
  File "/usr/lib/python2.4/site-packages/django/core/serializers/__init__.py", line 67, in serialize
    s.serialize(queryset, **options)
  File "/usr/lib/python2.4/site-packages/django/core/serializers/base.py", line 49, in serialize
    self.end_serialization()
  File "/usr/lib/python2.4/site-packages/django/core/serializers/json.py", line 24, in end_serialization
    simplejson.dump(self.objects, self.stream, cls=DjangoJSONEncoder, **self.options)
  File "/usr/lib/python2.4/site-packages/django/utils/simplejson/__init__.py", line 134, in dump
    check_circular=check_circular, allow_nan=allow_nan, indent=indent,
TypeError: __init__() got an unexpected keyword argument 'fields'

======================================================================
ERROR: test_json_serializer_stream (regressiontests.serializers_regress.tests.SerializerTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/lib/python2.4/site-packages/django/utils/functional.py", line 3, in _curried
    return _curried_func(*(args+moreargs), **dict(kwargs, **morekwargs))
  File "/home/bharring/archives/django/svn-django/tests/regressiontests/serializers_regress/tests.py", line 290, in fieldsTest
    serialized_data = serializers.serialize(format, [obj], indent=2, fields=('field1','field3'))
  File "/usr/lib/python2.4/site-packages/django/core/serializers/__init__.py", line 67, in serialize
    s.serialize(queryset, **options)
  File "/usr/lib/python2.4/site-packages/django/core/serializers/base.py", line 49, in serialize
    self.end_serialization()
  File "/usr/lib/python2.4/site-packages/django/core/serializers/json.py", line 24, in end_serialization
    simplejson.dump(self.objects, self.stream, cls=DjangoJSONEncoder, **self.options)
  File "/usr/lib/python2.4/site-packages/django/utils/simplejson/__init__.py", line 134, in dump
    check_circular=check_circular, allow_nan=allow_nan, indent=indent,
TypeError: __init__() got an unexpected keyword argument 'fields'

======================================================================
FAIL: Doctest: modeltests.serializers.models.__test__.API_TESTS
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/lib/python2.4/site-packages/django/test/_doctest.py", line 2161, in runTest
    raise self.failureException(self.format_failure(new.getvalue()))
AssertionError: Failed doctest test for modeltests.serializers.models.__test__.API_TESTS
  File "/home/bharring/archives/django/svn-django/tests/modeltests/serializers/models.py", line unknown line number, in API_TESTS

----------------------------------------------------------------------
File "/home/bharring/archives/django/svn-django/tests/modeltests/serializers/models.py", line ?, in modeltests.serializers.models.__test__.API_TESTS
Failed example:
    print serializers.serialize("json", Article.objects.all(), fields=('headline','pub_date'))
Exception raised:
    Traceback (most recent call last):
      File "/usr/lib/python2.4/site-packages/django/test/_doctest.py", line 1248, in __run
        compileflags, 1) in test.globs
      File "<doctest modeltests.serializers.models.__test__.API_TESTS[49]>", line 1, in ?
        print serializers.serialize("json", Article.objects.all(), fields=('headline','pub_date'))
      File "/usr/lib/python2.4/site-packages/django/core/serializers/__init__.py", line 67, in serialize
        s.serialize(queryset, **options)
      File "/usr/lib/python2.4/site-packages/django/core/serializers/base.py", line 49, in serialize
        self.end_serialization()
      File "/usr/lib/python2.4/site-packages/django/core/serializers/json.py", line 24, in end_serialization
        simplejson.dump(self.objects, self.stream, cls=DjangoJSONEncoder, **self.options)
      File "/usr/lib/python2.4/site-packages/django/utils/simplejson/__init__.py", line 134, in dump
        check_circular=check_circular, allow_nan=allow_nan, indent=indent,
    TypeError: __init__() got an unexpected keyword argument 'fields'


----------------------------------------------------------------------

comment:8 by Russell Keith-Magee, 17 years ago

Resolution: fixed
Status: reopenedclosed

I suspect PEBKAC :-)

Your error trace indicates that the call to simplejson.dump is on line 24 of serializers/json.py. A current trunk puts that statement on line 26.

Either you haven't updated to [5409], or you have a stale Django version floating around somewhere.

I have no idea what you're referring to when you talk about "Rev 3331 (this patch)" - the patch was revision [5409], on ticket #3466. Previous revision for the file was [5311].

in reply to:  8 comment:9 by (removed), 17 years ago

Replying to russellm:

I suspect PEBKAC :-)

Guess my mother was right when she said I was 'special'... :/

Your error trace indicates that the call to simplejson.dump is on line 24 of serializers/json.py. A current trunk puts that statement on line 26.
Either you haven't updated to [5409], or you have a stale Django version floating around somewhere.
I have no idea what you're referring to when you talk about "Rev 3331 (this patch)" - the patch was revision [5409], on ticket #3466. Previous revision for the file was [5311].

Two sets of failures there; stale version bleeding through for tests (site-packages), and listed the bzr proxy rev instead of the svn rev (use bzr+svn so I can do my own development locally).

So yeah, PEBKAC, I suck, pardon for the noise :/

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