2 * © 2013 xplosion interactive
5 package net.pterodactylus.sone.web.ajax;
9 import com.fasterxml.jackson.annotation.JsonAnyGetter;
10 import com.fasterxml.jackson.annotation.JsonProperty;
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.annotations.VisibleForTesting;
16 import com.google.common.collect.Maps;
19 * JSON return object for AJAX requests.
21 * @author <a href="mailto:d.roden@xplosion.de">David Roden</a>
23 public class JsonReturnObject {
25 /** Whether the request was successful. */
27 private final boolean success;
29 /** The returned values. */
30 private final Map<String, JsonNode> content = Maps.newHashMap();
33 * Creates a new JSON return object.
36 * {@code true} if the request was successful, {@code false} otherwise
38 public JsonReturnObject(boolean success) {
39 this.success = success;
47 * Returns whether the request was successful.
49 * @return {@code true} if the request was successful, {@code false} otherwise
52 public boolean isSuccess() {
57 * Returns the value stored under the given key.
60 * The key of the value to retrieve
61 * @return The value of the key, or {@code null} if there is no value for the
65 public JsonNode get(String key) {
66 return content.get(key);
70 * Returns the content of this object for serialization.
72 * @return The content of this object
75 public Map<String, JsonNode> getContent() {
84 * Stores the given value under the given key.
87 * The key under which to store the value
90 * @return This JSON return object
92 public JsonReturnObject put(String key, boolean value) {
93 return put(key, BooleanNode.valueOf(value));
97 * Stores the given value under the given key.
100 * The key under which to store the value
103 * @return This JSON return object
105 public JsonReturnObject put(String key, int value) {
106 return put(key, new IntNode(value));
110 * Stores the given value under the given key.
113 * The key under which to store the value
116 * @return This JSON return object
118 public JsonReturnObject put(String key, String value) {
119 return put(key, new TextNode(value));
123 * Stores the given value under the given key.
126 * The key under which to store the value
129 * @return This JSON return object
131 public JsonReturnObject put(String key, JsonNode value) {
132 content.put(key, value);