aboutsummaryrefslogtreecommitdiff
path: root/src/org/json/simple/parser
diff options
context:
space:
mode:
Diffstat (limited to 'src/org/json/simple/parser')
-rw-r--r--src/org/json/simple/parser/ContainerFactory.java26
-rw-r--r--src/org/json/simple/parser/ContentHandler.java110
-rw-r--r--src/org/json/simple/parser/JSONParser.java544
-rw-r--r--src/org/json/simple/parser/ParseException.java92
-rw-r--r--src/org/json/simple/parser/Yylex.java625
-rw-r--r--src/org/json/simple/parser/Yytoken.java60
6 files changed, 1457 insertions, 0 deletions
diff --git a/src/org/json/simple/parser/ContainerFactory.java b/src/org/json/simple/parser/ContainerFactory.java
new file mode 100644
index 0000000..182a899
--- /dev/null
+++ b/src/org/json/simple/parser/ContainerFactory.java
@@ -0,0 +1,26 @@
1package org.json.simple.parser;
2
3import java.util.List;
4import java.util.Map;
5
6/**
7 * Container factory for creating containers for JSON object and JSON array.
8 *
9 * @see org.json.simple.parser.JSONParser#parse(java.io.Reader,
10 * ContainerFactory)
11 *
12 * @author FangYidong<fangyidong@yahoo.com.cn>
13 */
14public interface ContainerFactory {
15 /**
16 * @return A Map instance to store JSON object, or null if you want to use
17 * org.json.simple.JSONObject.
18 */
19 Map<?, ?> createObjectContainer();
20
21 /**
22 * @return A List instance to store JSON array, or null if you want to use
23 * org.json.simple.JSONArray.
24 */
25 List<?> creatArrayContainer();
26}
diff --git a/src/org/json/simple/parser/ContentHandler.java b/src/org/json/simple/parser/ContentHandler.java
new file mode 100644
index 0000000..a704485
--- /dev/null
+++ b/src/org/json/simple/parser/ContentHandler.java
@@ -0,0 +1,110 @@
1package org.json.simple.parser;
2
3import java.io.IOException;
4
5/**
6 * A simplified and stoppable SAX-like content handler for stream processing of
7 * JSON text.
8 *
9 * @see org.xml.sax.ContentHandler
10 * @see org.json.simple.parser.JSONParser#parse(rejava.io.Reader,
11 * ContentHandler, boolean)
12 *
13 * @author FangYidong<fangyidong@yahoo.com.cn>
14 */
15public interface ContentHandler {
16 /**
17 * Receive notification of the beginning of JSON processing. The parser will
18 * invoke this method only once.
19 *
20 * @throws ParseException
21 * - JSONParser will stop and throw the same exception to the
22 * caller when receiving this exception.
23 */
24 void startJSON() throws ParseException, IOException;
25
26 /**
27 * Receive notification of the end of JSON processing.
28 *
29 * @throws ParseException
30 */
31 void endJSON() throws ParseException, IOException;
32
33 /**
34 * Receive notification of the beginning of a JSON object.
35 *
36 * @return false if the handler wants to stop parsing after return.
37 * @throws ParseException
38 * - JSONParser will stop and throw the same exception to the
39 * caller when receiving this exception.
40 * @see #endJSON
41 */
42 boolean startObject() throws ParseException, IOException;
43
44 /**
45 * Receive notification of the end of a JSON object.
46 *
47 * @return false if the handler wants to stop parsing after return.
48 * @throws ParseException
49 *
50 * @see #startObject
51 */
52 boolean endObject() throws ParseException, IOException;
53
54 /**
55 * Receive notification of the beginning of a JSON object entry.
56 *
57 * @param key
58 * - Key of a JSON object entry.
59 *
60 * @return false if the handler wants to stop parsing after return.
61 * @throws ParseException
62 *
63 * @see #endObjectEntry
64 */
65 boolean startObjectEntry(String key) throws ParseException, IOException;
66
67 /**
68 * Receive notification of the end of the value of previous object entry.
69 *
70 * @return false if the handler wants to stop parsing after return.
71 * @throws ParseException
72 *
73 * @see #startObjectEntry
74 */
75 boolean endObjectEntry() throws ParseException, IOException;
76
77 /**
78 * Receive notification of the beginning of a JSON array.
79 *
80 * @return false if the handler wants to stop parsing after return.
81 * @throws ParseException
82 *
83 * @see #endArray
84 */
85 boolean startArray() throws ParseException, IOException;
86
87 /**
88 * Receive notification of the end of a JSON array.
89 *
90 * @return false if the handler wants to stop parsing after return.
91 * @throws ParseException
92 *
93 * @see #startArray
94 */
95 boolean endArray() throws ParseException, IOException;
96
97 /**
98 * Receive notification of the JSON primitive values: java.lang.String,
99 * java.lang.Number, java.lang.Boolean null
100 *
101 * @param value
102 * - Instance of the following: java.lang.String,
103 * java.lang.Number, java.lang.Boolean null
104 *
105 * @return false if the handler wants to stop parsing after return.
106 * @throws ParseException
107 */
108 boolean primitive(Object value) throws ParseException, IOException;
109
110}
diff --git a/src/org/json/simple/parser/JSONParser.java b/src/org/json/simple/parser/JSONParser.java
new file mode 100644
index 0000000..4874eea
--- /dev/null
+++ b/src/org/json/simple/parser/JSONParser.java
@@ -0,0 +1,544 @@
1/*
2 * $Id: JSONParser.java,v 1.1 2006/04/15 14:10:48 platform Exp $
3 * Created on 2006-4-15
4 */
5package org.json.simple.parser;
6
7import java.io.IOException;
8import java.util.LinkedList;
9import java.util.List;
10import java.util.Map;
11
12import org.json.simple.JSONArray;
13import org.json.simple.JSONObject;
14
15import rejava.io.Reader;
16import rejava.io.StringReader;
17
18/**
19 * Parser for JSON text. Please note that JSONParser is NOT thread-safe.
20 *
21 * @author FangYidong<fangyidong@yahoo.com.cn>
22 */
23public class JSONParser {
24 public static final int S_INIT = 0;
25 public static final int S_IN_FINISHED_VALUE = 1;// string,number,boolean,null,object,array
26 public static final int S_IN_OBJECT = 2;
27 public static final int S_IN_ARRAY = 3;
28 public static final int S_PASSED_PAIR_KEY = 4;
29 public static final int S_IN_PAIR_VALUE = 5;
30 public static final int S_END = 6;
31 public static final int S_IN_ERROR = -1;
32
33 private LinkedList<?> handlerStatusStack;
34 private final Yylex lexer = new Yylex((Reader) null);
35 private Yytoken token = null;
36 private int status = JSONParser.S_INIT;
37
38 private int peekStatus(final LinkedList<?> statusStack) {
39 if (statusStack.size() == 0) {
40 return -1;
41 }
42 final Integer status = (Integer) statusStack.getFirst();
43 return status.intValue();
44 }
45
46 /**
47 * Reset the parser to the initial state without resetting the underlying
48 * reader.
49 *
50 */
51 public void reset() {
52 this.token = null;
53 this.status = JSONParser.S_INIT;
54 this.handlerStatusStack = null;
55 }
56
57 /**
58 * Reset the parser to the initial state with a new character reader.
59 *
60 * @param in
61 * - The new character reader.
62 * @throws IOException
63 * @throws ParseException
64 */
65 public void reset(final Reader in) {
66 this.lexer.yyreset(in);
67 this.reset();
68 }
69
70 /**
71 * @return The position of the beginning of the current token.
72 */
73 public int getPosition() {
74 return this.lexer.getPosition();
75 }
76
77 public Object parse(final String s) throws ParseException {