| 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 | */ |
| 16 | package com.google.gson; |
| 17 | |
| 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; |
| 24 | |
| 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; |
| 29 | |
| 30 | /** |
| 31 | * A streaming parser that allows reading of multiple {@link JsonElement}s from the specified reader |
| 32 | * asynchronously. |
| 33 | * |
| 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: |
| 37 | * |
| 38 | * <pre> |
| 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(); |
| 44 | * } |
| 45 | * } |
| 46 | * </pre> |
| 47 | * |
| 48 | * @author Inderjeet Singh |
| 49 | * @author Joel Leitch |
| 50 | * @since 1.4 |
| 51 | */ |
| 52 | public final class JsonStreamParser implements Iterator<JsonElement> { |
| 53 | private final JsonReader parser; |
| 54 | private final Object lock; |
| 55 | |
| 56 | /** |
| 57 | * @param json The string containing JSON elements concatenated to each other. |
| 58 | * @since 1.4 |
| 59 | */ |
| 60 | public JsonStreamParser(String json) { |
| 61 | this(new StringReader(json)); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * @param reader The data stream containing JSON elements concatenated to each other. |
| 66 | * @since 1.4 |
| 67 | */ |
| 68 | public JsonStreamParser(Reader reader) { |
| 69 | parser = new JsonReader(reader); |
| 70 | parser.setLenient(true); |
| 71 | lock = new Object(); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Returns the next available {@link JsonElement} on the reader. Null if none available. |
| 76 | * |
| 77 | * @return the next available {@link JsonElement} on the reader. Null if none available. |
| 78 | * @throws JsonParseException if the incoming stream is malformed JSON. |
| 79 | * @since 1.4 |
| 80 | */ |
| 81 | public JsonElement next() throws JsonParseException { |
| 82 | if (!hasNext()) { |
| 83 | throw new NoSuchElementException(); |
| 84 | } |
| 85 | |
| 86 | try { |
| 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; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | /** |
| 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 |
| 100 | * @since 1.4 |
| 101 | */ |
| 102 | public boolean hasNext() { |
| 103 | synchronized (lock) { |
| 104 | try { |
| 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); |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * This optional {@link Iterator} method is not relevant for stream parsing and hence is not |
| 116 | * implemented. |
| 117 | * @since 1.4 |
| 118 | */ |
| 119 | public void remove() { |
| 120 | throw new UnsupportedOperationException(); |
| 121 | } |
| 122 | } |