예제 #1
0
파일: json.py 프로젝트: seandstewart/iambic
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)
예제 #2
0
def tojson(instance: Model):
    try:
        return True, typic.tojson(instance)
    except ValueError as err:
        return False, err
예제 #3
0

@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")]
예제 #4
0
def test_json():
    parsed = parse.text(MD_RAW)
    assert render.json(parsed) == typic.tojson(parsed,
                                               indent=2,
                                               escape_forward_slashes=False)
예제 #5
0
def test_tojson_native(obj, expected):
    native = json.dumps(typic.primitive(obj)).replace("\n",
                                                      "").replace(" ", "")
    assert typic.tojson(obj).decode() == native == expected
예제 #6
0
def test_tojson(obj, expected):
    assert typic.tojson(obj) == expected
예제 #7
0
#!/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