ZeroIntensity / view.py

The Batteries-Detachable Web Framework
https://view.zintensity.dev
MIT License
206 stars 15 forks source link

Simple JSON Responses #86

Closed ZeroIntensity closed 5 months ago

ZeroIntensity commented 8 months ago

Feature description

As of now, a proper JSON response has to be done through something like this:

from view import new_app
import json

app = new_app()

@app.get("/")
async def index():
    return json.dumps({"foo": "bar"}), {"content-type": "application/json"}

app.run()

This is not at all ideal for view.py, so a much more simple way of doing it should be implemented.

Feature example API

The ideal solution would be:

from view import new_app

@app.get("/")
async def index():
    return {"a": "b"}

app.run()

However, this might need some modifications on the C end, since dictionaries are already used for parsing headers, which can cause some collisions:

return {"a": "b"}, {"x-www-foo": "hi"}  # view doesn't know which is headers and which is JSON

Instead, perhaps a json() function could be added? Or a JSON response class?

@app.get("/")
async def index():
    return json({"hello": "world"})

@app.get("/foo")
async def foo():
    return JSON({"hello": "world"})

Anything else?

No response