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

48 lines
1.1 KiB
Java
Raw Normal View History

2015-07-30 19:24:01 +02:00
package com.plotsquared.bukkit.chat;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
2016-06-13 06:47:50 +02:00
import com.google.gson.stream.JsonWriter;
import org.bukkit.configuration.serialization.ConfigurationSerializable;
/**
* 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-06-13 06:47:50 +02:00
private String _value;
public JsonString(CharSequence value) {
_value = value == null ? null : value.toString();
}
2016-03-23 02:41:37 +01:00
2016-06-13 06:47:50 +02:00
@Override
public void writeJson(JsonWriter writer) throws IOException {
writer.value(getValue());
}
2016-03-23 02:41:37 +01:00
2016-06-13 06:47:50 +02:00
public String getValue() {
return _value;
}
2016-03-23 02:41:37 +01:00
2016-06-13 06:47:50 +02:00
public Map<String, Object> serialize() {
HashMap<String, Object> theSingleValue = new HashMap<String, Object>();
theSingleValue.put("stringValue", _value);
return theSingleValue;
}
2016-03-23 02:41:37 +01:00
2016-06-13 06:47:50 +02:00
public static JsonString deserialize(Map<String, Object> map) {
return new JsonString(map.get("stringValue").toString());
}
2016-03-23 02:41:37 +01:00
2016-06-13 06:47:50 +02:00
@Override
public String toString() {
return _value;
}
2016-03-23 02:41:37 +01:00
}