-
Notifications
You must be signed in to change notification settings - Fork 35
JSON serialization
Ravi Teja Gudapati edited this page Jun 15, 2017
·
19 revisions
Decoding JSON from the request body and writing encoded JSON to the response are some of the most common tasks involved in implementing REST api. It is for this reason, Jaguar has built-in support for JSON serialization. Request
and Response
classes provide methods to make JSON serialization easy to use.
Request
object provides there methods to decode JSON body from the request:
-
bodyAsJsonMap
Deserializes request body into Dart Map -
bodyAsJsonList
Deserializes request body into Dart List -
bodyAsJson
Deserializes request body into Dart Map
Example:
...
Future<Response<String>> addBook(Context ctx) async {
// Decode request body as JSON Map
final Map<String, dynamic> json = await ctx.req.bodyAsJsonMap();
...
}
...
Encoding json is also simple.
The Response object has a json
method that allow you to easily send json.
This method take a parameter that must be a List
or a Map
.
The method use JSON.encode
under the hood so you can pass a Map
or a List
to the Response.json
function.
Here is an example :
...
Future<Response<String>> addBook(Context ctx) async {
...
// myJson is Map or a List
return Response.json(myJson);
}
...
Basics
- Route handler
- Path matching
- Path parameters
- Query parameters
- Serving static files
- Cookies
- Controller
- Parameter binding
- Hot reload
Serialization
Forms
Sessions
Authentication
- Basic authentication
- Form authentication
- JSON authentication
- Authorization
- OAuth
- MongoDb
- PostgreSQL
- MySQL
- Establish connection
- ORM
- Server sent events (SSE)
- Websockets
- systemd
- Docker
- AppEngine