File tree Expand file tree Collapse file tree 4 files changed +37
-4
lines changed
src/check_jsonschema/parsers Expand file tree Collapse file tree 4 files changed +37
-4
lines changed Original file line number Diff line number Diff line change @@ -11,6 +11,9 @@ Unreleased
11
11
.. vendor-insert-here
12
12
13
13
- Update vendored schemas (2024-02-05)
14
+ - Support the use of `orjson ` for faster JSON parsing when it is installed.
15
+ This makes it an optional parser which is preferred over the default
16
+ `json ` module when it is available.
14
17
15
18
0.27.4
16
19
------
Original file line number Diff line number Diff line change 1
1
from __future__ import annotations
2
2
3
3
import io
4
- import json
5
4
import pathlib
6
5
import typing as t
7
6
8
7
import ruamel .yaml
9
8
10
9
from ..identify_filetype import path_to_type
11
- from . import json5 , toml , yaml
10
+ from . import json5 , json_ , toml , yaml
12
11
13
- _PARSER_ERRORS : set [type [Exception ]] = {json .JSONDecodeError , yaml .ParseError }
12
+ _PARSER_ERRORS : set [type [Exception ]] = {json_ .JSONDecodeError , yaml .ParseError }
14
13
DEFAULT_LOAD_FUNC_BY_TAG : dict [str , t .Callable [[t .IO [bytes ]], t .Any ]] = {
15
- "json" : json .load ,
14
+ "json" : json_ .load ,
16
15
}
17
16
SUPPORTED_FILE_FORMATS = ["json" , "yaml" ]
18
17
if json5 .ENABLED :
Original file line number Diff line number Diff line change
1
+ from __future__ import annotations
2
+
3
+ import json
4
+ import typing as t
5
+
6
+ try :
7
+ import orjson
8
+
9
+ has_orjson = True
10
+ except ImportError :
11
+ has_orjson = False
12
+
13
+ JSONDecodeError = json .JSONDecodeError
14
+
15
+
16
+ def load (stream : t .IO [bytes ]) -> t .Any :
17
+ bin_data = stream .read ()
18
+ # if orjson is available, try it first
19
+ if has_orjson :
20
+ # in the event of a decode error, it may be that the data contains
21
+ # `Infinity`, `-Infinity`, or `NaN`
22
+ #
23
+ # however, do not failover to stdlib JSON -- it is not obvious that there's any
24
+ # need for check-jsonschema to support these invalid JSON datatypes
25
+ # if users encounter issues with this behavior in the future, we can revisit how
26
+ # JSON loading is handled
27
+ return orjson .loads (bin_data )
28
+ # failover to stdlib json
29
+ return json .loads (bin_data )
Original file line number Diff line number Diff line change @@ -6,6 +6,7 @@ envlist =
6
6
py{312,311,310,39,38}
7
7
py310-{notoml,tomli-format}
8
8
py{38,312}-{json5,pyjson5}
9
+ py{38,312}-{disable_orjson}
9
10
cov
10
11
skip_missing_interpreters = true
11
12
minversion = 4.0.0
22
23
mindeps: click ==8.0.0
23
24
mindeps: requests ==2.0.0
24
25
mindeps: importlib-resources ==1.4.0
26
+ !disable_orjson: orjson
25
27
json5: json5
26
28
pyjson5: pyjson5
27
29
tomli: tomli
You can’t perform that action at this time.
0 commit comments