package chat.server.call;
|
|
import java.math.BigDecimal;
|
import java.util.Date;
|
|
public class JSONObject {
|
|
private StringBuilder content;
|
private boolean empty;
|
|
public JSONObject() {
|
content = new StringBuilder();
|
empty = true;
|
}
|
|
public void beginObject() {
|
if (!empty) {
|
content.append(", ");
|
}
|
|
content.append("{");
|
empty = true;
|
}
|
|
public void beginObject(String name) {
|
if (!empty) {
|
content.append(", ");
|
}
|
|
content.append("\"" + toLower(name) + "\": {");
|
empty = true;
|
}
|
|
public void endObject() {
|
content.append("}");
|
empty = false;
|
}
|
|
public void beginArray() {
|
if (!empty) {
|
content.append(", ");
|
}
|
|
content.append("[");
|
empty = true;
|
}
|
|
public void beginArray(String name) {
|
if (!empty) {
|
content.append(", ");
|
}
|
|
content.append("\"" + toLower(name) + "\": [");
|
empty = true;
|
}
|
|
public void endArray() {
|
content.append("]");
|
empty = false;
|
}
|
|
public void addValue(String name, String value) {
|
if (!empty) {
|
content.append(", ");
|
}
|
|
content.append("\"" + toLower(name) + "\": \"" + value + "\"");
|
empty = false;
|
}
|
|
public void addValue(String name, int value) {
|
if (!empty) {
|
content.append(", ");
|
}
|
|
content.append("\"" + toLower(name) + "\": " + value);
|
empty = false;
|
}
|
|
public void addValue(String name, BigDecimal value) {
|
if (!empty) {
|
content.append(", ");
|
}
|
|
content.append("\"" + toLower(name) + "\": " + value);
|
empty = false;
|
}
|
|
public void addValue(String name, boolean value) {
|
if (!empty) {
|
content.append(", ");
|
}
|
|
content.append("\"" + toLower(name) + "\": " + (value ? "true" : "false"));
|
empty = false;
|
}
|
|
public void addValue(String name, Date value) {
|
if (!empty) {
|
content.append(", ");
|
}
|
|
content.append("\"" + toLower(name) + "\": \"" + value + "\"");
|
empty = false;
|
}
|
|
public void addValue(String value) {
|
if (!empty) {
|
content.append(", ");
|
}
|
|
content.append("\"" + value + "\"");
|
empty = false;
|
}
|
public void addValue(int value) {
|
if (!empty) {
|
content.append(", ");
|
}
|
|
content.append(value);
|
empty = false;
|
}
|
|
public void addValue(BigDecimal value) {
|
if (!empty) {
|
content.append(", ");
|
}
|
|
content.append(value);
|
empty = false;
|
}
|
|
public void addValue(boolean value) {
|
if (!empty) {
|
content.append(", ");
|
}
|
|
content.append(value ? "true" : "false");
|
empty = false;
|
}
|
|
public void addValue(Date value) {
|
if (!empty) {
|
content.append(", ");
|
}
|
|
content.append("\"" + value + "\"");
|
empty = false;
|
}
|
|
private String toLower(String name) {
|
if (name == null) {
|
return "empty";
|
}
|
|
return name.toLowerCase();
|
}
|
|
@Override
|
public String toString() {
|
return content.toString();
|
}
|
|
}
|