JPSemanticActionsProtocol Protocol Reference
| Conforms to | NSObject |
| Declared in | JPSemanticActionsProtocol.h |
Overview
JPSemanticActionsProtocol defines the interface between the underlaying JSON parser and the semantic actions object. The protocol must be implemented by concrete subclasses of JPSemanticActionsBase.
The JSON parser will not perform semantic actions by itself, instead it will notify the semantic actions object of certain parse events and provide necessary information for the event.
Parse events are signaled by the underlaying JSON parser via messages sent to the semantic actions object whose signature is prefixed with “parserFound”. The semantic actions object is supposed to implement a corresponding “semantic action” appropriate for the event and the current context and current state.
A “begin” message, that is a parserFoundArrayBegin or a parserFoundObjectBegin
message, indicates the start of a JSON container (a JSON Array, or a JSON Object).
An “end” message, that is a parserFoundArrayEnd or a parserFoundObjectEnd
message, indicates the end of the container which refers to the container
signaled with the “begin” message immediately preceding this “end” message.
Additionally, there are two events parserFoundKeyValuePairBeginWithKey:length:encoding:index:
and parserFoundKeyValuePairEnd which are sent when the parser encountered
a JSON string which is a key in the key-value pair of a JSON Object, and when
the parser encountered the end of the JSON Value associated to this key.
“begin” and “end” messages are strictly balanced and reflect the recursive, respectively hierarchical, structure of a JSON representation.
Finally, the underlaying parser sends messages parserFound<JSON_primitive>, that
is parserFoundString, parserFoundNull, parserFoundBoolean and
parserFoundNumber when it encounters a corresponding JSON primitive value.
If the semantic actions property generateEncodedStrings returns NO (the default)
the parser sends decoded JSON Strings (which include keys) to the semantic actions object.
Otherwise, if the property generateEncodedStrings returns YES, the parser sends
encoded JSON Strings to the semantic actions object. (see also JPSemanticActionsBase)
If the parser uses a representation generator as a semantic actions object, it usually
requires a properly decoded string which is equal to the original source string.
Below is a typical flow of the complete messages:
parserFoundArrayBegin
parserFoundObjectBegin
parserFoundKeyValuePairBeginWithKey:length:encoding:index:
parserFoundArrayBegin
parserFoundValueBeginAtIndex:
parserFoundString:length:hasMore:encoding:
parserFoundString:length:hasMore:encoding:
parserFoundValueEndAtIndex:
parserFoundValueBeginAtIndex:
parserFoundString:length:hasMore:encoding:
parserFoundValueEndAtIndex:
parserFoundArrayEnd
parserFoundKeyValuePairEnd
parserFoundKeyValuePairBeginWithKey:length:encoding:index:
parserFoundArrayBegin
parserFoundValueBeginAtIndex:
parserFoundNumber:length:
parserFoundValueEndAtIndex:
parserFoundValueBeginAtIndex:
parserFoundNumber:length:
parserFoundValueEndAtIndex:
..
parserFoundArrayEnd
parserFoundKeyValuePairEnd
parserFoundObjectEnd
parserFoundObjectBegin
..
parserFoundObjectEnd
parserFoundArrayEnd
A parserFoundString message may be possibly sent repeatedly, if the string
is handled in “chunks”. The method has a parameter hasMore which indicates
when the string is eventually complete. A data string will be split into chunks
if its size becomes large. The exact size of a chunk is dependent on the
implementation. Currently, this equals about 2000 code units. Occasionally,
this may become larger if the implementation can utilize an already allocated
buffer whose size is larger.
Implementing actions for “begin”, “end” and “found primitive value” messages will be usually sufficient to implement a semantic actions class which creates a JSON representation., e.g. with Foundation objects as containers and primitive values.
A concrete implementation would purposefully use an internal stack in order to aid the construction of a JSON representation, e.g. with Foundation objects.
Basically, a “representation generator” could be implemented as follows:
A “begin” message will be the first message sent from the parser – besides the more informative “parserFoundJsonBegin” event. A valid JSON starts with either a JSON Array or a JSON Object.
Now, a “begin” event may not be immediately followed by the corresponding “end” event – rather, since a JSON representation is a hierarchical data structure, “begin” may be followed by one or more other “begin” events. But eventually, the “begin” and “end” become balanced. Nonetheless, the basic principle of building a representation remains simple:
So, on a “begin” message, the generator would create the corresponding container (for Foundation, a NSArray or NSDictionary object) and push it onto its internal stack.
On a “found primitive value” message, the generator would simply construct a corresponding representation (e.g. a Foundation object) for a JSON String (Key), JSON Boolean, and a JSON Null and push them onto the stack as well.
On an “end” message the generator would pop all objects from laying above the current container to the top of the stack and insert them into it. Now, this container will become the top of the stack – which in turn may be an element of another container.
Note that the corresponding action on an “end” message for JSON Array respectively JSON Object do differ slightly, since a JSON Object expects key-value pairs on the stack and a JSON Array expects values in a sequence and must retain the order of elements.
The process ends with the last message, an “end” message, whose action is exactly the same: pop the elements from the stack and insert them into the current container. The current container is, of course, the last remaining object residing in the stack, and this becomes the final result.
The four methods
-parserFoundValueBeginAtIndex:,-parserFoundValueEndAtIndex:,-parserFoundValueBeginWithKey:length:encoding:indexand-parserFoundValueEndWithKey:length:encoding:index
are helpful when implementing a streaming API. These additional messages give fine grained control for all relevant parser events. For more information see JPSemanticActionsProtocol and JPStreamSemanticActions.
For instance, these messages will be required, reconstructing the JSON text from the parser events (possibly converted to another Unicode encoding, with unescaped Unicode characters, removed comments, etc.)
These message are invoked as follows:
parserFoundArrayBegin
parserFoundValueBeginAtIndex:
parserFoundValueEndAtIndex:
parserFoundArrayEnd
parserFoundObjectBegin
parserFoundKeyValuePairBeginWithKey:length:encoding:index:
parserFoundKeyValuePairEnd
parserFoundObjectEnd
Tasks
Other Methods
-
– resultRetrieves the (abstract) result of the semantic actions instance.
-
– setHasBOM:Sets the property hasBOM.
-
– clearClears internal cached data.
-
– cancelTerminates a run as soon as possible.
Delegate Methods
-
– parserFoundJsonBeginSent to the delegate when the JSON parser found the start of a JSON document.
-
– parserFoundJsonEndSent to the delegate when the JSON parser found the end of a JSON document.
-
– parserFinishedSent to the delegate when the JSON parser finished parsing the last JSON document from its input.
-
– parserDetectedErrorSent to the delegate when the JSON parser detected a parse error.
-
– parserFoundArrayBeginSent to the delegate when the JSON parser found the start of a JSON Array, that is when it encountered the ‘[’ character.
-
– parserFoundArrayEndSent to the delegate when the JSON parser found the end of a JSON Array, that is when it encountered the ‘]’ character.
-
– parserFoundObjectBeginSent to the delegate when the JSON parser found the start of a JSON Object, that is when it encountered the ‘{’ character.
-
– parserFoundObjectEndSent to the delegate when the JSON parser found the end of a JSON Object, that is when it encountered the ‘}’ character.
-
– parserFoundKeyValuePairBeginWithKey:length:encoding:index:Sent to the delegate when the parser found the start of the nth key-value pair within a JSON Object.
-
– parserFoundKeyValuePairEndSent to the delegate when the parser found the end of a key-value pair of a JSON Object.
-
– parserFoundValueBeginAtIndex:Sent to the delegate when the parser found the start of a JSON Value belonging to an JSON Array at the specified index.
-
– parserFoundValueEndAtIndex:Sent to the delegate when the parser found the end of a JSON Value belonging to an JSON Array at the specified index.
-
– parserFoundString:length:hasMore:encoding:Sent to the delegate when the JSON parser found a JSON String as a value.
-
– parserFoundNumber:length:Sent to the delegate when the JSON parser found a JSON Number.
-
– parserFoundBoolean:Sent to the delegate when the JSON parser found a JSON Boolean.
-
– parserFoundNullSent to the delegate when the JSON parser found a JSON Null.
Instance Methods
cancel
Terminates a run as soon as possible.
- (void)cancelDiscussion
The error state after a cancel may be undefined, but ideally it should be “canceled”. JPAsyncJsonParser will invoke this method when itself receiving the cancel message.
Declared In
JPSemanticActionsProtocol.hclear
Clears internal cached data.
- (void)clearDiscussion
Method clear shall clear internal data which has been collected during a run. That is, it shall reset error information and temporary data like caches.
Note: JPJsonParser and JPAsyncJsonParser do not invoke this method.
Declared In
JPSemanticActionsProtocol.hparserDetectedError
Sent to the delegate when the JSON parser detected a parse error.
- (void)parserDetectedErrorDiscussion
Delegates should implement this method as a minimal implementation in order to perform appropriate actions when the JSON parser detected a parse error.
Declared In
JPSemanticActionsProtocol.hparserFinished
Sent to the delegate when the JSON parser finished parsing the last JSON document from its input.
- (void)parserFinishedDiscussion
Delegates should implement this method as a minimal implementation in order to perform appropriate actions when the JSON parser finished to parse the input which possibly contains one or more JSON documents.
Declared In
JPSemanticActionsProtocol.hparserFoundArrayBegin
Sent to the delegate when the JSON parser found the start of a JSON Array, that is when it encountered the ‘[’ character.
- (void)parserFoundArrayBeginDeclared In
JPSemanticActionsProtocol.hparserFoundArrayEnd
Sent to the delegate when the JSON parser found the end of a JSON Array, that is when it encountered the ‘]’ character.
- (void)parserFoundArrayEndDeclared In
JPSemanticActionsProtocol.hparserFoundBoolean:
Sent to the delegate when the JSON parser found a JSON Boolean.
- (void)parserFoundBoolean:(BOOL)valueParameters
- value
Corresponds to the boolean value of the JSON Boolean.
Declared In
JPSemanticActionsProtocol.hparserFoundJsonBegin
Sent to the delegate when the JSON parser found the start of a JSON document.
- (void)parserFoundJsonBeginDiscussion
Delegates should implement this method as a minimal implementation in order to perform appropriate actions when the JSON parser starts to parse a JSON document.
Declared In
JPSemanticActionsProtocol.hparserFoundJsonEnd
Sent to the delegate when the JSON parser found the end of a JSON document.
- (void)parserFoundJsonEndDeclared In
JPSemanticActionsProtocol.hparserFoundKeyValuePairBeginWithKey:length:encoding:index:
Sent to the delegate when the parser found the start of the nth key-value pair within a JSON Object.
- (void)parserFoundKeyValuePairBeginWithKey:(const void *)bytes length:(size_t)length encoding:(NSStringEncoding)encoding index:(size_t)indexParameters
- bytes
A const void pointer to the start of the sequence of the JSON String.
- length
The number of bytes of the character sequence.
- encoding
The Unicode encoding scheme for the character sequence.
- index
The current index of the key-value pair of the JSON Object.
Discussion
If property generateEncodedStrings equals NO (default) the JSON parser will
pass the key as a decoded JSON String. If property generateEncodedStrings
equals YES the character sequence represents an encoded JSON string as specified
in RFC 4627.
Decoding a JSON string involves unescaping, possibly replacing certain Unicode characters with their replacement character as specified in the semantic actions configuration, and possibly converting from the source encoding to the specified Unicode encoding form. The resulting string should compare equal to the original string.
Subsequently, the delegate will be sent parse events which constitute the value associated to the key.
Note: The specified Unicode encoding scheme in parameter encoding
corresponds to the Library Build Option JSON_SEMANTIC_ACTIONS_STRING_ENCODING and
cannot be selected at runtime.
Declared In
JPSemanticActionsProtocol.hparserFoundKeyValuePairEnd
Sent to the delegate when the parser found the end of a key-value pair of a JSON Object.
- (void)parserFoundKeyValuePairEndDeclared In
JPSemanticActionsProtocol.hparserFoundNull
Sent to the delegate when the JSON parser found a JSON Null.
- (void)parserFoundNullDeclared In
JPSemanticActionsProtocol.hparserFoundNumber:length:
Sent to the delegate when the JSON parser found a JSON Number.
- (void)parserFoundNumber:(const char *)numberString length:(size_t)lengthParameters
- numberString
A const char pointer to the start of the sequence of the JSON Number encoded in ASCII.
- length
The number of bytes of the character sequence.
Discussion
The JSON parser will pass the JSON Number “as is”, though converted into ASCII which for number strings is equal to UTF-8 encoding.
Declared In
JPSemanticActionsProtocol.hparserFoundObjectBegin
Sent to the delegate when the JSON parser found the start of a JSON Object, that is when it encountered the ‘{’ character.
- (void)parserFoundObjectBeginDeclared In
JPSemanticActionsProtocol.hparserFoundObjectEnd
Sent to the delegate when the JSON parser found the end of a JSON Object, that is when it encountered the ‘}’ character.
- (bool)parserFoundObjectEndDeclared In
JPSemanticActionsProtocol.hparserFoundString:length:hasMore:encoding:
Sent to the delegate when the JSON parser found a JSON String as a value.
- (void)parserFoundString:(const void *)bytes length:(size_t)length hasMore:(BOOL)hasMore encoding:(NSStringEncoding)encodingParameters
- bytes
A void pointer to the start of the possibly partial sequence of the JSON String.
- length
The number of bytes of the sequence.
- hasMore
A boolean value which equals
YESin order to indicate that there are more characters available for this JSON string.
- encoding
The Unicode encoding scheme for the character sequence.
Discussion
If parameter hasMore equals YES, the parser found a large string and is
delivering the JSON String in several chunks.
If property generateEncodedStrings equals NO (default) each chunk has been
decoded and should compare equal to the original string. If property generateEncodedStrings
equals YES the character sequence represents an encoded JSON string as specified
in RFC 4627.
In case of multibyte encodings, it is guaranteed that the byte sequence will always end at a complete character boundary.
The parser will send as many chunks as necessary to complement the string through sending consecutive messages to the receiver.
Decoding a JSON string involves unescaping, possibly replacing certain Unicode characters with their replacement character as specified in the semantic actions configuration, and possibly converting from the source encoding to the specified Unicode encoding form. The resulting string should compare equal to the original string.
Note: The specified Unicode encoding scheme in parameter encoding
corresponds to the Library Build Option JSON_SEMANTIC_ACTIONS_STRING_ENCODING and
cannot be selected at runtime.
Declared In
JPSemanticActionsProtocol.hparserFoundValueBeginAtIndex:
Sent to the delegate when the parser found the start of a JSON Value belonging to an JSON Array at the specified index.
- (void)parserFoundValueBeginAtIndex:(size_t)indexParameters
- index
The index of at which the value is added to the JSON Array.
Declared In
JPSemanticActionsProtocol.hparserFoundValueEndAtIndex:
Sent to the delegate when the parser found the end of a JSON Value belonging to an JSON Array at the specified index.
- (void)parserFoundValueEndAtIndex:(size_t)indexParameters
- index
The index of at which the value is added to the JSON Array.
Discussion
The associated value at this index has been notified by the parser by the corresponding event which has been sent immediately before this message.
Declared In
JPSemanticActionsProtocol.hresult
Retrieves the (abstract) result of the semantic actions instance.
- (id)resultDiscussion
The property may return nil.
Declared In
JPSemanticActionsProtocol.hsetHasBOM:
Sets the property hasBOM.
- (void)setHasBOM:(BOOL)valueParameters
- value
A boolean value.
Discussion
The setHasBOM method will be invoked by the JPJsonParser and JPAsyncJsonParser when they try to determine the encoding of the input and thereby check for a BOM. The semantic actions instance may use this information if required.
Declared In
JPSemanticActionsProtocol.h