Android Guides | Samples

Android.Util.JsonWriter Class

Writes a JSON () encoded value to a stream, one token at a time.

See Also: JsonWriter


[Android.Runtime.Register("android/util/JsonWriter", DoNotGenerateAcw=true)]
public sealed class JsonWriter : Object, ICloseable, IDisposable


Writes a JSON () 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.

Encoding JSON

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:


Suppose we'd like to encode a stream of messages such as the following:

java Example

     "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:

java Example

public void writeJsonStream(OutputStream out, List messages) throws IOException {
     JsonWriter writer = new JsonWriter(new OutputStreamWriter(out, "UTF-8"));
     writer.setIndent("  ");
     writeMessagesArray(writer, messages);

   public void writeMessagesArray(JsonWriter writer, List messages) throws IOException {
     for (Message message : messages) {
       writeMessage(writer, message);

   public void writeMessage(JsonWriter writer, Message message) throws IOException {
     if (message.getGeo() != null) {"geo");
       writeDoublesArray(writer, message.getGeo());
     } else {"geo").nullValue();
     writeUser(writer, message.getUser());

   public void writeUser(JsonWriter writer, User user) throws IOException {

   public void writeDoublesArray(JsonWriter writer, List doubles) throws IOException {
     for (Double value : doubles) {

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.

[Android Documentation]


Namespace: Android.Util
Assembly: Mono.Android (in Mono.Android.dll)
Assembly Versions:
Since: Added in API level 11

The members of Android.Util.JsonWriter are listed below.

See Also: Object

Public Constructors

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

Public Properties

LenientBoolean. Returns true if this writer has relaxed syntax rules.

Protected Properties

ThresholdClassIntPtr. This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.
ThresholdTypeType. This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

Public Methods

BeginArray() : JsonWriter
Begins encoding a new array.
BeginArrayAsync() : System.Threading.Tasks.Task<JsonWriter>
BeginObject() : JsonWriter
Begins encoding a new object.
BeginObjectAsync() : System.Threading.Tasks.Task<JsonWriter>
Flushes and closes this writer and the underlying Writer.
EndArray() : JsonWriter
Ends encoding the current array.
EndArrayAsync() : System.Threading.Tasks.Task<JsonWriter>
EndObject() : JsonWriter
Ends encoding the current object.
EndObjectAsync() : System.Threading.Tasks.Task<JsonWriter>
Ensures all buffered data is written to the underlying Writer and flushes that writer.
FlushAsync() : Task
Name(String) : JsonWriter
Encodes the property name.
NameAsync(String) : System.Threading.Tasks.Task<JsonWriter>
NullValue() : JsonWriter
Encodes null.
NullValueAsync() : System.Threading.Tasks.Task<JsonWriter>
Sets the indentation string to be repeated for each level of indentation in the encoded document.
Value(Number) : JsonWriter
Encodes value.
Value(Boolean) : JsonWriter
Encodes value.
Value(Double) : JsonWriter
Encodes value.
Value(Int64) : JsonWriter
Encodes value.
Value(String) : JsonWriter
Encodes value.
ValueAsync(Number) : System.Threading.Tasks.Task<JsonWriter>
ValueAsync(Boolean) : System.Threading.Tasks.Task<JsonWriter>
ValueAsync(Double) : System.Threading.Tasks.Task<JsonWriter>
ValueAsync(Int64) : System.Threading.Tasks.Task<JsonWriter>
ValueAsync(String) : System.Threading.Tasks.Task<JsonWriter>