在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:avian2/jsonmerge开源软件地址:https://github.com/avian2/jsonmerge开源编程语言:Python 100.0%开源软件介绍:Merge a series of JSON documentsThis Python module allows you to merge a series of JSON documents into a single one. This problem often occurs for example when different authors fill in different parts of a common document and you need to construct a document that includes contributions from all the authors. It also helps when dealing with consecutive versions of a document where different fields get updated over time. Consider a trivial example with two documents: >>> base = { ... "foo": 1, ... "bar": [ "one" ], ... } >>> head = { ... "bar": [ "two" ], ... "baz": "Hello, world!" ... } We call the document we are merging changes into base and the changed document head. To merge these two documents using jsonmerge: >>> from pprint import pprint >>> from jsonmerge import merge >>> result = merge(base, head) >>> pprint(result, width=40) {'bar': ['two'], 'baz': 'Hello, world!', 'foo': 1} As you can see, when encountering an JSON object, jsonmerge by default returns fields that appear in either base or head document. For other JSON types, it simply replaces the older value. These principles are also applied in case of multiple nested JSON objects. In a more realistic use case however, you might want to apply different merge strategies to different parts of the document. You can tell jsonmerge how to do that using a syntax based on JSON schema. If you already have schemas for your document, you can simply expand them with some additional keywords. Apart from the custom keywords described below, jsonmerge by default uses the schema syntax defined in the Draft 4 of the JSON schema specification. You use the mergeStrategy schema keyword to specify the strategy. The default two strategies mentioned above are called objectMerge for objects and overwrite for all other types. Let's say you want to specify that the merged bar field in the example document above should contain elements from all documents, not just the latest one. You can do this with a schema like this: >>> schema = { ... "properties": { ... "bar": { ... "mergeStrategy": "append" ... } ... } ... } >>> from jsonmerge import Merger >>> merger = Merger(schema) >>> result = merger.merge(base, head) >>> pprint(result, width=40) {'bar': ['one', 'two'], 'baz': 'Hello, world!', 'foo': 1} Another common example is when you need to keep a versioned list of values that appeared in the series of documents: >>> schema = { ... "properties": { ... "foo": { ... "type": "object", ... "mergeStrategy": "version", ... "mergeOptions": { "limit": 5 } ... } ... }, ... "additionalProperties": False ... } >>> from jsonmerge import Merger >>> merger = Merger(schema) >>> rev1 = { ... 'foo': { ... 'greeting': 'Hello, World!' ... } ... } >>> rev2 = { ... 'foo': { ... 'greeting': 'Howdy, World!' ... } ... } >>> base = None >>> base = merger.merge(base, rev1, merge_options={ ... 'version': { ... 'metadata': { ... 'revision': 1 ... } ... } ... }) >>> base = merger.merge(base, rev2, merge_options={ ... 'version': { ... 'metadata': { ... 'revision': 2 ... } ... } ... }) >>> pprint(base, width=55) {'foo': [{'revision': 1, 'value': {'greeting': 'Hello, World!'}}, {'revision': 2, 'value': {'greeting': 'Howdy, World!'}}]} Note that we use the mergeOptions keyword in the schema to supply additional options to the merge strategy. In this case, we tell the version strategy to retain only 5 most recent versions of this field. We also used the merge_options argument to supply some options that are specific to each call of the merge method. Options specified this way are applied to all invocations of a specific strategy in a schema (in contrast to mergeOptions, which applies only to the strategy invocation in that specific location in the schema). Options specified in mergeOptions schema keyword override the options specified in the merge_options argument. The metadata option for the version strategy can contain some document meta-data that is included for each version of the field. metadata can contain an arbitrary JSON object. Example above also demonstrates how jsonmerge is typically used when merging more than two documents. Typically you start with an empty base and then consecutively merge different heads into it. A common source of problems are documents that do not match the schema used for merging. jsonmerge by itself does not validate input documents. It only uses the schema to obtain necessary information to apply appropriate merge strategies. Since the default strategies are used for parts of the document that are not covered by the schema it's easy to get unexpected output without any obvious errors raised by jsonmerge. In the following example, the property Foo (uppercase F) does not match foo (lowercase f) in the schema and hence the version strategy is not applied as with previous two revisions: >>> rev3 = { ... 'Foo': { ... 'greeting': 'Howdy, World!' ... } ... } >>> base = merger.merge(base, rev3, merge_options={ ... 'version': { ... 'metadata': { ... 'revision': 3 ... } ... } ... }) >>> pprint(base, width=55) {'Foo': {'greeting': 'Howdy, World!'}, 'foo': [{'revision': 1, 'value': {'greeting': 'Hello, World!'}}, {'revision': 2, 'value': {'greeting': 'Howdy, World!'}}]} Hence it is recommended to validate the input documents against the schema before passing them to jsonmerge. This practice is even more effective if the schema is filled in with more information than strictly necessary for jsonmerge (e.g. adding information about types, restrict valid object properties with additionalProperties, etc.): >>> from jsonschema import validate >>> validate(rev1, schema) >>> validate(rev2, schema) >>> validate(rev3, schema) Traceback (most recent call last): ... jsonschema.exceptions.ValidationError: Additional properties are not allowed ('Foo' was unexpected) If you care about well-formedness of your documents, you might also want to obtain a schema for the documents that the merge method creates. jsonmerge provides a way to automatically generate it from a schema for the input document: >>> result_schema = merger.get_schema() >>> pprint(result_schema, width=80) {'additionalProperties': False, 'properties': {'foo': {'items': {'properties': {'value': {'type': 'object'}}}, 'maxItems': 5, 'type': 'array'}}} Note that because of the version strategy, the type of the foo field changed from object to array. Merge strategiesThese are the currently implemented merge strategies.
If a merge strategy is not specified in the schema, objectMerge is used for objects and overwrite for all other values (but see also the section below regarding keywords that apply subschemas). You can implement your own strategies by making subclasses of jsonmerge.strategies.Strategy and passing them to Merger() constructor (see below). The Merger ClassThe Merger class allows you to further customize the merging of JSON data by allowing you to:
The Merger constructor takes the following arguments (all optional, except schema):
Support for keywords that apply subschemasComplex merging of documents with schemas that use keywords allOf, anyOf and oneOf can be problematic. Such documents do not have a well-defined type and might require merging of two values of different types, which will fail for some strategies. In such cases get_schema() might also return schemas that never validate. The overwrite strategy is usually the safest choice for such schemas. If you explicitly define a merge strategy at the same level as allOf, anyOf or oneOf keyword, then jsonmerge will use the defined strategy and not further process any subschemas under those keywords. The strategy however will descend as usual (e.g. objectMerge will take into account subschemas under the properties keyword at the same level as allOf). If a merge strategy is not explicitly defined and an allOf or anyOf keyword is present, jsonmerge will raise an error. If a merge strategy is not explicitly defined and an oneOf keyword is present, jsonmerge will continue on the branch of oneOf that validates both base and head. If no branch validates, it will raise an error. You can define more complex behaviors by defining for your own strategy that defines what to do in such cases. See docstring documentation for the Strategy class on how to do that. Security considerationsA JSON schema document can contain $ref references to external schemas. jsonmerge resolves URIs in these references using the mechanisms provided by the jsonschema module. External references can cause HTTP or similar network requests to be performed. If jsonmerge is used on untrusted input, this may lead to vulnerabilities similar to the XML External Entity (XXE) attack. Requirementsjsonmerge supports Python 2 (2.7) and Python 3 (3.5 and newer). You need jsonschema (https://pypi.python.org/pypi/jsonschema) module installed. InstallationTo install the latest jsonmerge release from the Python package index: pip install jsonmerge SourceThe latest development version is available on GitHub: https://github.com/avian2/jsonmerge To install from source, run the following from the top of the source distribution: pip install . jsonmerge uses Tox for testing. To run the test suite run: tox Reporting bugs and contributing codeThank you for contributing to jsonmerge! Free software wouldn't be possible without contributions from users like you. However, please consider that I maintain this project in my free time. Hence I ask you to follow this simple etiquette to minimize the amount of effort needed to include your contribution. Please use GitHub issues to report bugs. Make sure that your report includes:
Please use GitHub pull requests to contribute code. Make sure that your pull request:
LicenseCopyright 2021, Tomaz Solc <[email protected]> The MIT License (MIT) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论