org.json.JSONConverter Maven / Gradle / Ivy
package org.json;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class JSONConverter {
public static String jsonize(Object o) throws JSONException {
StringWriter writer = new StringWriter();
JSONWriter jsonWriter = new JSONWriter(writer);
jsonize(o, jsonWriter);
return writer.toString();
}
private static void jsonize(Object o, JSONWriter jsonWriter) throws JSONException {
if (o instanceof Map) {
jsonWriter.object();
@SuppressWarnings("unchecked")
Map m = (HashMap) o;
for (String key : m.keySet())
jsonWriter.key(key).value(m.get(key));
jsonWriter.endObject();
return;
} else if (o instanceof Collection) {
jsonWriter.array();
@SuppressWarnings("unchecked")
Collection © 2015 - 2025 Weber Informatics LLC | Privacy Policy