Package com.flipkart.zjsonpatch.mapping
This package provides an abstraction layer that enables zjsonpatch to work seamlessly with both Jackson 2.x and Jackson 3.x libraries. The mapping layer uses a wrapper pattern to provide a unified interface for JSON operations while maintaining compatibility with different Jackson versions that may be present on the classpath.
Architecture Overview
The mapping layer consists of several key components:
- Wrapper Interfaces - Define common operations for JSON nodes, arrays, and objects
- Version Bridge - Handles conversion between native Jackson types and wrappers
- Version Selector - Automatically detects and selects appropriate Jackson version
- Factory Provider - Provides version-appropriate JSON node factories
Core Interfaces
JsonNodeWrapper- Base wrapper for all JSON nodesArrayNodeWrapper- Wrapper for JSON array operationsObjectNodeWrapper- Wrapper for JSON object operationsJsonNodeFactoryWrapper- Wrapper for JSON node factory operations
Version Management
JacksonVersionBridge- Converts between wrapper and native types and auto-detects Jackson versions
Implementation Packages
com.flipkart.zjsonpatch.mapping.jackson2- Jackson 2.x specific implementationscom.flipkart.zjsonpatch.mapping.jackson3- Jackson 3.x specific implementations
Version Detection
The mapping layer automatically detects the Jackson version from the actual node types by checking if they are instances of known Jackson JsonNode types:
- Jackson 2.x nodes: instances of
com.fasterxml.jackson.databind.JsonNode - Jackson 3.x nodes: instances of
tools.jackson.databind.JsonNode
This instanceof-based approach properly handles inheritance, including user-defined subclasses of Jackson nodes. It ensures that each API (JsonDiff/Jackson3JsonDiff and JsonPatch/Jackson3JsonPatch) works correctly with its intended Jackson version, regardless of what versions are available on the classpath.
Usage Example
// Create JSON nodes using either Jackson 2.x or 3.x ObjectMapper
JsonNode source = mapper.readTree("{\"foo\":\"bar\"}");
JsonNode target = mapper.readTree("{\"foo\":\"baz\"}");
// For Jackson 2.x, use JsonDiff and JsonPatch:
JsonNode patch = JsonDiff.asJson(source, target);
JsonNode result = JsonPatch.apply(patch, source);
// For Jackson 3.x, use the Jackson3-prefixed classes:
JsonNode patch = Jackson3JsonDiff.asJson(source, target);
JsonNode result = Jackson3JsonPatch.apply(patch, source);
Performance Considerations
The wrapper layer introduces minimal overhead as it primarily delegates to the underlying Jackson implementations. Version detection is cached to avoid repeated Class.forName() lookups. The detection gracefully handles missing Jackson versions via exception handling in the isInstanceOf() method.
- Since:
- 0.6.0
- Author:
- Mariusz Sondecki
-
ClassDescriptionWrapper interface for array node operations.Decorator for Iterator to provide JsonNodeWrapper compatibility.Bridge for wrapping and unwrapping Jackson JsonNodes across different versions.Exception thrown when Jackson version-related operations fail.Decorator that wraps a ListIterator to provide JsonNodeWrapper instances instead of raw JSON nodes, enabling version-agnostic iteration.