pax_global_header00006660000000000000000000000064146450325060014517gustar00rootroot0000000000000052 comment=b2db6d3eabd52b0026dd36498b1d64802a07cff4 jsondiff-2.1.2/000077500000000000000000000000001464503250600133235ustar00rootroot00000000000000jsondiff-2.1.2/.github/000077500000000000000000000000001464503250600146635ustar00rootroot00000000000000jsondiff-2.1.2/.github/workflows/000077500000000000000000000000001464503250600167205ustar00rootroot00000000000000jsondiff-2.1.2/.github/workflows/pr_check.yml000066400000000000000000000011111464503250600212130ustar00rootroot00000000000000name: PR Check on: - pull_request - workflow_dispatch jobs: test: runs-on: ${{ matrix.os }} strategy: matrix: os: [ubuntu-latest, windows-latest] python-version: ["3.8", "3.9", "3.10", "3.11"] steps: - uses: actions/checkout@v3 - name: Set up Python ${{ matrix.python-version }} uses: actions/setup-python@v4 with: python-version: ${{ matrix.python-version }} - name: Install Dependencies run: | pip install .[dev] - name: Run Tests run: | python -m pytest jsondiff-2.1.2/.github/workflows/python_publish.yml000066400000000000000000000012541464503250600225140ustar00rootroot00000000000000name: Upload Python Package on: release: types: [published] workflow_dispatch: permissions: contents: read jobs: deploy: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Set up Python uses: actions/setup-python@v3 with: python-version: '3.x' - name: Install dependencies run: | python -m pip install --upgrade pip pip install build - name: Build package run: python -m build - name: Publish package uses: pypa/gh-action-pypi-publish@81e9d935c883d0b210363ab89cf05f3894778450 # v1.8.14 with: user: __token__ password: ${{ secrets.PYPI_API_TOKEN }} jsondiff-2.1.2/.gitignore000066400000000000000000000014071464503250600153150ustar00rootroot00000000000000# Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] # C extensions *.so # Distribution / packaging .Python env/ build/ develop-eggs/ dist/ downloads/ eggs/ .eggs/ lib/ lib64/ parts/ sdist/ var/ *.egg-info/ .installed.cfg *.egg # PyInstaller # Usually these files are written by a python script from a template # before PyInstaller builds the exe, so as to inject date/other infos into it. *.manifest *.spec # Installer logs pip-log.txt pip-delete-this-directory.txt # Unit test / coverage reports htmlcov/ .tox/ .coverage .coverage.* .cache coverage.xml *,cover # Translations *.mo *.pot # Django stuff: *.log # Sphinx documentation docs/_build/ # PyBuilder target/ # PyCharm .idea .hypothesis/ # Ignore auto-generated _version /jsondiff/_version.py jsondiff-2.1.2/CHANGELOG.md000066400000000000000000000034321464503250600151360ustar00rootroot00000000000000# Changelog ## 2.1.2 (Jul, 14th 2024) * really drop python<=3.7 support by @kloczek in https://github.com/xlwings/jsondiff/pull/78 * Added docstrings by @payam54 in https://github.com/xlwings/jsondiff/pull/79 * remove last bits of python2 support by @corytodd in https://github.com/xlwings/jsondiff/pull/80 ## 2.1.1 (Jun, 28th 2024) Fix pypi release readme formatting ## 2.1.0 (Jun, 28th 2024) Minimal conversion to pytest+hypothesis by @mgorny in #52 Added simple equality operator for class Symbol by @GregoirePelegrin in #55 jsondiff: fix symbol equality by @corytodd in #61 ci: add pytest workflow by @corytodd in #63 setup.py: migrate to pyproject.toml by @corytodd in #65 fix: better diffing of empty containers by @corytodd in #64 add rightonly jsondiff syntax by @ramwin in #60 Introduce YAML support by @corytodd in #67 packaging: revert to requirements files by @corytodd in #69 cli: handle deserialization errors by @corytodd in #72 ci: upload to pypi on github release by @corytodd in #77 ## 2.0.0 (Apr, 10th 2022) remove deprecated jsondiff entrypoint ## 1.3.1 (Jan 24th, 2022) Optionally allow different escape_str than '$' clarified the readme, closes #23 ## 1.3.0 (Dec, 21st 2019) Add license to setup.py Refactor recursive list-diff helper method to be iterative ## v1.2.0 (Jun 23rd, 2019) Deprecate the jsondiff command due to conflicts with json-patch ## v1.1.2 (Oct, 8th 2017) README: disable incorrect syntax highlight for usage message Maintaining consistency in checking type of object make generate_readme.py compatible with python3 ## v1.1.1 (Mar, 26th 2016) Exclude tests from installation ## v1.1.0 (Dec, 5th 2016) Added command line client ## v1.0.0 (Oct, 19th 2016) Stable release ## v0.2.0 (Dec, 15th 2015) Changed syntax ## v0.1.0 (Oct, 19th 2015) First releasejsondiff-2.1.2/LICENSE000066400000000000000000000021001464503250600143210ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2015 Zoomer Analytics LLC 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. jsondiff-2.1.2/MANIFEST.in000066400000000000000000000000421464503250600150550ustar00rootroot00000000000000include LICENSE include README.rstjsondiff-2.1.2/README.md000066400000000000000000000055611464503250600146110ustar00rootroot00000000000000# jsondiff Diff JSON and JSON-like structures in Python. ## Installation ``pip install jsondiff`` ## Quickstart ```python >>> import jsondiff as jd >>> from jsondiff import diff >>> diff({'a': 1, 'b': 2}, {'b': 3, 'c': 4}) {'c': 4, 'b': 3, delete: ['a']} >>> diff(['a', 'b', 'c'], ['a', 'b', 'c', 'd']) {insert: [(3, 'd')]} >>> diff(['a', 'b', 'c'], ['a', 'c']) {delete: [1]} # Typical diff looks like what you'd expect... >>> diff({'a': [0, {'b': 4}, 1]}, {'a': [0, {'b': 5}, 1]}) {'a': {1: {'b': 5}}} # ...but similarity is taken into account >>> diff({'a': [0, {'b': 4}, 1]}, {'a': [0, {'c': 5}, 1]}) {'a': {insert: [(1, {'c': 5})], delete: [1]}} # Support for various diff syntaxes >>> diff({'a': 1, 'b': 2}, {'b': 3, 'c': 4}, syntax='explicit') {insert: {'c': 4}, update: {'b': 3}, delete: ['a']} >>> diff({'a': 1, 'b': 2}, {'b': 3, 'c': 4}, syntax='symmetric') {insert: {'c': 4}, 'b': [2, 3], delete: {'a': 1}} >>> diff({'list': [1, 2, 3], "poplist": [1, 2, 3]}, {'list': [1, 3]}, syntax="rightonly") {"list": [1, 3], delete: ["poplist"]} # Special handling of sets >>> diff({'a', 'b', 'c'}, {'a', 'c', 'd'}) {discard: set(['b']), add: set(['d'])} # Load and dump JSON >>> print diff('["a", "b", "c"]', '["a", "c", "d"]', load=True, dump=True) {"$delete": [1], "$insert": [[2, "d"]]} # NOTE: Default keys in the result are objects, not strings! >>> d = diff({'a': 1, 'delete': 2}, {'b': 3, 'delete': 4}) >>> d {'delete': 4, 'b': 3, delete: ['a']} >>> d[jd.delete] ['a'] >>> d['delete'] 4 # Alternatively, you can use marshal=True to get back strings with a leading $ >>> diff({'a': 1, 'delete': 2}, {'b': 3, 'delete': 4}, marshal=True) {'delete': 4, 'b': 3, '$delete': ['a']} ``` ## Command Line Client Usage: ``` jdiff [-h] [-p] [-s {compact,symmetric,explicit}] [-i INDENT] [-f {json,yaml}] first second positional arguments: first second optional arguments: -h, --help show this help message and exit -p, --patch -s {compact,symmetric,explicit}, --syntax {compact,symmetric,explicit} Diff syntax controls how differences are rendered (default: compact) -i INDENT, --indent INDENT Number of spaces to indent. None is compact, no indentation. (default: None) -f {json,yaml}, --format {json,yaml} Specify file format for input and dump (default: json) ``` Examples: ```bash $ jdiff a.json b.json -i 2 $ jdiff a.json b.json -i 2 -s symmetric $ jdiff a.yaml b.yaml -f yaml -s symmetric ``` ## Development Install development dependencies and test locally with ```bash pip install -r requirements-dev.txt # ... do your work ... add tests ... pytest ``` ## Installing From Source To install from source run ```bash pip install . ``` This will install the library and cli for `jsondiff` as well as its runtime dependencies. ## Testing before release ```bash python -m build twine check dist/* ``` jsondiff-2.1.2/docs/000077500000000000000000000000001464503250600142535ustar00rootroot00000000000000jsondiff-2.1.2/docs/Makefile000066400000000000000000000163711464503250600157230ustar00rootroot00000000000000# Makefile for Sphinx documentation # # You can set these variables from the command line. SPHINXOPTS = SPHINXBUILD = sphinx-build PAPER = BUILDDIR = _build # User-friendly check for sphinx-build ifeq ($(shell which $(SPHINXBUILD) >/dev/null 2>&1; echo $$?), 1) $(error The '$(SPHINXBUILD)' command was not found. Make sure you have Sphinx installed, then set the SPHINXBUILD environment variable to point to the full path of the '$(SPHINXBUILD)' executable. Alternatively you can add the directory with the executable to your PATH. If you don't have Sphinx installed, grab it from http://sphinx-doc.org/) endif # Internal variables. PAPEROPT_a4 = -D latex_paper_size=a4 PAPEROPT_letter = -D latex_paper_size=letter ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . # the i18n builder cannot share the environment and doctrees with the others I18NSPHINXOPTS = $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) . .PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp devhelp epub latex latexpdf text man changes linkcheck doctest coverage gettext help: @echo "Please use \`make ' where is one of" @echo " html to make standalone HTML files" @echo " dirhtml to make HTML files named index.html in directories" @echo " singlehtml to make a single large HTML file" @echo " pickle to make pickle files" @echo " json to make JSON files" @echo " htmlhelp to make HTML files and a HTML help project" @echo " qthelp to make HTML files and a qthelp project" @echo " applehelp to make an Apple Help Book" @echo " devhelp to make HTML files and a Devhelp project" @echo " epub to make an epub" @echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter" @echo " latexpdf to make LaTeX files and run them through pdflatex" @echo " latexpdfja to make LaTeX files and run them through platex/dvipdfmx" @echo " text to make text files" @echo " man to make manual pages" @echo " texinfo to make Texinfo files" @echo " info to make Texinfo files and run them through makeinfo" @echo " gettext to make PO message catalogs" @echo " changes to make an overview of all changed/added/deprecated items" @echo " xml to make Docutils-native XML files" @echo " pseudoxml to make pseudoxml-XML files for display purposes" @echo " linkcheck to check all external links for integrity" @echo " doctest to run all doctests embedded in the documentation (if enabled)" @echo " coverage to run coverage check of the documentation (if enabled)" clean: rm -rf $(BUILDDIR)/* html: $(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html @echo @echo "Build finished. The HTML pages are in $(BUILDDIR)/html." dirhtml: $(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml @echo @echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml." singlehtml: $(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml @echo @echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml." pickle: $(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle @echo @echo "Build finished; now you can process the pickle files." json: $(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json @echo @echo "Build finished; now you can process the JSON files." htmlhelp: $(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp @echo @echo "Build finished; now you can run HTML Help Workshop with the" \ ".hhp project file in $(BUILDDIR)/htmlhelp." qthelp: $(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp @echo @echo "Build finished; now you can run "qcollectiongenerator" with the" \ ".qhcp project file in $(BUILDDIR)/qthelp, like this:" @echo "# qcollectiongenerator $(BUILDDIR)/qthelp/jsondiff.qhcp" @echo "To view the help file:" @echo "# assistant -collectionFile $(BUILDDIR)/qthelp/jsondiff.qhc" applehelp: $(SPHINXBUILD) -b applehelp $(ALLSPHINXOPTS) $(BUILDDIR)/applehelp @echo @echo "Build finished. The help book is in $(BUILDDIR)/applehelp." @echo "N.B. You won't be able to view it unless you put it in" \ "~/Library/Documentation/Help or install it in your application" \ "bundle." devhelp: $(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) $(BUILDDIR)/devhelp @echo @echo "Build finished." @echo "To view the help file:" @echo "# mkdir -p $$HOME/.local/share/devhelp/jsondiff" @echo "# ln -s $(BUILDDIR)/devhelp $$HOME/.local/share/devhelp/jsondiff" @echo "# devhelp" epub: $(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub @echo @echo "Build finished. The epub file is in $(BUILDDIR)/epub." latex: $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex @echo @echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex." @echo "Run \`make' in that directory to run these through (pdf)latex" \ "(use \`make latexpdf' here to do that automatically)." latexpdf: $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex @echo "Running LaTeX files through pdflatex..." $(MAKE) -C $(BUILDDIR)/latex all-pdf @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." latexpdfja: $(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex @echo "Running LaTeX files through platex and dvipdfmx..." $(MAKE) -C $(BUILDDIR)/latex all-pdf-ja @echo "pdflatex finished; the PDF files are in $(BUILDDIR)/latex." text: $(SPHINXBUILD) -b text $(ALLSPHINXOPTS) $(BUILDDIR)/text @echo @echo "Build finished. The text files are in $(BUILDDIR)/text." man: $(SPHINXBUILD) -b man $(ALLSPHINXOPTS) $(BUILDDIR)/man @echo @echo "Build finished. The manual pages are in $(BUILDDIR)/man." texinfo: $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo @echo @echo "Build finished. The Texinfo files are in $(BUILDDIR)/texinfo." @echo "Run \`make' in that directory to run these through makeinfo" \ "(use \`make info' here to do that automatically)." info: $(SPHINXBUILD) -b texinfo $(ALLSPHINXOPTS) $(BUILDDIR)/texinfo @echo "Running Texinfo files through makeinfo..." make -C $(BUILDDIR)/texinfo info @echo "makeinfo finished; the Info files are in $(BUILDDIR)/texinfo." gettext: $(SPHINXBUILD) -b gettext $(I18NSPHINXOPTS) $(BUILDDIR)/locale @echo @echo "Build finished. The message catalogs are in $(BUILDDIR)/locale." changes: $(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes @echo @echo "The overview file is in $(BUILDDIR)/changes." linkcheck: $(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck @echo @echo "Link check complete; look for any errors in the above output " \ "or in $(BUILDDIR)/linkcheck/output.txt." doctest: $(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest @echo "Testing of doctests in the sources finished, look at the " \ "results in $(BUILDDIR)/doctest/output.txt." coverage: $(SPHINXBUILD) -b coverage $(ALLSPHINXOPTS) $(BUILDDIR)/coverage @echo "Testing of coverage in the sources finished, look at the " \ "results in $(BUILDDIR)/coverage/python.txt." xml: $(SPHINXBUILD) -b xml $(ALLSPHINXOPTS) $(BUILDDIR)/xml @echo @echo "Build finished. The XML files are in $(BUILDDIR)/xml." pseudoxml: $(SPHINXBUILD) -b pseudoxml $(ALLSPHINXOPTS) $(BUILDDIR)/pseudoxml @echo @echo "Build finished. The pseudo-XML files are in $(BUILDDIR)/pseudoxml." jsondiff-2.1.2/docs/basic_use.rst000066400000000000000000000002101464503250600167330ustar00rootroot00000000000000Basic Use ========= Installation ------------ The first step is to install jsondiff, if you haven't already ``pip install jsondiff`` jsondiff-2.1.2/docs/conf.py000066400000000000000000000216331464503250600155570ustar00rootroot00000000000000# # jsondiff documentation build configuration file, created by # sphinx-quickstart on Fri Nov 13 17:39:49 2015. # # This file is execfile()d with the current directory set to its # containing dir. # # Note that not all possible configuration values are present in this # autogenerated file. # # All configuration values have a default; values that are commented out # serve to show the default. # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the # documentation root, use os.path.abspath to make it absolute, like shown here. #sys.path.insert(0, os.path.abspath('.')) # -- General configuration ------------------------------------------------ # If your documentation needs a minimal Sphinx version, state it here. #needs_sphinx = '1.0' # Add any Sphinx extension module names here, as strings. They can be # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom # ones. extensions = [] # Add any paths that contain templates here, relative to this directory. templates_path = ['_templates'] # The suffix(es) of source filenames. # You can specify multiple suffix as a list of string: # source_suffix = ['.rst', '.md'] source_suffix = '.rst' # The encoding of source files. #source_encoding = 'utf-8-sig' # The master toctree document. master_doc = 'index' # General information about the project. project = 'jsondiff' copyright = '2015, Eric Reynolds' author = 'Eric Reynolds' # The version info for the project you're documenting, acts as replacement for # |version| and |release|, also used in various other places throughout the # built documents. # # The short X.Y version. version = '0.1.0' # The full version, including alpha/beta/rc tags. release = '0.1.0' # The language for content autogenerated by Sphinx. Refer to documentation # for a list of supported languages. # # This is also used if you do content translation via gettext catalogs. # Usually you set "language" from the command line for these cases. language = None # There are two options for replacing |today|: either, you set today to some # non-false value, then it is used: #today = '' # Else, today_fmt is used as the format for a strftime call. #today_fmt = '%B %d, %Y' # List of patterns, relative to source directory, that match files and # directories to ignore when looking for source files. exclude_patterns = ['_build'] # The reST default role (used for this markup: `text`) to use for all # documents. #default_role = None # If true, '()' will be appended to :func: etc. cross-reference text. #add_function_parentheses = True # If true, the current module name will be prepended to all description # unit titles (such as .. function::). #add_module_names = True # If true, sectionauthor and moduleauthor directives will be shown in the # output. They are ignored by default. #show_authors = False # The name of the Pygments (syntax highlighting) style to use. pygments_style = 'sphinx' # A list of ignored prefixes for module index sorting. #modindex_common_prefix = [] # If true, keep warnings as "system message" paragraphs in the built documents. #keep_warnings = False # If true, `todo` and `todoList` produce output, else they produce nothing. todo_include_todos = False # -- Options for HTML output ---------------------------------------------- # The theme to use for HTML and HTML Help pages. See the documentation for # a list of builtin themes. #html_theme = 'alabaster' # Theme options are theme-specific and customize the look and feel of a theme # further. For a list of options available for each theme, see the # documentation. #html_theme_options = {} # Add any paths that contain custom themes here, relative to this directory. #html_theme_path = [] # The name for this set of Sphinx documents. If None, it defaults to # " v documentation". #html_title = None # A shorter title for the navigation bar. Default is the same as html_title. #html_short_title = None # The name of an image file (relative to this directory) to place at the top # of the sidebar. #html_logo = None # The name of an image file (within the static path) to use as favicon of the # docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 # pixels large. #html_favicon = None # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". html_static_path = ['_static'] # Add any extra paths that contain custom files (such as robots.txt or # .htaccess) here, relative to this directory. These files are copied # directly to the root of the documentation. #html_extra_path = [] # If not '', a 'Last updated on:' timestamp is inserted at every page bottom, # using the given strftime format. #html_last_updated_fmt = '%b %d, %Y' # If true, SmartyPants will be used to convert quotes and dashes to # typographically correct entities. #html_use_smartypants = True # Custom sidebar templates, maps document names to template names. #html_sidebars = {} # Additional templates that should be rendered to pages, maps page names to # template names. #html_additional_pages = {} # If false, no module index is generated. #html_domain_indices = True # If false, no index is generated. #html_use_index = True # If true, the index is split into individual pages for each letter. #html_split_index = False # If true, links to the reST sources are added to the pages. #html_show_sourcelink = True # If true, "Created using Sphinx" is shown in the HTML footer. Default is True. #html_show_sphinx = True # If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. #html_show_copyright = True # If true, an OpenSearch description file will be output, and all pages will # contain a tag referring to it. The value of this option must be the # base URL from which the finished HTML is served. #html_use_opensearch = '' # This is the file name suffix for HTML files (e.g. ".xhtml"). #html_file_suffix = None # Language to be used for generating the HTML full-text search index. # Sphinx supports the following languages: # 'da', 'de', 'en', 'es', 'fi', 'fr', 'hu', 'it', 'ja' # 'nl', 'no', 'pt', 'ro', 'ru', 'sv', 'tr' #html_search_language = 'en' # A dictionary with options for the search language support, empty by default. # Now only 'ja' uses this config value #html_search_options = {'type': 'default'} # The name of a javascript file (relative to the configuration directory) that # implements a search results scorer. If empty, the default will be used. #html_search_scorer = 'scorer.js' # Output file base name for HTML help builder. htmlhelp_basename = 'jsondiffdoc' # -- Options for LaTeX output --------------------------------------------- latex_elements = { # The paper size ('letterpaper' or 'a4paper'). #'papersize': 'letterpaper', # The font size ('10pt', '11pt' or '12pt'). #'pointsize': '10pt', # Additional stuff for the LaTeX preamble. #'preamble': '', # Latex figure (float) alignment #'figure_align': 'htbp', } # Grouping the document tree into LaTeX files. List of tuples # (source start file, target name, title, # author, documentclass [howto, manual, or own class]). latex_documents = [ (master_doc, 'jsondiff.tex', 'jsondiff Documentation', 'Eric Reynolds', 'manual'), ] # The name of an image file (relative to this directory) to place at the top of # the title page. #latex_logo = None # For "manual" documents, if this is true, then toplevel headings are parts, # not chapters. #latex_use_parts = False # If true, show page references after internal links. #latex_show_pagerefs = False # If true, show URL addresses after external links. #latex_show_urls = False # Documents to append as an appendix to all manuals. #latex_appendices = [] # If false, no module index is generated. #latex_domain_indices = True # -- Options for manual page output --------------------------------------- # One entry per manual page. List of tuples # (source start file, name, description, authors, manual section). man_pages = [ (master_doc, 'jsondiff', u'jsondiff Documentation', [author], 1) ] # If true, show URL addresses after external links. #man_show_urls = False # -- Options for Texinfo output ------------------------------------------- # Grouping the document tree into Texinfo files. List of tuples # (source start file, target name, title, author, # dir menu entry, description, category) texinfo_documents = [ (master_doc, 'jsondiff', 'jsondiff Documentation', author, 'jsondiff', 'One line description of project.', 'Miscellaneous'), ] # Documents to append as an appendix to all manuals. #texinfo_appendices = [] # If false, no module index is generated. #texinfo_domain_indices = True # How to display URL addresses: 'footnote', 'no', or 'inline'. #texinfo_show_urls = 'footnote' # If true, do not generate a @detailmenu in the "Top" node's menu. #texinfo_no_detailmenu = False jsondiff-2.1.2/docs/index.rst000066400000000000000000000010071464503250600161120ustar00rootroot00000000000000jsondiff ======== jsondiff is an MIT-licensed Python library which lets you compare, diff and patch JSON and JSON-like structures in Python. It has special support for * multiple and custom-defined diff syntaxes * Python sets * similarity-based list comparison .. note:: jsondiff is currently in an early stage. The API might change in backward incompatible ways. Contents: .. toctree:: :maxdepth: 2 basic_use Indices and tables ================== * :ref:`genindex` * :ref:`modindex` * :ref:`search` jsondiff-2.1.2/docs/make.bat000066400000000000000000000155111464503250600156630ustar00rootroot00000000000000@ECHO OFF REM Command file for Sphinx documentation if "%SPHINXBUILD%" == "" ( set SPHINXBUILD=sphinx-build ) set BUILDDIR=_build set ALLSPHINXOPTS=-d %BUILDDIR%/doctrees %SPHINXOPTS% . set I18NSPHINXOPTS=%SPHINXOPTS% . if NOT "%PAPER%" == "" ( set ALLSPHINXOPTS=-D latex_paper_size=%PAPER% %ALLSPHINXOPTS% set I18NSPHINXOPTS=-D latex_paper_size=%PAPER% %I18NSPHINXOPTS% ) if "%1" == "" goto help if "%1" == "help" ( :help echo.Please use `make ^` where ^ is one of echo. html to make standalone HTML files echo. dirhtml to make HTML files named index.html in directories echo. singlehtml to make a single large HTML file echo. pickle to make pickle files echo. json to make JSON files echo. htmlhelp to make HTML files and a HTML help project echo. qthelp to make HTML files and a qthelp project echo. devhelp to make HTML files and a Devhelp project echo. epub to make an epub echo. latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter echo. text to make text files echo. man to make manual pages echo. texinfo to make Texinfo files echo. gettext to make PO message catalogs echo. changes to make an overview over all changed/added/deprecated items echo. xml to make Docutils-native XML files echo. pseudoxml to make pseudoxml-XML files for display purposes echo. linkcheck to check all external links for integrity echo. doctest to run all doctests embedded in the documentation if enabled echo. coverage to run coverage check of the documentation if enabled goto end ) if "%1" == "clean" ( for /d %%i in (%BUILDDIR%\*) do rmdir /q /s %%i del /q /s %BUILDDIR%\* goto end ) REM Check if sphinx-build is available and fallback to Python version if any %SPHINXBUILD% 2> nul if errorlevel 9009 goto sphinx_python goto sphinx_ok :sphinx_python set SPHINXBUILD=python -m sphinx.__init__ %SPHINXBUILD% 2> nul if errorlevel 9009 ( echo. echo.The 'sphinx-build' command was not found. Make sure you have Sphinx echo.installed, then set the SPHINXBUILD environment variable to point echo.to the full path of the 'sphinx-build' executable. Alternatively you echo.may add the Sphinx directory to PATH. echo. echo.If you don't have Sphinx installed, grab it from echo.http://sphinx-doc.org/ exit /b 1 ) :sphinx_ok if "%1" == "html" ( %SPHINXBUILD% -b html %ALLSPHINXOPTS% %BUILDDIR%/html if errorlevel 1 exit /b 1 echo. echo.Build finished. The HTML pages are in %BUILDDIR%/html. goto end ) if "%1" == "dirhtml" ( %SPHINXBUILD% -b dirhtml %ALLSPHINXOPTS% %BUILDDIR%/dirhtml if errorlevel 1 exit /b 1 echo. echo.Build finished. The HTML pages are in %BUILDDIR%/dirhtml. goto end ) if "%1" == "singlehtml" ( %SPHINXBUILD% -b singlehtml %ALLSPHINXOPTS% %BUILDDIR%/singlehtml if errorlevel 1 exit /b 1 echo. echo.Build finished. The HTML pages are in %BUILDDIR%/singlehtml. goto end ) if "%1" == "pickle" ( %SPHINXBUILD% -b pickle %ALLSPHINXOPTS% %BUILDDIR%/pickle if errorlevel 1 exit /b 1 echo. echo.Build finished; now you can process the pickle files. goto end ) if "%1" == "json" ( %SPHINXBUILD% -b json %ALLSPHINXOPTS% %BUILDDIR%/json if errorlevel 1 exit /b 1 echo. echo.Build finished; now you can process the JSON files. goto end ) if "%1" == "htmlhelp" ( %SPHINXBUILD% -b htmlhelp %ALLSPHINXOPTS% %BUILDDIR%/htmlhelp if errorlevel 1 exit /b 1 echo. echo.Build finished; now you can run HTML Help Workshop with the ^ .hhp project file in %BUILDDIR%/htmlhelp. goto end ) if "%1" == "qthelp" ( %SPHINXBUILD% -b qthelp %ALLSPHINXOPTS% %BUILDDIR%/qthelp if errorlevel 1 exit /b 1 echo. echo.Build finished; now you can run "qcollectiongenerator" with the ^ .qhcp project file in %BUILDDIR%/qthelp, like this: echo.^> qcollectiongenerator %BUILDDIR%\qthelp\jsondiff.qhcp echo.To view the help file: echo.^> assistant -collectionFile %BUILDDIR%\qthelp\jsondiff.ghc goto end ) if "%1" == "devhelp" ( %SPHINXBUILD% -b devhelp %ALLSPHINXOPTS% %BUILDDIR%/devhelp if errorlevel 1 exit /b 1 echo. echo.Build finished. goto end ) if "%1" == "epub" ( %SPHINXBUILD% -b epub %ALLSPHINXOPTS% %BUILDDIR%/epub if errorlevel 1 exit /b 1 echo. echo.Build finished. The epub file is in %BUILDDIR%/epub. goto end ) if "%1" == "latex" ( %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex if errorlevel 1 exit /b 1 echo. echo.Build finished; the LaTeX files are in %BUILDDIR%/latex. goto end ) if "%1" == "latexpdf" ( %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex cd %BUILDDIR%/latex make all-pdf cd %~dp0 echo. echo.Build finished; the PDF files are in %BUILDDIR%/latex. goto end ) if "%1" == "latexpdfja" ( %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex cd %BUILDDIR%/latex make all-pdf-ja cd %~dp0 echo. echo.Build finished; the PDF files are in %BUILDDIR%/latex. goto end ) if "%1" == "text" ( %SPHINXBUILD% -b text %ALLSPHINXOPTS% %BUILDDIR%/text if errorlevel 1 exit /b 1 echo. echo.Build finished. The text files are in %BUILDDIR%/text. goto end ) if "%1" == "man" ( %SPHINXBUILD% -b man %ALLSPHINXOPTS% %BUILDDIR%/man if errorlevel 1 exit /b 1 echo. echo.Build finished. The manual pages are in %BUILDDIR%/man. goto end ) if "%1" == "texinfo" ( %SPHINXBUILD% -b texinfo %ALLSPHINXOPTS% %BUILDDIR%/texinfo if errorlevel 1 exit /b 1 echo. echo.Build finished. The Texinfo files are in %BUILDDIR%/texinfo. goto end ) if "%1" == "gettext" ( %SPHINXBUILD% -b gettext %I18NSPHINXOPTS% %BUILDDIR%/locale if errorlevel 1 exit /b 1 echo. echo.Build finished. The message catalogs are in %BUILDDIR%/locale. goto end ) if "%1" == "changes" ( %SPHINXBUILD% -b changes %ALLSPHINXOPTS% %BUILDDIR%/changes if errorlevel 1 exit /b 1 echo. echo.The overview file is in %BUILDDIR%/changes. goto end ) if "%1" == "linkcheck" ( %SPHINXBUILD% -b linkcheck %ALLSPHINXOPTS% %BUILDDIR%/linkcheck if errorlevel 1 exit /b 1 echo. echo.Link check complete; look for any errors in the above output ^ or in %BUILDDIR%/linkcheck/output.txt. goto end ) if "%1" == "doctest" ( %SPHINXBUILD% -b doctest %ALLSPHINXOPTS% %BUILDDIR%/doctest if errorlevel 1 exit /b 1 echo. echo.Testing of doctests in the sources finished, look at the ^ results in %BUILDDIR%/doctest/output.txt. goto end ) if "%1" == "coverage" ( %SPHINXBUILD% -b coverage %ALLSPHINXOPTS% %BUILDDIR%/coverage if errorlevel 1 exit /b 1 echo. echo.Testing of coverage in the sources finished, look at the ^ results in %BUILDDIR%/coverage/python.txt. goto end ) if "%1" == "xml" ( %SPHINXBUILD% -b xml %ALLSPHINXOPTS% %BUILDDIR%/xml if errorlevel 1 exit /b 1 echo. echo.Build finished. The XML files are in %BUILDDIR%/xml. goto end ) if "%1" == "pseudoxml" ( %SPHINXBUILD% -b pseudoxml %ALLSPHINXOPTS% %BUILDDIR%/pseudoxml if errorlevel 1 exit /b 1 echo. echo.Build finished. The pseudo-XML files are in %BUILDDIR%/pseudoxml. goto end ) :end jsondiff-2.1.2/jsondiff/000077500000000000000000000000001464503250600151255ustar00rootroot00000000000000jsondiff-2.1.2/jsondiff/__init__.py000066400000000000000000001154011464503250600172400ustar00rootroot00000000000000import json import yaml from json import JSONDecodeError from yaml import YAMLError from .symbols import * from .symbols import Symbol from ._version import __version__ # rules # - keys and strings which start with $ (or specified escape_str) are escaped to $$ (or escape_str * 2) # - when source is dict and diff is a dict -> patch # - when source is list and diff is a list patch dict -> patch # - else -> replacement class JsonDumper: def __init__(self, **kwargs): self.kwargs = kwargs def __call__(self, obj, dest=None): if dest is None: return json.dumps(obj, **self.kwargs) else: return json.dump(obj, dest, **self.kwargs) default_dumper = JsonDumper() class YamlDumper: """Write object as YAML string""" def __init__(self, **kwargs): self.kwargs = kwargs def __call__(self, obj, dest=None): """Format obj as a YAML string and optionally write to dest :param obj: dict to dump :param dest: file-like object :return: str """ return yaml.dump(obj, dest, **self.kwargs) class JsonLoader: def __init__(self, **kwargs): self.kwargs = kwargs def __call__(self, src): """Parse and return JSON data :param src: str|file-like source :return: dict parsed data """ if isinstance(src, str): return json.loads(src, **self.kwargs) else: return json.load(src, **self.kwargs) default_loader = JsonLoader() class YamlLoader: """Load YAML data from file-like object or string""" def __call__(self, src): """Parse and return YAML data :param src: str|file-like source :return: dict parsed data """ return yaml.safe_load(src) class Serializer: """Serializer helper loads and stores object data :param file_format: str json or yaml :param indent: int Output indentation in spaces :raise ValueError: file_path does not contains valid file_format data """ def __init__(self, file_format, indent): # pyyaml _can_ load json but is ~20 times slower and has known issues so use # the json from stdlib when json is specified. self.serializers = { "json": (JsonLoader(), JsonDumper(indent=indent)), "yaml": (YamlLoader(), YamlDumper(indent=indent)), } self.file_format = file_format if file_format not in self.serializers: raise ValueError(f"Unsupported serialization format {file_format}, expected one of {self.serializers.keys()}") def deserialize_file(self, src): """Deserialize file from the specified format :param file_path: str path to file :param src: str|file-like source :return dict :raise ValueError: file_path does not contain valid file_format data """ loader, _ = self.serializers[self.file_format] try: parsed = loader(src) except (JSONDecodeError, YAMLError) as ex: raise ValueError(f"Invalid {self.file_format} file") from ex return parsed def serialize_data(self, obj, stream): """Serialize obj and write to stream :param obj: dict to serialize :param stream: Writeable stream """ _, dumper = self.serializers[self.file_format] dumper(obj, stream) class JsonDiffSyntax: def emit_set_diff(self, a, b, s, added, removed): """ Emits the difference between two sets. :param a: The original set. :param b: The modified set. :param s: The path to the current location in the JSON structure. :param added: Elements that were added to 'b'. :param removed: Elements that were removed from 'a'. :raises NotImplementedError: This is an abstract method. """ raise NotImplementedError() def emit_list_diff(self, a, b, s, inserted, changed, deleted): """ Emits the difference between two lists. :param a: The original list. :param b: The modified list. :param s: The path to the current location in the JSON structure. :param inserted: Index and value of elements inserted into 'b'. :param changed: Index, original value, and new value of elements that have changed. :param deleted: Index and value of elements that were deleted from 'a'. :raises NotImplementedError: This is an abstract method. """ raise NotImplementedError() def emit_dict_diff(self, a, b, s, added, changed, removed): """ Emits the difference between two dictionaries. :param a: The original dictionary. :param b: The modified dictionary. :param s: The path to the current location in the JSON structure. :param added: Key-value pairs that were added to 'b'. :param changed: Keys and their corresponding old and new values for items that have changed. :param removed: Keys of items that were removed from 'a'. :raises NotImplementedError: This is an abstract method. """ raise NotImplementedError() def emit_value_diff(self, a, b, s): """ Emits the difference between two values. :param a: The original value. :param b: The modified value. :param s: The path to the current location in the JSON structure. :raises NotImplementedError: This is an abstract method. """ raise NotImplementedError() def patch(self, a, d): """ Applies a patch to a JSON structure. :param a: The original JSON structure. :param d: The patch to apply. :return: The patched JSON structure. :raises NotImplementedError: This is an abstract method. """ raise NotImplementedError() def unpatch(self, a, d): """ Reverses a patch on a JSON structure. :param a: The patched JSON structure. :param d: The patch that was applied. :return: The original JSON structure before the patch was applied. :raises NotImplementedError: This is an abstract method. """ raise NotImplementedError() class CompactJsonDiffSyntax: """ Provides a compact syntax for JSON differences, focusing on minimizing the output size. This class is designed to emit and apply differences between two JSON structures in a compact form, making it suitable for scenarios where bandwidth or storage efficiency is critical. Example: Given two JSON structures, `a` and `b`: a = {"name": "Alice", "age": 30, "skills": ["Python", "Django"]} b = {"name": "Alice", "age": 31, "skills": ["Python", "Django", "Flask"]} The `emit_dict_diff` method would produce a compact diff like: { "age": 31, "skills": {"insert": [(2, "Flask")]} } This diff can then be applied to `a` using the `patch` method to obtain `b`. """ def emit_set_diff(self, a, b, s, added, removed): """ Emits a compact representation of the difference between two sets. :param a: The original set. :param b: The modified set. :param s: Similarity score between the two sets. :param added: Elements added to the original set. :param removed: Elements removed from the original set. :return: A dictionary representing the changes in a compact form. """ if s == 0.0 or len(removed) == len(a): return {replace: b} if isinstance(b, dict) else b else: d = {} if removed: d[discard] = removed if added: d[add] = added return d def emit_list_diff(self, a, b, s, inserted, changed, deleted): """ Emits a compact representation of the difference between two lists. :param a: The original list. :param b: The modified list. :param s: Similarity score between the two lists. :param inserted: Elements inserted into the original list. :param changed: Elements changed in the original list. :param deleted: Elements deleted from the original list. :return: A dictionary representing the changes in a compact form. """ if s == 0.0: return {replace: b} if isinstance(b, dict) else b elif s == 1.0 and not (inserted or changed or deleted): return {} else: d = changed if inserted: d[insert] = inserted if deleted: d[delete] = [pos for pos, value in deleted] return d def emit_dict_diff(self, a, b, s, added, changed, removed): """ Emits a compact representation of the difference between two dictionaries. :param a: The original dictionary. :param b: The modified dictionary. :param s: Similarity score between the two dictionaries. :param added: Key-value pairs added to the original dictionary. :param changed: Key-value pairs changed in the original dictionary. :param removed: Keys removed from the original dictionary. :return: A dictionary representing the changes in a compact form. """ if s == 0.0: return {replace: b} if isinstance(b, dict) else b elif s == 1.0 and not (added or changed or removed): return {} else: changed.update(added) if removed: changed[delete] = list(removed.keys()) return changed def emit_value_diff(self, a, b, s): """ Emits a compact representation of the difference between two values. :param a: The original value. :param b: The modified value. :param s: Similarity score between the two values. :return: A dictionary or value representing the change in a compact form. """ if s == 1.0: return {} else: return {replace: b} if isinstance(b, dict) else b def patch(self, a, d): """ Applies a compact diff to a JSON structure to produce the modified structure. :param a: The original JSON structure. :param d: The compact diff to apply. :return: The modified JSON structure after applying the diff. """ if isinstance(d, dict): if not d: return a if replace in d: return d[replace] if isinstance(a, dict): a = dict(a) for k, v in d.items(): if k is delete: for kdel in v: del a[kdel] else: av = a.get(k, missing) if av is missing: a[k] = v else: a[k] = self.patch(av, v) return a elif isinstance(a, (list, tuple)): original_type = type(a) a = list(a) if delete in d: for pos in d[delete]: a.pop(pos) if insert in d: for pos, value in d[insert]: a.insert(pos, value) for k, v in d.items(): if k is not delete and k is not insert: k = int(k) a[k] = self.patch(a[k], v) if original_type is not list: a = original_type(a) return a elif isinstance(a, set): a = set(a) if discard in d: for x in d[discard]: a.discard(x) if add in d: for x in d[add]: a.add(x) return a return d class ExplicitJsonDiffSyntax: """ Provides an explicit syntax for JSON differences, focusing on clarity and readability. This class is designed to emit and apply differences between two JSON structures in a form that is easy to understand, making it suitable for scenarios where human readability of diffs is important. Example: Given two JSON structures, `a` and `b`: a = {"name": "Alice", "age": 30, "skills": ["Python", "Django"]} b = {"name": "Alice", "age": 31, "skills": ["Python", "Django", "Flask"]} The `emit_dict_diff` method would produce an explicit diff like: { "age": 31, "skills": {"insert": [(2, "Flask")]} } Unlike the compact syntax, this explicit form prioritizes readability and ease of understanding over minimizing size. This diff can then be applied to `a` using the `patch` method to obtain `b`. """ def emit_set_diff(self, a, b, s, added, removed): """ Emits an explicit representation of the difference between two sets. :param a: The original set. :param b: The modified set. :param s: Similarity score between the two sets. :param added: Elements added to the original set. :param removed: Elements removed from the original set. :return: A dictionary representing the changes in an explicit form. """ if s == 0.0 or len(removed) == len(a): return b else: d = {} if removed: d[discard] = removed if added: d[add] = added return d def emit_list_diff(self, a, b, s, inserted, changed, deleted): """ Emits an explicit representation of the difference between two lists. :param a: The original list. :param b: The modified list. :param s: Similarity score between the two lists. :param inserted: Elements inserted into the original list. :param changed: Elements changed in the original list. :param deleted: Elements deleted from the original list. :return: A dictionary representing the changes in an explicit form. """ if s == 0.0 and not (inserted or changed or deleted): return b elif s == 1.0 and not (inserted or changed or deleted): return {} else: d = changed if inserted: d[insert] = inserted if deleted: d[delete] = [pos for pos, value in deleted] return d def emit_dict_diff(self, a, b, s, added, changed, removed): """ Emits an explicit representation of the difference between two dictionaries. :param a: The original dictionary. :param b: The modified dictionary. :param s: Similarity score between the two dictionaries. :param added: Key-value pairs added to the original dictionary. :param changed: Key-value pairs changed in the original dictionary. :param removed: Keys removed from the original dictionary. :return: A dictionary representing the changes in an explicit form. """ if s == 0.0 and not (added or changed or removed): return b elif s == 1.0 and not (added or changed or removed): return {} else: d = {} if added: d[insert] = added if changed: d[update] = changed if removed: d[delete] = list(removed.keys()) return d def emit_value_diff(self, a, b, s): """ Emits an explicit representation of the difference between two values. :param a: The original value. :param b: The modified value. :param s: Similarity score between the two values. :return: A dictionary or value representing the change in an explicit form. """ if s == 1.0: return {} else: return b class SymmetricJsonDiffSyntax: """ Provides a symmetric syntax for JSON differences, focusing on maintaining both original and modified values. This class is designed to emit differences between two JSON structures in a way that both the original and modified values are kept, making it suitable for scenarios where tracking both versions of the data is important. Example: Given two JSON structures, `a` and `b`: a = {"name": "Alice", "age": 30, "skills": ["Python", "Django"]} b = {"name": "Alice", "age": 31, "skills": ["Python", "Django", "Flask"]} The `emit_dict_diff` method would produce a symmetric diff like: { "age": [30, 31], "skills": {"insert": [(2, "Flask")]} } This diff maintains both the original and modified values for the age field, and clearly shows the insertion in the skills list. This format is particularly useful for applications that need to display or process both versions of the data. The `patch` and `unpatch` methods can apply and reverse these diffs, respectively, allowing for flexible data manipulation. """ def emit_set_diff(self, a, b, s, added, removed): """ Emits a symmetric representation of the difference between two sets. :param a: The original set. :param b: The modified set. :param s: Similarity score between the two sets. :param added: Elements added to the original set. :param removed: Elements removed from the original set. :return: A dictionary representing the changes in a symmetric form. """ if s == 0.0 or len(removed) == len(a): return [a, b] else: d = {} if added: d[add] = added if removed: d[discard] = removed return d def emit_list_diff(self, a, b, s, inserted, changed, deleted): """ Emits a symmetric representation of the difference between two lists. :param a: The original list. :param b: The modified list. :param s: Similarity score between the two lists. :param inserted: Elements inserted into the original list. :param changed: Elements changed in the original list. :param deleted: Elements deleted from the original list. :return: A dictionary representing the changes in a symmetric form. """ if s == 0.0 and not (inserted or changed or deleted): return [a, b] elif s == 1.0 and not (inserted or changed or deleted): return {} else: d = changed if inserted: d[insert] = inserted if deleted: d[delete] = deleted return d def emit_dict_diff(self, a, b, s, added, changed, removed): """ Emits a symmetric representation of the difference between two dictionaries. :param a: The original dictionary. :param b: The modified dictionary. :param s: Similarity score between the two dictionaries. :param added: Key-value pairs added to the original dictionary. :param changed: Key-value pairs changed in the original dictionary. :param removed: Keys removed from the original dictionary. :return: A dictionary representing the changes in a symmetric form. """ if s == 0.0 and not (added or changed or removed): return [a, b] elif s == 1.0 and not (added or changed or removed): return {} else: d = changed if added: d[insert] = added if removed: d[delete] = removed return d def emit_value_diff(self, a, b, s): """ Emits a symmetric representation of the difference between two values. :param a: The original value. :param b: The modified value. :param s: Similarity score between the two values. :return: A list containing the original and modified values. """ if s == 1.0: return {} else: return [a, b] def patch(self, a, d): """ Applies a symmetric diff to a JSON structure to produce the modified structure. :param a: The original JSON structure. :param d: The symmetric diff to apply. :return: The modified JSON structure after applying the diff. """ if isinstance(d, list): _, b = d return b elif isinstance(d, dict): if not d: return a if isinstance(a, dict): a = dict(a) for k, v in d.items(): if k is delete: for kdel, _ in v.items(): del a[kdel] elif k is insert: for kk, vv in v.items(): a[kk] = vv else: a[k] = self.patch(a[k], v) return a elif isinstance(a, (list, tuple)): original_type = type(a) a = list(a) if delete in d: for pos, value in d[delete]: a.pop(pos) if insert in d: for pos, value in d[insert]: a.insert(pos, value) for k, v in d.items(): if k is not delete and k is not insert: k = int(k) a[k] = self.patch(a[k], v) if original_type is not list: a = original_type(a) return a elif isinstance(a, set): a = set(a) if discard in d: for x in d[discard]: a.discard(x) if add in d: for x in d[add]: a.add(x) return a raise Exception("Invalid symmetric diff") def unpatch(self, b, d): """ Reverses a symmetric diff on a JSON structure to produce the original structure. :param b: The modified JSON structure. :param d: The symmetric diff that was applied. :return: The original JSON structure before the diff was applied. """ if isinstance(d, list): a, _ = d return a elif isinstance(d, dict): if not d: return b if isinstance(b, dict): b = dict(b) for k, v in d.items(): if k is delete: for kk, vv in v.items(): b[kk] = vv elif k is insert: for kk, vv in v.items(): del b[kk] else: b[k] = self.unpatch(b[k], v) return b elif isinstance(b, (list, tuple)): original_type = type(b) b = list(b) for k, v in d.items(): if k is not delete and k is not insert: k = int(k) b[k] = self.unpatch(b[k], v) if insert in d: for pos, value in reversed(d[insert]): b.pop(pos) if delete in d: for pos, value in reversed(d[delete]): b.insert(pos, value) if original_type is not list: b = original_type(b) return b elif isinstance(b, set): b = set(b) if discard in d: for x in d[discard]: b.add(x) if add in d: for x in d[add]: b.discard(x) return b raise Exception("Invalid symmetric diff") class RightOnlyJsonDiffSyntax(CompactJsonDiffSyntax): """ Extends CompactJsonDiffSyntax to focus exclusively on the right (modified) values for lists, suitable for scenarios where only the latest state matters, ignoring the specific changes that led there. Compare to the CompactJsonDiffSyntax, I will not compare the difference in list, because in some senario we only care about the right value (in most cases means latest value). Instead, I will pop the later list value. Example: Given two JSON structures, `a` and `b`: a = {"name": "Alice", "age": 30, "skills": ["Python", "Django"]} b = {"name": "Alice", "age": 31, "skills": ["Python", "Django", "Flask"]} The `emit_dict_diff` method would produce a diff focusing on the updated and added fields: { "age": 31, "skills": ["Python", "Django", "Flask"] } And the `emit_list_diff` method directly returns the modified list without detailing the individual changes: ["Python", "Django", "Flask"] This approach simplifies the diff when the path from `a` to `b` is not as relevant as the final state represented by `b`. """ def emit_dict_diff(self, a, b, s, added, changed, removed): """ Emits a diff for dictionaries focusing on the final state, combining added and changed fields, and listing removed keys. :param a: The original dictionary. :param b: The modified dictionary. :param s: Similarity score between the two dictionaries. :param added: Key-value pairs added to the original dictionary. :param changed: Key-value pairs changed in the original dictionary. :param removed: Keys removed from the original dictionary. :return: A dictionary representing the final state or changes in a compact form. """ if s == 1.0: return {} else: changed.update(added) if removed: changed[delete] = list(removed.keys()) return changed def emit_list_diff(self, a, b, s, inserted, changed, deleted): """ Directly returns the modified list, disregarding the specifics of how it was altered from the original list. :param a: The original list. :param b: The modified list. :param s: Similarity score between the two lists. :param inserted: Elements inserted into the original list. :param changed: Elements changed in the original list. :param deleted: Elements deleted from the original list. :return: The modified list as the final state. """ if s == 0.0: return b elif s == 1.0: return {} else: return b builtin_syntaxes = { 'compact': CompactJsonDiffSyntax(), 'symmetric': SymmetricJsonDiffSyntax(), 'explicit': ExplicitJsonDiffSyntax(), 'rightonly': RightOnlyJsonDiffSyntax(), } class JsonDiffer: """ A class for computing differences between two JSON structures and applying patches based on these differences. Attributes: options (Options): Configuration options for the differ. _symbol_map (dict): A mapping of escaped symbols to their Symbol instances. Methods: diff(a, b, fp=None): Computes the difference between two JSON structures. similarity(a, b): Calculates the similarity score between two JSON structures. patch(a, d, fp=None): Applies a diff to a JSON structure to produce the modified structure. unpatch(b, d, fp=None): Reverses a diff on a JSON structure to produce the original structure. _unescape(x): Unescapes a string that has been escaped. unmarshal(d): Converts a marshaled (potentially escaped) structure back to its original form. _escape(o): Escapes a string or symbol that needs escaping. marshal(d): Converts a structure to a marshaled (potentially escaped) form. """ class Options: """ A placeholder class for options used by JsonDiffer. Options include syntax, load, dump, marshal, loader, dumper, and escape_str. """ pass def __init__(self, syntax='compact', load=False, dump=False, marshal=False, loader=default_loader, dumper=default_dumper, escape_str='$'): """ Initializes the JsonDiffer with specified options. :param syntax: The syntax to use for diffs. Defaults to 'compact'. :param load: Whether to automatically load JSON from strings or files. :param dump: Whether to automatically dump output to JSON strings or files. :param marshal: Whether to marshal diffs to handle special characters. :param loader: Custom function for loading JSON data. :param dumper: Custom function for dumping JSON data. :param escape_str: String used to escape special characters in keys. """ self.options = JsonDiffer.Options() self.options.syntax = builtin_syntaxes.get(syntax, syntax) self.options.load = load self.options.dump = dump self.options.marshal = marshal self.options.loader = loader self.options.dumper = dumper self.options.escape_str = escape_str self._symbol_map = { escape_str + symbol.label: symbol for symbol in _all_symbols_ } def _list_diff_0(self, C, X, Y): """ Helper method for computing list differences using dynamic programming. """ i, j = len(X), len(Y) r = [] while True: if i > 0 and j > 0: d, s = self._obj_diff(X[i-1], Y[j-1]) if s > 0 and C[i][j] == C[i-1][j-1] + s: r.append((0, d, j-1, s)) i, j = i - 1, j - 1 continue if j > 0 and (i == 0 or C[i][j-1] >= C[i-1][j]): r.append((1, Y[j-1], j-1, 0.0)) j = j - 1 continue if i > 0 and (j == 0 or C[i][j-1] < C[i-1][j]): r.append((-1, X[i-1], i-1, 0.0)) i = i - 1 continue return reversed(r) def _list_diff(self, X, Y): """ Computes the difference between two lists. """ # LCS m = len(X) n = len(Y) # An (m+1) times (n+1) matrix C = [[0 for j in range(n+1)] for i in range(m+1)] for i in range(1, m+1): for j in range(1, n+1): _, s = self._obj_diff(X[i-1], Y[j-1]) # Following lines are part of the original LCS algorithm # left in the code in case modification turns out to be problematic #if X[i-1] == Y[j-1]: # C[i][j] = C[i-1][j-1] + 1 #else: C[i][j] = max(C[i][j-1], C[i-1][j], C[i-1][j-1] + s) inserted = [] deleted = [] changed = {} tot_s = 0.0 for sign, value, pos, s in self._list_diff_0(C, X, Y): if sign == 1: inserted.append((pos, value)) elif sign == -1: deleted.insert(0, (pos, value)) elif sign == 0 and s < 1: changed[pos] = value tot_s += s tot_n = len(X) + len(inserted) if tot_n == 0: s = 1.0 else: s = tot_s / tot_n return self.options.syntax.emit_list_diff(X, Y, s, inserted, changed, deleted), s def _set_diff(self, a, b): """ Computes the difference between two sets. """ removed = a.difference(b) added = b.difference(a) if not removed and not added: return {}, 1.0 ranking = sorted( ( (self._obj_diff(x, y)[1], x, y) for x in removed for y in added ), reverse=True, key=lambda x: x[0] ) r2 = set(removed) a2 = set(added) n_common = len(a) - len(removed) s_common = float(n_common) for s, x, y in ranking: if x in r2 and y in a2: r2.discard(x) a2.discard(y) s_common += s n_common += 1 if not r2 or not a2: break n_tot = len(a) + len(added) s = s_common / n_tot if n_tot != 0 else 1.0 return self.options.syntax.emit_set_diff(a, b, s, added, removed), s def _dict_diff(self, a, b): """ Computes the difference between two dictionaries. """ removed = {} nremoved = 0 nadded = 0 nmatched = 0 smatched = 0.0 added = {} changed = {} for k, v in a.items(): w = b.get(k, missing) if w is missing: nremoved += 1 removed[k] = v else: nmatched += 1 d, s = self._obj_diff(v, w) if s < 1.0: changed[k] = d smatched += 0.5 + 0.5 * s for k, v in b.items(): if k not in a: nadded += 1 added[k] = v n_tot = nremoved + nmatched + nadded s = smatched / n_tot if n_tot != 0 else 1.0 return self.options.syntax.emit_dict_diff(a, b, s, added, changed, removed), s def _obj_diff(self, a, b): """ Computes the difference between any two JSON-compatible objects. """ if a is b: return self.options.syntax.emit_value_diff(a, b, 1.0), 1.0 if isinstance(a, dict) and isinstance(b, dict): return self._dict_diff(a, b) elif isinstance(a, tuple) and isinstance(b, tuple): return self._list_diff(a, b) elif isinstance(a, list) and isinstance(b, list): return self._list_diff(a, b) elif isinstance(a, set) and isinstance(b, set): return self._set_diff(a, b) elif a != b: return self.options.syntax.emit_value_diff(a, b, 0.0), 0.0 else: return self.options.syntax.emit_value_diff(a, b, 1.0), 1.0 def diff(self, a, b, fp=None): """ Computes the difference between two JSON structures. """ if self.options.load: a = self.options.loader(a) b = self.options.loader(b) d, s = self._obj_diff(a, b) if self.options.marshal or self.options.dump: d = self.marshal(d) if self.options.dump: return self.options.dumper(d, fp) else: return d def similarity(self, a, b): """ Calculates the similarity score between two JSON structures. """ if self.options.load: a = self.options.loader(a) b = self.options.loader(b) d, s = self._obj_diff(a, b) return s def patch(self, a, d, fp=None): """ Applies a diff to a JSON structure to produce the modified structure. """ if self.options.load: a = self.options.loader(a) d = self.options.loader(d) if self.options.marshal or self.options.load: d = self.unmarshal(d) b = self.options.syntax.patch(a, d) if self.options.dump: return self.options.dumper(b, fp) else: return b def unpatch(self, b, d, fp=None): """ Reverses a diff on a JSON structure to produce the original structure. """ if self.options.load: b = self.options.loader(b) d = self.options.loader(d) if self.options.marshal or self.options.load: d = self.unmarshal(d) a = self.options.syntax.unpatch(b, d) if self.options.dump: return self.options.dumper(a, fp) else: return a def _unescape(self, x): """ Unescapes a string that has been escaped. """ if isinstance(x, str): sym = self._symbol_map.get(x, None) if sym is not None: return sym if x.startswith(self.options.escape_str): return x[1:] return x def unmarshal(self, d): """ Converts a marshaled (potentially escaped) structure back to its original form. """ if isinstance(d, dict): return { self._unescape(k): self.unmarshal(v) for k, v in d.items() } elif isinstance(d, (list, tuple)): return type(d)( self.unmarshal(x) for x in d ) else: return self._unescape(d) def _escape(self, o): """ Escapes a string or symbol that needs escaping. """ if type(o) is Symbol: return self.options.escape_str + o.label if isinstance(o, str) and o.startswith(self.options.escape_str): return self.options.escape_str + o return o def marshal(self, d): """ Converts a structure to a marshaled (potentially escaped) form. """ if isinstance(d, dict): return { self._escape(k): self.marshal(v) for k, v in d.items() } elif isinstance(d, (list, tuple)): return type(d)( self.marshal(x) for x in d ) else: return self._escape(d) def diff(a, b, fp=None, cls=JsonDiffer, **kwargs): """ Computes the difference between two JSON structures using a specified JsonDiffer class. :param a: The original JSON structure. :param b: The modified JSON structure. :param fp: Optional file pointer to dump the diff to. :param cls: The JsonDiffer class or subclass to use for computing the diff. :param kwargs: Additional keyword arguments to pass to the JsonDiffer constructor. :return: The computed diff. """ return cls(**kwargs).diff(a, b, fp) def patch(a, d, fp=None, cls=JsonDiffer, **kwargs): """ Applies a diff to a JSON structure to produce the modified structure using a specified JsonDiffer class. :param a: The original JSON structure. :param d: The diff to apply. :param fp: Optional file pointer to dump the patched structure to. :param cls: The JsonDiffer class or subclass to use for applying the diff. :param kwargs: Additional keyword arguments to pass to the JsonDiffer constructor. :return: The patched JSON structure. """ return cls(**kwargs).patch(a, d, fp) def similarity(a, b, cls=JsonDiffer, **kwargs): """ Calculates the similarity score between two JSON structures using a specified JsonDiffer class. :param a: The first JSON structure. :param b: The second JSON structure. :param cls: The JsonDiffer class or subclass to use for calculating similarity. :param kwargs: Additional keyword arguments to pass to the JsonDiffer constructor. :return: A similarity score as a float between 0.0 and 1.0. """ return cls(**kwargs).similarity(a, b) __all__ = [ "similarity", "diff", "JsonDiffer", "JsonDumper", "JsonLoader", "YamlDumper", "YamlLoader", "Serializer", ] jsondiff-2.1.2/jsondiff/cli.py000066400000000000000000000035341464503250600162530ustar00rootroot00000000000000import argparse import jsondiff import sys def load_file(serializer, file_path): with open(file_path) as f: parsed = None try: parsed = serializer.deserialize_file(f) except ValueError: print(f"{file_path} is not valid {serializer.file_format}") except FileNotFoundError: print(f"{file_path} does not exist") return parsed def main(): parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) parser.add_argument("first") parser.add_argument("second") parser.add_argument("-p", "--patch", action="store_true", default=False) parser.add_argument("-s", "--syntax", choices=(jsondiff.builtin_syntaxes.keys()), default="compact", help="Diff syntax controls how differences are rendered") parser.add_argument("-i", "--indent", action="store", type=int, default=None, help="Number of spaces to indent. None is compact, no indentation.") parser.add_argument("-f", "--format", choices=("json", "yaml"), default="json", help="Specify file format for input and dump") args = parser.parse_args() serializer = jsondiff.Serializer(args.format, args.indent) parsed_first = load_file(serializer, args.first) parsed_second = load_file(serializer, args.second) if not (parsed_first and parsed_second): return 1 if args.patch: x = jsondiff.patch( parsed_first, parsed_second, marshal=True, syntax=args.syntax ) else: x = jsondiff.diff( parsed_first, parsed_second, marshal=True, syntax=args.syntax ) serializer.serialize_data(x, sys.stdout) return 0 if __name__ == '__main__': ret = main() sys.exit(ret) jsondiff-2.1.2/jsondiff/symbols.py000066400000000000000000000042641464503250600171750ustar00rootroot00000000000000class Symbol: """ Symbol Usage Explanation: $add: Indicates keys or indices where new elements have been added. $discard: Indicates elements that have been removed from a set. $delete: Indicates keys or indices where elements have been deleted. $insert: Used in lists to specify new elements inserted at specific indices. $update: Used to indicate that the value of an existing key has changed. $replace: Used to completely replace the value at a given location. These symbols are used within the diff structures returned by methods of JsonDiffer classes to represent different types of changes between two JSON structures. For example: - In a dictionary, $add might be used to show new keys added, $delete to show keys that were removed, and $update for keys whose values have changed. - In a list, $insert could indicate new items added at specific positions, and $delete could show items removed from specific positions. - The $replace symbol is generally used when an entire section of the JSON (be it a list, dict, or value) is replaced with another. These symbols help in succinctly representing changes in a structured way, making it easier to apply or revert changes programmatically. """ def __init__(self, label): self._label = label @property def label(self): return self._label def __repr__(self): return self.label def __str__(self): return "$" + self.label def __eq__(self, other): return self.label == other.label def __hash__(self) -> int: return hash(self.label) missing = Symbol('missing') identical = Symbol('identical') delete = Symbol('delete') insert = Symbol('insert') update = Symbol('update') add = Symbol('add') discard = Symbol('discard') replace = Symbol('replace') left = Symbol('left') right = Symbol('right') _all_symbols_ = [ missing, identical, delete, insert, update, add, discard, replace, left, right ] __all__ = [ 'missing', 'identical', 'delete', 'insert', 'update', 'add', 'discard', 'replace', 'left', 'right', '_all_symbols_' ] jsondiff-2.1.2/pyproject.toml000066400000000000000000000021241464503250600162360ustar00rootroot00000000000000[build-system] requires = ["setuptools>=64.0.0", "setuptools_scm>=8", "wheel"] build-backend = "setuptools.build_meta" [project] name = "jsondiff" description = "Diff JSON and JSON-like structures in Python" dynamic = ["version", "dependencies", "optional-dependencies"] readme = "README.md" license= {file = "LICENSE" } requires-python = ">=3.8" authors = [ { name = "Zoomer Analytics LLC", email = "eric.reynolds@zoomeranalytics.com"} ] keywords = ['json', 'diff', 'diffing', 'difference', 'patch', 'delta', 'dict', 'LCS'] classifiers = [ 'License :: OSI Approved :: MIT License', 'Programming Language :: Python :: 3', ] [project.urls] "Homepage" = "https://github.com/xlwings/jsondiff" "Bug Tracker" = "https://github.com/xlwings/jsondiff/issues" [project.scripts] jdiff = "jsondiff.cli:main" [tool.setuptools.packages.find] include = ["jsondiff*"] exclude = ["tests*"] [tool.setuptools.dynamic] dependencies = {file=["requirements.txt"]} [tool.setuptools_scm] version_file = "jsondiff/_version.py" [tool.setuptools.dynamic.optional-dependencies] dev = {file=["requirements-dev.txt"]} jsondiff-2.1.2/requirements-dev.txt000066400000000000000000000000461464503250600173630ustar00rootroot00000000000000hypothesis pytest setuptools-scm buildjsondiff-2.1.2/requirements.txt000066400000000000000000000000071464503250600166040ustar00rootroot00000000000000pyyaml jsondiff-2.1.2/setup.py000066400000000000000000000001141464503250600150310ustar00rootroot00000000000000# Maintained for legacy compatibility from setuptools import setup setup() jsondiff-2.1.2/tests/000077500000000000000000000000001464503250600144655ustar00rootroot00000000000000jsondiff-2.1.2/tests/__init__.py000066400000000000000000000000001464503250600165640ustar00rootroot00000000000000jsondiff-2.1.2/tests/data/000077500000000000000000000000001464503250600153765ustar00rootroot00000000000000jsondiff-2.1.2/tests/data/test_01.json000066400000000000000000000000611464503250600175450ustar00rootroot00000000000000{ "hello": "world", "data": [ 1,2,3 ] }jsondiff-2.1.2/tests/data/test_01.yaml000066400000000000000000000000441464503250600175370ustar00rootroot00000000000000hello: world data: - 1 - 2 - 3jsondiff-2.1.2/tests/generate_readme.py000066400000000000000000000017351464503250600201540ustar00rootroot00000000000000# this is used for generating the repo front page def do(cmd, comment=None): if comment: print("# " + comment) print(">>> " + cmd) c = compile(cmd, filename="", mode='single') eval(c, globals()) print() do('from jsondiff import diff') do("diff({'a': 1, 'b': 2}, {'b': 3, 'c': 4})") do("diff(['a', 'b', 'c'], ['a', 'b', 'c', 'd'])") do("diff(['a', 'b', 'c'], ['a', 'c'])") do("diff({'a': [0, {'b': 4}, 1]}, {'a': [0, {'b': 5}, 1]})", "Typical diff looks like what you'd expect...") do("diff({'a': [0, {'b': 4}, 1]}, {'a': [0, {'c': 5}, 1]})", "...but similarity is taken into account") do("diff({'a': 1, 'b': 2}, {'b': 3, 'c': 4}, syntax='explicit')", "Support for various diff syntaxes") do("diff({'a': 1, 'b': 2}, {'b': 3, 'c': 4}, syntax='symmetric')") do("diff({'a', 'b', 'c'}, {'a', 'c', 'd'})", "Special handling of sets") do("print(diff('[\"a\", \"b\", \"c\"]', '[\"a\", \"c\", \"d\"]', load=True, dump=True))", "Load and dump JSON") jsondiff-2.1.2/tests/test_jsondiff.py000066400000000000000000000240671464503250600177110ustar00rootroot00000000000000import io import logging import os.path import sys import unittest import pytest import jsondiff from jsondiff import diff, replace, add, discard, insert, delete, JsonDiffer from .utils import generate_random_json, perturbate_json from hypothesis import given, settings, strategies logging.basicConfig( level=logging.INFO, format=( '%(asctime)s %(pathname)s[line:%(lineno)d] %(levelname)s %(message)s' ), ) logging.getLogger("jsondiff").setLevel(logging.DEBUG) def generate_scenario(rng): a = generate_random_json(rng, sets=True) b = perturbate_json(a, rng, sets=True) return a, b def generate_scenario_no_sets(rng): a = generate_random_json(rng, sets=False) b = perturbate_json(a, rng, sets=False) return a, b class JsonDiffTests(unittest.TestCase): def test_a(self): self.assertEqual({}, diff(1, 1)) self.assertEqual({}, diff(True, True)) self.assertEqual({}, diff('abc', 'abc')) self.assertEqual({}, diff([1, 2], [1, 2])) self.assertEqual({}, diff((1, 2), (1, 2))) self.assertEqual({}, diff({1, 2}, {1, 2})) self.assertEqual({}, diff({'a': 1, 'b': 2}, {'a': 1, 'b': 2})) self.assertEqual({}, diff([], [])) self.assertEqual({}, diff(None, None)) self.assertEqual({}, diff({}, {})) self.assertEqual({}, diff(set(), set())) self.assertEqual(2, diff(1, 2)) self.assertEqual(False, diff(True, False)) self.assertEqual('def', diff('abc', 'def')) self.assertEqual([3, 4], diff([1, 2], [3, 4])) self.assertEqual((3, 4), diff((1, 2), (3, 4))) self.assertEqual({3, 4}, diff({1, 2}, {3, 4})) self.assertEqual({replace: {'c': 3, 'd': 4}}, diff({'a': 1, 'b': 2}, {'c': 3, 'd': 4})) self.assertEqual({replace: {'c': 3, 'd': 4}}, diff([1, 2], {'c': 3, 'd': 4})) self.assertEqual(123, diff({'a': 1, 'b': 2}, 123)) self.assertEqual({delete: ['b']}, diff({'a': 1, 'b': 2}, {'a': 1})) self.assertEqual({'b': 3}, diff({'a': 1, 'b': 2}, {'a': 1, 'b': 3})) self.assertEqual({'c': 3}, diff({'a': 1, 'b': 2}, {'a': 1, 'b': 2, 'c': 3})) self.assertEqual({delete: ['b'], 'c': 3}, diff({'a': 1, 'b': 2}, {'a': 1, 'c': 3})) self.assertEqual({add: {3}}, diff({1, 2}, {1, 2, 3})) self.assertEqual({add: {3}, discard: {4}}, diff({1, 2, 4}, {1, 2, 3})) self.assertEqual({discard: {4}}, diff({1, 2, 4}, {1, 2})) self.assertEqual({insert: [(1, 'b')]}, diff(['a', 'c'], ['a', 'b', 'c'])) self.assertEqual({insert: [(1, 'b')], delete: [3, 0]}, diff(['x', 'a', 'c', 'x'], ['a', 'b', 'c'])) self.assertEqual( {insert: [(2, 'b')], delete: [4, 0], 1: {'v': 20}}, diff(['x', 'a', {'v': 11}, 'c', 'x'], ['a', {'v': 20}, 'b', 'c']) ) self.assertEqual( {insert: [(2, 'b')], delete: [4, 0], 1: {'v': 20}}, diff(['x', 'a', {'u': 10, 'v': 11}, 'c', 'x'], ['a', {'u': 10, 'v': 20}, 'b', 'c']) ) def test_marshal(self): differ = JsonDiffer() d = { delete: 3, '$delete': 4, insert: 4, '$$something': 1 } dm = differ.marshal(d) self.assertEqual(d, differ.unmarshal(dm)) @given(strategies.randoms().map(generate_scenario_no_sets)) @settings(max_examples=1000) def test_dump(self, scenario): a, b = scenario diff(a, b, syntax='compact', dump=True) diff(a, b, syntax='explicit', dump=True) diff(a, b, syntax='symmetric', dump=True) @given(strategies.randoms().map(generate_scenario)) @settings(max_examples=1000) def test_compact_syntax(self, scenario): a, b = scenario differ = JsonDiffer(syntax='compact') d = differ.diff(a, b) self.assertEqual(b, differ.patch(a, d)) dm = differ.marshal(d) self.assertEqual(d, differ.unmarshal(dm)) @given(strategies.randoms().map(generate_scenario)) @settings(max_examples=1000) def test_explicit_syntax(self, scenario): a, b = scenario differ = JsonDiffer(syntax='explicit') d = differ.diff(a, b) # self.assertEqual(b, differ.patch(a, d)) dm = differ.marshal(d) self.assertEqual(d, differ.unmarshal(dm)) @given(strategies.randoms().map(generate_scenario)) @settings(max_examples=1000) def test_symmetric_syntax(self, scenario): a, b = scenario differ = JsonDiffer(syntax='symmetric') d = differ.diff(a, b) self.assertEqual(b, differ.patch(a, d)) self.assertEqual(a, differ.unpatch(b, d)) dm = differ.marshal(d) self.assertEqual(d, differ.unmarshal(dm)) def test_long_arrays(self): size = 100 a = [{'a': i, 'b': 2 * i} for i in range(1, size)] b = [{'a': i, 'b': 3 * i} for i in range(1, size)] r = sys.getrecursionlimit() sys.setrecursionlimit(size - 1) try: diff(a, b) except RecursionError: self.fail('cannot diff long arrays') finally: sys.setrecursionlimit(r) @pytest.mark.parametrize( ("a", "b", "syntax", "expected"), [ pytest.param([], [{"a": True}], "explicit", {insert: [(0, {"a": True})]}, id="issue59_"), pytest.param([{"a": True}], [], "explicit", {delete: [0]}, id="issue59_"), pytest.param([], [{"a": True}], "compact", [{"a": True}], id="issue59_"), pytest.param([{"a": True}], [], "compact", [], id="issue59_"), pytest.param([], [{"a": True}], "symmetric", {insert: [(0, {"a": True})]}, id="issue59_"), pytest.param([{"a": True}], [], "symmetric", {delete: [(0, {"a": True})]}, id="issue59_"), pytest.param({1: 2}, {5: 3}, "symmetric", {delete: {1: 2}, insert: {5: 3}}, id="issue36_"), pytest.param({1: 2}, {5: 3}, "compact", {replace: {5: 3}}, id="issue36_"), ], ) class TestSpecificIssue: def test_issue(self, a, b, syntax, expected): actual = diff(a, b, syntax=syntax) assert actual == expected class TestRightOnly(unittest.TestCase): def test_right_only_syntax(self): a = {"poplist": [1, 2, 3]} b = {} self.assertEqual( diff(a, b, syntax="rightonly", marshal=True), { "$delete": ["poplist"], } ) a = {1: 2, 2: 3, 'list': [1, 2, 3], 'samelist': [1, 2, 3], "poplist": [1, 2, 3]} b = {1: 2, 2: 4, 'list': [1, 3], 'samelist': [1, 2, 3]} self.assertEqual( diff(a, b, syntax="rightonly", marshal=True), { 2: 4, "list": [1, 3], "$delete": ["poplist"], } ) self.assertEqual( diff(a, b, syntax="rightonly"), { 2: 4, "list": [1, 3], delete: ["poplist"], } ) c = [1, 2, 3] d = [4, 5, 6] self.assertEqual( diff(c, d, syntax="rightonly", marshal=True), [4, 5, 6], ) class TestLoaders(unittest.TestCase): here = os.path.dirname(__file__) data_dir = os.path.join(here, "data") def test_json_string_loader(self): json_str = '{"hello": "world", "data": [1,2,3]}' expected = {"hello": "world", "data": [1, 2, 3]} loader = jsondiff.JsonLoader() actual = loader(json_str) self.assertEqual(expected, actual) def test_json_file_loader(self): json_file = os.path.join(TestLoaders.data_dir, "test_01.json") expected = {"hello": "world", "data": [1, 2, 3]} loader = jsondiff.JsonLoader() with open(json_file) as f: actual = loader(f) self.assertEqual(expected, actual) def test_yaml_string_loader(self): json_str = """--- hello: world data: - 1 - 2 - 3 """ expected = {"hello": "world", "data": [1, 2, 3]} loader = jsondiff.YamlLoader() actual = loader(json_str) self.assertEqual(expected, actual) def test_yaml_file_loader(self): yaml_file = os.path.join(TestLoaders.data_dir, "test_01.yaml") expected = {"hello": "world", "data": [1, 2, 3]} loader = jsondiff.YamlLoader() with open(yaml_file) as f: actual = loader(f) self.assertEqual(expected, actual) class TestDumpers(unittest.TestCase): def test_json_dump_string(self): data = {"hello": "world", "data": [1, 2, 3]} expected = '{"hello": "world", "data": [1, 2, 3]}' dumper = jsondiff.JsonDumper() actual = dumper(data) self.assertEqual(expected, actual) def test_json_dump_string_indented(self): data = {"hello": "world", "data": [1, 2, 3]} expected = """{ "hello": "world", "data": [ 1, 2, 3 ] }""" dumper = jsondiff.JsonDumper(indent=4) actual = dumper(data) self.assertEqual(expected, actual) def test_json_dump_string_fp(self): data = {"hello": "world", "data": [1, 2, 3]} expected = """{ "hello": "world", "data": [ 1, 2, 3 ] }""" dumper = jsondiff.JsonDumper(indent=4) buffer = io.StringIO() dumper(data, buffer) self.assertEqual(expected, buffer.getvalue()) def test_yaml_dump_string(self): data = {"hello": "world", "data": [1, 2, 3]} expected = """data: - 1 - 2 - 3 hello: world """ # Sort keys just to make things deterministic dumper = jsondiff.YamlDumper(sort_keys=True) actual = dumper(data) self.assertEqual(expected, actual) def test_yaml_dump_string_fp(self): data = {"hello": "world", "data": [1, 2, 3]} expected = """data: - 1 - 2 - 3 hello: world """ # Sort keys just to make things deterministic dumper = jsondiff.YamlDumper(sort_keys=True) buffer = io.StringIO() dumper(data, buffer) self.assertEqual(expected, buffer.getvalue()) jsondiff-2.1.2/tests/utils.py000066400000000000000000000042321464503250600162000ustar00rootroot00000000000000def generate_random_string(rng): return ''.join(rng.choice('abcdefg098765$&.()[]{}\n') for _ in range(rng.randint(0, 6))) def generate_random_json(rng, max_depth=4, sets=False, hashable=False): types = [None, bool, float, str] if max_depth > 1: types += [tuple] if not hashable: types += [list, dict] if sets: types += [set] ty = rng.choice(types) if ty is None: return None if ty is bool: return rng.randint(0, 1) == 1 if ty is float: return round(rng.random(), 2) if ty is str: return generate_random_string(rng) if ty is list or ty is tuple: return ty( generate_random_json(rng, max_depth-1, sets=sets, hashable=hashable) for _ in range(rng.randint(0, 7)) ) if ty is set: return { generate_random_json(rng, max_depth-1, hashable=True) for _ in range(rng.randint(0, 7)) } return { generate_random_string(rng): generate_random_json(rng, max_depth-1, sets=sets, hashable=hashable) for _ in range(5) } def pertubate_string(s, rng): if rng.random() < 0.6: return s else: return generate_random_string(rng) def perturbate_json(obj, rng, max_depth=4, sets=False, hashable=False): if rng.random() < 0.8: if type(obj) is dict: return { pertubate_string(k, rng): perturbate_json(v, rng, max_depth-1, sets=sets, hashable=hashable) for k, v in obj.items() } if type(obj) is set: return { perturbate_json(v, rng, max_depth-1, sets=sets, hashable=True) for v in obj if rng.random() < 0.9 } if isinstance(obj, (tuple, list)): return type(obj)( perturbate_json(v, rng, max_depth-1, sets=sets, hashable=hashable) for v in obj if rng.random() < 0.9 ) if rng.random() > max_depth / 5.0: return type(obj)(obj) if obj is not None else None return generate_random_json(rng, max_depth, sets=sets, hashable=hashable)