MCPcopy Create free account
hub / github.com/cdgriffith/Box / _from_json

Function _from_json

box/converters.py:141–170  ·  view source on GitHub ↗
(
    json_string: str | None = None,
    filename: str | PathLike | None = None,
    encoding: str = "utf-8",
    errors: str = "strict",
    multiline: bool = False,
    **kwargs,
)

Source from the content-addressed store, hash-verified

139
140
141def _from_json(
142 json_string: str | None = None,
143 filename: str | PathLike | None = None,
144 encoding: str = "utf-8",
145 errors: str = "strict",
146 multiline: bool = False,
147 **kwargs,
148):
149 if filename:
150 with open(filename, "r", encoding=encoding, errors=errors) as f:
151 if multiline:
152 data = [
153 json.loads(line.strip(), **kwargs)
154 for line in f
155 if line.strip() and not line.strip().startswith("#")
156 ]
157 else:
158 data = json.load(f, **kwargs)
159 elif json_string or (multiline and json_string is not None):
160 if multiline:
161 data = [
162 json.loads(line.strip(), **kwargs)
163 for line in StringIO(json_string, newline=None)
164 if line.strip() and not line.strip().startswith("#")
165 ]
166 else:
167 data = json.loads(json_string, **kwargs)
168 else:
169 raise BoxError("from_json requires a string or filename")
170 return data
171
172
173def _to_yaml(

Callers 2

from_jsonMethod · 0.90
from_jsonMethod · 0.90

Calls 1

BoxErrorClass · 0.90

Tested by

no test coverage detected