‘jsondata.JSONDataSerializer’ - Module¶
Basic features for the persistence of JSON based in-memory data.
JSONDataSerializer¶
-
class
jsondata.JSONDataSerializer.
JSONDataSerializer
(appname, *args, **kargs)[source]¶ Persistency of JSON based data for the class jsondata.JSONData.
This class provides for persistency of data managed by jsondata.JSONData.
- Attributes:
- data: The data tree of JSON based objects provided
- by the module ‘json’.
- schema: The validator for ‘data’ provided by
- the module ‘jsonschema’.
- Common call parameters provided by the methods of this class are:
- targetnode := addressreference
- The target node of called method. The ‘targetnode’ in general represents the target of the called method. In most cases this has to be a reference to a container for the modification and/or insertion of resulting elements. The methods require the change of contained items, which involves the application of a ‘key’ pointing to the hook in point of the reference to the modification.
- key := key-value
- The hook-in point for references of modified entries within the targetnode container. The following values are supported:
- sourcenode := addressreference
- The in-memory node address of the source branch for the method, e.g. ‘copy’ or ‘move’ operation.
The address references supported in this class refer the resulting in-memory representation of a pointer path. The target is a node within a Python data representation as provided by the package ‘json‘ and compatible packages, e.g. ‘ujson‘. The supported input syntax is one of the following interchangeable formats:
# The reference to a in-memory-node. addressreference := ( nodereference | addressreference-source ) nodereference:= ( <in-memory> | '' ) <in-memory> := "Memory representation of a JSON node, a 'dict' or a 'list'. The in-memory Python node reference has to be located within the document, due to performance reasons this is not verified by default. The 'nodereference' could be converted from the 'addressreference-source' representation." '' := "Represents the whole document in accordance to RFC6901. Same as 'self.data'." # The source of the syntax for the description of the reference # pointer path to a node. This is applicable on paths to be created. addressreference-source := ( JSONPointer ) JSONPointer:="A JSONPointer object in accordance to RFC6901. for additional information on input formats refer to the class documentation. This class provides a fully qualified path pointer, which could be converted into any of the required representations."
For hooks by ‘key-value’ within addressed containers:
key-value:=(None|<list-index>|<dict-key>) None := "When the 'key' parameter is 'None', the action optionally could be based on the keys of the 'sourcenode'. The contents of the branch replace the node contents when the type of the branch matches the hook." <list-index>:=('-'|int) <dict-key>:="Valid for a 'dict' only, sets key/value pair, where present is replace, new is created." '-' := "Valid for a 'list' only, appends to present." int := "Valid for a 'list' only, replaces present when 0 < #int < len(Node)."
In the parameter lists of methods used term ‘pointer’ is either an object of class ‘JSONPointer’, or a list of pointer path entries.
The JSON types ‘object’ and ‘array’ behave in Python slightly different in accordance to RFC6902. The main difference arise from the restrictions on applicable key values. Whereas the ranges are limited logically by the actual container sizes, the object types provide free and unlimited keys. The limit is set by type restriction to unicode and ‘non-nil’ only for keys.
Attributes¶
- JSONDataSerializer.data: JSON object data tree.
- JSONDataSerializer.schema: JSONschema object data tree.
Methods¶
__init__¶
-
JSONDataSerializer.
__init__
(appname, *args, **kargs)[source]¶ Loads and validates a JSON definition with the corresponding schema file.
- Args:
- appname: Name of the application. An arbitrary string representing the
- name of an application. The name is mainly used for the default name prefix of the JSON data and schema.
- args*: Optional position parameters, these branch_replace corresponding key
- parameters. filelist, pathlist, filepathlist, schemafile
- **kargs:
- datafile: Filepathname of JSON data file, when provided a further
search by pathlist, filelist, and filepathlist is suppressed. Therefore it has to be a valid filepathname.
default:= <appname>.json
filelist: List of valid filenames.
default:= <appname>.json- filepathlist: List of filepathnames. These are not prefixed by search
path components, but made absolute.
default:= []
- filepriority: [firstonly, lastonly, all]
Defines the handling of multiple occurrences of a filename at varios positions. This option thus may only be altered in conjunction with ‘pathlist’.
default:= all
indent_str: Defied the indentation of ‘str’.
default:= 4interactive: Hints on command line call for optional change of display format.
default:= False- loadcached: Caching of load for JSON data files.
Loads either completely into cache before transferring to production entries, or immediately into production parameters, which may take an effect on the remaining parameters to be loaded.
default:= False
- nodefaultpath: Ignores the default paths, the exception is the
base configuration, which still is searched within the default paths exclusively.
default:= False
- nosubdata: Supresses the load of sub-data files.
- default:= False
- pathlist: List of pathnames for search of a valid filename.
Either a PATH like string, or a list of single paths.
default:= ../dirname(__file__)/etc/:dirname(__file__)/:/etc/:$HOME/etc/
- requires: [all, base, one]
Defines how to handle missing or invalid files.
default:= all
schema: A valid in-meory JSONschema.
default:= Noneschemafile: Filepathname of JSONschema file.
default:= <appname>.jsd- validator: [default, draft3, off, ]
Sets schema validator for the data file. The values are: default=validate, draft3=Draft3Validator, off=None
default:= validate
- printdata: branch=None
Pretty print resulting final data of branch.
default:= top
- printschema: branch=None
Pretty print resulting schema.
default:= top
- debug: Displays extended state data for developers.
- Requires __debug__==True.
- verbose: Extends the amount of the display of
- processing data.
- Returns:
- Results in an initialized object.
- Raises:
NameError:
JSONDataSourceFile:
JSONDataAmbiguity:
JSONDataValue:
jsonschema.ValidationError:
jsonschema.SchemaError:
json_export¶
-
JSONDataSerializer.
json_export
(sourcenode, fname, **kargs)[source]¶ Exports current data for later import.
The exported data is a snapshot of current state.
- Args:
fname: File name for the exported data.
- sourcenode: Base of sub-tree for export.
- None for complete JSON document.
- **kargs:
- ffs.
- Returns:
- When successful returns ‘True’, else returns either ‘False’, or raises an exception.
- Raises:
- JSONDataTargetFile:
json_import¶
-
JSONDataSerializer.
json_import
(targetnode, key, datafile, schemafile=None, **kargs)[source]¶ Imports and validates JSON based data.
The contained data in ‘datafile’ could be either the initial data tree, or a new branch defined by a fresh tree structure. The ‘targetnode’ defines the parent container where the new branch has to be hooked-in.
- Args:
- targetnode:
- Target container for the inclusion of the loaded branch. For the default:=’None’ the ‘self.data’ is used.
- key:
- The hook within the targetnode,
- datafile:
- JSON data filename containing the subtree for the target branch.
- schemafile:
- JSON-Schema filename for validation of the subtree/branch.
- **kargs:
- matchcondition:
Defines the criteria for comparison of present child nodes in the target container. The value is a list of criteria combined by logical AND. The criteria may vary due to the requirement and the type of applied container.
- For information on applicable values refer to:
- ‘JSONDataSerializer.isApplicable()’
- validator: [default, draft3, off, ]
Sets schema validator for the data file. The values are: default=validate, draft3=Draft3Validator, off=None.
default:= validate
- Returns:
- When successful returns ‘True’, else returns either ‘False’, or raises an exception.
- Raises:
JSONData:
JSONDataValue:
JSONDataSourceFile:
printData¶
-
JSONDataSerializer.
printData
(pretty=True, **kargs)[source]¶ Prints structured data.
- Args:
pretty: Activates pretty printer for treeview, else flat.
sourcefile: Loads data from ‘sourcefile’ into ‘source’.
default:=Nonesource: Prints data within ‘source’.
default:=self.data- Returns:
- When successful returns ‘True’, else returns either ‘False’, or raises an exception.
- Raises:
JSONDataAmbiguity:
forwarded from ‘json’
printSchema¶
-
JSONDataSerializer.
printSchema
(pretty=True, **kargs)[source]¶ Prints structured schema.
- Args:
pretty: Activates pretty printer for treeview, else flat.
sourcefile: Loads schema from ‘sourcefile’ into ‘source’.
default:=Nonesource: Prints schema within ‘source’.
default:=self.schema- Returns:
- When successful returns ‘True’, else returns either ‘False’, or raises an exception.
- Raises:
JSONDataAmbiguity:
forwarded from ‘json’
setSchema¶
-
JSONDataSerializer.
setSchema
(schemafile=None, targetnode=None, **kargs)[source]¶ Sets schema or inserts a new branch into the current assigned schema.
The main schema(targetnode==None) is the schema related to the current instance. Additional branches could be added by importing the specific schema definitions into the main schema. These could either kept volatile as a temporary runtime extension, or stored into a new schema file in order as extension of the original for later combined reuse.
- Args:
- schemafile:
- JSON-Schema filename for validation of the subtree/branch. See also **kargs[‘schema’].
- targetnode:
- Target container hook for the inclusion of the loaded branch.
- **kargs:
- schema:
In-memory JSON-Schema as an alternative to schemafile. When provided the ‘schemafile’ is ignored.
default:=None
- validator: [default, draft3, off, ]
Sets schema validator for the data file. The values are: default=validate, draft3=Draft3Validator, off=None.
default:= validate
- persistent:
Stores the ‘schema’ persistently into ‘schemafile’ after completion of update including addition of branches. Requires valid ‘schemafile’.
default:=False
- Returns:
- When successful returns ‘True’, else returns either ‘False’, or raises an exception.
Raises:
JSONData:
JSONDataSourceFile:
JSONDataValue: