package chat.server.call;
|
|
import java.io.UnsupportedEncodingException;
|
import java.math.BigDecimal;
|
import java.net.URLDecoder;
|
import java.net.URLEncoder;
|
import java.util.Date;
|
import java.util.List;
|
|
public class StringBuilderJSONWriter implements IJSONWriter {
|
|
private StringBuilder content;
|
private boolean empty;
|
private boolean encode;
|
private boolean nameReady;
|
private boolean autoLowerName;
|
|
|
public StringBuilderJSONWriter() {
|
content = new StringBuilder();
|
empty = true;
|
encode = true;
|
nameReady = false;
|
autoLowerName = false;
|
}
|
|
public void beginObject() {
|
if (!nameReady && !empty) {
|
content.append(", ");
|
}
|
|
content.append("{");
|
empty = true;
|
nameReady = false;
|
}
|
|
public void beginObject(String name) {
|
if (!empty) {
|
content.append(", ");
|
}
|
|
if (name != null) {
|
content.append("\"" + toLower(name) + "\": {");
|
}
|
else {
|
content.append("{");
|
}
|
|
empty = true;
|
}
|
|
public void endObject() {
|
content.append("}");
|
empty = false;
|
nameReady = false;
|
}
|
|
public void beginArray() {
|
if (!nameReady && !empty) {
|
content.append(", ");
|
}
|
|
content.append("[");
|
empty = true;
|
nameReady = false;
|
}
|
|
public void beginArray(String name) {
|
if (!empty) {
|
content.append(", ");
|
}
|
|
if (name != null) {
|
content.append("\"" + toLower(name) + "\": [");
|
}
|
else {
|
content.append("[");
|
}
|
|
empty = true;
|
nameReady = false;
|
}
|
|
public void endArray() {
|
content.append("]");
|
empty = false;
|
nameReady = false;
|
}
|
|
public void write(String name, String value) {
|
if (!empty) {
|
content.append(", ");
|
}
|
|
content.append("\"" + toLower(name) + "\": \"" + encodeValue(value) + "\"");
|
empty = false;
|
}
|
|
public void write(String name, String value, boolean needEncode) {
|
if (!empty) {
|
content.append(", ");
|
}
|
|
if (needEncode) {
|
content.append("\"" + toLower(name) + "\":\"" + encodeValue(value) + "\"");
|
}
|
else {
|
content.append("\"" + toLower(name) + "\":\"" + value + "\"");
|
}
|
|
empty = false;
|
}
|
|
public void write(String name, Integer value) {
|
if (!empty) {
|
content.append(", ");
|
}
|
|
content.append("\"" + toLower(name) + "\": " + String.valueOf(value));
|
empty = false;
|
}
|
|
public void write(String name, Double value) {
|
if (!empty) {
|
content.append(", ");
|
}
|
|
content.append("\"" + toLower(name) + "\": " + String.valueOf(value));
|
empty = false;
|
}
|
|
public void write(String name, Long value) {
|
if (!empty) {
|
content.append(", ");
|
}
|
|
content.append("\"" + toLower(name) + "\": " + String.valueOf(value));
|
empty = false;
|
}
|
|
public void write(String name, BigDecimal value) {
|
if (!empty) {
|
content.append(", ");
|
}
|
|
content.append("\"" + toLower(name) + "\": " + String.valueOf(value));
|
empty = false;
|
}
|
|
public void write(String name, boolean value) {
|
if (!empty) {
|
content.append(", ");
|
}
|
|
content.append("\"" + toLower(name) + "\": " + (value ? "true" : "false"));
|
empty = false;
|
}
|
|
public void write(String name, Date value) {
|
if (!empty) {
|
content.append(", ");
|
}
|
|
content.append("\"" + toLower(name) + "\": \"" + value + "\"");
|
empty = false;
|
}
|
|
|
public void write(String name, IJsonProvider jsonProvider) {
|
writeName(name);
|
|
if (jsonProvider == null) {
|
writeNull();
|
return;
|
}
|
|
jsonProvider.writeJSONObject(this);
|
}
|
|
public void write(String name, List<? extends IJsonProvider> list) {
|
writeName(name);
|
|
if (list == null) {
|
writeNull();
|
return;
|
}
|
|
beginArray();
|
|
for (IJsonProvider json: list) {
|
json.writeJSONObject(this);
|
}
|
|
endArray();
|
}
|
|
public void write(IJsonProvider jsonProvider) {
|
if (jsonProvider == null) {
|
writeNull();
|
return;
|
}
|
|
jsonProvider.writeJSONObject(this);
|
}
|
|
public void writeJSON(String name, String json) {
|
if (!empty) {
|
content.append(", ");
|
}
|
|
if (name != null) {
|
content.append("\"" + toLower(name) + "\": " + json);
|
}
|
else {
|
content.append(json);
|
}
|
|
empty = false;
|
}
|
|
public void writeNull(String name) {
|
if (!empty) {
|
content.append(", ");
|
}
|
|
if (name != null) {
|
content.append("\"" + toLower(name) + "\": null");
|
}
|
else {
|
content.append("null");
|
}
|
|
empty = false;
|
nameReady = false;
|
}
|
|
public void writeName(String name) {
|
if (!empty) {
|
content.append(", ");
|
}
|
|
if (name != null) {
|
content.append("\"" + toLower(name) + "\": ");
|
}
|
|
nameReady = true;
|
}
|
|
public void writeValue(String value) {
|
content.append("\"" + encodeValue(value) + "\"");
|
empty = false;
|
nameReady = false;
|
}
|
|
public void writeValue(int value) {
|
content.append(String.valueOf(value));
|
empty = false;
|
nameReady = false;
|
}
|
|
public void writeValue(BigDecimal value) {
|
content.append(String.valueOf(value));
|
empty = false;
|
nameReady = false;
|
}
|
|
public void writeValue(boolean value) {
|
content.append(value ? "true" : "false");
|
empty = false;
|
nameReady = false;
|
}
|
|
public void writeValue(Date value) {
|
content.append("\"" + value + "\"");
|
empty = false;
|
nameReady = false;
|
}
|
|
public void writeNull() {
|
content.append("null");
|
empty = false;
|
nameReady = false;
|
}
|
|
public void writeJSON(String json) {
|
if (!empty) {
|
content.append(", ");
|
}
|
|
content.append(json);
|
empty = false;
|
nameReady = false;
|
}
|
|
private String toLower(String name) {
|
if (name == null) {
|
return "empty";
|
}
|
|
if (autoLowerName) {
|
return name.toLowerCase();
|
}
|
else {
|
return name;
|
}
|
}
|
|
private String encodeValue(String value) {
|
if (!encode) {
|
return value;
|
}
|
|
String result = "";
|
|
if (value == null) {
|
return result;
|
}
|
|
try {
|
result = URLEncoder.encode(value, "UTF-8");
|
result = result.replace("+", "%20");
|
}
|
catch (Exception e) {
|
result = value;
|
}
|
|
return result;
|
}
|
|
public boolean isEncode() {
|
return encode;
|
}
|
|
public void setEncode(boolean encode) {
|
this.encode = encode;
|
}
|
|
@Override
|
public String encode(String value) {
|
try {
|
String result = URLEncoder.encode(value, "UTF-8");
|
return result;
|
}
|
catch (UnsupportedEncodingException e) {
|
}
|
|
return value;
|
}
|
|
public StringBuilder getContent() {
|
return content;
|
}
|
|
public static void main(String[] args) throws UnsupportedEncodingException {
|
String value = "%E9%82%B5%E5%98%89%E7%A2%A7%E9%92%B0";
|
System.out.println(URLDecoder.decode(value, "UTF-8"));
|
}
|
}
|