EMMA Coverage Report (generated Wed Oct 26 13:47:17 EDT 2011)
[all classes][com.google.gson]

COVERAGE SUMMARY FOR SOURCE FILE [Streams.java]

nameclass, %method, %block, %line, %
Streams.java50%  (2/4)31%  (5/16)66%  (266/405)56%  (47.4/84)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class Streams$AppendableWriter0%   (0/1)0%   (0/6)0%   (0/39)0%   (0/12)
Streams$AppendableWriter (Appendable): void 0%   (0/1)0%   (0/11)0%   (0/4)
Streams$AppendableWriter (Appendable, Streams$1): void 0%   (0/1)0%   (0/4)0%   (0/1)
close (): void 0%   (0/1)0%   (0/1)0%   (0/1)
flush (): void 0%   (0/1)0%   (0/1)0%   (0/1)
write (char [], int, int): void 0%   (0/1)0%   (0/15)0%   (0/3)
write (int): void 0%   (0/1)0%   (0/7)0%   (0/2)
     
class Streams$AppendableWriter$CurrentWrite0%   (0/1)0%   (0/4)0%   (0/22)0%   (0/4)
Streams$AppendableWriter$CurrentWrite (): void 0%   (0/1)0%   (0/3)0%   (0/1)
charAt (int): char 0%   (0/1)0%   (0/5)0%   (0/1)
length (): int 0%   (0/1)0%   (0/4)0%   (0/1)
subSequence (int, int): CharSequence 0%   (0/1)0%   (0/10)0%   (0/1)
     
class Streams100% (1/1)80%  (4/5)75%  (201/269)69%  (47.6/69)
Streams (): void 0%   (0/1)0%   (0/3)0%   (0/2)
parse (JsonReader): JsonElement 100% (1/1)26%  (10/38)29%  (4/14)
writerForAppendable (Appendable): Writer 100% (1/1)58%  (7/12)58%  (0.6/1)
write (JsonElement, boolean, JsonWriter): void 100% (1/1)83%  (118/142)81%  (26/32)
parseRecursive (JsonReader): JsonElement 100% (1/1)89%  (66/74)85%  (17/20)
     
class Streams$1100% (1/1)100% (1/1)87%  (65/75)86%  (0.9/1)
<static initializer> 100% (1/1)87%  (65/75)86%  (0.9/1)

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 */
19package com.google.gson;
20 
21import com.google.gson.stream.JsonReader;
22import com.google.gson.stream.JsonWriter;
23import com.google.gson.stream.MalformedJsonException;
24import java.io.EOFException;
25import java.io.IOException;
26import java.io.Writer;
27import java.util.Map;
28 
29/**
30 * Reads and writes GSON parse trees over streams.
31 */
32final 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}

[all classes][com.google.gson]
EMMA 2.0.5312 (C) Vladimir Roubtsov