Permalink
Browse files

Use anyjson.loads/dumps instead of serialize/deserialize

  • Loading branch information...
1 parent ffab0a6 commit 78d509280f16c105b4643a49b2aada43f0eaa932 @ask committed Apr 10, 2012
View
@@ -5,15 +5,14 @@
if __name__ == "__main__" and __package__ is None:
__package__ = "celery.bin.celery"
+import anyjson
import sys
from importlib import import_module
from optparse import OptionParser, make_option as Option
from pprint import pformat
from textwrap import wrap
-from anyjson import deserialize
-
from .. import __version__
from ..app import app_or_default, current_app
from ..platforms import EX_OK, EX_FAILURE, EX_UNAVAILABLE, EX_USAGE
@@ -217,12 +216,12 @@ def run(self, name, *_, **kw):
# Positional args.
args = kw.get("args") or ()
if isinstance(args, basestring):
- args = deserialize(args)
+ args = anyjson.loads(args)
# Keyword args.
kwargs = kw.get("kwargs") or {}
if isinstance(kwargs, basestring):
- kwargs = deserialize(kwargs)
+ kwargs = anyjson.loads(kwargs)
# Expires can be int/float.
expires = kw.get("expires") or None
View
@@ -11,13 +11,13 @@
"""
from __future__ import absolute_import
+import anyjson
import importlib
import os
import re
import traceback
import warnings
-from anyjson import deserialize
from datetime import datetime
from kombu.utils.encoding import safe_str
@@ -137,7 +137,7 @@ def config_from_object(self, obj, silent=False):
def cmdline_config_parser(self, args, namespace="celery",
re_type=re.compile(r"\((\w+)\)"),
- extra_types={"json": deserialize},
+ extra_types={"json": anyjson.loads},
override_types={"tuple": "json",
"list": "json",
"dict": "json"}):
View
@@ -11,6 +11,7 @@
"""
from __future__ import absolute_import
+import anyjson
import sys
import urllib2
@@ -21,8 +22,6 @@
except ImportError: # pragma: no cover
from cgi import parse_qsl # noqa
-from anyjson import deserialize
-
from .. import __version__ as celery_version
from .base import Task as BaseTask
@@ -63,12 +62,12 @@ def utf8dict(tup): # noqa
for key, value in tup)
-def extract_response(raw_response):
+def extract_response(raw_response, loads=anyjson.loads):
"""Extract the response text from a raw JSON response."""
if not raw_response:
raise InvalidResponseError("Empty response")
try:
- payload = deserialize(raw_response)
+ payload = loads(raw_response)
except ValueError, exc:
raise InvalidResponseError, InvalidResponseError(
str(exc)), sys.exc_info()[2]
@@ -11,7 +11,7 @@
except ImportError: # py3k
from urllib.request import addinfourl # noqa
-from anyjson import serialize
+from anyjson import dumps
from kombu.utils.encoding import from_utf8
from celery.task import http
@@ -42,15 +42,15 @@ def _response(res):
def success_response(value):
- return _response(serialize({"status": "success", "retval": value}))
+ return _response(dumps({"status": "success", "retval": value}))
def fail_response(reason):
- return _response(serialize({"status": "failure", "reason": reason}))
+ return _response(dumps({"status": "failure", "reason": reason}))
def unknown_response():
- return _response(serialize({"status": "u.u.u.u", "retval": True}))
+ return _response(dumps({"status": "u.u.u.u", "retval": True}))
class TestEncodings(Case):
@@ -83,10 +83,7 @@ def test_is_JSON_serializable(self):
{"routing_key": "CPU-bound"})
s.args = list(s.args) # tuples are not preserved
# but this doesn't matter.
- print(dict(s))
- self.assertEqual(s,
- subtask(anyjson.deserialize(
- anyjson.serialize(s))))
+ self.assertEqual(s, subtask(anyjson.loads(anyjson.dumps(s))))
def test_repr(self):
s = MockTask.subtask((2, ), {"cache": True})
@@ -547,7 +547,7 @@ def test_from_message(self):
us = u"æØåveéðƒeæ"
body = {"task": mytask.name, "id": uuid(),
"args": [2], "kwargs": {us: "bar"}}
- m = Message(None, body=anyjson.serialize(body), backend="foo",
+ m = Message(None, body=anyjson.dumps(body), backend="foo",
content_type="application/json",
content_encoding="utf-8")
tw = TaskRequest.from_message(m, m.decode())
@@ -563,7 +563,7 @@ def test_from_message(self):
def test_from_message_empty_args(self):
body = {"task": mytask.name, "id": uuid()}
- m = Message(None, body=anyjson.serialize(body), backend="foo",
+ m = Message(None, body=anyjson.dumps(body), backend="foo",
content_type="application/json",
content_encoding="utf-8")
tw = TaskRequest.from_message(m, m.decode())
@@ -573,7 +573,7 @@ def test_from_message_empty_args(self):
def test_from_message_missing_required_fields(self):
body = {}
- m = Message(None, body=anyjson.serialize(body), backend="foo",
+ m = Message(None, body=anyjson.dumps(body), backend="foo",
content_type="application/json",
content_encoding="utf-8")
with self.assertRaises(KeyError):
@@ -582,7 +582,7 @@ def test_from_message_missing_required_fields(self):
def test_from_message_nonexistant_task(self):
body = {"task": "cu.mytask.doesnotexist", "id": uuid(),
"args": [2], "kwargs": {u"æØåveéðƒeæ": "bar"}}
- m = Message(None, body=anyjson.serialize(body), backend="foo",
+ m = Message(None, body=anyjson.dumps(body), backend="foo",
content_type="application/json",
content_encoding="utf-8")
with self.assertRaises(KeyError):
@@ -1,6 +1,6 @@
from django.http import HttpResponse
-from anyjson import serialize
+from anyjson import dumps
def multiply(request):
@@ -9,4 +9,4 @@ def multiply(request):
retval = x * y
response = {"status": "success", "retval": retval}
- return HttpResponse(serialize(response), mimetype="application/json")
+ return HttpResponse(dumps(response), mimetype="application/json")

0 comments on commit 78d5092

Please sign in to comment.