共用方式為


JsonWriteContext Class

  • java.lang.Object
    • com.azure.json.JsonWriteContext

public final class JsonWriteContext

Context of JSON handling.

Writing context is immutable, any calls to updateContext(JsonToken token) will result in either a previous context being returned or the creation of a new context.

Field Summary

Modifier and Type Field and Description
static final JsonWriteContext COMPLETED

Final writing context.

static final JsonWriteContext ROOT

Initial writing context.

Method Summary

Modifier and Type Method and Description
JsonWriteContext getParent()

Gets the parent JsonWriteContext.

JsonWriteState getWriteState()

Gets the JsonWriteState associated to the writing context.

JsonWriteContext updateContext(JsonToken token)

Updates the context based on the JsonToken that was written.

void validateToken(JsonToken token)

Determines whether the JsonToken is allowed to be written based on the JsonWriteState.

Methods inherited from java.lang.Object

Field Details

COMPLETED

public static final JsonWriteContext COMPLETED

Final writing context.

ROOT

public static final JsonWriteContext ROOT

Initial writing context.

Method Details

getParent

public JsonWriteContext getParent()

Gets the parent JsonWriteContext.

ROOT and COMPLETED are terminal writing contexts and don't have parent contexts. These are the only writing contexts that will return null.

Returns:

The parent writing context.

getWriteState

public JsonWriteState getWriteState()

Gets the JsonWriteState associated to the writing context.

Returns:

The JsonWriteState associated to the writing context.

updateContext

public JsonWriteContext updateContext(JsonToken token)

Updates the context based on the JsonToken that was written.

Tokens BOOLEAN, NULL, NUMBER, and STRING can mutate the current state in three different ways. If the current context is ROOT then COMPLETED is the updated context as the JSON stream has completed writing. If the current context is ARRAY then this context is returned without mutation as writing an element to an array doesn't complete the array. Otherwise, the parent context is returned as the only other legal context is FIELD and writing a value completes the field.

Tokens END_OBJECT and END_ARRAY complete the current context and prepare set the parent context for return. If the parent context is ROOT then COMPLETED is the updated context as the JSON stream has completed writing. Otherwise, if the parent context is FIELD that will be completed as well as the field has completed writing.

Tokens START_OBJECT, START_ARRAY, and FIELD_NAME create a child context where the current context becomes the parent context.

Field and value APIs in JsonWriter, such as writeStringField(String fieldName, String value), are self-closing operations that will maintain the current context.

Parameters:

token - The JsonToken triggering the update.

Returns:

The updated writing context.

validateToken

public void validateToken(JsonToken token)

Determines whether the JsonToken is allowed to be written based on the JsonWriteState.

The following is the allowed JsonToken based on the JsonWriteState.

Any token that isn't allowed based on the context will result in an IllegalStateException.

Field and value APIs in JsonWriter, such as writeStringField(String fieldName, String value), will validate with FIELD_NAME as they're self-closing operations.

Parameters:

token - The JsonToken that is being validated for being writable in the current state.

Applies to