A new streaming JSON API.
Change-Id: Iefa7ee44ddacbe22ea9ebca3165ac59030659bf2
This commit is contained in:
392
api/current.xml
392
api/current.xml
@@ -180176,6 +180176,398 @@
|
||||
</parameter>
|
||||
</method>
|
||||
</class>
|
||||
<class name="JsonReader"
|
||||
extends="java.lang.Object"
|
||||
abstract="false"
|
||||
static="false"
|
||||
final="true"
|
||||
deprecated="not deprecated"
|
||||
visibility="public"
|
||||
>
|
||||
<implements name="java.io.Closeable">
|
||||
</implements>
|
||||
<constructor name="JsonReader"
|
||||
type="android.util.JsonReader"
|
||||
static="false"
|
||||
final="false"
|
||||
deprecated="not deprecated"
|
||||
visibility="public"
|
||||
>
|
||||
<parameter name="in" type="java.io.Reader">
|
||||
</parameter>
|
||||
</constructor>
|
||||
<method name="booleanValue"
|
||||
return="boolean"
|
||||
abstract="false"
|
||||
native="false"
|
||||
synchronized="false"
|
||||
static="false"
|
||||
final="false"
|
||||
deprecated="not deprecated"
|
||||
visibility="public"
|
||||
>
|
||||
</method>
|
||||
<method name="close"
|
||||
return="void"
|
||||
abstract="false"
|
||||
native="false"
|
||||
synchronized="false"
|
||||
static="false"
|
||||
final="false"
|
||||
deprecated="not deprecated"
|
||||
visibility="public"
|
||||
>
|
||||
<exception name="IOException" type="java.io.IOException">
|
||||
</exception>
|
||||
</method>
|
||||
<method name="doubleValue"
|
||||
return="double"
|
||||
abstract="false"
|
||||
native="false"
|
||||
synchronized="false"
|
||||
static="false"
|
||||
final="false"
|
||||
deprecated="not deprecated"
|
||||
visibility="public"
|
||||
>
|
||||
</method>
|
||||
<method name="intValue"
|
||||
return="int"
|
||||
abstract="false"
|
||||
native="false"
|
||||
synchronized="false"
|
||||
static="false"
|
||||
final="false"
|
||||
deprecated="not deprecated"
|
||||
visibility="public"
|
||||
>
|
||||
</method>
|
||||
<method name="isInt"
|
||||
return="boolean"
|
||||
abstract="false"
|
||||
native="false"
|
||||
synchronized="false"
|
||||
static="false"
|
||||
final="false"
|
||||
deprecated="not deprecated"
|
||||
visibility="public"
|
||||
>
|
||||
</method>
|
||||
<method name="isLong"
|
||||
return="boolean"
|
||||
abstract="false"
|
||||
native="false"
|
||||
synchronized="false"
|
||||
static="false"
|
||||
final="false"
|
||||
deprecated="not deprecated"
|
||||
visibility="public"
|
||||
>
|
||||
</method>
|
||||
<method name="longValue"
|
||||
return="long"
|
||||
abstract="false"
|
||||
native="false"
|
||||
synchronized="false"
|
||||
static="false"
|
||||
final="false"
|
||||
deprecated="not deprecated"
|
||||
visibility="public"
|
||||
>
|
||||
</method>
|
||||
<method name="name"
|
||||
return="java.lang.String"
|
||||
abstract="false"
|
||||
native="false"
|
||||
synchronized="false"
|
||||
static="false"
|
||||
final="false"
|
||||
deprecated="not deprecated"
|
||||
visibility="public"
|
||||
>
|
||||
</method>
|
||||
<method name="name"
|
||||
return="java.lang.String"
|
||||
abstract="false"
|
||||
native="false"
|
||||
synchronized="false"
|
||||
static="false"
|
||||
final="false"
|
||||
deprecated="not deprecated"
|
||||
visibility="public"
|
||||
>
|
||||
<parameter name="defaultName" type="java.lang.String">
|
||||
</parameter>
|
||||
</method>
|
||||
<method name="next"
|
||||
return="android.util.JsonToken"
|
||||
abstract="false"
|
||||
native="false"
|
||||
synchronized="false"
|
||||
static="false"
|
||||
final="false"
|
||||
deprecated="not deprecated"
|
||||
visibility="public"
|
||||
>
|
||||
<exception name="IOException" type="java.io.IOException">
|
||||
</exception>
|
||||
</method>
|
||||
<method name="skipValue"
|
||||
return="void"
|
||||
abstract="false"
|
||||
native="false"
|
||||
synchronized="false"
|
||||
static="false"
|
||||
final="false"
|
||||
deprecated="not deprecated"
|
||||
visibility="public"
|
||||
>
|
||||
<exception name="IOException" type="java.io.IOException">
|
||||
</exception>
|
||||
</method>
|
||||
<method name="stringValue"
|
||||
return="java.lang.String"
|
||||
abstract="false"
|
||||
native="false"
|
||||
synchronized="false"
|
||||
static="false"
|
||||
final="false"
|
||||
deprecated="not deprecated"
|
||||
visibility="public"
|
||||
>
|
||||
</method>
|
||||
</class>
|
||||
<class name="JsonToken"
|
||||
extends="java.lang.Enum"
|
||||
abstract="false"
|
||||
static="false"
|
||||
final="true"
|
||||
deprecated="not deprecated"
|
||||
visibility="public"
|
||||
>
|
||||
<method name="valueOf"
|
||||
return="android.util.JsonToken"
|
||||
abstract="false"
|
||||
native="false"
|
||||
synchronized="false"
|
||||
static="true"
|
||||
final="false"
|
||||
deprecated="not deprecated"
|
||||
visibility="public"
|
||||
>
|
||||
<parameter name="name" type="java.lang.String">
|
||||
</parameter>
|
||||
</method>
|
||||
<method name="values"
|
||||
return="android.util.JsonToken[]"
|
||||
abstract="false"
|
||||
native="false"
|
||||
synchronized="false"
|
||||
static="true"
|
||||
final="true"
|
||||
deprecated="not deprecated"
|
||||
visibility="public"
|
||||
>
|
||||
</method>
|
||||
</class>
|
||||
<class name="JsonWriter"
|
||||
extends="java.lang.Object"
|
||||
abstract="false"
|
||||
static="false"
|
||||
final="true"
|
||||
deprecated="not deprecated"
|
||||
visibility="public"
|
||||
>
|
||||
<constructor name="JsonWriter"
|
||||
type="android.util.JsonWriter"
|
||||
static="false"
|
||||
final="false"
|
||||
deprecated="not deprecated"
|
||||
visibility="public"
|
||||
>
|
||||
<parameter name="out" type="java.io.Writer">
|
||||
</parameter>
|
||||
</constructor>
|
||||
<method name="beginArray"
|
||||
return="android.util.JsonWriter"
|
||||
abstract="false"
|
||||
native="false"
|
||||
synchronized="false"
|
||||
static="false"
|
||||
final="false"
|
||||
deprecated="not deprecated"
|
||||
visibility="public"
|
||||
>
|
||||
<exception name="IOException" type="java.io.IOException">
|
||||
</exception>
|
||||
</method>
|
||||
<method name="beginObject"
|
||||
return="android.util.JsonWriter"
|
||||
abstract="false"
|
||||
native="false"
|
||||
synchronized="false"
|
||||
static="false"
|
||||
final="false"
|
||||
deprecated="not deprecated"
|
||||
visibility="public"
|
||||
>
|
||||
<exception name="IOException" type="java.io.IOException">
|
||||
</exception>
|
||||
</method>
|
||||
<method name="close"
|
||||
return="void"
|
||||
abstract="false"
|
||||
native="false"
|
||||
synchronized="false"
|
||||
static="false"
|
||||
final="false"
|
||||
deprecated="not deprecated"
|
||||
visibility="public"
|
||||
>
|
||||
<exception name="IOException" type="java.io.IOException">
|
||||
</exception>
|
||||
</method>
|
||||
<method name="endArray"
|
||||
return="android.util.JsonWriter"
|
||||
abstract="false"
|
||||
native="false"
|
||||
synchronized="false"
|
||||
static="false"
|
||||
final="false"
|
||||
deprecated="not deprecated"
|
||||
visibility="public"
|
||||
>
|
||||
<exception name="IOException" type="java.io.IOException">
|
||||
</exception>
|
||||
</method>
|
||||
<method name="endObject"
|
||||
return="android.util.JsonWriter"
|
||||
abstract="false"
|
||||
native="false"
|
||||
synchronized="false"
|
||||
static="false"
|
||||
final="false"
|
||||
deprecated="not deprecated"
|
||||
visibility="public"
|
||||
>
|
||||
<exception name="IOException" type="java.io.IOException">
|
||||
</exception>
|
||||
</method>
|
||||
<method name="flush"
|
||||
return="void"
|
||||
abstract="false"
|
||||
native="false"
|
||||
synchronized="false"
|
||||
static="false"
|
||||
final="false"
|
||||
deprecated="not deprecated"
|
||||
visibility="public"
|
||||
>
|
||||
<exception name="IOException" type="java.io.IOException">
|
||||
</exception>
|
||||
</method>
|
||||
<method name="name"
|
||||
return="android.util.JsonWriter"
|
||||
abstract="false"
|
||||
native="false"
|
||||
synchronized="false"
|
||||
static="false"
|
||||
final="false"
|
||||
deprecated="not deprecated"
|
||||
visibility="public"
|
||||
>
|
||||
<parameter name="name" type="java.lang.String">
|
||||
</parameter>
|
||||
<exception name="IOException" type="java.io.IOException">
|
||||
</exception>
|
||||
</method>
|
||||
<method name="nullValue"
|
||||
return="android.util.JsonWriter"
|
||||
abstract="false"
|
||||
native="false"
|
||||
synchronized="false"
|
||||
static="false"
|
||||
final="false"
|
||||
deprecated="not deprecated"
|
||||
visibility="public"
|
||||
>
|
||||
<exception name="IOException" type="java.io.IOException">
|
||||
</exception>
|
||||
</method>
|
||||
<method name="setIndentSpaces"
|
||||
return="void"
|
||||
abstract="false"
|
||||
native="false"
|
||||
synchronized="false"
|
||||
static="false"
|
||||
final="false"
|
||||
deprecated="not deprecated"
|
||||
visibility="public"
|
||||
>
|
||||
<parameter name="indent" type="int">
|
||||
</parameter>
|
||||
</method>
|
||||
<method name="value"
|
||||
return="android.util.JsonWriter"
|
||||
abstract="false"
|
||||
native="false"
|
||||
synchronized="false"
|
||||
static="false"
|
||||
final="false"
|
||||
deprecated="not deprecated"
|
||||
visibility="public"
|
||||
>
|
||||
<parameter name="value" type="java.lang.String">
|
||||
</parameter>
|
||||
<exception name="IOException" type="java.io.IOException">
|
||||
</exception>
|
||||
</method>
|
||||
<method name="value"
|
||||
return="android.util.JsonWriter"
|
||||
abstract="false"
|
||||
native="false"
|
||||
synchronized="false"
|
||||
static="false"
|
||||
final="false"
|
||||
deprecated="not deprecated"
|
||||
visibility="public"
|
||||
>
|
||||
<parameter name="value" type="boolean">
|
||||
</parameter>
|
||||
<exception name="IOException" type="java.io.IOException">
|
||||
</exception>
|
||||
</method>
|
||||
<method name="value"
|
||||
return="android.util.JsonWriter"
|
||||
abstract="false"
|
||||
native="false"
|
||||
synchronized="false"
|
||||
static="false"
|
||||
final="false"
|
||||
deprecated="not deprecated"
|
||||
visibility="public"
|
||||
>
|
||||
<parameter name="value" type="double">
|
||||
</parameter>
|
||||
<exception name="IOException" type="java.io.IOException">
|
||||
</exception>
|
||||
</method>
|
||||
<method name="value"
|
||||
return="android.util.JsonWriter"
|
||||
abstract="false"
|
||||
native="false"
|
||||
synchronized="false"
|
||||
static="false"
|
||||
final="false"
|
||||
deprecated="not deprecated"
|
||||
visibility="public"
|
||||
>
|
||||
<parameter name="value" type="long">
|
||||
</parameter>
|
||||
<exception name="IOException" type="java.io.IOException">
|
||||
</exception>
|
||||
</method>
|
||||
</class>
|
||||
<class name="Log"
|
||||
extends="java.lang.Object"
|
||||
abstract="false"
|
||||
|
||||
1006
core/java/android/util/JsonReader.java
Normal file
1006
core/java/android/util/JsonReader.java
Normal file
File diff suppressed because it is too large
Load Diff
68
core/java/android/util/JsonScope.java
Normal file
68
core/java/android/util/JsonScope.java
Normal file
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright (C) 2010 The Android Open Source Project
|
||||
*
|
||||
* 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 android.util;
|
||||
|
||||
/**
|
||||
* Lexical scoping elements within a JSON reader or writer.
|
||||
*/
|
||||
enum JsonScope {
|
||||
|
||||
/**
|
||||
* An array with no elements requires no separators or newlines before
|
||||
* it is closed.
|
||||
*/
|
||||
EMPTY_ARRAY,
|
||||
|
||||
/**
|
||||
* A array with at least one value requires a comma and newline before
|
||||
* the next element.
|
||||
*/
|
||||
NONEMPTY_ARRAY,
|
||||
|
||||
/**
|
||||
* An object with no name/value pairs requires no separators or newlines
|
||||
* before it is closed.
|
||||
*/
|
||||
EMPTY_OBJECT,
|
||||
|
||||
/**
|
||||
* An object whose most recent element is a key. The next element must
|
||||
* be a value.
|
||||
*/
|
||||
DANGLING_NAME,
|
||||
|
||||
/**
|
||||
* An object with at least one name/value pair requires a comma and
|
||||
* newline before the next element.
|
||||
*/
|
||||
NONEMPTY_OBJECT,
|
||||
|
||||
/**
|
||||
* No object or array has been started.
|
||||
*/
|
||||
EMPTY_DOCUMENT,
|
||||
|
||||
/**
|
||||
* A document with at an array or object.
|
||||
*/
|
||||
NONEMPTY_DOCUMENT,
|
||||
|
||||
/**
|
||||
* A document that's been closed and cannot be accessed.
|
||||
*/
|
||||
CLOSED,
|
||||
}
|
||||
82
core/java/android/util/JsonToken.java
Normal file
82
core/java/android/util/JsonToken.java
Normal file
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* Copyright (C) 2010 The Android Open Source Project
|
||||
*
|
||||
* 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 android.util;
|
||||
|
||||
/**
|
||||
* A structure, name or value type in a JSON-encoded string.
|
||||
*/
|
||||
public enum JsonToken {
|
||||
|
||||
/**
|
||||
* The opening of a JSON array. Written using {@link JsonWriter#beginObject}
|
||||
* and read using {@link JsonReader#beginObject}.
|
||||
*/
|
||||
BEGIN_ARRAY,
|
||||
|
||||
/**
|
||||
* The closing of a JSON array. Written using {@link JsonWriter#endArray}
|
||||
* and read using {@link JsonReader#endArray}.
|
||||
*/
|
||||
END_ARRAY,
|
||||
|
||||
/**
|
||||
* The opening of a JSON object. Written using {@link JsonWriter#beginObject}
|
||||
* and read using {@link JsonReader#beginObject}.
|
||||
*/
|
||||
BEGIN_OBJECT,
|
||||
|
||||
/**
|
||||
* The closing of a JSON object. Written using {@link JsonWriter#endObject}
|
||||
* and read using {@link JsonReader#endObject}.
|
||||
*/
|
||||
END_OBJECT,
|
||||
|
||||
/**
|
||||
* A JSON property name. Within objects, tokens alternate between names and
|
||||
* their values. Written using {@link JsonWriter#name} and read using {@link
|
||||
* JsonReader#nextName}
|
||||
*/
|
||||
NAME,
|
||||
|
||||
/**
|
||||
* A JSON string.
|
||||
*/
|
||||
STRING,
|
||||
|
||||
/**
|
||||
* A JSON number represented in this API by a Java {@code double}, {@code
|
||||
* long}, or {@code int}.
|
||||
*/
|
||||
NUMBER,
|
||||
|
||||
/**
|
||||
* A JSON {@code true} or {@code false}.
|
||||
*/
|
||||
BOOLEAN,
|
||||
|
||||
/**
|
||||
* A JSON {@code null}.
|
||||
*/
|
||||
NULL,
|
||||
|
||||
/**
|
||||
* The end of the JSON stream. This sentinel value is returned by {@link
|
||||
* JsonReader#peek()} to signal that the JSON-encoded value has no more
|
||||
* tokens.
|
||||
*/
|
||||
END_DOCUMENT
|
||||
}
|
||||
475
core/java/android/util/JsonWriter.java
Normal file
475
core/java/android/util/JsonWriter.java
Normal file
@@ -0,0 +1,475 @@
|
||||
/*
|
||||
* Copyright (C) 2010 The Android Open Source Project
|
||||
*
|
||||
* 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 android.util;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Writer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Writes a JSON (<a href="http://www.ietf.org/rfc/rfc4627.txt">RFC 4627</a>)
|
||||
* encoded value to a stream, one token at a time. The stream includes both
|
||||
* literal values (strings, numbers, booleans and nulls) as well as the begin
|
||||
* and end delimiters of objects and arrays.
|
||||
*
|
||||
* <h3>Encoding JSON</h3>
|
||||
* To encode your data as JSON, create a new {@code JsonWriter}. Each JSON
|
||||
* document must contain one top-level array or object. Call methods on the
|
||||
* writer as you walk the structure's contents, nesting arrays and objects as
|
||||
* necessary:
|
||||
* <ul>
|
||||
* <li>To write <strong>arrays</strong>, first call {@link #beginArray()}.
|
||||
* Write each of the array's elements with the appropriate {@link #value}
|
||||
* methods or by nesting other arrays and objects. Finally close the array
|
||||
* using {@link #endArray()}.
|
||||
* <li>To write <strong>objects</strong>, first call {@link #beginObject()}.
|
||||
* Write each of the object's properties by alternating calls to
|
||||
* {@link #name} with the property's value. Write property values with the
|
||||
* appropriate {@link #value} method or by nesting other objects or arrays.
|
||||
* Finally close the object using {@link #endObject()}.
|
||||
* </ul>
|
||||
*
|
||||
* <h3>Example</h3>
|
||||
* Suppose we'd like to encode a stream of messages such as the following: <pre> {@code
|
||||
* [
|
||||
* {
|
||||
* "id": 912345678901,
|
||||
* "text": "How do I write JSON on Android?",
|
||||
* "geo": null,
|
||||
* "user": {
|
||||
* "name": "android_newb",
|
||||
* "followers_count": 41
|
||||
* }
|
||||
* },
|
||||
* {
|
||||
* "id": 912345678902,
|
||||
* "text": "@android_newb just use android.util.JsonWriter!",
|
||||
* "geo": [50.454722, -104.606667],
|
||||
* "user": {
|
||||
* "name": "jesse",
|
||||
* "followers_count": 2
|
||||
* }
|
||||
* }
|
||||
* ]}</pre>
|
||||
* This code encodes the above structure: <pre> {@code
|
||||
* public void writeJsonStream(OutputStream out, List<Message> messages) throws IOException {
|
||||
* JsonWriter writer = new JsonWriter(new OutputStreamWriter(out, "UTF-8"));
|
||||
* writer.setIndentSpaces(4);
|
||||
* writeMessagesArray(writer, messages);
|
||||
* writer.close();
|
||||
* }
|
||||
*
|
||||
* public void writeMessagesArray(JsonWriter writer, List<Message> messages) throws IOException {
|
||||
* writer.beginArray();
|
||||
* for (Message message : messages) {
|
||||
* writeMessage(writer, message);
|
||||
* }
|
||||
* writer.endArray();
|
||||
* }
|
||||
*
|
||||
* public void writeMessage(JsonWriter writer, Message message) throws IOException {
|
||||
* writer.beginObject();
|
||||
* writer.name("id").value(message.getId());
|
||||
* writer.name("text").value(message.getText());
|
||||
* if (message.getGeo() != null) {
|
||||
* writer.name("geo");
|
||||
* writeDoublesArray(writer, message.getGeo());
|
||||
* } else {
|
||||
* writer.name("geo").nullValue();
|
||||
* }
|
||||
* writer.name("user");
|
||||
* writeUser(writer, message.getUser());
|
||||
* writer.endObject();
|
||||
* }
|
||||
*
|
||||
* public void writeUser(JsonWriter writer, User user) throws IOException {
|
||||
* writer.beginObject();
|
||||
* writer.name("name").value(user.getName());
|
||||
* writer.name("followers_count").value(user.getFollowersCount());
|
||||
* writer.endObject();
|
||||
* }
|
||||
*
|
||||
* public void writeDoublesArray(JsonWriter writer, List<Double> doubles) throws IOException {
|
||||
* writer.beginArray();
|
||||
* for (Double value : doubles) {
|
||||
* writer.value(value);
|
||||
* }
|
||||
* writer.endArray();
|
||||
* }}</pre>
|
||||
*
|
||||
* <p>Each {@code JsonWriter} may be used to write a single JSON stream.
|
||||
* Instances of this class are not thread safe. Calls that would result in a
|
||||
* malformed JSON string will fail with an {@link IllegalStateException}.
|
||||
*/
|
||||
public final class JsonWriter {
|
||||
|
||||
/** The output data, containing at most one top-level array or object. */
|
||||
private final Writer out;
|
||||
|
||||
private final List<JsonScope> stack = new ArrayList<JsonScope>();
|
||||
{
|
||||
stack.add(JsonScope.EMPTY_DOCUMENT);
|
||||
}
|
||||
|
||||
/**
|
||||
* A string containing a full set of spaces for a single level of
|
||||
* indentation, or null for no pretty printing.
|
||||
*/
|
||||
private String indent;
|
||||
|
||||
/**
|
||||
* The name/value separator; either ":" or ": ".
|
||||
*/
|
||||
private String separator = ":";
|
||||
|
||||
/**
|
||||
* Creates a new instance that writes a JSON-encoded stream to {@code out}.
|
||||
* For best performance, ensure {@link Writer} is buffered; wrapping in
|
||||
* {@link java.io.BufferedWriter BufferedWriter} if necessary.
|
||||
*/
|
||||
public JsonWriter(Writer out) {
|
||||
if (out == null) {
|
||||
throw new NullPointerException("out == null");
|
||||
}
|
||||
this.out = out;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the number of spaces to indent each line in the encoded document.
|
||||
* If {@code indent == 0} the encoded document will be compact. If {@code
|
||||
* indent > 0}, the encoded document will be more human-readable.
|
||||
*/
|
||||
public void setIndentSpaces(int indent) {
|
||||
if (indent < 0) {
|
||||
throw new IllegalArgumentException("indent < 0");
|
||||
}
|
||||
|
||||
if (indent > 0) {
|
||||
char[] indentChars = new char[indent];
|
||||
Arrays.fill(indentChars, ' ');
|
||||
this.indent = new String(indentChars);
|
||||
this.separator = ": ";
|
||||
} else {
|
||||
this.indent = null;
|
||||
this.separator = ":";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Begins encoding a new array. Each call to this method must be paired with
|
||||
* a call to {@link #endArray}.
|
||||
*
|
||||
* @return this writer.
|
||||
*/
|
||||
public JsonWriter beginArray() throws IOException {
|
||||
return open(JsonScope.EMPTY_ARRAY, "[");
|
||||
}
|
||||
|
||||
/**
|
||||
* Ends encoding the current array.
|
||||
*
|
||||
* @return this writer.
|
||||
*/
|
||||
public JsonWriter endArray() throws IOException {
|
||||
return close(JsonScope.EMPTY_ARRAY, JsonScope.NONEMPTY_ARRAY, "]");
|
||||
}
|
||||
|
||||
/**
|
||||
* Begins encoding a new object. Each call to this method must be paired
|
||||
* with a call to {@link #endObject}.
|
||||
*
|
||||
* @return this writer.
|
||||
*/
|
||||
public JsonWriter beginObject() throws IOException {
|
||||
return open(JsonScope.EMPTY_OBJECT, "{");
|
||||
}
|
||||
|
||||
/**
|
||||
* Ends encoding the current object.
|
||||
*
|
||||
* @return this writer.
|
||||
*/
|
||||
public JsonWriter endObject() throws IOException {
|
||||
return close(JsonScope.EMPTY_OBJECT, JsonScope.NONEMPTY_OBJECT, "}");
|
||||
}
|
||||
|
||||
/**
|
||||
* Enters a new scope by appending any necessary whitespace and the given
|
||||
* bracket.
|
||||
*/
|
||||
private JsonWriter open(JsonScope empty, String openBracket) throws IOException {
|
||||
beforeValue(true);
|
||||
stack.add(empty);
|
||||
out.write(openBracket);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Closes the current scope by appending any necessary whitespace and the
|
||||
* given bracket.
|
||||
*/
|
||||
private JsonWriter close(JsonScope empty, JsonScope nonempty, String closeBracket)
|
||||
throws IOException {
|
||||
JsonScope context = peek();
|
||||
if (context != nonempty && context != empty) {
|
||||
throw new IllegalStateException("Nesting problem: " + stack);
|
||||
}
|
||||
|
||||
stack.remove(stack.size() - 1);
|
||||
if (context == nonempty) {
|
||||
newline();
|
||||
}
|
||||
out.write(closeBracket);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value on the top of the stack.
|
||||
*/
|
||||
private JsonScope peek() {
|
||||
return stack.get(stack.size() - 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace the value on the top of the stack with the given value.
|
||||
*/
|
||||
private void replaceTop(JsonScope topOfStack) {
|
||||
stack.set(stack.size() - 1, topOfStack);
|
||||
}
|
||||
|
||||
/**
|
||||
* Encodes the property name.
|
||||
*
|
||||
* @param name the name of the forthcoming value. May not be null.
|
||||
* @return this writer.
|
||||
*/
|
||||
public JsonWriter name(String name) throws IOException {
|
||||
if (name == null) {
|
||||
throw new NullPointerException("name == null");
|
||||
}
|
||||
beforeName();
|
||||
string(name);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encodes {@code value}.
|
||||
*
|
||||
* @param value the literal string value, or null to encode a null literal.
|
||||
* @return this writer.
|
||||
*/
|
||||
public JsonWriter value(String value) throws IOException {
|
||||
if (value == null) {
|
||||
return nullValue();
|
||||
}
|
||||
beforeValue(false);
|
||||
string(value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encodes {@code null}.
|
||||
*
|
||||
* @return this writer.
|
||||
*/
|
||||
public JsonWriter nullValue() throws IOException {
|
||||
beforeValue(false);
|
||||
out.write("null");
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encodes {@code value}.
|
||||
*
|
||||
* @return this writer.
|
||||
*/
|
||||
public JsonWriter value(boolean value) throws IOException {
|
||||
beforeValue(false);
|
||||
out.write(value ? "true" : "false");
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encodes {@code value}.
|
||||
*
|
||||
* @param value a finite value. May not be {@link Double#isNaN() NaNs} or
|
||||
* {@link Double#isInfinite() infinities}.
|
||||
* @return this writer.
|
||||
*/
|
||||
public JsonWriter value(double value) throws IOException {
|
||||
if (Double.isNaN(value) || Double.isInfinite(value)) {
|
||||
throw new IllegalArgumentException("Numeric values must be finite, but was " + value);
|
||||
}
|
||||
beforeValue(false);
|
||||
out.append(Double.toString(value));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encodes {@code value}.
|
||||
*
|
||||
* @return this writer.
|
||||
*/
|
||||
public JsonWriter value(long value) throws IOException {
|
||||
beforeValue(false);
|
||||
out.write(Long.toString(value));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures all buffered data is written to the underlying {@link Writer}
|
||||
* and flushes that writer.
|
||||
*/
|
||||
public void flush() throws IOException {
|
||||
out.flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* Flushes and closes this writer and the underlying {@link Writer}.
|
||||
*
|
||||
* @throws IOException if the JSON document is incomplete.
|
||||
*/
|
||||
public void close() throws IOException {
|
||||
out.close();
|
||||
|
||||
if (peek() != JsonScope.NONEMPTY_DOCUMENT) {
|
||||
throw new IOException("Incomplete document");
|
||||
}
|
||||
}
|
||||
|
||||
private void string(String value) throws IOException {
|
||||
out.write("\"");
|
||||
for (int i = 0, length = value.length(); i < length; i++) {
|
||||
char c = value.charAt(i);
|
||||
|
||||
/*
|
||||
* From RFC 4627, "All Unicode characters may be placed within the
|
||||
* quotation marks except for the characters that must be escaped:
|
||||
* quotation mark, reverse solidus, and the control characters
|
||||
* (U+0000 through U+001F)."
|
||||
*/
|
||||
switch (c) {
|
||||
case '"':
|
||||
case '\\':
|
||||
case '/':
|
||||
out.write('\\');
|
||||
out.write(c);
|
||||
break;
|
||||
|
||||
case '\t':
|
||||
out.write("\\t");
|
||||
break;
|
||||
|
||||
case '\b':
|
||||
out.write("\\b");
|
||||
break;
|
||||
|
||||
case '\n':
|
||||
out.write("\\n");
|
||||
break;
|
||||
|
||||
case '\r':
|
||||
out.write("\\r");
|
||||
break;
|
||||
|
||||
case '\f':
|
||||
out.write("\\f");
|
||||
break;
|
||||
|
||||
default:
|
||||
if (c <= 0x1F) {
|
||||
out.write(String.format("\\u%04x", (int) c));
|
||||
} else {
|
||||
out.write(c);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
out.write("\"");
|
||||
}
|
||||
|
||||
private void newline() throws IOException {
|
||||
if (indent == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
out.write("\n");
|
||||
for (int i = 1; i < stack.size(); i++) {
|
||||
out.write(indent);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts any necessary separators and whitespace before a name. Also
|
||||
* adjusts the stack to expect the name's value.
|
||||
*/
|
||||
private void beforeName() throws IOException {
|
||||
JsonScope context = peek();
|
||||
if (context == JsonScope.NONEMPTY_OBJECT) { // first in object
|
||||
out.write(',');
|
||||
} else if (context != JsonScope.EMPTY_OBJECT) { // not in an object!
|
||||
throw new IllegalStateException("Nesting problem: " + stack);
|
||||
}
|
||||
newline();
|
||||
replaceTop(JsonScope.DANGLING_NAME);
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts any necessary separators and whitespace before a literal value,
|
||||
* inline array, or inline object. Also adjusts the stack to expect either a
|
||||
* closing bracket or another element.
|
||||
*
|
||||
* @param root true if the value is a new array or object, the two values
|
||||
* permitted as top-level elements.
|
||||
*/
|
||||
private void beforeValue(boolean root) throws IOException {
|
||||
switch (peek()) {
|
||||
case EMPTY_DOCUMENT: // first in document
|
||||
if (!root) {
|
||||
throw new IllegalStateException(
|
||||
"JSON must start with an array or an object.");
|
||||
}
|
||||
replaceTop(JsonScope.NONEMPTY_DOCUMENT);
|
||||
break;
|
||||
|
||||
case EMPTY_ARRAY: // first in array
|
||||
replaceTop(JsonScope.NONEMPTY_ARRAY);
|
||||
newline();
|
||||
break;
|
||||
|
||||
case NONEMPTY_ARRAY: // another in array
|
||||
out.append(',');
|
||||
newline();
|
||||
break;
|
||||
|
||||
case DANGLING_NAME: // value for name
|
||||
out.append(separator);
|
||||
replaceTop(JsonScope.NONEMPTY_OBJECT);
|
||||
break;
|
||||
|
||||
case NONEMPTY_DOCUMENT:
|
||||
throw new IllegalStateException(
|
||||
"JSON must have only one top-level value.");
|
||||
|
||||
default:
|
||||
throw new IllegalStateException("Nesting problem: " + stack);
|
||||
}
|
||||
}
|
||||
}
|
||||
415
core/tests/coretests/src/android/util/JsonReaderTest.java
Normal file
415
core/tests/coretests/src/android/util/JsonReaderTest.java
Normal file
@@ -0,0 +1,415 @@
|
||||
/*
|
||||
* Copyright (C) 2010 The Android Open Source Project
|
||||
*
|
||||
* 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 android.util;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
|
||||
public final class JsonReaderTest extends TestCase {
|
||||
|
||||
public void testReadArray() throws IOException {
|
||||
JsonReader reader = new JsonReader(new StringReader("[true, true]"));
|
||||
reader.beginArray();
|
||||
assertEquals(true, reader.nextBoolean());
|
||||
assertEquals(true, reader.nextBoolean());
|
||||
reader.endArray();
|
||||
assertEquals(JsonToken.END_DOCUMENT, reader.peek());
|
||||
}
|
||||
|
||||
public void testReadObject() throws IOException {
|
||||
JsonReader reader = new JsonReader(new StringReader(
|
||||
"{\"a\": \"android\", \"b\": \"banana\"}"));
|
||||
reader.beginObject();
|
||||
assertEquals("a", reader.nextName());
|
||||
assertEquals("android", reader.nextString());
|
||||
assertEquals("b", reader.nextName());
|
||||
assertEquals("banana", reader.nextString());
|
||||
reader.endObject();
|
||||
assertEquals(JsonToken.END_DOCUMENT, reader.peek());
|
||||
}
|
||||
|
||||
public void testSkipObject() throws IOException {
|
||||
JsonReader reader = new JsonReader(new StringReader(
|
||||
"{\"a\": { \"c\": [], \"d\": [true, true, {}] }, \"b\": \"banana\"}"));
|
||||
reader.beginObject();
|
||||
assertEquals("a", reader.nextName());
|
||||
reader.skipValue();
|
||||
assertEquals("b", reader.nextName());
|
||||
reader.skipValue();
|
||||
reader.endObject();
|
||||
assertEquals(JsonToken.END_DOCUMENT, reader.peek());
|
||||
}
|
||||
|
||||
public void testHelloWorld() throws IOException {
|
||||
String json = "{\n" +
|
||||
" \"hello\": true,\n" +
|
||||
" \"foo\": [\"world\"]\n" +
|
||||
"}";
|
||||
JsonReader reader = new JsonReader(new StringReader(json));
|
||||
reader.beginObject();
|
||||
assertEquals("hello", reader.nextName());
|
||||
assertEquals(true, reader.nextBoolean());
|
||||
assertEquals("foo", reader.nextName());
|
||||
reader.beginArray();
|
||||
assertEquals("world", reader.nextString());
|
||||
reader.endArray();
|
||||
reader.endObject();
|
||||
assertEquals(JsonToken.END_DOCUMENT, reader.peek());
|
||||
}
|
||||
|
||||
public void testNulls() {
|
||||
try {
|
||||
new JsonReader(null);
|
||||
fail();
|
||||
} catch (NullPointerException expected) {
|
||||
}
|
||||
}
|
||||
|
||||
public void testEmptyString() throws IOException {
|
||||
try {
|
||||
new JsonReader(new StringReader("")).beginArray();
|
||||
} catch (IOException expected) {
|
||||
}
|
||||
try {
|
||||
new JsonReader(new StringReader("")).beginObject();
|
||||
} catch (IOException expected) {
|
||||
}
|
||||
}
|
||||
|
||||
public void testNoTopLevelObject() throws IOException {
|
||||
try {
|
||||
new JsonReader(new StringReader("true")).nextBoolean();
|
||||
} catch (IOException expected) {
|
||||
}
|
||||
}
|
||||
|
||||
public void testCharacterUnescaping() throws IOException {
|
||||
String json = "[\"a\","
|
||||
+ "\"a\\\"\","
|
||||
+ "\"\\\"\","
|
||||
+ "\":\","
|
||||
+ "\",\","
|
||||
+ "\"\\b\","
|
||||
+ "\"\\f\","
|
||||
+ "\"\\n\","
|
||||
+ "\"\\r\","
|
||||
+ "\"\\t\","
|
||||
+ "\" \","
|
||||
+ "\"\\\\\","
|
||||
+ "\"{\","
|
||||
+ "\"}\","
|
||||
+ "\"[\","
|
||||
+ "\"]\","
|
||||
+ "\"\\u0000\","
|
||||
+ "\"\\u0019\","
|
||||
+ "\"\\u20AC\""
|
||||
+ "]";
|
||||
JsonReader reader = new JsonReader(new StringReader(json));
|
||||
reader.beginArray();
|
||||
assertEquals("a", reader.nextString());
|
||||
assertEquals("a\"", reader.nextString());
|
||||
assertEquals("\"", reader.nextString());
|
||||
assertEquals(":", reader.nextString());
|
||||
assertEquals(",", reader.nextString());
|
||||
assertEquals("\b", reader.nextString());
|
||||
assertEquals("\f", reader.nextString());
|
||||
assertEquals("\n", reader.nextString());
|
||||
assertEquals("\r", reader.nextString());
|
||||
assertEquals("\t", reader.nextString());
|
||||
assertEquals(" ", reader.nextString());
|
||||
assertEquals("\\", reader.nextString());
|
||||
assertEquals("{", reader.nextString());
|
||||
assertEquals("}", reader.nextString());
|
||||
assertEquals("[", reader.nextString());
|
||||
assertEquals("]", reader.nextString());
|
||||
assertEquals("\0", reader.nextString());
|
||||
assertEquals("\u0019", reader.nextString());
|
||||
assertEquals("\u20AC", reader.nextString());
|
||||
reader.endArray();
|
||||
assertEquals(JsonToken.END_DOCUMENT, reader.peek());
|
||||
}
|
||||
|
||||
public void testIntegersWithFractionalPartSpecified() throws IOException {
|
||||
JsonReader reader = new JsonReader(new StringReader("[1.0,1.0,1.0]"));
|
||||
reader.beginArray();
|
||||
assertEquals(1.0, reader.nextDouble());
|
||||
assertEquals(1, reader.nextInt());
|
||||
assertEquals(1L, reader.nextLong());
|
||||
}
|
||||
|
||||
public void testDoubles() throws IOException {
|
||||
String json = "[-0.0,"
|
||||
+ "1.0,"
|
||||
+ "1.7976931348623157E308,"
|
||||
+ "4.9E-324,"
|
||||
+ "0.0,"
|
||||
+ "-0.5,"
|
||||
+ "2.2250738585072014E-308,"
|
||||
+ "3.141592653589793,"
|
||||
+ "2.718281828459045]";
|
||||
JsonReader reader = new JsonReader(new StringReader(json));
|
||||
reader.beginArray();
|
||||
assertEquals(-0.0, reader.nextDouble());
|
||||
assertEquals(1.0, reader.nextDouble());
|
||||
assertEquals(1.7976931348623157E308, reader.nextDouble());
|
||||
assertEquals(4.9E-324, reader.nextDouble());
|
||||
assertEquals(0.0, reader.nextDouble());
|
||||
assertEquals(-0.5, reader.nextDouble());
|
||||
assertEquals(2.2250738585072014E-308, reader.nextDouble());
|
||||
assertEquals(3.141592653589793, reader.nextDouble());
|
||||
assertEquals(2.718281828459045, reader.nextDouble());
|
||||
reader.endArray();
|
||||
assertEquals(JsonToken.END_DOCUMENT, reader.peek());
|
||||
}
|
||||
|
||||
public void testNonFiniteDoubles() throws IOException {
|
||||
String json = "[NaN]";
|
||||
JsonReader reader = new JsonReader(new StringReader(json));
|
||||
reader.beginArray();
|
||||
try {
|
||||
reader.nextDouble();
|
||||
fail();
|
||||
} catch (NumberFormatException expected) {
|
||||
}
|
||||
}
|
||||
|
||||
public void testLongs() throws IOException {
|
||||
String json = "[0,0,0,"
|
||||
+ "1,1,1,"
|
||||
+ "-1,-1,-1,"
|
||||
+ "-9223372036854775808,"
|
||||
+ "9223372036854775807]";
|
||||
JsonReader reader = new JsonReader(new StringReader(json));
|
||||
reader.beginArray();
|
||||
assertEquals(0L, reader.nextLong());
|
||||
assertEquals(0, reader.nextInt());
|
||||
assertEquals(0.0, reader.nextDouble());
|
||||
assertEquals(1L, reader.nextLong());
|
||||
assertEquals(1, reader.nextInt());
|
||||
assertEquals(1.0, reader.nextDouble());
|
||||
assertEquals(-1L, reader.nextLong());
|
||||
assertEquals(-1, reader.nextInt());
|
||||
assertEquals(-1.0, reader.nextDouble());
|
||||
try {
|
||||
reader.nextInt();
|
||||
fail();
|
||||
} catch (NumberFormatException expected) {
|
||||
}
|
||||
assertEquals(Long.MIN_VALUE, reader.nextLong());
|
||||
try {
|
||||
reader.nextInt();
|
||||
fail();
|
||||
} catch (NumberFormatException expected) {
|
||||
}
|
||||
assertEquals(Long.MAX_VALUE, reader.nextLong());
|
||||
reader.endArray();
|
||||
assertEquals(JsonToken.END_DOCUMENT, reader.peek());
|
||||
}
|
||||
|
||||
/**
|
||||
* This test fails because there's no double for 9223372036854775806, and
|
||||
* our long parsing uses Double.parseDouble() for fractional values.
|
||||
*/
|
||||
public void testHighPrecisionLong() throws IOException {
|
||||
String json = "[9223372036854775806.000]";
|
||||
JsonReader reader = new JsonReader(new StringReader(json));
|
||||
reader.beginArray();
|
||||
assertEquals(9223372036854775806L, reader.nextLong());
|
||||
reader.endArray();
|
||||
}
|
||||
|
||||
public void testNumberWithOctalPrefix() throws IOException {
|
||||
String json = "[01]";
|
||||
JsonReader reader = new JsonReader(new StringReader(json));
|
||||
reader.beginArray();
|
||||
try {
|
||||
reader.nextInt();
|
||||
fail();
|
||||
} catch (NumberFormatException expected) {
|
||||
}
|
||||
try {
|
||||
reader.nextLong();
|
||||
fail();
|
||||
} catch (NumberFormatException expected) {
|
||||
}
|
||||
try {
|
||||
reader.nextDouble();
|
||||
fail();
|
||||
} catch (NumberFormatException expected) {
|
||||
}
|
||||
assertEquals("01", reader.nextString());
|
||||
reader.endArray();
|
||||
assertEquals(JsonToken.END_DOCUMENT, reader.peek());
|
||||
}
|
||||
|
||||
public void testBooleans() throws IOException {
|
||||
JsonReader reader = new JsonReader(new StringReader("[true,false]"));
|
||||
reader.beginArray();
|
||||
assertEquals(true, reader.nextBoolean());
|
||||
assertEquals(false, reader.nextBoolean());
|
||||
reader.endArray();
|
||||
assertEquals(JsonToken.END_DOCUMENT, reader.peek());
|
||||
}
|
||||
|
||||
public void testMixedCaseLiterals() throws IOException {
|
||||
JsonReader reader = new JsonReader(new StringReader("[True,TruE,False,FALSE,NULL,nulL]"));
|
||||
reader.beginArray();
|
||||
assertEquals(true, reader.nextBoolean());
|
||||
assertEquals(true, reader.nextBoolean());
|
||||
assertEquals(false, reader.nextBoolean());
|
||||
assertEquals(false, reader.nextBoolean());
|
||||
reader.nextNull();
|
||||
reader.nextNull();
|
||||
reader.endArray();
|
||||
assertEquals(JsonToken.END_DOCUMENT, reader.peek());
|
||||
}
|
||||
|
||||
public void testMissingValue() throws IOException {
|
||||
JsonReader reader = new JsonReader(new StringReader("{\"a\":}"));
|
||||
reader.beginObject();
|
||||
assertEquals("a", reader.nextName());
|
||||
try {
|
||||
reader.nextString();
|
||||
fail();
|
||||
} catch (IOException expected) {
|
||||
}
|
||||
}
|
||||
|
||||
public void testPrematureEndOfInput() throws IOException {
|
||||
JsonReader reader = new JsonReader(new StringReader("{\"a\":true,"));
|
||||
reader.beginObject();
|
||||
assertEquals("a", reader.nextName());
|
||||
assertEquals(true, reader.nextBoolean());
|
||||
try {
|
||||
reader.nextName();
|
||||
fail();
|
||||
} catch (IOException expected) {
|
||||
}
|
||||
}
|
||||
|
||||
public void testPrematurelyClosed() throws IOException {
|
||||
try {
|
||||
JsonReader reader = new JsonReader(new StringReader("{\"a\":[]}"));
|
||||
reader.beginObject();
|
||||
reader.close();
|
||||
reader.nextName();
|
||||
fail();
|
||||
} catch (IllegalStateException expected) {
|
||||
}
|
||||
|
||||
try {
|
||||
JsonReader reader = new JsonReader(new StringReader("{\"a\":[]}"));
|
||||
reader.close();
|
||||
reader.beginObject();
|
||||
fail();
|
||||
} catch (IllegalStateException expected) {
|
||||
}
|
||||
|
||||
try {
|
||||
JsonReader reader = new JsonReader(new StringReader("{\"a\":true}"));
|
||||
reader.beginObject();
|
||||
reader.nextName();
|
||||
reader.peek();
|
||||
reader.close();
|
||||
reader.nextBoolean();
|
||||
fail();
|
||||
} catch (IllegalStateException expected) {
|
||||
}
|
||||
}
|
||||
|
||||
public void testNextFailuresDoNotAdvance() throws IOException {
|
||||
JsonReader reader = new JsonReader(new StringReader("{\"a\":true}"));
|
||||
reader.beginObject();
|
||||
try {
|
||||
reader.nextString();
|
||||
fail();
|
||||
} catch (IllegalStateException expected) {
|
||||
}
|
||||
assertEquals("a", reader.nextName());
|
||||
try {
|
||||
reader.nextName();
|
||||
fail();
|
||||
} catch (IllegalStateException expected) {
|
||||
}
|
||||
try {
|
||||
reader.beginArray();
|
||||
fail();
|
||||
} catch (IllegalStateException expected) {
|
||||
}
|
||||
try {
|
||||
reader.endArray();
|
||||
fail();
|
||||
} catch (IllegalStateException expected) {
|
||||
}
|
||||
try {
|
||||
reader.beginObject();
|
||||
fail();
|
||||
} catch (IllegalStateException expected) {
|
||||
}
|
||||
try {
|
||||
reader.endObject();
|
||||
fail();
|
||||
} catch (IllegalStateException expected) {
|
||||
}
|
||||
assertEquals(true, reader.nextBoolean());
|
||||
try {
|
||||
reader.nextString();
|
||||
fail();
|
||||
} catch (IllegalStateException expected) {
|
||||
}
|
||||
try {
|
||||
reader.nextName();
|
||||
fail();
|
||||
} catch (IllegalStateException expected) {
|
||||
}
|
||||
try {
|
||||
reader.beginArray();
|
||||
fail();
|
||||
} catch (IllegalStateException expected) {
|
||||
}
|
||||
try {
|
||||
reader.endArray();
|
||||
fail();
|
||||
} catch (IllegalStateException expected) {
|
||||
}
|
||||
reader.endObject();
|
||||
assertEquals(JsonToken.END_DOCUMENT, reader.peek());
|
||||
reader.close();
|
||||
}
|
||||
|
||||
public void testStringNullIsNotNull() throws IOException {
|
||||
JsonReader reader = new JsonReader(new StringReader("[\"null\"]"));
|
||||
reader.beginArray();
|
||||
try {
|
||||
reader.nextNull();
|
||||
fail();
|
||||
} catch (IllegalStateException expected) {
|
||||
}
|
||||
}
|
||||
|
||||
public void testNullLiteralIsNotAString() throws IOException {
|
||||
JsonReader reader = new JsonReader(new StringReader("[null]"));
|
||||
reader.beginArray();
|
||||
try {
|
||||
reader.nextString();
|
||||
fail();
|
||||
} catch (IllegalStateException expected) {
|
||||
}
|
||||
}
|
||||
}
|
||||
419
core/tests/coretests/src/android/util/JsonWriterTest.java
Normal file
419
core/tests/coretests/src/android/util/JsonWriterTest.java
Normal file
@@ -0,0 +1,419 @@
|
||||
/*
|
||||
* Copyright (C) 2010 The Android Open Source Project
|
||||
*
|
||||
* 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 android.util;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.StringWriter;
|
||||
|
||||
public final class JsonWriterTest extends TestCase {
|
||||
|
||||
public void testWrongTopLevelType() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
try {
|
||||
jsonWriter.value("a");
|
||||
fail();
|
||||
} catch (IllegalStateException expected) {
|
||||
}
|
||||
}
|
||||
|
||||
public void testTwoNames() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
jsonWriter.beginObject();
|
||||
jsonWriter.name("a");
|
||||
try {
|
||||
jsonWriter.name("a");
|
||||
fail();
|
||||
} catch (IllegalStateException expected) {
|
||||
}
|
||||
}
|
||||
|
||||
public void testNameWithoutValue() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
jsonWriter.beginObject();
|
||||
jsonWriter.name("a");
|
||||
try {
|
||||
jsonWriter.endObject();
|
||||
fail();
|
||||
} catch (IllegalStateException expected) {
|
||||
}
|
||||
}
|
||||
|
||||
public void testValueWithoutName() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
jsonWriter.beginObject();
|
||||
try {
|
||||
jsonWriter.value(true);
|
||||
fail();
|
||||
} catch (IllegalStateException expected) {
|
||||
}
|
||||
}
|
||||
|
||||
public void testMultipleTopLevelValues() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
jsonWriter.beginArray().endArray();
|
||||
try {
|
||||
jsonWriter.beginArray();
|
||||
fail();
|
||||
} catch (IllegalStateException expected) {
|
||||
}
|
||||
}
|
||||
|
||||
public void testBadNestingObject() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
jsonWriter.beginArray();
|
||||
jsonWriter.beginObject();
|
||||
try {
|
||||
jsonWriter.endArray();
|
||||
fail();
|
||||
} catch (IllegalStateException expected) {
|
||||
}
|
||||
}
|
||||
|
||||
public void testBadNestingArray() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
jsonWriter.beginArray();
|
||||
jsonWriter.beginArray();
|
||||
try {
|
||||
jsonWriter.endObject();
|
||||
fail();
|
||||
} catch (IllegalStateException expected) {
|
||||
}
|
||||
}
|
||||
|
||||
public void testNullName() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
jsonWriter.beginObject();
|
||||
try {
|
||||
jsonWriter.name(null);
|
||||
fail();
|
||||
} catch (NullPointerException expected) {
|
||||
}
|
||||
}
|
||||
|
||||
public void testNullStringValue() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
jsonWriter.beginObject();
|
||||
jsonWriter.name("a");
|
||||
jsonWriter.value(null);
|
||||
jsonWriter.endObject();
|
||||
assertEquals("{\"a\":null}", stringWriter.toString());
|
||||
}
|
||||
|
||||
public void testNonFiniteDoubles() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
jsonWriter.beginArray();
|
||||
try {
|
||||
jsonWriter.value(Double.NaN);
|
||||
fail();
|
||||
} catch (IllegalArgumentException expected) {
|
||||
}
|
||||
try {
|
||||
jsonWriter.value(Double.NEGATIVE_INFINITY);
|
||||
fail();
|
||||
} catch (IllegalArgumentException expected) {
|
||||
}
|
||||
try {
|
||||
jsonWriter.value(Double.POSITIVE_INFINITY);
|
||||
fail();
|
||||
} catch (IllegalArgumentException expected) {
|
||||
}
|
||||
}
|
||||
|
||||
public void testDoubles() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
jsonWriter.beginArray();
|
||||
jsonWriter.value(-0.0);
|
||||
jsonWriter.value(1.0);
|
||||
jsonWriter.value(Double.MAX_VALUE);
|
||||
jsonWriter.value(Double.MIN_VALUE);
|
||||
jsonWriter.value(0.0);
|
||||
jsonWriter.value(-0.5);
|
||||
jsonWriter.value(Double.MIN_NORMAL);
|
||||
jsonWriter.value(Math.PI);
|
||||
jsonWriter.value(Math.E);
|
||||
jsonWriter.endArray();
|
||||
jsonWriter.close();
|
||||
assertEquals("[-0.0,"
|
||||
+ "1.0,"
|
||||
+ "1.7976931348623157E308,"
|
||||
+ "4.9E-324,"
|
||||
+ "0.0,"
|
||||
+ "-0.5,"
|
||||
+ "2.2250738585072014E-308,"
|
||||
+ "3.141592653589793,"
|
||||
+ "2.718281828459045]", stringWriter.toString());
|
||||
}
|
||||
|
||||
public void testLongs() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
jsonWriter.beginArray();
|
||||
jsonWriter.value(0);
|
||||
jsonWriter.value(1);
|
||||
jsonWriter.value(-1);
|
||||
jsonWriter.value(Long.MIN_VALUE);
|
||||
jsonWriter.value(Long.MAX_VALUE);
|
||||
jsonWriter.endArray();
|
||||
jsonWriter.close();
|
||||
assertEquals("[0,"
|
||||
+ "1,"
|
||||
+ "-1,"
|
||||
+ "-9223372036854775808,"
|
||||
+ "9223372036854775807]", stringWriter.toString());
|
||||
}
|
||||
|
||||
public void testBooleans() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
jsonWriter.beginArray();
|
||||
jsonWriter.value(true);
|
||||
jsonWriter.value(false);
|
||||
jsonWriter.endArray();
|
||||
assertEquals("[true,false]", stringWriter.toString());
|
||||
}
|
||||
|
||||
public void testNulls() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
jsonWriter.beginArray();
|
||||
jsonWriter.nullValue();
|
||||
jsonWriter.endArray();
|
||||
assertEquals("[null]", stringWriter.toString());
|
||||
}
|
||||
|
||||
public void testStrings() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
jsonWriter.beginArray();
|
||||
jsonWriter.value("a");
|
||||
jsonWriter.value("a\"");
|
||||
jsonWriter.value("\"");
|
||||
jsonWriter.value(":");
|
||||
jsonWriter.value(",");
|
||||
jsonWriter.value("\b");
|
||||
jsonWriter.value("\f");
|
||||
jsonWriter.value("\n");
|
||||
jsonWriter.value("\r");
|
||||
jsonWriter.value("\t");
|
||||
jsonWriter.value(" ");
|
||||
jsonWriter.value("\\");
|
||||
jsonWriter.value("{");
|
||||
jsonWriter.value("}");
|
||||
jsonWriter.value("[");
|
||||
jsonWriter.value("]");
|
||||
jsonWriter.value("\0");
|
||||
jsonWriter.value("\u0019");
|
||||
jsonWriter.endArray();
|
||||
assertEquals("[\"a\","
|
||||
+ "\"a\\\"\","
|
||||
+ "\"\\\"\","
|
||||
+ "\":\","
|
||||
+ "\",\","
|
||||
+ "\"\\b\","
|
||||
+ "\"\\f\","
|
||||
+ "\"\\n\","
|
||||
+ "\"\\r\","
|
||||
+ "\"\\t\","
|
||||
+ "\" \","
|
||||
+ "\"\\\\\","
|
||||
+ "\"{\","
|
||||
+ "\"}\","
|
||||
+ "\"[\","
|
||||
+ "\"]\","
|
||||
+ "\"\\u0000\","
|
||||
+ "\"\\u0019\"]", stringWriter.toString());
|
||||
}
|
||||
|
||||
public void testEmptyArray() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
jsonWriter.beginArray();
|
||||
jsonWriter.endArray();
|
||||
assertEquals("[]", stringWriter.toString());
|
||||
}
|
||||
|
||||
public void testEmptyObject() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
jsonWriter.beginObject();
|
||||
jsonWriter.endObject();
|
||||
assertEquals("{}", stringWriter.toString());
|
||||
}
|
||||
|
||||
public void testObjectsInArrays() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
jsonWriter.beginArray();
|
||||
jsonWriter.beginObject();
|
||||
jsonWriter.name("a").value(5);
|
||||
jsonWriter.name("b").value(false);
|
||||
jsonWriter.endObject();
|
||||
jsonWriter.beginObject();
|
||||
jsonWriter.name("c").value(6);
|
||||
jsonWriter.name("d").value(true);
|
||||
jsonWriter.endObject();
|
||||
jsonWriter.endArray();
|
||||
assertEquals("[{\"a\":5,\"b\":false},"
|
||||
+ "{\"c\":6,\"d\":true}]", stringWriter.toString());
|
||||
}
|
||||
|
||||
public void testArraysInObjects() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
jsonWriter.beginObject();
|
||||
jsonWriter.name("a");
|
||||
jsonWriter.beginArray();
|
||||
jsonWriter.value(5);
|
||||
jsonWriter.value(false);
|
||||
jsonWriter.endArray();
|
||||
jsonWriter.name("b");
|
||||
jsonWriter.beginArray();
|
||||
jsonWriter.value(6);
|
||||
jsonWriter.value(true);
|
||||
jsonWriter.endArray();
|
||||
jsonWriter.endObject();
|
||||
assertEquals("{\"a\":[5,false],"
|
||||
+ "\"b\":[6,true]}", stringWriter.toString());
|
||||
}
|
||||
|
||||
public void testDeepNestingArrays() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
for (int i = 0; i < 20; i++) {
|
||||
jsonWriter.beginArray();
|
||||
}
|
||||
for (int i = 0; i < 20; i++) {
|
||||
jsonWriter.endArray();
|
||||
}
|
||||
assertEquals("[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]", stringWriter.toString());
|
||||
}
|
||||
|
||||
public void testDeepNestingObjects() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
jsonWriter.beginObject();
|
||||
for (int i = 0; i < 20; i++) {
|
||||
jsonWriter.name("a");
|
||||
jsonWriter.beginObject();
|
||||
}
|
||||
for (int i = 0; i < 20; i++) {
|
||||
jsonWriter.endObject();
|
||||
}
|
||||
jsonWriter.endObject();
|
||||
assertEquals("{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":"
|
||||
+ "{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{\"a\":{"
|
||||
+ "}}}}}}}}}}}}}}}}}}}}}", stringWriter.toString());
|
||||
}
|
||||
|
||||
public void testRepeatedName() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
jsonWriter.beginObject();
|
||||
jsonWriter.name("a").value(true);
|
||||
jsonWriter.name("a").value(false);
|
||||
jsonWriter.endObject();
|
||||
// JsonWriter doesn't attempt to detect duplicate names
|
||||
assertEquals("{\"a\":true,\"a\":false}", stringWriter.toString());
|
||||
}
|
||||
|
||||
public void testPrettyPrintObject() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
jsonWriter.setIndentSpaces(3);
|
||||
|
||||
jsonWriter.beginObject();
|
||||
jsonWriter.name("a").value(true);
|
||||
jsonWriter.name("b").value(false);
|
||||
jsonWriter.name("c").value(5.0);
|
||||
jsonWriter.name("e").nullValue();
|
||||
jsonWriter.name("f").beginArray();
|
||||
jsonWriter.value(6.0);
|
||||
jsonWriter.value(7.0);
|
||||
jsonWriter.endArray();
|
||||
jsonWriter.name("g").beginObject();
|
||||
jsonWriter.name("h").value(8.0);
|
||||
jsonWriter.name("i").value(9.0);
|
||||
jsonWriter.endObject();
|
||||
jsonWriter.endObject();
|
||||
|
||||
String expected = "{\n"
|
||||
+ " \"a\": true,\n"
|
||||
+ " \"b\": false,\n"
|
||||
+ " \"c\": 5.0,\n"
|
||||
+ " \"e\": null,\n"
|
||||
+ " \"f\": [\n"
|
||||
+ " 6.0,\n"
|
||||
+ " 7.0\n"
|
||||
+ " ],\n"
|
||||
+ " \"g\": {\n"
|
||||
+ " \"h\": 8.0,\n"
|
||||
+ " \"i\": 9.0\n"
|
||||
+ " }\n"
|
||||
+ "}";
|
||||
assertEquals(expected, stringWriter.toString());
|
||||
}
|
||||
|
||||
public void testPrettyPrintArray() throws IOException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
JsonWriter jsonWriter = new JsonWriter(stringWriter);
|
||||
jsonWriter.setIndentSpaces(3);
|
||||
|
||||
jsonWriter.beginArray();
|
||||
jsonWriter.value(true);
|
||||
jsonWriter.value(false);
|
||||
jsonWriter.value(5.0);
|
||||
jsonWriter.nullValue();
|
||||
jsonWriter.beginObject();
|
||||
jsonWriter.name("a").value(6.0);
|
||||
jsonWriter.name("b").value(7.0);
|
||||
jsonWriter.endObject();
|
||||
jsonWriter.beginArray();
|
||||
jsonWriter.value(8.0);
|
||||
jsonWriter.value(9.0);
|
||||
jsonWriter.endArray();
|
||||
jsonWriter.endArray();
|
||||
|
||||
String expected = "[\n"
|
||||
+ " true,\n"
|
||||
+ " false,\n"
|
||||
+ " 5.0,\n"
|
||||
+ " null,\n"
|
||||
+ " {\n"
|
||||
+ " \"a\": 6.0,\n"
|
||||
+ " \"b\": 7.0\n"
|
||||
+ " },\n"
|
||||
+ " [\n"
|
||||
+ " 8.0,\n"
|
||||
+ " 9.0\n"
|
||||
+ " ]\n"
|
||||
+ "]";
|
||||
assertEquals(expected, stringWriter.toString());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user