Skip to content

Commit

Permalink
- Support ujson if installed.
Browse files Browse the repository at this point in the history
- If installed but no wish to use it set HUG_USE_UJSON=0 environmental variable
  • Loading branch information
Dani Gonzalez committed Oct 23, 2017
1 parent aec8a6f commit 366a15a
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 11 deletions.
5 changes: 2 additions & 3 deletions hug/api.py
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
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
10 changes: 10 additions & 0 deletions hug/json_module.py
@@ -0,0 +1,10 @@
import os

HUG_USE_UJSON = bool(os.environ.get('HUG_USE_UJSON', 1))
try:
if HUG_USE_UJSON:
import ujson as json
else:
import json
except ImportError:
import json
3 changes: 1 addition & 2 deletions hug/output_format.py
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
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
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
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 366a15a

Please sign in to comment.