2 * Copyright (C) 2010 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.
17 package com
.google
.gson
.internal
;
19 import com
.google
.gson
.JsonElement
;
20 import com
.google
.gson
.JsonIOException
;
21 import com
.google
.gson
.JsonNull
;
22 import com
.google
.gson
.JsonParseException
;
23 import com
.google
.gson
.JsonSyntaxException
;
24 import com
.google
.gson
.internal
.bind
.TypeAdapters
;
25 import com
.google
.gson
.stream
.JsonReader
;
26 import com
.google
.gson
.stream
.JsonWriter
;
27 import com
.google
.gson
.stream
.MalformedJsonException
;
28 import java
.io
.EOFException
;
29 import java
.io
.IOException
;
30 import java
.io
.Writer
;
33 * Reads and writes GSON parse trees over streams.
35 public final class Streams
{
37 * Takes a reader in any state and returns the next value as a JsonElement.
39 public static JsonElement
parse(JsonReader reader
) throws JsonParseException
{
40 boolean isEmpty
= true;
44 return TypeAdapters
.JSON_ELEMENT
.read(reader
);
45 } catch (EOFException e
) {
47 * For compatibility with JSON 1.5 and earlier, we return a JsonNull for
48 * empty documents instead of throwing.
51 return JsonNull
.INSTANCE
;
53 // The stream ended prematurely so it is likely a syntax error.
54 throw new JsonSyntaxException(e
);
55 } catch (MalformedJsonException e
) {
56 throw new JsonSyntaxException(e
);
57 } catch (IOException e
) {
58 throw new JsonIOException(e
);
59 } catch (NumberFormatException e
) {
60 throw new JsonSyntaxException(e
);
65 * Writes the JSON element to the writer, recursively.
67 public static void write(JsonElement element
, JsonWriter writer
) throws IOException
{
68 TypeAdapters
.JSON_ELEMENT
.write(writer
, element
);
71 public static Writer
writerForAppendable(Appendable appendable
) {
72 return appendable
instanceof Writer ?
(Writer
) appendable
: new AppendableWriter(appendable
);
76 * Adapts an {@link Appendable} so it can be passed anywhere a {@link Writer}
79 private static final class AppendableWriter
extends Writer
{
80 private final Appendable appendable
;
81 private final CurrentWrite currentWrite
= new CurrentWrite();
83 private AppendableWriter(Appendable appendable
) {
84 this.appendable
= appendable
;
87 @Override public void write(char[] chars
, int offset
, int length
) throws IOException
{
88 currentWrite
.chars
= chars
;
89 appendable
.append(currentWrite
, offset
, offset
+ length
);
92 @Override public void write(int i
) throws IOException
{
93 appendable
.append((char) i
);
96 @Override public void flush() {}
97 @Override public void close() {}
100 * A mutable char sequence pointing at a single char[].
102 static class CurrentWrite
implements CharSequence
{
104 public int length() {
107 public char charAt(int i
) {
110 public CharSequence
subSequence(int start
, int end
) {
111 return new String(chars
, start
, end
- start
);