OSDN Git Service

start v1.101.107-SNAPSHOT
[jovsonz/Jovsonz.git] / src / main / java / jp / sourceforge / jovsonz / Json.java
1 /*
2  * JSON utilities
3  *
4  * License : The MIT License
5  * Copyright(c) 2009 olyutorskii
6  */
7
8 package jp.sourceforge.jovsonz;
9
10 import java.io.IOException;
11 import java.io.Reader;
12
13 /**
14  * JSON各種共通ユーティリティ。
15  */
16 public final class Json {
17
18     /** MIME タイプ。 */
19     public static final String MIME_TYPE = "application/json";
20
21
22     /**
23      * 隠しコンストラクタ。
24      */
25     private Json(){
26         assert false;
27         throw new AssertionError();
28     }
29
30     /**
31      * JSON最上位構造から文字出力を開始する。
32      *
33      * @param appout 出力先
34      * @param topValue OBJECT型かARRAY型のValue
35      * @throws NullPointerException 引数がnull
36      * @throws JsVisitException 何らかの理由で処理中断
37      * @throws IOException 出力エラー
38      */
39     public static void dumpJson(Appendable appout, JsComposition<?> topValue)
40             throws NullPointerException,
41                    JsVisitException,
42                    IOException {
43         if(appout == null || topValue == null){
44             throw new NullPointerException();
45         }
46
47         JsonAppender appender = new JsonAppender(appout);
48
49         try{
50             topValue.traverse(appender);
51         }catch(JsVisitException e){
52             assert appender.hasIOException();
53             throw appender.getIOException();
54         }
55
56         return;
57     }
58
59     /**
60      * JSONの各種Valueを文字ソースから読み取る。
61      *
62      * @param source 文字入力
63      * @return 各種Value。
64      *  0個以上連続するホワイトスペースと共にソースの終わりに達したときはnull
65      * @throws IOException 入力エラー
66      * @throws JsParseException パースエラー
67      */
68     static JsValue parseValue(JsonSource source)
69             throws IOException, JsParseException{
70         source.skipWhiteSpace();
71         if( ! source.hasMore() ) return null;
72
73         JsValue result;
74         result = JsObject .parseObject (source);
75         if(result != null) return result;
76         result = JsArray  .parseArray  (source);
77         if(result != null) return result;
78         result = JsString .parseString (source);
79         if(result != null) return result;
80         result = JsNull   .parseNull   (source);
81         if(result != null) return result;
82         result = JsBoolean.parseBoolean(source);
83         if(result != null) return result;
84         result = JsNumber .parseNumber (source);
85
86         if(result == null){
87             throw new JsParseException(JsParseException.ERRMSG_INVALIDTOKEN,
88                                        source.getLineNumber() );
89         }
90
91         return result;
92     }
93
94     /**
95      * JSONの最上位構造を文字ソースから読み取る。
96      *
97      * @param source 文字入力ソース
98      * @return JSON最上位構造。OBJECT型かARRAY型のいずれか。
99      *  入力が0個以上のホワイトスペースのみで埋められていた場合はnull。
100      * @throws IOException 入力エラー
101      * @throws JsParseException パースエラー
102      */
103     private static JsComposition<?> parseJson(JsonSource source)
104             throws IOException, JsParseException{
105         JsValue topValue = parseValue(source);
106         if(topValue == null) return null;
107
108         if( ! (topValue instanceof JsComposition) ){
109             throw new JsParseException(JsParseException.ERRMSG_INVALIDROOT,
110                                        source.getLineNumber() );
111         }
112         JsComposition<?> result = (JsComposition) topValue;
113
114         return result;
115     }
116
117     /**
118      * JSONの最上位構造を文字リーダから読み取る。
119      *
120      * @param source 文字入力リーダ
121      * @return JSON最上位構造。OBJECT型かARRAY型のいずれか。
122      *  入力が0個以上のホワイトスペースのみで埋められていた場合はnull。
123      * @throws IOException 入力エラー
124      * @throws JsParseException パースエラー
125      */
126     public static JsComposition<?> parseJson(Reader source)
127             throws IOException, JsParseException{
128         JsonSource jsonSource = new JsonSource(source);
129         return parseJson(jsonSource);
130     }
131
132 }