JSON Manipulation and Comparison

Overview

The JSON Manipulation and Comparison module provides a set of APIs and utilities designed to modify JSON data structures in-memory, perform deep equality checks for validation purposes, and manage memory safely during these operations. This module builds upon the foundational JSON parsing and representation layer by enabling dynamic updates to JSON arrays and objects and ensuring that JSON values can be reliably compared for structural equivalence.

Its core purpose is to allow client code to manipulate parsed JSON data flexibly and to verify JSON data integrity through deep comparison, all while handling memory allocation and deallocation responsibly to avoid leaks or corruption.

Core Concepts and Functionality

Modification of JSON Data Structures

JSON data in memory is represented as a tree of json_value objects, each with a type indicating whether it is a null, boolean, number, string, array, or object. This module includes functions that enable:

The module employs dynamic arrays internally to store elements of JSON arrays and key-value pairs for JSON objects, with growth strategies that double the capacity when limits are reached, ensuring efficient memory use and amortized constant-time insertions.

Deep Equality Checks

To validate whether two json_value trees represent equivalent JSON data, the module provides a deep equality function, similar to json_equal(). This function performs a recursive comparison that:

This thorough comparison ensures that two JSON structures are equal in both shape and content, which is essential for testing, caching, synchronization, and data validation scenarios.

Memory Management

The module includes careful memory management routines that:

This approach guarantees safe and predictable memory behavior during JSON manipulations.

Interaction with Other Components

The module depends on the JSON parsing and representation functionality (JSON Parsing and Representation) for creating the initial in-memory json_value trees. Once parsed, the manipulation functions operate on these trees to add, modify, or remove elements.

Similarly, serialization and printing functions from the JSON Serialization and Testing topic consume the modified JSON structures produced by this module to generate formatted JSON output.

The deep equality checks integrate closely with testing workflows to assert correctness of parsing, manipulation, or serialization by comparing expected and actual JSON trees.

Key Workflows and API Usage

Modifying JSON Objects and Arrays

Comparing JSON Values

To check if two JSON json_value trees are structurally equal:

bool are_equal = json_equal(value1, value2);

This performs recursive comparison based on type and content, including nested arrays and objects.

Memory Handling

When JSON values are no longer needed:

json_free(value);

This frees the entire JSON subtree rooted at value, including all nested arrays and objects.

Internal Design Patterns and Concepts

Visualization of JSON Manipulation and Comparison Workflow

flowchart TD
JSONText[JSON Text Input]
JSONParse["json_parse()"]
JSONTree[In-memory json_value Tree]
ModifyOps{Modify JSON?}
ArrayPush["json_array_push()"]
ObjectSet["json_object_set_take_key()"]
CompareCheck["json_equal()"]
Serialize["json_stringify()"]
OutputText[JSON Text Output]
FreeMem["json_free()"]
JSONText --> JSONParse --> JSONTree
JSONTree --> ModifyOps
ModifyOps -->|Yes| ArrayPush
ModifyOps -->|Yes| ObjectSet
ModifyOps -->|No| CompareCheck
ArrayPush --> JSONTree
ObjectSet --> JSONTree
JSONTree --> CompareCheck
CompareCheck --> Serialize
Serialize --> OutputText
JSONTree --> FreeMem

This diagram illustrates the flow from JSON text input through parsing, optional modification using array push or object set operations, deep equality comparison, serialization back to text, and finally memory cleanup.

References