mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-10-27 20:42:26 +00:00
* stash some changes * add more robust testing * mypy * point PR at V5 * introduce spec file * codespell * test fix * fix workflow environment setup * fix workflow environment setup * fix workflow environment setup * add pyyaml to dependencies * split lint jobs * fix workflow environment setup * fix workflow environment setup * workflow env setup * workflow env setup * clean up code comments * add auth hook entrypoints * codespell * add codegen feature * codespell * move _unpack into dispatchers for consistency with codegen packages * surface nested models in the response * fix missing coverage in CI * socrata updates * test fix * detect plotly output * add --include and --exclude flags from generate-extension command * cap test matrix at python 3.14 * no useless comments * platform controller command description split * merge URL overloads from path params * exclude none and unset from model dump --------- Co-authored-by: deeleeramone <> Co-authored-by: Copilot <copilot@github.com>
47 lines
1.3 KiB
Python
47 lines
1.3 KiB
Python
"""JSON output adapter — emits raw JSON to stdout, no console styling."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
import sys
|
|
from typing import Any
|
|
|
|
import pandas as pd
|
|
|
|
|
|
def _to_serializable(data: Any) -> Any:
|
|
if hasattr(data, "model_dump"):
|
|
return data.model_dump(exclude_unset=True, exclude_none=True).get("results")
|
|
if isinstance(data, pd.DataFrame):
|
|
return data.to_dict(orient="records")
|
|
if isinstance(data, pd.Series):
|
|
return data.to_dict()
|
|
return data
|
|
|
|
|
|
class JsonOutput:
|
|
"""JSON output adapter — full payload, indented, written to stdout."""
|
|
|
|
def display(
|
|
self,
|
|
data: Any,
|
|
title: str = "",
|
|
export: bool = False,
|
|
chart: bool = False,
|
|
) -> None:
|
|
"""Serialize ``data`` as JSON to stdout."""
|
|
if export:
|
|
return
|
|
results = _to_serializable(data)
|
|
try:
|
|
output = json.dumps(results, indent=2, default=str)
|
|
except (TypeError, ValueError) as exc:
|
|
error = json.dumps(
|
|
{"error": {"type": type(exc).__name__, "message": str(exc)}}
|
|
)
|
|
sys.stdout.write(error + "\n")
|
|
sys.stdout.flush()
|
|
return
|
|
sys.stdout.write(output + "\n")
|
|
sys.stdout.flush()
|