mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-10-28 21:12:16 +00:00
* merge branch develop with main * bump desktop version * fix the merge * sync Cargo.lock to desktop 2.0.0 * fix cli conftest.py --------- Co-authored-by: deeleeramone <>
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()
|