2 * © 2013 xplosion interactive
5 package net.pterodactylus.sone.web.ajax;
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.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. */
31 private final Map<String, JsonNode> content = Maps.newHashMap();
34 * Creates a new JSON return object.
37 * {@code true} if the request was successful, {@code false} otherwise
39 public JsonReturnObject(boolean success) {
40 this.success = success;
48 * Returns whether the request was successful.
50 * @return {@code true} if the request was successful, {@code false} otherwise
53 public boolean isSuccess() {
58 * Returns the value stored under the given key.
61 * The key of the value to retrieve
62 * @return The value of the key, or {@code null} if there is no value for the
66 public JsonNode get(String key) {
67 return content.get(key);
75 * Stores the given value under the given key.
78 * The key under which to store the value
81 * @return This JSON return object
83 public JsonReturnObject put(String key, boolean value) {
84 return put(key, BooleanNode.valueOf(value));
88 * Stores the given value under the given key.
91 * The key under which to store the value
94 * @return This JSON return object
96 public JsonReturnObject put(String key, int value) {
97 return put(key, new IntNode(value));
101 * Stores the given value under the given key.
104 * The key under which to store the value
107 * @return This JSON return object
109 public JsonReturnObject put(String key, String value) {
110 return put(key, new TextNode(value));
114 * Stores the given value under the given key.
117 * The key under which to store the value
120 * @return This JSON return object
122 public JsonReturnObject put(String key, JsonNode value) {
123 content.put(key, value);