Package: json-visitor
PyJsonVisitorImplements aJSONparser using the Visitor pattern.Table of ContentsVisitor PatternUsageAdaptersSimple AdaptersProvided Simple AdaptersContextual AdaptersProvided Contextual AdaptersSubscriptionsTerminal UtilityAcknowledgementsVisitor PatternTheVisitor patternis a structural software pattern which allows the separation of an algorithm from the algorithm's input through the use of contextual event handlers. ABaseAdapterclass provides the contextual event handlers for differentJSONelement types and a child class overrides only the event handlers required by the specific algorithm. Since each adapter is completely independent and a specific algorithm's implementation is separated from the input JSON elements, multiple adapters can process the same JSON element events in parallel without interferrence.UsageNormal usage would consist of writing anadapterderived from aBaseAdapterto handle the published events, although twosubscriptionshave been provided for external subscriber event publishing and aterminal utilityhas been provided to expose the event sequence for adapter development.AdaptersThere are two categories of adapters provided,simple adaptersandcontextual adapters. The primary difference between the simple adapters and the contextual adapters are amount of scope data stored through the iteration: the simple adapters do not store scope data while the contextual adapters store scope data applicable to the scope.Simple AdaptersThe simple adapters provide the following event handlers:before_document_start()process_document_start()after_document_start()before_document_end()process_document_end()after_document_end()before_object_start()process_object_start()after_object_start()before_object_end()process_object_end()after_object_end()before_list_start()process_list_start()after_list_start()before_list_end()process_list_end()after_list_end()before_list_item_start()process_list_item_start()after_list_item_start()before_list_item_end()process_list_item_end()after_list_item_end()before_list_item_value_start()process_list_item_value_start()after_list_item_value_start()before_list_item_value_end()process_list_item_value_end()after_list_item_value_end()before_member_start()process_member_start()after_member_start()before_member_end()process_member_end()after_member_end()before_member_key( name )process_member_key( name )after_member_key( name )before_member_value_start()process_member_value_start()after_member_value_start()before_member_value_end()process_member_value_end()after_member_value_end()before_value( value )process_value( value )after_value( value )The simple adapters do not store any element scope data.Provided Simple AdaptersBaseAdapter: The parent adapter class from which all other adapters are derived.CompositeAdapter: This is an adapter contains a list of other adapters. When an event is handled by theCompositeAdapter, the event is published to all adapters theCompositeAdapterhas in its list. TheCompositeAdapterinherits from theBaseAdapter.ScopeAdapter: This adapter builds the element scope for any element as the JSON file is being parsed. TheScopeAdapterinherits from theBaseAdapter.ScopeInspectionAdapter: This adapter prints the node events from the JSON file to standard output as the JSON file is being parsed; only theprocess_*event handlers are invoked. TheScopeInspectionAdapterinherits from theScopeAdapter.Contextual AdaptersThe contextual adapters provide all of the event handlers provided by the simple adapters as well as the following event handlers:before_document( root_scope)process_document( root_scope )after_document( root_scope )before_object( members )process_object( members )after_object( members )before_member( name, value )process_member( name, value )after_member( name, value )before_list( items )process_list( items )after_list( items )before_list_item( index, value )process_list_item( index, value )after_list_item( index, value )The contextual adapters store the necessary element scope data required to associate an element with the element's subordinate data.Provided Contextual AdaptersBaseAdapter: The parent adapter class from which all other contextual adapters are derived. TheBaseAdapterinherits from theScopeAdapter.CompositeAdapter: This adapter contains a list of other adapters. When an event is handled by theCompositeAdapter, the event is published to all adapters theCompositeAdapterhas in its list. TheCompositeAdapterinherits from the contextualBaseAdapter.InspectionAdapter: This adapter prints the node events from the JSON file to standard output as the JSON file is being parsed; only theprocess_*event handlers are invoked. TheInspectionAdapterinherits from the contextualBaseAdapterand theScopeInspectionAdapter.Default Event Handler BehaviorThe event handlers implemented in theBaseAdapterclasses call a different method based on the event handler's timing; this method as implemented in the simpleBaseAdapterclass has an empty body but can be overridden in a child class to apply a default behavior to all event handlers with specific timing. The methods are as follows:Thebefore_*event handlers invoke thedefault_before( *args, **kwargs )method.Theprocess_*event handlers invoke thedefault_process( *args, **kwargs )method.Theafter_*event handlers invoke thedefault_after( *args, **kwargs )method.SubscriptionsTwo subscriptions are provided for event consumption by adapter-external subscribers. Callbacks are registered with the subscription using theregister( subscription_key, callback )method; valid subscription keys are retrieved using thesubscription_keysproperty.SimpleSubscription: This subscription publishes the events in the simpleBaseAdapter.ContextualSubscription: This subscription publishes the events in the contextualBaseAdapter.Terminal UtilityIf thejson_visitorpackage is invoked on the terminal (usingpython3 -m json_visitor), theInspectionAdapterprints out the JSON element nodes in a given input source. The utility's help describes the options and inputs supported:usage: json_visitor [-h] [-i | -I] [-f <file path>] [-s <string literal>]

Processes the input JSON strings or files and outputs the visitation events.

optional arguments:
  -h, --help            show this help message and exit
  -i, --output-processing-info
                        Enables the output of the processing information. This
                        is the default.
  -I, --suppress-processing-info
                        Suppresses the output of the processing information.
  -f <file path>, --file <file path>
                        JSON file path to process; relative paths are relative
                        to the current working directory.
  -s <string literal>, --string <string literal>
                        JSON string literal to process.The command (on Linux)python3 -m json_visitor -s "{ \"v\": [ 1, 2,3, 4, 5, 6, { \"key0\": \"value\", \"key1\": {} }] }"results in the following output:document_start
object_start
member_start
member_key: (0: v)
member_value_start
list_start
list_item_start
list_item_value_start
value: (0: 1)
list_item_value_end
list_item_end
process_list_item: (0: 0), (1: 1)
list_item_start
list_item_value_start
value: (0: 2)
list_item_value_end
list_item_end
process_list_item: (0: 1), (1: 2)
list_item_start
list_item_value_start
value: (0: 3)
list_item_value_end
list_item_end
process_list_item: (0: 2), (1: 3)
list_item_start
list_item_value_start
value: (0: 4)
list_item_value_end
list_item_end
process_list_item: (0: 3), (1: 4)
list_item_start
list_item_value_start
value: (0: 5)
list_item_value_end
list_item_end
process_list_item: (0: 4), (1: 5)
list_item_start
list_item_value_start
value: (0: 6)
list_item_value_end
list_item_end
process_list_item: (0: 5), (1: 6)
list_item_start
list_item_value_start
object_start
member_start
member_key: (0: key0)
member_value_start
value: (0: value)
member_value_end
member_end
process_member: (0: key0), (1: value)
member_start
member_key: (0: key1)
member_value_start
object_start
object_end
process_object: (0: {})
member_value_end
member_end
process_member: (0: key1), (1: {})
object_end
process_object: (0: {'key0': 'value', 'key1': {}})
list_item_value_end
list_item_end
process_list_item: (0: 6), (1: {'key0': 'value', 'key1': {}})
list_end
process_list: (0: (1, 2, 3, 4, 5, 6, {'key0': 'value', 'key1': {}}))
member_value_end
member_end
process_member: (0: v), (1: (1, 2, 3, 4, 5, 6, {'key0': 'value', 'key1': {}}))
object_end
process_object: (0: {'v': (1, 2, 3, 4, 5, 6, {'key0': 'value', 'key1': {}})})
document_end
process_document: (0: {'v': (1, 2, 3, 4, 5, 6, {'key0': 'value', 'key1': {}})})
Number of scope events processed: 103AcknowledgementsThe hard work of turning JSON into an event stream is done by theijsonpackage, without which the Visitor interface would have been much more challenging to develop.