source: Dev/branches/play-2.0.1/documentation/manual/sandbox/Javajson.md @ 322

Last change on this file since 322 was 322, checked in by hendrikvanantwerpen, 13 years ago

Added Play! framework and application with Jena dependency. Working on
the basic things now (login/register), after that start implementing
our data model.

File size: 1.2 KB
Line 
1# JSON support
2
3Play includes the [Jackson](http://jackson.codehaus.org/) library as a dependency _(A good tutorial of Jackson can be found [here](http://wiki.fasterxml.com/JacksonInFiveMinutes))_. What this means in practice is that one can serialize from and to JSON format.
4
5# Render JSON
6As for rendering JSON, there is a helper method ```play.json.Render.toJson``` that can be used to send JSON as a response. For example:
7
8```
9import play.*;
10import play.mvc.*;
11import static play.libs.Json.toJson;
12
13import java.util.Map;
14import java.util.HashMap;
15
16public class MyController extends Controller {
17 
18  public static Result index() {
19    Map<String,String> d = new HashMap<String,String>();
20    d.put("peter","foo");
21    d.put("yay","value");
22    return ok(toJson(d));
23   }   
24}
25```
26
27# Google Guava
28The code above can be rewritten for brevity using the Google Guava collections library:
29
30```
31import com.google.common.collect.ImmutableMap;
32import play.*;
33import play.mvc.*;
34import static play.libs.Json.toJson;
35
36public class MyController extends Controller {
37 
38  public static Result index() {
39    return ok(toJson(ImmutableMap.of(
40        "peter", "foo",
41        "yay", "value")));
42   }
43}
44```
Note: See TracBrowser for help on using the repository browser.