package org.dynmap.web;

import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.List;
import java.util.Map;

import org.json.simple.JSONObject;

public class Json {
    public static String stringifyJson(Object o) {
        StringBuilder sb = new StringBuilder();
        appendJson(o, sb);
        return sb.toString();
    }

    public static void appendJson(Object o, StringBuilder s) {
        if (o == null) {
            s.append("null");
        } else if (o instanceof Boolean) {
            s.append(((Boolean) o) ? "true" : "false");
        } else if (o instanceof String) {
            s.append("\"" + JSONObject.escape((String)o) + "\"");
        } else if (o instanceof Integer || o instanceof Long || o instanceof Float || o instanceof Double) {
            s.append(o.toString());
        } else if (o instanceof Map<?, ?>) {
            Map<?, ?> m = (Map<?, ?>) o;
            s.append("{");
            boolean first = true;
            for (Map.Entry<?, ?> entry : m.entrySet()) {
                if (first)
                    first = false;
                else
                    s.append(",");

                appendJson(entry.getKey(), s);
                s.append(": ");
                appendJson(entry.getValue(), s);
            }
            s.append("}");
        } else if (o instanceof List<?>) {
            List<?> l = (List<?>) o;
            s.append("[");
            int count = 0;
            for (int i = 0; i < l.size(); i++) {
                if (count++ > 0) s.append(",");
                appendJson(l.get(i), s);
            }
            s.append("]");
        } else if (o.getClass().isArray()) {
            int length = Array.getLength(o);
            s.append("[");
            int count = 0;
            for (int i = 0; i < length; i++) {
                if (count++ > 0) s.append(",");
                appendJson(Array.get(o, i), s);
            }
            s.append("]");
        } else if (o instanceof Object) /* TODO: Always true, maybe interface? */ {
            s.append("{");
            boolean first = true;

            Class<?> c = o.getClass();
            for(Field field : c.getFields()) {
                if (!Modifier.isPublic(field.getModifiers()))
                    continue;
                String fieldName = field.getName();
                Object fieldValue;
                try {
                     fieldValue = field.get(o);
                } catch (IllegalArgumentException e) {
                    continue;
                } catch (IllegalAccessException e) {
                    continue;
                }

                if (first)
                    first = false;
                else
                    s.append(",");
                appendJson(fieldName, s);
                s.append(": ");
                appendJson(fieldValue, s);
            }
            s.append("}");
        } else {
            s.append("undefined");
        }
    }
}

@@ -378,24 +378,1298 @@
 g();%0A    %7D%0A%0A
+    public static void escape(String s, StringBuilder s2) %7B%0A        for(int i=0;i%3Cs.length();i++)%7B%0A            char ch=s.charAt(i);%0A            switch(ch)%7B%0A            case '%22':%0A                s2.append(%22%5C%5C%5C%22%22);%0A                break;%0A            case '%5C%5C':%0A                s2.append(%22%5C%5C%5C%5C%22);%0A                break;%0A            case '%5Cb':%0A                s2.append(%22%5C%5Cb%22);%0A                break;%0A            case '%5Cf':%0A                s2.append(%22%5C%5Cf%22);%0A                break;%0A            case '%5Cn':%0A                s2.append(%22%5C%5Cn%22);%0A                break;%0A            case '%5Cr':%0A                s2.append(%22%5C%5Cr%22);%0A                break;%0A            case '%5Ct':%0A                s2.append(%22%5C%5Ct%22);%0A                break;%0A            case '/':%0A                s2.append(%22%5C%5C/%22);%0A                break;%0A            default:%0A                if((ch%3E='%5Cu0000' && ch%3C='%5Cu001F') %7C%7C (ch%3E='%5Cu007F'))%7B%0A                    String ss=Integer.toHexString(ch);%0A                    s2.append(%22%5C%5Cu%22);%0A                    for(int k=0;k%3C4-ss.length();k++)%7B%0A                        s2.append('0');%0A                    %7D%0A                    s2.append(ss.toUpperCase());%0A                %7D%0A                else%7B%0A                    s2.append(ch);%0A                %7D%0A            %7D%0A        %7D//for%0A    %7D%0A%0A
     public s
@@ -1715,24 +1715,24 @@
 uilder s) %7B%0A
-
         if (
@@ -1944,22 +1944,23 @@
 %22%5C%22%22
- + JSONObject.
+);%0A            
 esca
@@ -1975,12 +1975,35 @@
 ng)o
-) + 
+, s);%0A            s.append(
 %22%5C%22%22

Make JSON unicode encoding more thorough - don't assume anything outside ASCII should not be escaped