JsonWriter Class
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Writes a JSON (RFC 4627) encoded value to a stream, one token at a time.
[Android.Runtime.Register("android/util/JsonWriter", DoNotGenerateAcw=true)]
public sealed class JsonWriter : Java.Lang.Object, IDisposable, Java.IO.ICloseable
[<Android.Runtime.Register("android/util/JsonWriter", DoNotGenerateAcw=true)>]
type JsonWriter = class
inherit Object
interface ICloseable
interface IJavaObject
interface IDisposable
interface IJavaPeerable
- Inheritance
- Attributes
- Implements
Remarks
Writes a JSON (RFC 4627) 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 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 #beginArray(). Write each of the array's elements with the appropriate #value methods or by nesting other arrays and objects. Finally close the array using #endArray(). <li>To write <strong>objects</strong>, first call #beginObject(). Write each of the object's properties by alternating calls to #name with the property's value. Write property values with the appropriate #value method or by nesting other objects or arrays. Finally close the object using #endObject(). </ul>
<h3>Example</h3> Suppose we'd like to encode a stream of messages such as the following:
{@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
}
}
]}
This code encodes the above structure:
{@code
public void writeJsonStream(OutputStream out, List<Message> messages) throws IOException {
JsonWriter writer = new JsonWriter(new OutputStreamWriter(out, "UTF-8"));
writer.setIndent(" ");
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();
}}
Each 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 IllegalStateException.
Java documentation for android.util.JsonWriter.
Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.
Constructors
| Name | Description |
|---|---|
| JsonWriter(Writer) |
Creates a new instance that writes a JSON-encoded stream to |
Properties
| Name | Description |
|---|---|
| Class |
Returns the runtime class of this |
| Handle |
The handle to the underlying Android instance. (Inherited from Object) |
| JniIdentityHashCode |
Gets the identity hash code assigned to this Java peer by the interop runtime. (Inherited from Object) |
| JniManagedPeerState |
Gets the state flags last assigned by the runtime. (Inherited from JavaObject) |
| JniPeerMembers |
Gets the JNI metadata used to access members of this Java binding. |
| Lenient |
Returns true if this writer has relaxed syntax rules. -or- Configure this writer to relax its syntax rules. |
| PeerReference |
Gets the JNI object reference for this Java peer. (Inherited from Object) |
| ThresholdClass |
This API supports the Mono for Android infrastructure and is not intended to be used directly from your code. (Inherited from Object) |
| ThresholdType |
This API supports the Mono for Android infrastructure and is not intended to be used directly from your code. (Inherited from Object) |
Methods
| Name | Description |
|---|---|
| BeginArray() |
Begins encoding a new array. |
| BeginArrayAsync() |
Runs BeginArray() asynchronously. |
| BeginObject() |
Begins encoding a new object. |
| BeginObjectAsync() |
Runs BeginObject() asynchronously. |
| Clone() |
Creates and returns a copy of this object. (Inherited from Object) |
| Close() |
Flushes and closes this writer and the underlying |
| Construct(JniObjectReference, JniObjectReferenceOptions) |
Delegates construction of this peer to the runtime's value manager. (Inherited from JavaObject) |
| Dispose() |
Releases the resources held by this Java peer. (Inherited from Object) |
| Dispose(Boolean) |
Releases the resources held by this Java peer. (Inherited from Object) |
| DisposeUnlessReferenced() |
Asks the value manager to dispose this peer unless its mapping retains this same managed instance. (Inherited from JavaObject) |
| EndArray() |
Ends encoding the current array. |
| EndArrayAsync() |
Runs EndArray() asynchronously. |
| EndObject() |
Ends encoding the current object. |
| EndObjectAsync() |
Runs EndObject() asynchronously. |
| Equals(Object) |
Tests whether another managed peer refers to the same Java object. (Inherited from JavaObject) |
| Equals(Object) |
Indicates whether some other object is "equal to" this one. (Inherited from Object) |
| Flush() |
Ensures all buffered data is written to the underlying |
| FlushAsync() |
Runs Flush() asynchronously. |
| GetHashCode() |
Returns a hash code value for the object. (Inherited from Object) |
| JavaFinalize() |
Obsolete.
Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. (Inherited from Object) |
| Name(String) |
Encodes the property name. |
| NameAsync(String) |
Runs Name(String) asynchronously. |
| Notify() |
Wakes up a single thread that is waiting on this object's monitor. (Inherited from Object) |
| NotifyAll() |
Wakes up all threads that are waiting on this object's monitor. (Inherited from Object) |
| NullValue() |
Encodes |
| NullValueAsync() |
Runs NullValue() asynchronously. |
| SetHandle(IntPtr, JniHandleOwnership) |
Sets the Handle property. (Inherited from Object) |
| SetIndent(String) |
Sets the indentation string to be repeated for each level of indentation in the encoded document. |
| SetPeerReference(JniObjectReference, JniObjectReferenceOptions) |
Stores a reference value in the control block without creating a JNI reference copy. (Inherited from JavaObject) |
| ToArray<T>() |
Creates a managed array from this Java array wrapper. (Inherited from Object) |
| ToString() |
Returns a string representation of the object. (Inherited from Object) |
| UnregisterFromRuntime() |
Unregisters this Java peer from the interop runtime. (Inherited from Object) |
| Value(Boolean) |
Encodes |
| Value(Double) |
Encodes |
| Value(Int64) |
Encodes |
| Value(Number) |
Encodes |
| Value(String) |
Encodes |
| ValueAsync(Boolean) |
Runs Value(Boolean) asynchronously. |
| ValueAsync(Double) |
Runs Value(Double) asynchronously. |
| ValueAsync(Int64) |
Runs Value(Int64) asynchronously. |
| ValueAsync(Number) |
Runs Value(Number) asynchronously. |
| ValueAsync(String) |
Runs Value(String) asynchronously. |
| Wait() |
Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>. (Inherited from Object) |
| Wait(Int64, Int32) |
Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed. (Inherited from Object) |
| Wait(Int64) |
Causes the current thread to wait until it is awakened, typically by being <em>notified</em> or <em>interrupted</em>, or until a certain amount of real time has elapsed. (Inherited from Object) |
Explicit Interface Implementations
| Name | Description |
|---|---|
| IJavaPeerable.Disposed() |
Marks this peer as disposed and calls |
| IJavaPeerable.Finalized() |
Marks this peer as disposed and calls |
| IJavaPeerable.JniObjectReferenceControlBlock |
Gets the native control-block pointer used by this peer. (Inherited from JavaObject) |
| IJavaPeerable.SetJniIdentityHashCode(Int32) |
Stores the identity hash code supplied by the runtime. (Inherited from JavaObject) |
| IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates) |
Replaces the stored managed peer state flags. (Inherited from JavaObject) |
| IJavaPeerable.SetPeerReference(JniObjectReference) |
Updates the peer reference and releases a disposed peer's control block when the reference is invalid. (Inherited from JavaObject) |
| IJavaPeerable.UnregisterFromRuntime() |
Removes this instance from the runtime's Java object lookup. |
Extension Methods
| Name | Description |
|---|---|
| GetJniTypeName(IJavaPeerable) |
Gets the JNI name of the type of the instance |
| JavaAs<TResult>(IJavaPeerable) |
Try to coerce |
| JavaCast<TResult>(IJavaObject) |
Performs an Android runtime-checked type conversion. |
| JavaCast<TResult>(IJavaObject) |
Reuses a compatible managed peer or obtains a wrapper of the requested type. |
| TryJavaCast<TResult>(IJavaPeerable, TResult) |
Try to coerce |