@@ -58,7 +58,7 @@ Once you have a Mojo project set up locally,
5858
5959 ``` toml
6060 [dependencies ]
61- lightbug_http = " >=0.26.1.2 ,<0.26.2 "
61+ lightbug_http = " >=0.26.2.0 ,<0.26.3 "
6262 ```
6363
64643 . Run ` pixi install ` at the root of your project, where ` pixi.toml ` is located
@@ -151,6 +151,33 @@ struct ExampleRouter(HTTPService):
151151
152152We plan to add more advanced routing functionality in a future library called ` lightbug_api ` , see [ Roadmap] ( #roadmap ) for more details.
153153
154+ ### JSON
155+
156+ Use ` json_decode[T] ` to deserialize a request body into a typed struct, and ` Json(value) ` to return a JSON response:
157+
158+ ``` mojo
159+ from lightbug_http import OK, HTTPRequest, HTTPResponse, HTTPService
160+ from lightbug_http.http.json import Json, json_decode
161+
162+ @fieldwise_init
163+ struct GreetRequest(Movable, Defaultable):
164+ var name: String
165+ fn __init__(out self): self.name = ""
166+
167+ @fieldwise_init
168+ struct GreetResponse(Movable, Defaultable):
169+ var message: String
170+ fn __init__(out self): self.message = ""
171+
172+ @fieldwise_init
173+ struct JsonService(HTTPService):
174+ fn func(mut self, req: HTTPRequest) raises -> HTTPResponse:
175+ var body = json_decode[GreetRequest](req)
176+ return OK(Json(GreetResponse(String("Hello, ", body.name, "!"))))
177+ ```
178+
179+ JSON support is powered by [ emberjson] ( https://github.com/bgreni/EmberJson ) .
180+
154181
155182<p align =" right " >(<a href =" #readme-top " >back to top</a >)</p >
156183
@@ -190,7 +217,7 @@ Check out the examples directory for more example services built with Lightbug,
190217
191218We're working on support for the following (contributors welcome!):
192219
193- - [ ] [ JSON support] ( https://github.com/saviorand/lightbug_http/issues/4 )
220+ - [x ] [ JSON support] ( https://github.com/saviorand/lightbug_http/issues/4 )
194221 - [ ] Complete HTTP/1.x support compliant with RFC 9110/9112 specs (see issues)
195222 - [ ] [ SSL/HTTPS support] ( https://github.com/saviorand/lightbug_http/issues/20 )
196223 - [ ] [ Multiple simultaneous connections] ( https://github.com/saviorand/lightbug_http/issues/5 ) , [ parallelization and performance optimizations] ( https://github.com/saviorand/lightbug_http/issues/6 )
0 commit comments