Parse JSON string using the specified JSON engine Parameters ---------- value: str or bytes A JSON string or bytes object engine: str (default None) The JSON decoding engine to use. One of: - if "json", parse JSON using built in json module
(value, engine=None)
| 299 | |
| 300 | |
| 301 | def from_json_plotly(value, engine=None): |
| 302 | """ |
| 303 | Parse JSON string using the specified JSON engine |
| 304 | |
| 305 | Parameters |
| 306 | ---------- |
| 307 | value: str or bytes |
| 308 | A JSON string or bytes object |
| 309 | |
| 310 | engine: str (default None) |
| 311 | The JSON decoding engine to use. One of: |
| 312 | - if "json", parse JSON using built in json module |
| 313 | - if "orjson", parse using the faster orjson module, requires the orjson |
| 314 | package |
| 315 | - if "auto" use orjson module if available, otherwise use the json module |
| 316 | |
| 317 | If not specified, the default engine is set to the current value of |
| 318 | plotly.io.json.config.default_engine. |
| 319 | |
| 320 | Returns |
| 321 | ------- |
| 322 | dict |
| 323 | |
| 324 | See Also |
| 325 | -------- |
| 326 | from_json_plotly : Parse JSON with plotly conventions into a dict |
| 327 | """ |
| 328 | orjson = get_module("orjson", should_load=True) |
| 329 | |
| 330 | # Validate value |
| 331 | # -------------- |
| 332 | if not isinstance(value, (str, bytes)): |
| 333 | raise ValueError( |
| 334 | """ |
| 335 | from_json_plotly requires a string or bytes argument but received value of type {typ} |
| 336 | Received value: {value}""".format(typ=type(value), value=value) |
| 337 | ) |
| 338 | |
| 339 | # Determine json engine |
| 340 | if engine is None: |
| 341 | engine = config.default_engine |
| 342 | |
| 343 | if engine == "auto": |
| 344 | if orjson is not None: |
| 345 | engine = "orjson" |
| 346 | else: |
| 347 | engine = "json" |
| 348 | elif engine not in ["orjson", "json"]: |
| 349 | raise ValueError("Invalid json engine: %s" % engine) |
| 350 | |
| 351 | if engine == "orjson": |
| 352 | JsonConfig.validate_orjson() |
| 353 | # orjson handles bytes input natively |
| 354 | value_dict = orjson.loads(value) |
| 355 | else: |
| 356 | # decode bytes to str for built-in json module |
| 357 | if isinstance(value, bytes): |
| 358 | value = value.decode("utf-8") |
no test coverage detected