
org.zbus.json.JSONObject Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of zbus-client Show documentation
Show all versions of zbus-client Show documentation
lightweight message queue, service bus
The newest version!
package org.zbus.json;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class JSONObject extends HashMap implements JSONAware, JSONStreamAware{
private static final long serialVersionUID = -503443796854799292L;
public JSONObject() {
super();
}
public JSONObject(Map map) {
super(map);
}
public static void writeJSONString(Map map, Writer out) throws IOException {
if(map == null){
out.write("null");
return;
}
boolean first = true;
Iterator> iter = map.entrySet().iterator();
out.write('{');
while(iter.hasNext()){
if(first)
first = false;
else
out.write(',');
Map.Entry entry= iter.next();
out.write('\"');
out.write(escape(String.valueOf(entry.getKey())));
out.write('\"');
out.write(':');
JSONValue.writeJSONString(entry.getValue(), out);
}
out.write('}');
}
public void writeJSONString(Writer out) throws IOException{
writeJSONString(this, out);
}
public static String toJSONString(Map map){
final StringWriter writer = new StringWriter();
try {
writeJSONString(map, writer);
return writer.toString();
} catch (IOException e) {
// This should never happen with a StringWriter
throw new RuntimeException(e);
}
}
public String toJSONString(){
return toJSONString(this);
}
public String toString(){
return toJSONString();
}
public static String toString(String key,Object value){
StringBuffer sb = new StringBuffer();
sb.append('\"');
if(key == null)
sb.append("null");
else
JSONValue.escape(key, sb);
sb.append('\"').append(':');
sb.append(JSONValue.toJSONString(value));
return sb.toString();
}
/**
* Escape quotes, \, /, \r, \n, \b, \f, \t and other control characters (U+0000 through U+001F).
* It's the same as JSONValue.escape() only for compatibility here.
*
* @see org.zbus.json.simple.JSONValue#escape(String)
*
* @param s
* @return
*/
public static String escape(String s){
return JSONValue.escape(s);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy