JsonComparator (json_comparator v1.0.3)

View Source

Provides functionality for comparing JSON structures with configurable comparison options.

Summary

Functions

Compares two JSON structures for equality with configurable options.

Functions

compare(json1, json2, opts \\ [])

Compares two JSON structures for equality with configurable options.

This function performs a deep comparison of two JSON structures, supporting various data types including maps, lists, DateTime objects, and structs. It provides configurable behavior for list comparison and DateTime precision.

By default, this function stops and returns on the first difference found. Use the deep_compare: true option to collect all differences.

Parameters

  • json1 - First JSON structure to compare
  • json2 - Second JSON structure to compare
  • opts - Optional keyword list of comparison options:
    • :strict_list_order - When true, lists must have identical order to be considered equal. Defaults to false
    • :truncate_datetime_microseconds - When true, DateTime comparisons ignore microseconds. Defaults to true
    • :error_message - Custom error message template to use when differences are found. The string %{path} will be replaced with the path where the difference was found. Defaults to "Submitted JSONs do not match: %{path}"
    • :deep_compare - When true, collects all differences instead of stopping at the first one. Defaults to false. When true, returns {:error, differences} where differences is a list of {path, details} tuples.

Returns

  • :ok - When the structures are equal according to the comparison rules
  • {:error, message} - When differences are found and deep_compare: false, where message is a string indicating the path where the first difference was encountered
  • {:error, differences} - When differences are found and deep_compare: true, where differences is a list of tuples containing path and details about each difference