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 | |
---|
3 | Play 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 |
---|
6 | As 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 | ``` |
---|
9 | import play.*; |
---|
10 | import play.mvc.*; |
---|
11 | import static play.libs.Json.toJson; |
---|
12 | |
---|
13 | import java.util.Map; |
---|
14 | import java.util.HashMap; |
---|
15 | |
---|
16 | public 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 |
---|
28 | The code above can be rewritten for brevity using the Google Guava collections library: |
---|
29 | |
---|
30 | ``` |
---|
31 | import com.google.common.collect.ImmutableMap; |
---|
32 | import play.*; |
---|
33 | import play.mvc.*; |
---|
34 | import static play.libs.Json.toJson; |
---|
35 | |
---|
36 | public 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.