| 1 | /** |
| 2 | * Licensed to jclouds, Inc. (jclouds) under one or more |
| 3 | * contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. jclouds licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package com.google.gson; |
| 20 | |
| 21 | import com.google.gson.stream.JsonReader; |
| 22 | import com.google.gson.stream.JsonWriter; |
| 23 | import com.google.gson.stream.MalformedJsonException; |
| 24 | import java.io.EOFException; |
| 25 | import java.io.IOException; |
| 26 | import java.io.Writer; |
| 27 | import java.util.Map; |
| 28 | |
| 29 | /** |
| 30 | * Reads and writes GSON parse trees over streams. |
| 31 | */ |
| 32 | final class Streams { |
| 33 | |
| 34 | /** |
| 35 | * Takes a reader in any state and returns the next value as a JsonElement. |
| 36 | */ |
| 37 | static JsonElement parse(JsonReader reader) throws JsonParseException { |
| 38 | boolean isEmpty = true; |
| 39 | try { |
| 40 | reader.peek(); |
| 41 | isEmpty = false; |
| 42 | return parseRecursive(reader); |
| 43 | } catch (EOFException e) { |
| 44 | /* |
| 45 | * For compatibility with JSON 1.5 and earlier, we return a JsonNull for |
| 46 | * empty documents instead of throwing. |
| 47 | */ |
| 48 | if (isEmpty) { |
| 49 | return JsonNull.createJsonNull(); |
| 50 | } |
| 51 | throw new JsonIOException(e); |
| 52 | } catch (MalformedJsonException e) { |
| 53 | throw new JsonSyntaxException(e); |
| 54 | } catch (IOException e) { |
| 55 | throw new JsonIOException(e); |
| 56 | } catch (NumberFormatException e) { |
| 57 | throw new JsonSyntaxException(e); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | private static JsonElement parseRecursive(JsonReader reader) throws IOException { |
| 62 | switch (reader.peek()) { |
| 63 | case STRING: |
| 64 | return new JsonPrimitive(reader.nextString()); |
| 65 | case NUMBER: |
| 66 | String number = reader.nextString(); |
| 67 | return new JsonPrimitive(JsonPrimitive.stringToNumber(number)); |
| 68 | case BOOLEAN: |
| 69 | return new JsonPrimitive(reader.nextBoolean()); |
| 70 | case NULL: |
| 71 | reader.nextNull(); |
| 72 | return JsonNull.createJsonNull(); |
| 73 | case BEGIN_ARRAY: |
| 74 | JsonArray array = new JsonArray(); |
| 75 | reader.beginArray(); |
| 76 | while (reader.hasNext()) { |
| 77 | array.add(parseRecursive(reader)); |
| 78 | } |
| 79 | reader.endArray(); |
| 80 | return array; |
| 81 | case BEGIN_OBJECT: |
| 82 | JsonObject object = new JsonObject(); |
| 83 | reader.beginObject(); |
| 84 | while (reader.hasNext()) { |
| 85 | object.add(reader.nextName(), parseRecursive(reader)); |
| 86 | } |
| 87 | reader.endObject(); |
| 88 | return object; |
| 89 | case END_DOCUMENT: |
| 90 | case NAME: |
| 91 | case END_OBJECT: |
| 92 | case END_ARRAY: |
| 93 | default: |
| 94 | throw new IllegalArgumentException(); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Writes the JSON element to the writer, recursively. |
| 100 | */ |
| 101 | static void write(JsonElement element, boolean serializeNulls, JsonWriter writer) |
| 102 | throws IOException { |
| 103 | if (element == null || element.isJsonNull()) { |
| 104 | if (serializeNulls) { |
| 105 | writer.nullValue(); |
| 106 | } |
| 107 | //BEGIN JCLOUDS PATCH |
| 108 | // * @see <a href="http://code.google.com/p/google-gson/issues/detail?id=326"/> |
| 109 | } else if (element instanceof JsonLiteral ) { |
| 110 | writer.value(JsonLiteral.class.cast(element)); |
| 111 | //END JCLOUDS PATCH |
| 112 | } else if (element.isJsonPrimitive()) { |
| 113 | JsonPrimitive primitive = element.getAsJsonPrimitive(); |
| 114 | if (primitive.isNumber()) { |
| 115 | writer.value(primitive.getAsNumber()); |
| 116 | } else if (primitive.isBoolean()) { |
| 117 | writer.value(primitive.getAsBoolean()); |
| 118 | } else { |
| 119 | writer.value(primitive.getAsString()); |
| 120 | } |
| 121 | |
| 122 | } else if (element.isJsonArray()) { |
| 123 | writer.beginArray(); |
| 124 | for (JsonElement e : element.getAsJsonArray()) { |
| 125 | /* always print null when its parent element is an array! */ |
| 126 | if (e.isJsonNull()) { |
| 127 | writer.nullValue(); |
| 128 | continue; |
| 129 | } |
| 130 | write(e, serializeNulls, writer); |
| 131 | } |
| 132 | writer.endArray(); |
| 133 | |
| 134 | } else if (element.isJsonObject()) { |
| 135 | writer.beginObject(); |
| 136 | for (Map.Entry<String, JsonElement> e : element.getAsJsonObject().entrySet()) { |
| 137 | JsonElement value = e.getValue(); |
| 138 | if (!serializeNulls && value.isJsonNull()) { |
| 139 | continue; |
| 140 | } |
| 141 | writer.name(e.getKey()); |
| 142 | write(value, serializeNulls, writer); |
| 143 | } |
| 144 | writer.endObject(); |
| 145 | |
| 146 | } else { |
| 147 | throw new IllegalArgumentException("Couldn't write " + element.getClass()); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | static Writer writerForAppendable(Appendable appendable) { |
| 152 | return appendable instanceof Writer ? (Writer) appendable : new AppendableWriter(appendable); |
| 153 | } |
| 154 | |
| 155 | /** |
| 156 | * Adapts an {@link Appendable} so it can be passed anywhere a {@link Writer} |
| 157 | * is used. |
| 158 | */ |
| 159 | private static class AppendableWriter extends Writer { |
| 160 | private final Appendable appendable; |
| 161 | private final CurrentWrite currentWrite = new CurrentWrite(); |
| 162 | |
| 163 | private AppendableWriter(Appendable appendable) { |
| 164 | this.appendable = appendable; |
| 165 | } |
| 166 | |
| 167 | @Override public void write(char[] chars, int offset, int length) throws IOException { |
| 168 | currentWrite.chars = chars; |
| 169 | appendable.append(currentWrite, offset, offset + length); |
| 170 | } |
| 171 | |
| 172 | @Override public void write(int i) throws IOException { |
| 173 | appendable.append((char) i); |
| 174 | } |
| 175 | |
| 176 | @Override public void flush() {} |
| 177 | @Override public void close() {} |
| 178 | |
| 179 | /** |
| 180 | * A mutable char sequence pointing at a single char[]. |
| 181 | */ |
| 182 | static class CurrentWrite implements CharSequence { |
| 183 | char[] chars; |
| 184 | public int length() { |
| 185 | return chars.length; |
| 186 | } |
| 187 | public char charAt(int i) { |
| 188 | return chars[i]; |
| 189 | } |
| 190 | public CharSequence subSequence(int start, int end) { |
| 191 | return new String(chars, start, end - start); |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | } |