Skip to content

Commit

Permalink
[qa] use orjson instead of json lib everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanBldy committed Sep 25, 2023
1 parent fec1a47 commit 0eaff19
Show file tree
Hide file tree
Showing 18 changed files with 39 additions and 27 deletions.
2 changes: 1 addition & 1 deletion tests/auth/test_auth_route.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import json
import orjson as json

from tests.base import ApiDBTestCase

Expand Down
2 changes: 1 addition & 1 deletion tests/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import datetime
import unittest
import json
import orjson as json
import os
import ntpath

Expand Down
6 changes: 3 additions & 3 deletions tests/misc/test_commands.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import json
import orjson as json
import datetime

from tests.base import ApiDBTestCase
Expand Down Expand Up @@ -34,7 +34,7 @@ def test_clean_auth_tokens_revoked(self):
},
"revoked": False,
}
).encode("utf-8"),
),
)
self.store.add(
"testkey2",
Expand All @@ -45,7 +45,7 @@ def test_clean_auth_tokens_revoked(self):
},
"revoked": True,
}
).encode("utf-8"),
),
)
self.assertEqual(len(self.store.keys()), 2)
commands.clean_auth_tokens()
Expand Down
2 changes: 1 addition & 1 deletion tests/source/csv/test_import_assets.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
import json
import orjson as json

from tests.base import ApiDBTestCase
from zou.app import db
Expand Down
2 changes: 1 addition & 1 deletion tests/source/shotgun/base.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import json
import orjson as json

from tests.base import ApiDBTestCase

Expand Down
4 changes: 1 addition & 3 deletions zou/app/blueprints/auth/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ def wrong_auth_handler(identity_user=None):

@identity_loaded.connect_via(app)
def on_identity_loaded(sender, identity):
if identity.id is not None:
from zou.app.services import persons_service

if isinstance(identity.id, str):
try:
identity.user = persons_service.get_person(identity.id)

Expand Down
2 changes: 1 addition & 1 deletion zou/app/blueprints/comments/resources.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import json
import orjson as json

from flask import abort, request, send_file as flask_send_file
from flask_restful import Resource, reqparse
Expand Down
2 changes: 1 addition & 1 deletion zou/app/blueprints/crud/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import math
import json
import orjson as json
import sqlalchemy.orm as orm

from flask import request, abort, current_app
Expand Down
1 change: 0 additions & 1 deletion zou/app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from zou.app.utils.env import envtobool, env_with_semicolon_to_list

PROPAGATE_EXCEPTIONS = True
RESTFUL_JSON = {"ensure_ascii": False}
DEBUG = envtobool("DEBUG", False)
DEBUG_PORT = int(os.getenv("DEBUG_PORT", 5000))

Expand Down
2 changes: 1 addition & 1 deletion zou/app/services/file_tree_service.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
import re
import json
import orjson as json

from collections import OrderedDict
from slugify import slugify
Expand Down
2 changes: 1 addition & 1 deletion zou/app/services/playlists_service.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import base64

import json
import orjson as json
import os
import zlib

Expand Down
4 changes: 2 additions & 2 deletions zou/app/utils/api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from flask_restful import Api, output_json
from flask_restful import Api
from zou.app.utils.flask import output_json


def configure_api_from_blueprint(blueprint, route_tuples):
Expand All @@ -13,7 +14,6 @@ def configure_api_from_blueprint(blueprint, route_tuples):
api = Api(blueprint, catch_all_404s=True)

api.representations = {
"application/json; charset=utf-8": output_json,
"application/json": output_json,
}

Expand Down
2 changes: 1 addition & 1 deletion zou/app/utils/commands.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# coding: utf-8

import os
import json
import orjson as json
import datetime
import tempfile

Expand Down
4 changes: 0 additions & 4 deletions zou/app/utils/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@ def serialize_value(value):
return serialize_orm_arrays(value)
elif isinstance(value, bytes):
return value.decode("utf-8")
elif isinstance(value, str):
return value
elif isinstance(value, int):
return value
elif isinstance(value, list):
return serialize_list(value)
elif isinstance(value, Locale):
Expand Down
23 changes: 21 additions & 2 deletions zou/app/utils/flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,28 @@
from werkzeug.user_agent import UserAgent
from werkzeug.utils import cached_property
from flask.json.provider import JSONProvider
from flask import current_app, make_response
import orjson

orjson_options = orjson.OPT_NON_STR_KEYS


def output_json(data, code, headers=None):
"""Makes a Flask response with a JSON encoded body"""

# If we're in debug mode, and the indent is not set, we set it to a
# reasonable value here. Note that this won't override any existing value
# that was set.
option = orjson_options
if current_app.debug:
option |= orjson.OPT_INDENT_2

dumped = orjson.dumps(data, option=option).decode("utf-8")

resp = make_response(dumped, code)
resp.headers.extend(headers or {})
return resp


class ORJSONProvider(JSONProvider):
def __init__(self, *args, **kwargs):
Expand All @@ -14,8 +34,7 @@ def loads(self, s, **kwargs):
return orjson.loads(s)

def dumps(self, obj, **kwargs):
# decode back to str, as orjson returns bytes
return orjson.dumps(obj, option=orjson.OPT_NON_STR_KEYS)
return orjson.dumps(obj, option=orjson_options).decode("utf-8")


class ParsedUserAgent(UserAgent):
Expand Down
2 changes: 1 addition & 1 deletion zou/app/utils/remote_job.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import nomad
import base64
import json
import orjson as json
import textwrap
import time

Expand Down
2 changes: 1 addition & 1 deletion zou/remote/config_payload.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
import json
import orjson as json
import sys

from pathlib import Path
Expand Down
2 changes: 1 addition & 1 deletion zou/remote/playlist.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env python
import base64
import json
import orjson as json
import logging
import os
import sys
Expand Down

0 comments on commit 0eaff19

Please sign in to comment.