JSONDataSerializer - JSON Persistency¶
The class JSONDataSerializer provides persistency and modularity for JSON data structures and branches.
Load¶
JSON data can be simply loaded from a file, which is either formatted as structured tree view, or a simple (long-) line containing the whole JSON data.
The JSON data:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | {
"address":{
"streetAddress": "21 2nd Street",
"city":"New York",
"houseNumber":12
},
"phoneNumber":
[
{
"type":"home",
"number":"212 555-1234"
},
{
"type":"office",
"number":"313 444-555"
},
{
"type":"mobile",
"number":"777 666-555"
}
]
}
|
is easily loaded by the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | # -*- coding:utf-8 -*-
from __future__ import absolute_import
from __future__ import print_function
import os
from jsondata.jsondataserializer import JSONDataSerializer
from jsondata import MS_DRAFT4
# name of application, used for several filenames as MS_DRAFT4
appname = "jsondc"
# JSON data
datafile = os.path.abspath(os.path.dirname(
__file__)) + os.sep + str('datafile.json')
# JSON schema
schemafile = os.path.abspath(os.path.dirname(
__file__)) + os.sep + str('schema.jsd')
# standard call options
kargs = {}
kargs['datafile'] = datafile
kargs['schemafile'] = schemafile
kargs['nodefaultpath'] = True
kargs['nosubdata'] = True
kargs['pathlist'] = os.path.dirname(__file__)
kargs['validator'] = MS_DRAFT4
# load JSON file
jsondata = JSONDataSerializer(appname, **kargs)
print(jsondata)
pass
|
Save¶
The data e.g. as loaded by the previous example could be easily stored in a file by the following code. Just append the lines to the previous example.
1 2 3 4 | outfile = os.path.abspath(os.path.dirname(
__file__)) + os.sep + str('outfile.json')
jsondata.json_export(outfile, pretty=False, force=True)
|
Import and Export Branches¶
The package jsondata makes it inparticular easy to import and export subtrees called branches into/from a JSON data structucture.
For examples see JSON Patch Management.
Validation of Branches By JSON Schemes¶
The package jsondata provides either for the validation of the whole JSON data by one schema, or the modular validation of sub-branches by multiple schemes.
For examples see JSON Patch Management.