Use Jackson to create JSON pages, get rid of utils-json dependency.
[Sone.git] / src / main / java / net / pterodactylus / sone / web / ajax / JsonReturnObject.java
1 /*
2  * © 2013 xplosion interactive
3  */
4
5 package net.pterodactylus.sone.web.ajax;
6
7 import java.util.Map;
8
9 import com.fasterxml.jackson.annotation.JsonProperty;
10 import com.fasterxml.jackson.annotation.JsonUnwrapped;
11 import com.fasterxml.jackson.databind.JsonNode;
12 import com.fasterxml.jackson.databind.node.BooleanNode;
13 import com.fasterxml.jackson.databind.node.IntNode;
14 import com.fasterxml.jackson.databind.node.TextNode;
15 import com.google.common.collect.Maps;
16
17 /**
18  * JSON return object for AJAX requests.
19  *
20  * @author <a href="mailto:d.roden@xplosion.de">David Roden</a>
21  */
22 public class JsonReturnObject {
23
24         /** Whether the request was successful. */
25         @JsonProperty
26         private final boolean success;
27
28         /** The returned values. */
29         @JsonUnwrapped
30         private final Map<String, JsonNode> content = Maps.newHashMap();
31
32         /**
33          * Creates a new JSON return object.
34          *
35          * @param success
36          *              {@code true} if the request was successful, {@code false} otherwise
37          */
38         public JsonReturnObject(boolean success) {
39                 this.success = success;
40         }
41
42         //
43         // ACTIONS
44         //
45
46         /**
47          * Stores the given value under the given key.
48          *
49          * @param key
50          *              The key under which to store the value
51          * @param value
52          *              The value to store
53          * @return This JSON return object
54          */
55         public JsonReturnObject put(String key, boolean value) {
56                 return put(key, BooleanNode.valueOf(value));
57         }
58
59         /**
60          * Stores the given value under the given key.
61          *
62          * @param key
63          *              The key under which to store the value
64          * @param value
65          *              The value to store
66          * @return This JSON return object
67          */
68         public JsonReturnObject put(String key, int value) {
69                 return put(key, new IntNode(value));
70         }
71
72         /**
73          * Stores the given value under the given key.
74          *
75          * @param key
76          *              The key under which to store the value
77          * @param value
78          *              The value to store
79          * @return This JSON return object
80          */
81         public JsonReturnObject put(String key, String value) {
82                 return put(key, new TextNode(value));
83         }
84
85         /**
86          * Stores the given value under the given key.
87          *
88          * @param key
89          *              The key under which to store the value
90          * @param value
91          *              The value to store
92          * @return This JSON return object
93          */
94         public JsonReturnObject put(String key, JsonNode value) {
95                 content.put(key, value);
96                 return this;
97         }
98
99 }