@@ -23,7 +23,7 @@ class JsonSchemaValidator:
2323
2424 @not_keyword
2525 def __init__ (
26- self , schema : Optional [str | Path | dict [str , Any ]] = None , fail_on_error : bool = True
26+ self , schema : Optional [str | Path | dict [str , Any ]] = None , fail_on_error : bool = True , encoding : str = "utf-8-sig"
2727 ) -> None :
2828 """
2929 Initialize the JSON validator and optionally load a schema.
@@ -39,6 +39,9 @@ def __init__(
3939 | When ``True`` (default), validation errors raise an exception immediately.
4040 | When ``False``, validation issues are appended to attribute `error_list` for
4141 | later inspection.
42+ |
43+ | encoding: str, optional
44+ | When provided, json files will be read with the provided encoding.
4245
4346 Raises
4447 ------
@@ -52,12 +55,13 @@ def __init__(
5255 How To Import
5356 -------
5457 | ***** Settings *****
55- | Library JsonSchemaValidator schema=${CURDIR}/schemas/order_schema.json fail_on_error=${True}
58+ | Library JsonSchemaValidator schema=${CURDIR}/schemas/order_schema.json fail_on_error=${True} encoding=utf-8
5659 """
5760
5861 self .error_list = []
5962 self .fail_on_error = fail_on_error
6063 self .schema_loaded = False
64+ self .encoding = encoding
6165
6266 if schema is not None :
6367 self .load_new_schema (schema = schema )
@@ -468,7 +472,7 @@ def _read_json(self, file: str | Path) -> dict[str, Any]:
468472 if not pfile .is_file ():
469473 raise ValueError (f"Expected a file but got: { pfile } " )
470474
471- with pfile .open ("r" , encoding = "utf-8" ) as jf :
475+ with pfile .open ("r" , encoding = self . encoding ) as jf :
472476 return json .load (jf )
473477
474478
0 commit comments