📄 json.java
字号:
case 'f': scratch[i++]='\f'; break; case 'n': scratch[i++]='\n'; break; case 'r': scratch[i++]='\r'; break; case 't': scratch[i++]='\t'; break; case 'u': char uc=(char)((TypeUtil.convertHexDigit((byte)source.next())<<12)+ (TypeUtil.convertHexDigit((byte)source.next())<<8)+ (TypeUtil.convertHexDigit((byte)source.next())<<4)+ (TypeUtil.convertHexDigit((byte)source.next()))); scratch[i++]=uc; break; default: scratch[i++]=c; } } else if (c=='\\') { escape=true; continue; } else if (c=='\"') { // Return string that fits within scratch buffer return toString(scratch,0,i); } else scratch[i++]=c; } // Missing end quote, but return string anyway ? if (b==null) return toString(scratch,0,i); } else b=new StringBuffer(getStringBufferSize()); // parse large string into string buffer synchronized (b) { while (source.hasNext()) { char c=source.next(); if (escape) { escape=false; switch (c) { case '"': b.append('"'); break; case '\\': b.append('\\'); break; case '/': b.append('/'); break; case 'b': b.append('\b'); break; case 'f': b.append('\f'); break; case 'n': b.append('\n'); break; case 'r': b.append('\r'); break; case 't': b.append('\t'); break; case 'u': char uc=(char)((TypeUtil.convertHexDigit((byte)source.next())<<12)+ (TypeUtil.convertHexDigit((byte)source.next())<<8)+ (TypeUtil.convertHexDigit((byte)source.next())<<4)+ (TypeUtil.convertHexDigit((byte)source.next()))); b.append(uc); break; default: b.append(c); } } else if (c=='\\') { escape=true; continue; } else if (c=='\"') break; else b.append(c); } return b.toString(); } } public Number parseNumber(Source source) { boolean minus=false; long number=0; StringBuffer buffer=null; longLoop: while (source.hasNext()) { char c=source.peek(); switch (c) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': number=number*10+(c-'0'); source.next(); break; case '-': case '+': if (number!=0) throw new IllegalStateException("bad number"); minus=true; source.next(); break; case '.': case 'e': case 'E': buffer=new StringBuffer(16); if(minus) buffer.append('-'); buffer.append(number); buffer.append(c); source.next(); break longLoop; default: break longLoop; } } if (buffer==null) return TypeUtil.newLong(minus?-1*number:number); synchronized (buffer) { doubleLoop: while (source.hasNext()) { char c=source.peek(); switch (c) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case '-': case '.': case '+': case 'e': case 'E': buffer.append(c); source.next(); break; default: break doubleLoop; } } return new Double(buffer.toString()); } } protected void seekTo(char seek, Source source) { while (source.hasNext()) { char c=source.peek(); if (c==seek) return; if (!Character.isWhitespace(c)) throw new IllegalStateException("Unexpected '"+c+" while seeking '"+seek+"'"); source.next(); } throw new IllegalStateException("Expected '"+seek+"'"); } protected char seekTo(String seek, Source source) { while (source.hasNext()) { char c=source.peek(); if (seek.indexOf(c)>=0) { return c; } if (!Character.isWhitespace(c)) throw new IllegalStateException("Unexpected '"+c+"' while seeking one of '"+seek+"'"); source.next(); } throw new IllegalStateException("Expected one of '"+seek+"'"); } protected static void complete(String seek, Source source) { int i=0; while (source.hasNext()&&i<seek.length()) { char c=source.next(); if (c!=seek.charAt(i++)) throw new IllegalStateException("Unexpected '"+c+" while seeking \""+seek+"\""); } if (i<seek.length()) throw new IllegalStateException("Expected \""+seek+"\""); } public interface Source { boolean hasNext(); char next(); char peek(); char[] scratchBuffer(); } public static class StringSource implements Source { private final String string; private int index; private char[] scratch; public StringSource(String s) { string=s; } public boolean hasNext() { if (index<string.length()) return true; scratch=null; return false; } public char next() { return string.charAt(index++); } public char peek() { return string.charAt(index); } public String toString() { return string.substring(0,index)+"|||"+string.substring(index); } public char[] scratchBuffer() { if (scratch==null) scratch=new char[string.length()]; return scratch; } } public static class ReaderSource implements Source { private Reader _reader; private int _next=-1; private char[] scratch; public ReaderSource(Reader r) { _reader=r; } public void setReader(Reader reader) { _reader=reader; _next=-1; } public boolean hasNext() { getNext(); if (_next<0) { scratch=null; return false; } return true; } public char next() { getNext(); char c=(char)_next; _next=-1; return c; } public char peek() { getNext(); return (char)_next; } private void getNext() { if (_next<0) { try { _next=_reader.read(); } catch (IOException e) { throw new RuntimeException(e); } } } public char[] scratchBuffer() { if (scratch==null) scratch=new char[1024]; return scratch; } } /* ------------------------------------------------------------ */ /** * JSON Output class for use by {@link Convertible}. */ public interface Output { public void addClass(Class c); public void add(Object obj); public void add(String name, Object value); public void add(String name, double value); public void add(String name, long value); public void add(String name, boolean value); } /* ------------------------------------------------------------ */ /* ------------------------------------------------------------ */ /** JSON Convertible object. * Object can implement this interface in a similar way to the * {@link Externalizable} interface is used to allow classes to * provide their own serialization mechanism. * <p> * A JSON.Convertible object may be written to a JSONObject * or initialized from a Map of field names to values. * <p> * If the JSON is to be convertible back to an Object, then * the method {@link Output#addClass(Class)} must be called from within toJSON() * */ public interface Convertible { public void toJSON(Output out); public void fromJSON(Map object); } /* ------------------------------------------------------------ */ /** Static JSON Convertor. * <p> * may be implemented to provide static convertors for objects that may be registered * with {@link JSON#registerConvertor(Class, org.mortbay.util.ajax.JSON.Convertor). * These convertors are looked up by class, interface and * super class by {@link JSON#getConvertor(Class)}. Convertors should be used when the * classes to be converted cannot implement {@link Convertible} or {@link Generator}. */ public interface Convertor { public void toJSON(Object obj, Output out); public Object fromJSON(Map object); } /* ------------------------------------------------------------ */ /** JSON Generator. * A class that can add it's JSON representation directly to a StringBuffer. * This is useful for object instances that are frequently converted and wish to * avoid multiple Conversions */ public interface Generator { public void addJSON(StringBuffer buffer); } /* ------------------------------------------------------------ */ /** A Literal JSON generator * A utility instance of {@link JSON.Generator} that holds a pre-generated string on JSON text. */ public static class Literal implements Generator { private String _json; /* ------------------------------------------------------------ */ /** Construct a literal JSON instance for use by {@link JSON#toString(Object)}. * If {@link Log#isDebugEnabled()} is true, the JSON will be parsed to check validity * @param json A literal JSON string. */ public Literal(String json) { if (Log.isDebugEnabled()) parse(json); _json=json; } public String toString() { return _json; } public void addJSON(StringBuffer buffer) { buffer.append(_json); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -