]>
iEval git - unical.git/blob - gson/com/google/gson/JsonStreamParser.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
.EOFException
;
19 import java
.io
.IOException
;
20 import java
.io
.Reader
;
21 import java
.io
.StringReader
;
22 import java
.util
.Iterator
;
23 import java
.util
.NoSuchElementException
;
25 import com
.google
.gson
.internal
.Streams
;
26 import com
.google
.gson
.stream
.JsonReader
;
27 import com
.google
.gson
.stream
.JsonToken
;
28 import com
.google
.gson
.stream
.MalformedJsonException
;
31 * A streaming parser that allows reading of multiple {@link JsonElement}s from the specified reader
34 * <p>This class is conditionally thread-safe (see Item 70, Effective Java second edition). To
35 * properly use this class across multiple threads, you will need to add some external
36 * synchronization. For example:
39 * JsonStreamParser parser = new JsonStreamParser("['first'] {'second':10} 'third'");
40 * JsonElement element;
41 * synchronized (parser) { // synchronize on an object shared by threads
42 * if (parser.hasNext()) {
43 * element = parser.next();
48 * @author Inderjeet Singh
52 public final class JsonStreamParser
implements Iterator
<JsonElement
> {
53 private final JsonReader parser
;
54 private final Object lock
;
57 * @param json The string containing JSON elements concatenated to each other.
60 public JsonStreamParser(String json
) {
61 this(new StringReader(json
));
65 * @param reader The data stream containing JSON elements concatenated to each other.
68 public JsonStreamParser(Reader reader
) {
69 parser
= new JsonReader(reader
);
70 parser
.setLenient(true);
75 * Returns the next available {@link JsonElement} on the reader. Null if none available.
77 * @return the next available {@link JsonElement} on the reader. Null if none available.
78 * @throws JsonParseException if the incoming stream is malformed JSON.
81 public JsonElement
next() throws JsonParseException
{
83 throw new NoSuchElementException();
87 return Streams
.parse(parser
);
88 } catch (StackOverflowError e
) {
89 throw new JsonParseException("Failed parsing JSON source to Json", e
);
90 } catch (OutOfMemoryError e
) {
91 throw new JsonParseException("Failed parsing JSON source to Json", e
);
92 } catch (JsonParseException e
) {
93 throw e
.getCause() instanceof EOFException ?
new NoSuchElementException() : e
;
98 * Returns true if a {@link JsonElement} is available on the input for consumption
99 * @return true if a {@link JsonElement} is available on the input, false otherwise
102 public boolean hasNext() {
103 synchronized (lock
) {
105 return parser
.peek() != JsonToken
.END_DOCUMENT
;
106 } catch (MalformedJsonException e
) {
107 throw new JsonSyntaxException(e
);
108 } catch (IOException e
) {
109 throw new JsonIOException(e
);
115 * This optional {@link Iterator} method is not relevant for stream parsing and hence is not
119 public void remove() {
120 throw new UnsupportedOperationException();
This page took 0.046556 seconds and 4 git commands to generate.