Json.Utf8JsonWriter

Write UTF-8 encoded JSON text with no caching

WSupported on Windows
USupported on Unix
VSupported on OpenVMS

 

namespace Json
public class Utf8JsonWriter

The Utf8JsonWriter class provides an API to write UTF-8 encoded JSON text sequentially with no caching. The methods below that end with “Value” are intended to be used when writing an array element. If you’re writing members of a JSON object, you should use the name/value pair forms instead.

Methods

CreateUtf8JsonWriter

public CreateUtf8JsonWriter(targetStream), @Utf8JsonWriter

Creates a Utf8JsonWriter, passing in a StringBuilder object that will act as a buffer for the requested JSON data.

Flush

public Flush(), void 

Ensures all written data has been encoded into the StringBuilder buffer that was passed in during construction, making it visible to the output destination.

Reset

public Reset(), void

Resets the internal state of this instance but does not clear the StringBuilder buffer that was passed in during construction.

WriteBase64String

public WriteBase64String(name, data), void

Writes a name/value pair where the value is a string that needs to be Base64 encoded.

WriteBase64StringValue

public WriteBase64StringValue(data), void

Writes a string value as a Base64-encoded JSON string as an element of a JSON array.

WriteBoolean

public WriteBoolean(name, data), void

Writes a name/value pair where the value is a JSON literal true or false.

WriteBooleanValue

public WriteBooleanValue(data), void

Writes a Boolean value as a JSON literal true or false as an element of a JSON array.

WriteEndArray

public WriteEndArray(), void

Closes an open array. Use with WriteStartArray.

WriteEndObject

public WriteEndObject(), void

Closes an open object. Use with WriteStartObject,

WriteNull

public WriteNull(name), void

Writes a name/value pair where the value is null.

WriteNullValue

public WriteNullValue(), voidl

Writes a null value as an element of a JSON array.

WriteNumber

public WriteNumber(name, data), void

Writes a name/value pair where the value is a number (integer, decimal, or implied-decimal).

WriteNumberValue

public WriteNumberValue(data), void

Writes a number (integer, decimal, or implied-decimal) as an element of a JSON array.

WriteStartArray

public WriteStartArray(), void

Writes an unnamed beginning of a JSON array.

or

public WriteStartArray(name), void

Writes the beginning of a JSON array at a member location specified by name. If you provide a name, it creates a key/value pair of the specified name and the array.

Either form of WriteStartArray must be paired with WriteEndArray.

WriteStartObject

public WriteStartObject(), void

Writes an unnamed beginning of a JSON object.

or

public WriteStartObject(name), void

Writes the beginning of aJSON object at a member location specified by name. If you provide a name, it creates a key/value pair of the specified name and the object.

Either form of WriteStartObject must be paired with WriteEndObject.

WriteString

public WriteString(name, data), void

Writes a name/value pair of a JSON object where both the name and value are strings.

WriteStringValue

public WriteStringValue(data), void

Writes a string value as an element of a JSON array

Examples

Sample JSON:

{
     [ 
          {
               “name”: “fred”,
               “height”:182,
               “active”: true,
               “properties”: [null, 10]
          }
     ]
}

This would make that sample:

data jsonBuffer = new StringBuilder()
data jsonWriter = Utf8JsonWriter.CreateUtf8JsonWriter(jsonBuffer)
jsonWriter.WriteStartObject()
jsonWriter.WriteStartArray()
jsonWriter.WriteStartObject()
jsonWriter.WriteString(“name”, “fred”)
jsonWriter.WriteNumber(“height”, 182)
jsonWriter.WriteBoolean(“active”, true)
jsonWriter.WriteStartArray(“properties”)
jsonWriter.WriteNullValue()
jsonWriter.WriteNumberValue(10)
jsonWriter.WriteEndArray()
jsonWriter.WriteEndObject()
jsonWriter.WriteEndArray()
jsonWriter.WriteEndObject()
jsonWriter.Flush()

Also see Understanding the Synergy JSON API for another example.