Class JsonWriteContext
Writing context is immutable, any calls to updateContext(JsonToken)
will result in either a previous context
being returned or the creation of a new context.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final JsonWriteContext
Final writing context.static final JsonWriteContext
Initial writing context. -
Method Summary
Modifier and TypeMethodDescriptionGets the parentJsonWriteContext
.Gets theJsonWriteState
associated to the writing context.updateContext
(JsonToken token) Updates the context based on theJsonToken
that was written.void
validateToken
(JsonToken token) Determines whether theJsonToken
is allowed to be written based on theJsonWriteState
.
-
Field Details
-
ROOT
Initial writing context. -
COMPLETED
Final writing context.
-
-
Method Details
-
getParent
Gets the parentJsonWriteContext
.ROOT
andCOMPLETED
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
Gets theJsonWriteState
associated to the writing context.- Returns:
- The
JsonWriteState
associated to the writing context.
-
validateToken
Determines whether theJsonToken
is allowed to be written based on theJsonWriteState
.The following is the allowed
JsonTokens
based on theJsonWriteState
.JsonWriteState.ROOT
-JsonToken.START_OBJECT
,JsonToken.START_ARRAY
,JsonToken.BOOLEAN
,JsonToken.NULL
,JsonToken.NUMBER
,JsonToken.STRING
JsonWriteState.OBJECT
-JsonToken.END_OBJECT
,JsonToken.FIELD_NAME
JsonWriteState.ARRAY
-JsonToken.START_OBJECT
,JsonToken.START_ARRAY
,JsonToken.END_ARRAY
,JsonToken.BOOLEAN
,JsonToken.NULL
,JsonToken.NUMBER
,JsonToken.STRING
JsonWriteState.FIELD
-JsonToken.START_OBJECT
,JsonToken.START_ARRAY
,JsonToken.BOOLEAN
,JsonToken.NULL
,JsonToken.NUMBER
,JsonToken.STRING
JsonWriteState.COMPLETED
- none
IllegalStateException
.Field and value APIs in
JsonWriter
, such asJsonWriter.writeStringField(String, String)
, will validate withJsonToken.FIELD_NAME
as they're self-closing operations.- Parameters:
token
- TheJsonToken
that is being validated for being writable in the current state.- Throws:
IllegalStateException
- If theJsonToken
is invalid based on theJsonWriteState
.
-
updateContext
Updates the context based on theJsonToken
that was written.Tokens
JsonToken.BOOLEAN
,JsonToken.NULL
,JsonToken.NUMBER
, andJsonToken.STRING
can mutate the current state in three different ways. If the current context isROOT
thenCOMPLETED
is the updated context as the JSON stream has completed writing. If the current context isJsonWriteState.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 isJsonWriteState.FIELD
and writing a value completes the field.Tokens
JsonToken.END_OBJECT
andJsonToken.END_ARRAY
complete the current context and prepare set the parent context for return. If the parent context isROOT
thenCOMPLETED
is the updated context as the JSON stream has completed writing. Otherwise, if the parent context isJsonWriteState.FIELD
that will be completed as well as the field has completed writing.Tokens
JsonToken.START_OBJECT
,JsonToken.START_ARRAY
, andJsonToken.FIELD_NAME
create a child context where the current context becomes the parent context.Field and value APIs in
JsonWriter
, such asJsonWriter.writeStringField(String, String)
, are self-closing operations that will maintain the current context.- Parameters:
token
- TheJsonToken
triggering the update.- Returns:
- The updated writing context.
-