| 139 | |
| 140 | |
| 141 | def _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 | |
| 173 | def _to_yaml( |