X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2Fajax%2FJsonReturnObject.java;fp=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fweb%2Fajax%2FJsonReturnObject.java;h=3b237da9eb654c722b1aaf4a57d58215cb062c68;hb=9fbafff8f3467329e7c8ad24d5d13273962ade28;hp=0000000000000000000000000000000000000000;hpb=d9f0a171bab2137df371a7c4948a95d1975116c0;p=Sone.git diff --git a/src/main/java/net/pterodactylus/sone/web/ajax/JsonReturnObject.java b/src/main/java/net/pterodactylus/sone/web/ajax/JsonReturnObject.java new file mode 100644 index 0000000..3b237da --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/web/ajax/JsonReturnObject.java @@ -0,0 +1,99 @@ +/* + * © 2013 xplosion interactive + */ + +package net.pterodactylus.sone.web.ajax; + +import java.util.Map; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonUnwrapped; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.BooleanNode; +import com.fasterxml.jackson.databind.node.IntNode; +import com.fasterxml.jackson.databind.node.TextNode; +import com.google.common.collect.Maps; + +/** + * JSON return object for AJAX requests. + * + * @author David Roden + */ +public class JsonReturnObject { + + /** Whether the request was successful. */ + @JsonProperty + private final boolean success; + + /** The returned values. */ + @JsonUnwrapped + private final Map content = Maps.newHashMap(); + + /** + * Creates a new JSON return object. + * + * @param success + * {@code true} if the request was successful, {@code false} otherwise + */ + public JsonReturnObject(boolean success) { + this.success = success; + } + + // + // ACTIONS + // + + /** + * Stores the given value under the given key. + * + * @param key + * The key under which to store the value + * @param value + * The value to store + * @return This JSON return object + */ + public JsonReturnObject put(String key, boolean value) { + return put(key, BooleanNode.valueOf(value)); + } + + /** + * Stores the given value under the given key. + * + * @param key + * The key under which to store the value + * @param value + * The value to store + * @return This JSON return object + */ + public JsonReturnObject put(String key, int value) { + return put(key, new IntNode(value)); + } + + /** + * Stores the given value under the given key. + * + * @param key + * The key under which to store the value + * @param value + * The value to store + * @return This JSON return object + */ + public JsonReturnObject put(String key, String value) { + return put(key, new TextNode(value)); + } + + /** + * Stores the given value under the given key. + * + * @param key + * The key under which to store the value + * @param value + * The value to store + * @return This JSON return object + */ + public JsonReturnObject put(String key, JsonNode value) { + content.put(key, value); + return this; + } + +}