PlotSquared/Bukkit/src/main/java/com/plotsquared/bukkit/chat/JsonString.java

48 lines
1.3 KiB
Java
Raw Normal View History

2015-07-30 19:24:01 +02:00
package com.plotsquared.bukkit.chat;
2016-03-23 02:41:37 +01:00
import com.google.gson.stream.JsonWriter;
import com.intellectualcrafters.configuration.serialization.ConfigurationSerializable;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* Represents a JSON string value.
* Writes by this object will not write name values nor begin/end objects in the JSON stream.
* All writes merely write the represented string value.
*/
2015-09-13 06:04:31 +02:00
final class JsonString implements JsonRepresentedObject, ConfigurationSerializable {
2016-03-23 02:41:37 +01:00
2016-03-29 06:56:44 +02:00
private final String value;
2016-03-23 02:41:37 +01:00
public JsonString(CharSequence value) {
2016-03-29 06:56:44 +02:00
this.value = value == null ? null : value.toString();
2015-09-11 12:09:22 +02:00
}
2016-03-23 02:41:37 +01:00
public static JsonString deserialize(Map<String, Object> map) {
return new JsonString(map.get("stringValue").toString());
}
2015-09-11 12:09:22 +02:00
@Override
2016-03-23 02:41:37 +01:00
public void writeJson(JsonWriter writer) throws IOException {
2015-09-11 12:09:22 +02:00
writer.value(getValue());
}
2016-03-23 02:41:37 +01:00
2015-09-13 06:04:31 +02:00
public String getValue() {
2016-03-29 06:56:44 +02:00
return this.value;
2015-09-11 12:09:22 +02:00
}
2016-03-23 02:41:37 +01:00
2015-09-11 12:09:22 +02:00
@Override
2015-09-13 06:04:31 +02:00
public Map<String, Object> serialize() {
2016-03-23 02:41:37 +01:00
HashMap<String, Object> theSingleValue = new HashMap<>();
2016-03-29 06:56:44 +02:00
theSingleValue.put("stringValue", this.value);
2015-09-11 12:09:22 +02:00
return theSingleValue;
}
2016-03-23 02:41:37 +01:00
2015-09-11 12:09:22 +02:00
@Override
2015-09-13 06:04:31 +02:00
public String toString() {
2016-03-29 06:56:44 +02:00
return this.value;
2015-09-11 12:09:22 +02:00
}
}