How json-iterator/go works
Main Components
• Client Interface Layer: This is the API exposed to users, where the configuration (e.g., ConfigCompatibleWithStandardLibrary) is defined. This acts as the entry-point for json.Marshal and json.Unmarshal calls.
• Core JSON Engine: Represented by files like jsoniter.go, adapter.go, and config.go, this is where the main logic of the library resides, handling JSON encoding and decoding.
• Iteration and Parsing Modules: Files like iter.go (and its variants such as iter_array.go, iter_object.go, iter_float.go, etc.) form a key module for traversing JSON structures efficiently.
• Stream Processing: With files like stream.go, stream_int.go, and stream_str.go, this module manages reading/writing JSON in a streamed fashion.
• Reflection-Based Conversion: Represented by numerous “reflect” files (reflect.go, reflect_array.go, reflect_struct_encoder.go, etc.), this module supports dynamic JSON marshaling and unmarshaling when reflection is needed.
• Extension and Adapter Modules: Extra components such as those in the "extra" directory (e.g., naming_strategy.go, binary_as_string_codec.go) extend the core functionality and fine-tune performance or compatibility.
• Testing & Benchmarking: Multiple directories (any_tests, api_tests, extension_tests, misc_tests, skip_tests, type_tests, value_tests) along with “benchmarks” clearly indicate rigorous testing and performance evaluation. Although not part of the deployment architecture, they reinforce the design’s emphasis on correctness and performance validation.
• Build & Configuration: A set of configuration files (Gopkg.toml, build.sh, .travis.yml, etc.) and dependency management files (Gopkg.lock, go.mod, go.sum) represent the infrastructure supporting continuous integration and dependency control.
Relationships and Interactions
• The Client Interface Layer calls into the Core JSON Engine which then dispatches work to underlying modules.
• The Core JSON Engine routes JSON data to either the Iteration/Parser modules or the Reflection modules based on data types and configuration.
• The Stream module operates as a specialized channel for large or continuously flowing JSON streams.
• Extension components act as plug-ins or adapters that can override or extend default behaviors.
• Testing components verify the behavior of each core module and the integration between them while benchmarks provide performance metrics.
Important Architectural Patterns/Principles
• Separation of Concerns: Each component (parsing, streaming, reflection, extensions) is isolated with a clear responsibility.
• Modular Design: The codebase is structured into multiple small modules/files (each handling a specific facet of JSON encoding/decoding) that inter-operate.
• Performance Optimization: The presence of multiple “iter” modules, “stream” modules, and benchmarking tests indicates a design heavily optimized for speed and low memory allocation.