def render_json(tree: ast.Play) -> str: """The renderer for a JSON document. This callable accepts a :class:`ast.PlayTree` and returns a valid JSON document. It's mainly included for api-completion, since it actually just accesses already public properties. Parameters ---------- tree The play tree, as loaded via :func:`iambic.parse.text` or :func:`iambic.parse.data` """ return typic.tojson(tree, indent=2, escape_forward_slashes=False)
def tojson(instance: Model): try: return True, typic.tojson(instance) except ValueError as err: return False, err
@typic.klass class Klass: attr: str class Other: def __init__(self, attr: str): self.attr = attr if __name__ == "__main__": Klass(attr="foo") Klass("foo") Klass.transmute("foo") Klass.validate({"attr": "foo"}) Klass("foo").primitive() Klass("foo").primitive(lazy=True) Klass("foo").tojson() Klass("foo").tojson(indent=0) Klass("foo").tojson(ensure_ascii=False) typic.primitive(Klass("foo")) k: Klass = typic.transmute(Klass, "foo") v = typic.validate(Klass, {"attr": "foo"}) j: str = typic.tojson(Klass("foo")) o: Other = Klass("foo").translate(Other) fields = [*Klass("foo").iterate()] iterfields = [*Klass("foo")]
def test_json(): parsed = parse.text(MD_RAW) assert render.json(parsed) == typic.tojson(parsed, indent=2, escape_forward_slashes=False)
def test_tojson_native(obj, expected): native = json.dumps(typic.primitive(obj)).replace("\n", "").replace(" ", "") assert typic.tojson(obj).decode() == native == expected
def test_tojson(obj, expected): assert typic.tojson(obj) == expected
#!/usr/bin/env python # -*- coding: UTF-8 -*- import typic from iambic import parse from tests.static import MD_RAW JSON = typic.tojson(parse.text(MD_RAW)) def test_data(): parsed = parse.text(MD_RAW) data = parse.data(JSON) assert data == parsed