OSDN Git Service

Merge release/v1.101.104
[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      * @param appout 出力先
33      * @param topValue OBJECT型かARRAY型のValue
34      * @throws NullPointerException 引数がnull
35      * @throws JsVisitException 何らかの理由で処理中断
36      * @throws IOException 出力エラー
37      */
38     public static void dumpJson(Appendable appout, JsComposition<?> topValue)
39             throws NullPointerException,
40                    JsVisitException,
41                    IOException {
42         if(appout == null || topValue == null){
43             throw new NullPointerException();
44         }
45
46         JsonAppender appender = new JsonAppender(appout);
47
48         try{
49             topValue.traverse(appender);
50         }catch(JsVisitException e){
51             assert appender.hasIOException();
52             throw appender.getIOException();
53         }
54
55         return;
56     }
57
58     /**
59      * JSONの各種Valueを文字ソースから読み取る。
60      * @param source 文字入力
61      * @return 各種Value。
62      * 0個以上連続するホワイトスペースと共にソースの終わりに達したときはnull
63      * @throws IOException 入力エラー
64      * @throws JsParseException パースエラー
65      */
66     static JsValue parseValue(JsonSource source)
67             throws IOException, JsParseException{
68         source.skipWhiteSpace();
69         if( ! source.hasMore() ) return null;
70
71         JsValue result;
72         result = JsObject .parseObject (source);
73         if(result != null) return result;
74         result = JsArray  .parseArray  (source);
75         if(result != null) return result;
76         result = JsString .parseString (source);
77         if(result != null) return result;
78         result = JsNull   .parseNull   (source);
79         if(result != null) return result;
80         result = JsBoolean.parseBoolean(source);
81         if(result != null) return result;
82         result = JsNumber .parseNumber (source);
83
84         if(result == null){
85             throw new JsParseException(JsParseException.ERRMSG_INVALIDTOKEN,
86                                        source.getLineNumber() );
87         }
88
89         return result;
90     }
91
92     /**
93      * JSONの最上位構造を文字ソースから読み取る。
94      * @param source 文字入力ソース
95      * @return JSON最上位構造。OBJECT型かARRAY型のいずれか。
96      * 入力が0個以上のホワイトスペースのみで埋められていた場合はnull。
97      * @throws IOException 入力エラー
98      * @throws JsParseException パースエラー
99      */
100     private static JsComposition<?> parseJson(JsonSource source)
101             throws IOException, JsParseException{
102         JsValue topValue = parseValue(source);
103         if(topValue == null) return null;
104
105         if( ! (topValue instanceof JsComposition) ){
106             throw new JsParseException(JsParseException.ERRMSG_INVALIDROOT,
107                                        source.getLineNumber() );
108         }
109         JsComposition<?> result = (JsComposition) topValue;
110
111         return result;
112     }
113
114     /**
115      * JSONの最上位構造を文字リーダから読み取る。
116      * @param source 文字入力リーダ
117      * @return JSON最上位構造。OBJECT型かARRAY型のいずれか。
118      * 入力が0個以上のホワイトスペースのみで埋められていた場合はnull。
119      * @throws IOException 入力エラー
120      * @throws JsParseException パースエラー
121      */
122     public static JsComposition<?> parseJson(Reader source)
123             throws IOException, JsParseException{
124         JsonSource jsonSource = new JsonSource(source);
125         return parseJson(jsonSource);
126     }
127
128 }