Skip to content

Commit

Permalink
Add support for orjson (ikalchev#412)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Jun 28, 2022
1 parent 519f22f commit 03aec46
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 9 deletions.
9 changes: 4 additions & 5 deletions pyhap/hap_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"""
import asyncio
from http import HTTPStatus
import json
import logging
from urllib.parse import parse_qs, urlparse
import uuid
Expand All @@ -25,7 +24,7 @@
from pyhap.util import long_to_bytes

from .hap_crypto import hap_hkdf, pad_tls_nonce
from .util import to_hap_json
from .util import to_hap_json, from_hap_json

# iOS will terminate the connection if it does not respond within
# 10 seconds, so we only allow 9 seconds to avoid this.
Expand Down Expand Up @@ -614,7 +613,7 @@ def handle_set_characteristics(self):
self.send_response(HTTPStatus.UNAUTHORIZED)
return

requested_chars = json.loads(self.request_body.decode("utf-8"))
requested_chars = from_hap_json(self.request_body.decode("utf-8"))
logger.debug(
"%s: Set characteristics content: %s", self.client_address, requested_chars
)
Expand All @@ -639,7 +638,7 @@ def handle_prepare(self):
self.send_response(HTTPStatus.UNAUTHORIZED)
return

request = json.loads(self.request_body.decode("utf-8"))
request = from_hap_json(self.request_body.decode("utf-8"))
logger.debug("%s: prepare content: %s", self.client_address, request)

response = self.accessory_handler.prepare(request, self.client_address)
Expand Down Expand Up @@ -744,7 +743,7 @@ def _send_tlv_pairing_response(self, data):

def handle_resource(self):
"""Get a snapshot from the camera."""
data = json.loads(self.request_body.decode("utf-8"))
data = from_hap_json(self.request_body.decode("utf-8"))

if self.accessory_handler.accessory.category == CATEGORY_BRIDGE:
accessory = self.accessory_handler.accessory.accessories.get(data["aid"])
Expand Down
14 changes: 11 additions & 3 deletions pyhap/util.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import asyncio
import base64
import functools
import json
import random
import socket
from uuid import UUID

import orjson

from .const import BASE_UUID

ALPHANUM = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Expand Down Expand Up @@ -157,9 +158,16 @@ def hap_type_to_uuid(hap_type):

def to_hap_json(dump_obj):
"""Convert an object to HAP json."""
return json.dumps(dump_obj, separators=(",", ":")).encode("utf-8")
return orjson.dumps(dump_obj) # pylint: disable=no-member


def to_sorted_hap_json(dump_obj):
"""Convert an object to sorted HAP json."""
return json.dumps(dump_obj, sort_keys=True, separators=(",", ":")).encode("utf-8")
return orjson.dumps( # pylint: disable=no-member
dump_obj, option=orjson.OPT_SORT_KEYS # pylint: disable=no-member
)


def from_hap_json(json_str):
"""Convert json to an object."""
return orjson.loads(json_str) # pylint: disable=no-member
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
h11
chacha20poly1305-reuseable
cryptography
orjson
zeroconf
1 change: 1 addition & 0 deletions requirements_all.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
base36
cryptography
orjson
pyqrcode
h11
zeroconf
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
README = f.read()


REQUIRES = ["cryptography", "chacha20poly1305-reuseable", "zeroconf>=0.36.2", "h11"]
REQUIRES = ["cryptography", "chacha20poly1305-reuseable", "orjson>=3.7.2", "zeroconf>=0.36.2", "h11"]


setup(
Expand Down

0 comments on commit 03aec46

Please sign in to comment.