]>
Commit | Line | Data |
---|---|---|
1 | /*\r | |
2 | * Copyright (C) 2009 Google Inc.\r | |
3 | *\r | |
4 | * Licensed under the Apache License, Version 2.0 (the "License");\r | |
5 | * you may not use this file except in compliance with the License.\r | |
6 | * You may obtain a copy of the License at\r | |
7 | *\r | |
8 | * http://www.apache.org/licenses/LICENSE-2.0\r | |
9 | *\r | |
10 | * Unless required by applicable law or agreed to in writing, software\r | |
11 | * distributed under the License is distributed on an "AS IS" BASIS,\r | |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r | |
13 | * See the License for the specific language governing permissions and\r | |
14 | * limitations under the License.\r | |
15 | */\r | |
16 | package com.google.gson;\r | |
17 | \r | |
18 | import java.io.IOException;\r | |
19 | import java.io.Reader;\r | |
20 | import java.io.StringReader;\r | |
21 | \r | |
22 | import com.google.gson.internal.Streams;\r | |
23 | import com.google.gson.stream.JsonReader;\r | |
24 | import com.google.gson.stream.JsonToken;\r | |
25 | import com.google.gson.stream.MalformedJsonException;\r | |
26 | \r | |
27 | /**\r | |
28 | * A parser to parse Json into a parse tree of {@link JsonElement}s\r | |
29 | *\r | |
30 | * @author Inderjeet Singh\r | |
31 | * @author Joel Leitch\r | |
32 | * @since 1.3\r | |
33 | */\r | |
34 | public final class JsonParser {\r | |
35 | \r | |
36 | /**\r | |
37 | * Parses the specified JSON string into a parse tree\r | |
38 | *\r | |
39 | * @param json JSON text\r | |
40 | * @return a parse tree of {@link JsonElement}s corresponding to the specified JSON\r | |
41 | * @throws JsonParseException if the specified text is not valid JSON\r | |
42 | * @since 1.3\r | |
43 | */\r | |
44 | public JsonElement parse(String json) throws JsonSyntaxException {\r | |
45 | return parse(new StringReader(json));\r | |
46 | }\r | |
47 | \r | |
48 | /**\r | |
49 | * Parses the specified JSON string into a parse tree\r | |
50 | *\r | |
51 | * @param json JSON text\r | |
52 | * @return a parse tree of {@link JsonElement}s corresponding to the specified JSON\r | |
53 | * @throws JsonParseException if the specified text is not valid JSON\r | |
54 | * @since 1.3\r | |
55 | */\r | |
56 | public JsonElement parse(Reader json) throws JsonIOException, JsonSyntaxException {\r | |
57 | try {\r | |
58 | JsonReader jsonReader = new JsonReader(json);\r | |
59 | JsonElement element = parse(jsonReader);\r | |
60 | if (!element.isJsonNull() && jsonReader.peek() != JsonToken.END_DOCUMENT) {\r | |
61 | throw new JsonSyntaxException("Did not consume the entire document.");\r | |
62 | }\r | |
63 | return element;\r | |
64 | } catch (MalformedJsonException e) {\r | |
65 | throw new JsonSyntaxException(e);\r | |
66 | } catch (IOException e) {\r | |
67 | throw new JsonIOException(e);\r | |
68 | } catch (NumberFormatException e) {\r | |
69 | throw new JsonSyntaxException(e);\r | |
70 | }\r | |
71 | }\r | |
72 | \r | |
73 | /**\r | |
74 | * Returns the next value from the JSON stream as a parse tree.\r | |
75 | *\r | |
76 | * @throws JsonParseException if there is an IOException or if the specified\r | |
77 | * text is not valid JSON\r | |
78 | * @since 1.6\r | |
79 | */\r | |
80 | public JsonElement parse(JsonReader json) throws JsonIOException, JsonSyntaxException {\r | |
81 | boolean lenient = json.isLenient();\r | |
82 | json.setLenient(true);\r | |
83 | try {\r | |
84 | return Streams.parse(json);\r | |
85 | } catch (StackOverflowError e) {\r | |
86 | throw new JsonParseException("Failed parsing JSON source: " + json + " to Json", e);\r | |
87 | } catch (OutOfMemoryError e) {\r | |
88 | throw new JsonParseException("Failed parsing JSON source: " + json + " to Json", e);\r | |
89 | } finally {\r | |
90 | json.setLenient(lenient);\r | |
91 | }\r | |
92 | }\r | |
93 | }\r |