4 * License : The MIT License
5 * Copyright(c) 2009 olyutorskii
8 package jp.sourceforge.jindolf.json;
10 import java.io.IOException;
11 import java.io.Reader;
16 public final class Json{
23 throw new AssertionError();
28 * JSON最上位構造から文字出力を開始する。
30 * @param value JSONのObjectかArray
31 * @throws IOException 出力エラー
32 * @throws IllegalArgumentException 出力対象がObjectでもArrayでもない。
34 public static void writeJsonTop(Appendable appout, JsValue value)
36 IllegalArgumentException {
37 if( ! (value instanceof JsObject) && ! (value instanceof JsArray) ){
38 throw new IllegalArgumentException();
41 JsonAppender appender = new JsonAppender(appout);
44 value.traverse(appender);
45 }catch(JsVisitException e){
46 Throwable cause = e.getCause();
47 if(cause instanceof IOException){
48 throw (IOException) cause;
49 }else if(cause instanceof RuntimeException){
50 throw (RuntimeException) cause;
51 }else if(cause instanceof Error){
65 * JSON規格のwhitespace文字を判定する。
67 * @return whitespaceならtrue
69 public static boolean isWhitespace(char ch){
70 if(ch == '\t' ) return true;
71 if(ch == '\r' ) return true;
72 if(ch == '\n' ) return true;
73 if(ch == '\u0020') return true;
80 * @throws IOException 入力エラー
82 static void skipWhiteSpace(JsonReader reader)
85 int chData = reader.read();
86 if(chData < '\u0000') break;
87 if( ! isWhitespace((char)chData) ){
88 reader.unread(chData);
96 * 各種定数(true,false,null)を文字ストリームから読み取る。
97 * 長さ0の文字定数には無条件でfalseを返す。
100 * @return 文字定数が文字入力に現れればtrue。
101 * 見つからないもしくはストリームの終わりに達したときはfalse
102 * @throws IOException 入力エラー
103 * @throws IllegalArgumentException 文字定数が長すぎる
105 static boolean parseConst(JsonReader reader,
108 IllegalArgumentException {
109 int textLength = text.length();
110 if(textLength <= 0) return false;
111 if(textLength >= JsonReader.PUSHBACK_TOKENS){
112 throw new IllegalArgumentException();
115 int[] backData = new int[textLength - 1];
119 int chData = reader.read();
120 if(chData != text.charAt(readed)){
121 if(chData >= '\u0000') reader.unread(chData);
122 for(int pos = readed - 1; pos >= 0; pos--){
123 reader.unread(backData[pos]);
128 if(readed >= backData.length) return true;
130 backData[readed++] = chData;
137 * JSONの各種Valueを文字ストリームから読み取る。
140 * @throws IOException 入力エラー
141 * @throws JsParseException パースエラー
143 public static JsValue parseValue(Reader reader)
144 throws IOException, JsParseException{
146 if(reader instanceof JsonReader){
147 jsreader = (JsonReader) reader;
149 jsreader = new JsonReader(reader);
152 return parseValue(jsreader);
156 * JSONの各種Valueを文字ストリームから読み取る。
158 * @return 各種Value。ストリームの終わりに達したときはnull
159 * @throws IOException 入力エラー
160 * @throws JsParseException パースエラー
162 static JsValue parseValue(JsonReader reader)
163 throws IOException, JsParseException{
164 skipWhiteSpace(reader);
166 if(parseConst(reader, JsNull.NULL.toString())){
168 }else if(parseConst(reader, JsBoolean.TRUE.toString())){
169 return JsBoolean.TRUE;
170 }else if(parseConst(reader, JsBoolean.FALSE.toString())){
171 return JsBoolean.FALSE;
174 int head = reader.read();
175 if(head < '\u0000') return null;
177 if( head == '-' || ('0' <= head && head <= '9') ){
179 return JsNumber.parseNumber(reader);
180 }else if(head == '{'){
182 return JsObject.parseObject(reader);
183 }else if(head == '['){
185 return JsArray.parseArray(reader);
186 }else if(head == '"'){
188 return JsString.parseString(reader);
191 throw new JsParseException();