]>
iEval git - unical.git/blob - src/com/google/gson/JsonParser.java
2 * Copyright (C) 2009 Google Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com
.google
.gson
;
18 import java
.io
.IOException
;
19 import java
.io
.Reader
;
20 import java
.io
.StringReader
;
22 import com
.google
.gson
.internal
.Streams
;
23 import com
.google
.gson
.stream
.JsonReader
;
24 import com
.google
.gson
.stream
.JsonToken
;
25 import com
.google
.gson
.stream
.MalformedJsonException
;
28 * A parser to parse Json into a parse tree of {@link JsonElement}s
30 * @author Inderjeet Singh
34 public final class JsonParser
{
37 * Parses the specified JSON string into a parse tree
39 * @param json JSON text
40 * @return a parse tree of {@link JsonElement}s corresponding to the specified JSON
41 * @throws JsonParseException if the specified text is not valid JSON
44 public JsonElement
parse(String json
) throws JsonSyntaxException
{
45 return parse(new StringReader(json
));
49 * Parses the specified JSON string into a parse tree
51 * @param json JSON text
52 * @return a parse tree of {@link JsonElement}s corresponding to the specified JSON
53 * @throws JsonParseException if the specified text is not valid JSON
56 public JsonElement
parse(Reader json
) throws JsonIOException
, JsonSyntaxException
{
58 JsonReader jsonReader
= new JsonReader(json
);
59 JsonElement element
= parse(jsonReader
);
60 if (!element
.isJsonNull() && jsonReader
.peek() != JsonToken
.END_DOCUMENT
) {
61 throw new JsonSyntaxException("Did not consume the entire document.");
64 } catch (MalformedJsonException e
) {
65 throw new JsonSyntaxException(e
);
66 } catch (IOException e
) {
67 throw new JsonIOException(e
);
68 } catch (NumberFormatException e
) {
69 throw new JsonSyntaxException(e
);
74 * Returns the next value from the JSON stream as a parse tree.
76 * @throws JsonParseException if there is an IOException or if the specified
77 * text is not valid JSON
80 public JsonElement
parse(JsonReader json
) throws JsonIOException
, JsonSyntaxException
{
81 boolean lenient
= json
.isLenient();
82 json
.setLenient(true);
84 return Streams
.parse(json
);
85 } catch (StackOverflowError e
) {
86 throw new JsonParseException("Failed parsing JSON source: " + json
+ " to Json", e
);
87 } catch (OutOfMemoryError e
) {
88 throw new JsonParseException("Failed parsing JSON source: " + json
+ " to Json", e
);
90 json
.setLenient(lenient
);
This page took 0.046983 seconds and 4 git commands to generate.