JsonWriter Class

Definition

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.Interop.IJavaPeerable, 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
JsonWriter
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

JsonWriter(Writer)

Creates a new instance that writes a JSON-encoded stream to out.

Properties

Class

Returns the runtime class of this Object.

(Inherited from Object)
Handle

The handle to the underlying Android instance.

(Inherited from Object)
JniIdentityHashCode (Inherited from Object)
JniPeerMembers
Lenient

Returns true if this writer has relaxed syntax rules. -or- Configure this writer to relax its syntax rules.

PeerReference (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

BeginArray()

Begins encoding a new array.

BeginArrayAsync()
BeginObject()

Begins encoding a new object.

BeginObjectAsync()
Clone()

Creates and returns a copy of this object.

(Inherited from Object)
Close()

Flushes and closes this writer and the underlying Writer.

Dispose() (Inherited from Object)
Dispose(Boolean) (Inherited from Object)
EndArray()

Ends encoding the current array.

EndArrayAsync()
EndObject()

Ends encoding the current object.

EndObjectAsync()
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 Writer and flushes that writer.

FlushAsync()
GetHashCode()

Returns a hash code value for the object.

(Inherited from Object)
JavaFinalize()

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)
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 null.

NullValueAsync()
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.

ToArray<T>() (Inherited from Object)
ToString()

Returns a string representation of the object.

(Inherited from Object)
UnregisterFromRuntime() (Inherited from Object)
Value(Boolean)

Encodes value.

Value(Double)

Encodes value.

Value(Int64)

Encodes value.

Value(Number)

Encodes value.

Value(String)

Encodes value.

ValueAsync(Boolean)
ValueAsync(Double)
ValueAsync(Int64)
ValueAsync(Number)
ValueAsync(String)
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)

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, 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)

Explicit Interface Implementations

IJavaPeerable.Disposed() (Inherited from Object)
IJavaPeerable.DisposeUnlessReferenced() (Inherited from Object)
IJavaPeerable.Finalized() (Inherited from Object)
IJavaPeerable.JniManagedPeerState (Inherited from Object)
IJavaPeerable.SetJniIdentityHashCode(Int32) (Inherited from Object)
IJavaPeerable.SetJniManagedPeerState(JniManagedPeerStates) (Inherited from Object)
IJavaPeerable.SetPeerReference(JniObjectReference) (Inherited from Object)

Extension Methods

JavaCast<TResult>(IJavaObject)

Performs an Android runtime-checked type conversion.

JavaCast<TResult>(IJavaObject)
GetJniTypeName(IJavaPeerable)

Applies to