1 package net.minidev.json.parser;
2
3 /*
4 * Copyright 2011 JSON-SMART authors
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 import static net.minidev.json.parser.ParseException.ERROR_UNEXPECTED_EOF;
19 import net.minidev.json.JSONValue;
20 import net.minidev.json.writer.JsonReaderI;
21
22 /**
23 * Parser for JSON text. Please note that JSONParser is NOT thread-safe.
24 *
25 * @author Uriel Chemouni <uchemouni@gmail.com>
26 */
27 class JSONParserString extends JSONParserMemory {
28 private String in;
29
30 public JSONParserString(int permissiveMode) {
31 super(permissiveMode);
32 }
33
34 /**
35 * use to return Primitive Type, or String, Or JsonObject or JsonArray
36 * generated by a ContainerFactory
37 */
38 public Object parse(String in) throws ParseException {
39 return parse(in, JSONValue.defaultReader.DEFAULT);
40 }
41
42 //
43 //
44 //
45 //
46 //
47 //
48 //
49
50 /**
51 * use to return Primitive Type, or String, Or JsonObject or JsonArray
52 * generated by a ContainerFactory
53 */
54 public <T> T parse(String in, JsonReaderI<T> mapper) throws ParseException {
55 this.base = mapper.base;
56 this.in = in;
57 this.len = in.length();
58 return parse(mapper);
59 }
60
61 protected void extractString(int beginIndex, int endIndex) {
62 xs = in.substring(beginIndex, endIndex);
63 }
64
65 protected void extractStringTrim(int start, int stop) {
66 while (start < stop-1 && Character.isWhitespace(in.charAt(start))) {
67 start++;
68 }
69 while (stop-1 > start && Character.isWhitespace(in.charAt(stop-1))) {
70 stop--;
71 }
72 extractString(start, stop);
73 }
74
75 protected int indexOf(char c, int pos) {
76 return in.indexOf(c, pos);
77 }
78
79 /**
80 * Read next char or END OF INPUT
81 */
82 protected void read() {
83 if (++pos >= len)
84 this.c = EOI;
85 else
86 this.c = in.charAt(pos);
87 }
88
89 /**
90 * Same as read() in memory parsing
91 */
92 protected void readS() {
93 if (++pos >= len)
94 this.c = EOI;
95 else
96 this.c = in.charAt(pos);
97 }
98
99 /**
100 * read data can not be EOI
101 */
102 protected void readNoEnd() throws ParseException {
103 if (++pos >= len) {
104 this.c = EOI;
105 throw new ParseException(pos - 1, ERROR_UNEXPECTED_EOF, "EOF");
106 } else
107 this.c = in.charAt(pos);
108 }
109 }
110