Remove trailing whitespace
[unical.git] / gson / com / google / gson / JsonParser.java
CommitLineData
070d3ab2
MG
1/*
2 * Copyright (C) 2009 Google Inc.
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16package com.google.gson;
17
18import java.io.IOException;
19import java.io.Reader;
20import java.io.StringReader;
21
22import com.google.gson.internal.Streams;
23import com.google.gson.stream.JsonReader;
24import com.google.gson.stream.JsonToken;
25import com.google.gson.stream.MalformedJsonException;
26
27/**
28 * A parser to parse Json into a parse tree of {@link JsonElement}s
29 *
30 * @author Inderjeet Singh
31 * @author Joel Leitch
32 * @since 1.3
33 */
34public final class JsonParser {
35
36 /**
37 * Parses the specified JSON string into a parse tree
38 *
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
42 * @since 1.3
43 */
44 public JsonElement parse(String json) throws JsonSyntaxException {
45 return parse(new StringReader(json));
46 }
47
48 /**
49 * Parses the specified JSON string into a parse tree
50 *
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
54 * @since 1.3
55 */
56 public JsonElement parse(Reader json) throws JsonIOException, JsonSyntaxException {
57 try {
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.");
62 }
63 return element;
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);
70 }
71 }
72
73 /**
74 * Returns the next value from the JSON stream as a parse tree.
75 *
76 * @throws JsonParseException if there is an IOException or if the specified
77 * text is not valid JSON
78 * @since 1.6
79 */
80 public JsonElement parse(JsonReader json) throws JsonIOException, JsonSyntaxException {
81 boolean lenient = json.isLenient();
82 json.setLenient(true);
83 try {
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);
89 } finally {
90 json.setLenient(lenient);
91 }
92 }
93}
This page took 0.014124 seconds and 4 git commands to generate.