📄 json.java
字号:
buffer.append(number); } public void appendString(StringBuffer buffer, String string) { if (string==null) { appendNull(buffer); return; } QuotedStringTokenizer.quote(buffer,string); } // Parsing utilities protected String toString(char[] buffer,int offset,int length) { return new String(buffer,offset,length); } protected Map newMap() { return new HashMap(); } protected Object[] newArray(int size) { return new Object[size]; } protected JSON contextForArray() { return this; } protected JSON contextFor(String field) { return this; } protected Object convertTo(Class type,Map map) { if (type!=null&&Convertible.class.isAssignableFrom(type)) { try { Convertible conv=(Convertible)type.newInstance(); conv.fromJSON(map); return conv; } catch (Exception e) { throw new RuntimeException(e); } } Convertor convertor=getConvertor(type); if (convertor!=null) { return convertor.fromJSON(map); } return map; } /** * Register a {@link Convertor} for a class or interface. * @param forClass The class or interface that the convertor applies to * @param convertor the convertor */ public void addConvertor(Class forClass, Convertor convertor) { _convertors.put(forClass,convertor); } /** * Lookup a convertor for a class. * <p> * If no match is found for the class, then the interfaces for the class are tried. If still no * match is found, then the super class and it's interfaces are tried recursively. * @param forClass The class * @return a {@link Convertor} or null if none were found. */ protected Convertor getConvertor(Class forClass) { Class c=forClass; Convertor convertor=(Convertor)_convertors.get(c); if (convertor==null && this!=__default) convertor=__default.getConvertor(forClass); while (convertor==null&&c!=null&&c!=Object.class) { Class[] ifs=c.getInterfaces(); int i=0; while (convertor==null&&ifs!=null&&i<ifs.length) convertor=(Convertor)_convertors.get(ifs[i++]); if (convertor==null) { c=c.getSuperclass(); convertor=(Convertor)_convertors.get(c); } } return convertor; } public Object parse(Source source, boolean stripOuterComment) { int comment_state=0; // 0=no comment, 1="/", 2="/*", 3="/* *" -1="//" if (!stripOuterComment) return parse(source); int strip_state=1; // 0=no strip, 1=wait for /*, 2= wait for */ Object o=null; while (source.hasNext()) { char c=source.peek(); // handle // or /* comment if (comment_state==1) { switch (c) { case '/': comment_state=-1; break; case '*': comment_state=2; if (strip_state==1) { comment_state=0; strip_state=2; } } } // handle /* */ comment else if (comment_state>1) { switch (c) { case '*': comment_state=3; break; case '/': if (comment_state==3) { comment_state=0; if (strip_state==2) return o; } else comment_state=2; break; default: comment_state=2; } } // handle // comment else if (comment_state<0) { switch (c) { case '\r': case '\n': comment_state=0; default: break; } } // handle unknown else { if (!Character.isWhitespace(c)) { if (c=='/') comment_state=1; else if (c=='*') comment_state=3; else if (o==null) { o=parse(source); continue; } } } source.next(); } return o; } public Object parse(Source source) { int comment_state=0; // 0=no comment, 1="/", 2="/*", 3="/* *" -1="//" while (source.hasNext()) { char c=source.peek(); // handle // or /* comment if (comment_state==1) { switch (c) { case '/': comment_state=-1; break; case '*': comment_state=2; } } // handle /* */ comment else if (comment_state>1) { switch (c) { case '*': comment_state=3; break; case '/': if (comment_state==3) comment_state=0; else comment_state=2; break; default: comment_state=2; } } // handle // comment else if (comment_state<0) { switch (c) { case '\r': case '\n': comment_state=0; break; default: break; } } // handle unknown else { switch (c) { case '{': return parseObject(source); case '[': return parseArray(source); case '"': return parseString(source); case '-': return parseNumber(source); case 'n': complete("null",source); return null; case 't': complete("true",source); return Boolean.TRUE; case 'f': complete("false",source); return Boolean.FALSE; case 'u': complete("undefined",source); return null; case '/': comment_state=1; break; default: if (Character.isDigit(c)) return parseNumber(source); else if (Character.isWhitespace(c)) break; return handleUnknown(source, c); } } source.next(); } return null; } protected Object handleUnknown(Source source, char c) { throw new IllegalStateException("unknown char '"+c+"'("+(int)c+") in "+source); } protected Object parseObject(Source source) { if (source.next()!='{') throw new IllegalStateException(); Map map=newMap(); char next=seekTo("\"}",source); while (source.hasNext()) { if (next=='}') { source.next(); break; } String name=parseString(source); seekTo(':',source); source.next(); Object value=contextFor(name).parse(source); map.put(name,value); seekTo(",}",source); next=source.next(); if (next=='}') break; else next=seekTo("\"}",source); } String classname=(String)map.get("class"); if (classname!=null) { try { Class c=Loader.loadClass(JSON.class,classname); return convertTo(c,map); } catch (ClassNotFoundException e) { e.printStackTrace(); } } return map; } protected Object parseArray(Source source) { if (source.next()!='[') throw new IllegalStateException(); int size=0; ArrayList list=null; Object item=null; boolean coma=true; while (source.hasNext()) { char c=source.peek(); switch (c) { case ']': source.next(); switch(size) { case 0: return newArray(0); case 1: Object array = newArray(1); Array.set(array,0,item); return array; default: return list.toArray(newArray(list.size())); } case ',': if (coma) throw new IllegalStateException(); coma=true; source.next(); break; default: if (Character.isWhitespace(c)) source.next(); else { coma=false; if (size++==0) item=contextForArray().parse(source); else if (list==null) { list=new ArrayList(); list.add(item); item=contextForArray().parse(source); list.add(item); item=null; } else { item=contextForArray().parse(source); list.add(item); item=null; } } } } throw new IllegalStateException("unexpected end of array"); } protected String parseString(Source source) { if (source.next()!='"') throw new IllegalStateException(); boolean escape=false; StringBuffer b=null; final char[] scratch=source.scratchBuffer(); if (scratch!=null) { int i=0; while (source.hasNext()) { if(i>=scratch.length) { // we have filled the scratch buffer, so we must // use the StringBuffer for a large string b=new StringBuffer(scratch.length*2); b.append(scratch,0,i); break; } char c=source.next(); if (escape) { escape=false; switch (c) { case '"': scratch[i++]='"'; break; case '\\': scratch[i++]='\\'; break; case '/': scratch[i++]='/'; break; case 'b': scratch[i++]='\b'; break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -