Skip to content

Commit

Permalink
Merge pull request #577 from danigosa/feature/ujson_support
Browse files Browse the repository at this point in the history
Feature: Support ujson if installed
  • Loading branch information
timothycrosley committed Oct 30, 2017
2 parents aec8a6f + b26c952 commit 367b50b
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 11 deletions.
5 changes: 2 additions & 3 deletions hug/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"""
from __future__ import absolute_import

import json
import sys
from collections import OrderedDict, namedtuple
from distutils.util import strtobool
Expand All @@ -31,13 +30,13 @@
from wsgiref.simple_server import make_server

import falcon
from falcon import HTTP_METHODS

import hug.defaults
import hug.output_format
from falcon import HTTP_METHODS
from hug import introspect
from hug._async import asyncio, ensure_future
from hug._version import current
from hug.json_module import json


INTRO = """
Expand Down
3 changes: 1 addition & 2 deletions hug/input_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@
"""
from __future__ import absolute_import

import json as json_converter
import re
from cgi import parse_multipart
from urllib.parse import parse_qs as urlencoded_converter

from falcon.util.uri import parse_query_string

from hug.format import content_type, underscore
from hug.json_module import json as json_converter


@content_type('text/plain')
Expand Down
27 changes: 27 additions & 0 deletions hug/json_module.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import os

HUG_USE_UJSON = bool(os.environ.get('HUG_USE_UJSON', 1))
try: # pragma: no cover
if HUG_USE_UJSON:
import ujson as json

class dumps_proxy:
"""Proxies the call so non supported kwargs are skipped
and it enables escape_forward_slashes to simulate built-in json
"""
_dumps = json.dumps

def __call__(self, *args, **kwargs):
kwargs.pop('default', None)
kwargs.pop('separators', None)
kwargs.update(escape_forward_slashes=False)
try:
return self._dumps(*args, **kwargs)
except Exception as exc:
raise TypeError("Type[ujson] is not Serializable", exc)

json.dumps = dumps_proxy()
else:
import json
except ImportError: # pragma: no cover
import json
3 changes: 1 addition & 2 deletions hug/output_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
from __future__ import absolute_import

import base64
import json as json_converter
import mimetypes
import os
import re
Expand All @@ -35,9 +34,9 @@

import falcon
from falcon import HTTP_NOT_FOUND

from hug import introspect
from hug.format import camelcase, content_type
from hug.json_module import json as json_converter

IMAGE_TYPES = ('png', 'jpg', 'bmp', 'eps', 'gif', 'im', 'jpeg', 'msp', 'pcx', 'ppm', 'spider', 'tiff', 'webp', 'xbm',
'cur', 'dcx', 'fli', 'flc', 'gbr', 'gd', 'ico', 'icns', 'imt', 'iptc', 'naa', 'mcidas', 'mpo', 'pcd',
Expand Down
3 changes: 1 addition & 2 deletions hug/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"""
from __future__ import absolute_import

import json
import sys
from functools import partial
from io import BytesIO
Expand All @@ -30,9 +29,9 @@

from falcon import HTTP_METHODS
from falcon.testing import StartResponseMock, create_environ

from hug import output_format
from hug.api import API
from hug.json_module import json


def call(method, api_or_module, url, body='', headers=None, params=None, query_string='', scheme='http', **kwargs):
Expand Down
4 changes: 2 additions & 2 deletions hug/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@

import uuid as native_uuid
from decimal import Decimal
from json import loads as load_json

import hug._empty as empty
from hug import introspect
from hug.exceptions import InvalidTypeData
from hug.json_module import json as json_converter


class Type(object):
Expand Down Expand Up @@ -241,7 +241,7 @@ class JSON(Type):
def __call__(self, value):
if type(value) in (str, bytes):
try:
return load_json(value)
return json_converter.loads(value)
except Exception:
raise ValueError('Incorrectly formatted JSON provided')
else:
Expand Down
1 change: 1 addition & 0 deletions requirements/development.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ tox==2.7.0
wheel==0.29.0
pytest-xdist==1.14.0
marshmallow==2.6.0
ujson==1.35

0 comments on commit 367b50b

Please sign in to comment.