应用带有扩展名的JSONPatches(RFC 6902)
jsonpatchext的Python项目详细描述
python json补丁ext
在Python中应用JSON补丁
此模块将Pythonjsonpatch模块扩展到 添加“check”、“mutate”和“merge”操作。在
示例请参见源代码
- 网址:https://github.com/RangelReale/python-json-patch-ext
- 存储库:https://github.com/RangelReale/python-json-patch-ext.git
- 文档:https://python-json-patch-ext.readthedocs.org/
- PyPI:https://pypi.python.org/pypi/jsonpatchext
示例
fromjsonpatchextimportJsonPatchExt,JsonPatchTestFaileddefStartsWithComparator(current,compare):ifnotcurrent.startswith(compare):msg='{0} ({1}) does not starts with {2} ({3})'raiseJsonPatchTestFailed(msg.format(current,type(current),compare,type(compare)))defRemoveLastMutator(current,value):returncurrent[:-1]patch=JsonPatchExt([{'op':'add','path':'/foo','value':{'bar':'bar'}},{'op':'check','path':'/foo/bar','value':'bar','cmp':'equals'},{'op':'merge','path':'/foo','value':{'newbar':'newbarvalue'}},{'op':'check','path':'/foo/newbar','value':'newb','cmp':'custom','comparator':StartsWithComparator},{'op':'mutate','path':'/foo/newbar','mut':'uppercase'},{'op':'mutate','path':'/foo/newbar','mut':'custom','mutator':RemoveLastMutator},])doc={}result=patch.apply(doc)print(result)checkpatch=JsonPatchExt([{'op':'check','path':'/foo/bar','value':'bar','cmp':'equals'},{'op':'check','path':'/foo/newbar','value':'NEWB','cmp':'custom','comparator':StartsWithComparator},])result=checkpatch.check(result)print(result)
输出:
^{pr2}$作者
范围实数(rangelspam@gmail.com)
- 项目
标签: