static String toJSON(Document document, String separator, boolean topLevel) {
        if (separator == null) {
            separator = "";
        }
        String json = separator + "{" + separator;
        boolean first = true;
        for (String key : document.keySet()) {
            if (!first) {
                json = json + "," + separator;
            }
            first = false;
            Object doc = document.get(key);
            if (doc instanceof Document[]) {
                Document[] docs = (Document[]) doc;
                String temp = "";
                for (int i=0;i<docs.length;i++) {
                     temp = temp + toJSON(docs[i],separator+ " ",false);
                }
                json = json + key + ":" + separator + "[" + temp + separator + "]";
            } else if (doc instanceof Document) {
                json = json + key + " : " + toJSON((Document)doc,separator+ " ",false);
            } else {
                Object value = document.get(key);
                if (value == null) {
                    json = json + key + " : null";
                } else if (value instanceof Object[]) {
                    json = json + key + jsonStringArray(toStringArray((Object[])value));
                } else if (value instanceof byte[]) {
                    json = json + key + " : " + new String((byte[])value);
                } else {
                    json = json + key + " : " + value;
                }
            }
        }
        return json + separator + "}";
    }