JsonWriteContext Class
- java.
lang. Object - com.
azure. json. JsonWriteContext
- com.
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
Json |
COMPLETED
Final writing context. |
static final
Json |
ROOT
Initial writing context. |
Method Summary
Modifier and Type | Method and Description |
---|---|
Json |
getParent()
Gets the parent JsonWriteContext. |
Json |
getWriteState()
Gets the JsonWriteState associated to the writing context. |
Json |
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:
getWriteState
public JsonWriteState getWriteState()
Gets the JsonWriteState associated to the writing context.
Returns:
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:
Returns:
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.
- ROOT - START_OBJECT, START_ARRAY, BOOLEAN, NULL, NUMBER, STRING
- OBJECT - END_OBJECT, FIELD_NAME
- ARRAY - START_OBJECT, START_ARRAY, END_ARRAY, BOOLEAN, NULL, NUMBER, STRING
- FIELD - START_OBJECT, START_ARRAY, BOOLEAN, NULL, NUMBER, STRING
- COMPLETED - none
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:
Applies to
Azure SDK for Java