Skip to content

Commit c779c9d

Browse files
Zaczerojonhealy1
andauthored
Make orjson usage more consistent (#402)
**Description:** It's a required dependency of core. May as well use it instead of the stdlib json where suitable. **PR Checklist:** - [x] Code is formatted and linted (run `pre-commit run --all-files`) - [x] Tests pass (run `make test`) - [x] Documentation has been updated to reflect changes, if applicable - [x] Changes are added to the changelog --------- Co-authored-by: Jonathan Healy <[email protected]>
1 parent ae81078 commit c779c9d

File tree

4 files changed

+13
-12
lines changed

4 files changed

+13
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
1414

1515
### Changed
1616

17+
- Make `orjson` usage more consistent [#402](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/pull/402)
1718
- Improved datetime query handling to only check start and end datetime values when datetime is None [#396](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/pull/396)
1819
- Optimize data_loader.py script [#395](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/pull/395)
1920
- Refactored test configuration to use shared app config pattern [#399](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/pull/399)

stac_fastapi/core/stac_fastapi/core/route_dependencies.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
import importlib
44
import inspect
5-
import json
65
import logging
76
import os
87
from typing import List
98

9+
import orjson
1010
from fastapi import Depends
1111
from jsonschema import validate
1212

@@ -84,14 +84,14 @@
8484

8585
def get_route_dependencies_conf(route_dependencies_env: str) -> list:
8686
"""Get Route dependencies configuration from file or environment variable."""
87-
if os.path.exists(route_dependencies_env):
88-
with open(route_dependencies_env, encoding="utf-8") as route_dependencies_file:
89-
route_dependencies_conf = json.load(route_dependencies_file)
87+
if os.path.isfile(route_dependencies_env):
88+
with open(route_dependencies_env, "rb") as f:
89+
route_dependencies_conf = orjson.loads(f.read())
9090

9191
else:
9292
try:
93-
route_dependencies_conf = json.loads(route_dependencies_env)
94-
except json.JSONDecodeError as exception:
93+
route_dependencies_conf = orjson.loads(route_dependencies_env)
94+
except orjson.JSONDecodeError as exception:
9595
_LOGGER.error("Invalid JSON format for route dependencies. %s", exception)
9696
raise
9797

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/database_logic.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
"""Database logic."""
22

33
import asyncio
4-
import json
54
import logging
65
from base64 import urlsafe_b64decode, urlsafe_b64encode
76
from copy import deepcopy
87
from typing import Any, Dict, Iterable, List, Optional, Tuple, Type, Union
98

109
import attr
1110
import elasticsearch.helpers as helpers
11+
import orjson
1212
from elasticsearch.dsl import Q, Search
1313
from elasticsearch.exceptions import NotFoundError as ESNotFoundError
1414
from starlette.requests import Request
@@ -503,7 +503,7 @@ async def execute_search(
503503
search_after = None
504504

505505
if token:
506-
search_after = json.loads(urlsafe_b64decode(token).decode())
506+
search_after = orjson.loads(urlsafe_b64decode(token))
507507

508508
query = search.query.to_dict() if search.query else None
509509

@@ -543,7 +543,7 @@ async def execute_search(
543543
next_token = None
544544
if len(hits) > limit and limit < max_result_window:
545545
if hits and (sort_array := hits[limit - 1].get("sort")):
546-
next_token = urlsafe_b64encode(json.dumps(sort_array).encode()).decode()
546+
next_token = urlsafe_b64encode(orjson.dumps(sort_array)).decode()
547547

548548
matched = (
549549
es_response["hits"]["total"]["value"]

stac_fastapi/opensearch/stac_fastapi/opensearch/database_logic.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
"""Database logic."""
22

33
import asyncio
4-
import json
54
import logging
65
from base64 import urlsafe_b64decode, urlsafe_b64encode
76
from copy import deepcopy
87
from typing import Any, Dict, Iterable, List, Optional, Tuple, Type, Union
98

109
import attr
10+
import orjson
1111
from opensearchpy import exceptions, helpers
1212
from opensearchpy.helpers.query import Q
1313
from opensearchpy.helpers.search import Search
@@ -527,7 +527,7 @@ async def execute_search(
527527
search_after = None
528528

529529
if token:
530-
search_after = json.loads(urlsafe_b64decode(token).decode())
530+
search_after = orjson.loads(urlsafe_b64decode(token))
531531
if search_after:
532532
search_body["search_after"] = search_after
533533

@@ -567,7 +567,7 @@ async def execute_search(
567567
next_token = None
568568
if len(hits) > limit and limit < max_result_window:
569569
if hits and (sort_array := hits[limit - 1].get("sort")):
570-
next_token = urlsafe_b64encode(json.dumps(sort_array).encode()).decode()
570+
next_token = urlsafe_b64encode(orjson.dumps(sort_array)).decode()
571571

572572
matched = (
573573
es_response["hits"]["total"]["value"]

0 commit comments

Comments
 (0)