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
FieldsModifier and TypeFieldDescriptionstatic final JsonWriteContextFinal writing context.static final JsonWriteContextInitial writing context. -
Method Summary
Modifier and TypeMethodDescriptionGets the parentJsonWriteContext.Gets theJsonWriteStateassociated to the writing context.updateContext(JsonToken token) Updates the context based on theJsonTokenthat was written.voidvalidateToken(JsonToken token) Determines whether theJsonTokenis allowed to be written based on theJsonWriteState.
-
Field Details
-
ROOT
Initial writing context. -
COMPLETED
Final writing context.
-
-
Method Details
-
getParent
Gets the parentJsonWriteContext.ROOTandCOMPLETEDare 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 theJsonWriteStateassociated to the writing context.- Returns:
- The
JsonWriteStateassociated to the writing context.
-
validateToken
Determines whether theJsonTokenis allowed to be written based on theJsonWriteState.The following is the allowed
JsonTokensbased on theJsonWriteState.JsonWriteState.ROOT-JsonToken.START_OBJECT,JsonToken.START_ARRAY,JsonToken.BOOLEAN,JsonToken.NULL,JsonToken.NUMBER,JsonToken.STRINGJsonWriteState.OBJECT-JsonToken.END_OBJECT,JsonToken.FIELD_NAMEJsonWriteState.ARRAY-JsonToken.START_OBJECT,JsonToken.START_ARRAY,JsonToken.END_ARRAY,JsonToken.BOOLEAN,JsonToken.NULL,JsonToken.NUMBER,JsonToken.STRINGJsonWriteState.FIELD-JsonToken.START_OBJECT,JsonToken.START_ARRAY,JsonToken.BOOLEAN,JsonToken.NULL,JsonToken.NUMBER,JsonToken.STRINGJsonWriteState.COMPLETED- none
IllegalStateException.Field and value APIs in
JsonWriter, such asJsonWriter.writeStringField(String, String), will validate withJsonToken.FIELD_NAMEas they're self-closing operations.- Parameters:
token- TheJsonTokenthat is being validated for being writable in the current state.- Throws:
IllegalStateException- If theJsonTokenis invalid based on theJsonWriteState.
-
updateContext
Updates the context based on theJsonTokenthat was written.Tokens
JsonToken.BOOLEAN,JsonToken.NULL,JsonToken.NUMBER, andJsonToken.STRINGcan mutate the current state in three different ways. If the current context isROOTthenCOMPLETEDis the updated context as the JSON stream has completed writing. If the current context isJsonWriteState.ARRAYthen 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.FIELDand writing a value completes the field.Tokens
JsonToken.END_OBJECTandJsonToken.END_ARRAYcomplete the current context and prepare set the parent context for return. If the parent context isROOTthenCOMPLETEDis the updated context as the JSON stream has completed writing. Otherwise, if the parent context isJsonWriteState.FIELDthat will be completed as well as the field has completed writing.Tokens
JsonToken.START_OBJECT,JsonToken.START_ARRAY, andJsonToken.FIELD_NAMEcreate 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- TheJsonTokentriggering the update.- Returns:
- The updated writing context.
-