From fa40b4c63a57e92e87b3fb6079b01fd5304a01c1 Mon Sep 17 00:00:00 2001 From: Jesse Wilson Date: Thu, 2 Sep 2010 00:10:02 +0000 Subject: [PATCH] Rename GsonReader to Streams; check that class in. --- gson/src/main/java/com/google/gson/Gson.java | 2 +- .../main/java/com/google/gson/JsonParser.java | 2 +- .../com/google/gson/JsonStreamParser.java | 2 +- .../main/java/com/google/gson/Streams.java | 173 ++++++++++++++++++ 4 files changed, 176 insertions(+), 3 deletions(-) create mode 100644 gson/src/main/java/com/google/gson/Streams.java diff --git a/gson/src/main/java/com/google/gson/Gson.java b/gson/src/main/java/com/google/gson/Gson.java index 0857cda5..627108ca 100644 --- a/gson/src/main/java/com/google/gson/Gson.java +++ b/gson/src/main/java/com/google/gson/Gson.java @@ -443,7 +443,7 @@ public final class Gson { public T fromJson(Reader json, Type typeOfT) throws JsonParseException { JsonReader jsonReader = new JsonReader(json); jsonReader.setLenient(true); - JsonElement root = GsonReader.parse(jsonReader); + JsonElement root = Streams.parse(jsonReader); return (T) fromJson(root, typeOfT); } diff --git a/gson/src/main/java/com/google/gson/JsonParser.java b/gson/src/main/java/com/google/gson/JsonParser.java index 2771fdfd..d44495ef 100755 --- a/gson/src/main/java/com/google/gson/JsonParser.java +++ b/gson/src/main/java/com/google/gson/JsonParser.java @@ -53,7 +53,7 @@ public final class JsonParser { try { JsonReader jsonReader = new JsonReader(json); jsonReader.setLenient(true); - return GsonReader.parse(jsonReader); + return Streams.parse(jsonReader); } catch (StackOverflowError e) { throw new JsonParseException("Failed parsing JSON source: " + json + " to Json", e); } catch (OutOfMemoryError e) { diff --git a/gson/src/main/java/com/google/gson/JsonStreamParser.java b/gson/src/main/java/com/google/gson/JsonStreamParser.java index 7001d6a3..7a2ce3f8 100644 --- a/gson/src/main/java/com/google/gson/JsonStreamParser.java +++ b/gson/src/main/java/com/google/gson/JsonStreamParser.java @@ -82,7 +82,7 @@ public final class JsonStreamParser implements Iterator { } try { - return GsonReader.parse(parser); + return Streams.parse(parser); } catch (StackOverflowError e) { throw new JsonParseException("Failed parsing JSON source to Json", e); } catch (OutOfMemoryError e) { diff --git a/gson/src/main/java/com/google/gson/Streams.java b/gson/src/main/java/com/google/gson/Streams.java new file mode 100644 index 00000000..99d5a80a --- /dev/null +++ b/gson/src/main/java/com/google/gson/Streams.java @@ -0,0 +1,173 @@ +/* + * Copyright (C) 2010 Google Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.gson; + +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonWriter; +import java.io.EOFException; +import java.io.IOException; +import java.io.Writer; +import java.util.Map; + +/** + * Reads and writes GSON parse trees over streams. + */ +final class Streams { + + /** + * Takes a reader in any state and returns the next value as a JsonElement. + */ + static JsonElement parse(JsonReader reader) throws JsonParseException { + try { + switch (reader.peek()) { + case STRING: + return new JsonPrimitive(reader.nextString()); + case NUMBER: + String number = reader.nextString(); + return new JsonPrimitive(JsonPrimitive.stringToNumber(number)); + case BOOLEAN: + return new JsonPrimitive(reader.nextBoolean()); + case NULL: + reader.nextNull(); + return JsonNull.createJsonNull(); + case BEGIN_ARRAY: + JsonArray array = new JsonArray(); + reader.beginArray(); + while (reader.hasNext()) { + array.add(parse(reader)); + } + reader.endArray(); + return array; + case BEGIN_OBJECT: + JsonObject object = new JsonObject(); + reader.beginObject(); + while (reader.hasNext()) { + object.add(reader.nextName(), parse(reader)); + } + reader.endObject(); + return object; + case END_DOCUMENT: + case NAME: + case END_OBJECT: + case END_ARRAY: + default: + throw new IllegalArgumentException(); + } + } catch (IOException e) { + if (e instanceof EOFException) { + return JsonNull.createJsonNull(); + } + throw new JsonParseException(e); + } catch (NumberFormatException e) { + throw new JsonParseException(e); + } + } + + /** + * Writes the JSON element to the writer, recursively. + */ + static void write(JsonElement element, boolean serializeNulls, JsonWriter writer) + throws IOException { + if (element == null || element.isJsonNull()) { + if (serializeNulls) { + writer.nullValue(); + } + + } else if (element.isJsonPrimitive()) { + JsonPrimitive primitive = element.getAsJsonPrimitive(); + if (primitive.isNumber()) { + writer.value(primitive.getAsNumber()); + } else if (primitive.isBoolean()) { + writer.value(primitive.getAsBoolean()); + } else { + writer.value(primitive.getAsString()); + } + + } else if (element.isJsonArray()) { + writer.beginArray(); + for (JsonElement e : element.getAsJsonArray()) { + /* always print null when its parent element is an array! */ + if (e.isJsonNull()) { + writer.nullValue(); + continue; + } + write(e, serializeNulls, writer); + } + writer.endArray(); + + } else if (element.isJsonObject()) { + writer.beginObject(); + for (Map.Entry e : element.getAsJsonObject().entrySet()) { + JsonElement value = e.getValue(); + if (!serializeNulls && value.isJsonNull()) { + continue; + } + writer.name(e.getKey()); + write(value, serializeNulls, writer); + } + writer.endObject(); + + } else { + throw new IllegalArgumentException("Couldn't write " + element.getClass()); + } + } + + static Writer writerForAppendable(Appendable appendable) { + return appendable instanceof Writer ? (Writer) appendable : new AppendableWriter(appendable); + } + + /** + * Adapts an {@link Appendable} so it can be passed anywhere a {@link Writer} + * is used. + */ + private static class AppendableWriter extends Writer { + private final Appendable appendable; + private final CurrentWrite currentWrite = new CurrentWrite(); + + private AppendableWriter(Appendable appendable) { + this.appendable = appendable; + } + + @Override public void write(char[] chars, int offset, int length) throws IOException { + currentWrite.chars = chars; + appendable.append(currentWrite, offset, offset + length); + } + + @Override public void write(int i) throws IOException { + appendable.append((char) i); + } + + @Override public void flush() throws IOException {} + @Override public void close() throws IOException {} + + /** + * A mutable char sequence pointing at a single char[]. + */ + static class CurrentWrite implements CharSequence { + char[] chars; + public int length() { + return chars.length; + } + public char charAt(int i) { + return chars[i]; + } + public CharSequence subSequence(int start, int end) { + return new String(chars, start, end - start); + } + } + } +}