Files
manifoldbt/python/manifoldbt/_serde.py
T
Jimmy7892 67ab17280b Initial commit: manifoldbt public repo
Python DSL, examples, docs, benchmarks, and tests.
Rust engine distributed as pre-compiled wheel via PyPI.
2026-03-17 16:15:40 +01:00

31 lines
892 B
Python

"""Internal helpers to produce JSON matching Rust serde externally-tagged enums."""
from __future__ import annotations
import math
from typing import Any
def scalar_value_to_json(value: Any) -> Any:
"""Convert a Python value to a Rust ScalarValue JSON representation.
Rust serde format (externally tagged):
None -> "Null"
bool -> {"Bool": v}
int -> {"Int64": v}
float -> {"Float64": v}
str -> {"Utf8": v}
"""
if value is None:
return "Null"
if isinstance(value, bool):
return {"Bool": value}
if isinstance(value, int):
return {"Int64": value}
if isinstance(value, float):
if math.isnan(value):
return "NaN"
return {"Float64": value}
if isinstance(value, str):
return {"Utf8": value}
raise TypeError(f"Cannot convert {type(value).__name__} to ScalarValue")