From 366a15a10b305320052098714116b21901648f97 Mon Sep 17 00:00:00 2001 From: Dani Gonzalez Date: Mon, 23 Oct 2017 08:54:27 -0400 Subject: [PATCH] - Support ujson if installed. - If installed but no wish to use it set HUG_USE_UJSON=0 environmental variable --- hug/api.py | 5 ++--- hug/input_format.py | 3 +-- hug/json_module.py | 10 ++++++++++ hug/output_format.py | 3 +-- hug/test.py | 3 +-- hug/types.py | 4 ++-- requirements/development.txt | 1 + 7 files changed, 18 insertions(+), 11 deletions(-) create mode 100644 hug/json_module.py diff --git a/hug/api.py b/hug/api.py index 149db68b..dbf8ca10 100644 --- a/hug/api.py +++ b/hug/api.py @@ -21,7 +21,6 @@ """ from __future__ import absolute_import -import json import sys from collections import OrderedDict, namedtuple from distutils.util import strtobool @@ -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 = """ diff --git a/hug/input_format.py b/hug/input_format.py index bef96e32..cc4e4ebc 100644 --- a/hug/input_format.py +++ b/hug/input_format.py @@ -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') diff --git a/hug/json_module.py b/hug/json_module.py new file mode 100644 index 00000000..5812da19 --- /dev/null +++ b/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 diff --git a/hug/output_format.py b/hug/output_format.py index d8dabda1..fbd240ce 100644 --- a/hug/output_format.py +++ b/hug/output_format.py @@ -22,7 +22,6 @@ from __future__ import absolute_import import base64 -import json as json_converter import mimetypes import os import re @@ -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', diff --git a/hug/test.py b/hug/test.py index 71a3e896..d231d484 100644 --- a/hug/test.py +++ b/hug/test.py @@ -21,7 +21,6 @@ """ from __future__ import absolute_import -import json import sys from functools import partial from io import BytesIO @@ -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): diff --git a/hug/types.py b/hug/types.py index 4a85b7bd..29513ca2 100644 --- a/hug/types.py +++ b/hug/types.py @@ -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): @@ -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: diff --git a/requirements/development.txt b/requirements/development.txt index 66464304..41c8156f 100644 --- a/requirements/development.txt +++ b/requirements/development.txt @@ -12,3 +12,4 @@ tox==2.7.0 wheel==0.29.0 pytest-xdist==1.14.0 marshmallow==2.6.0 +ujson==1.35 \ No newline at end of file