How json-iterator/java works
Project Type and Purpose
• This is an open-source Java library (“json-iterator/java”) for high-performance JSON parsing and serialization.
• It provides:
• A runtime JSON iterator (JsonIterator) and streaming API (JsonStream).
• Multiple code generation strategies (static, dynamic, SPI-based).
• Extension modules for compatibility (Gson, Jackson), extras (Base64, datetime), fuzzy parsing, “any” type support, etc.
• It includes two sample applications (demo and android-demo) to illustrate usage and codegen integration.
File Structure Analysis
Top-level
• pom.xml, Travis and CI config, LICENSE, CHANGELOG, README.
• android-demo/ (Android Studio sample).
• demo/ (Maven console sample).
• src/ (core library).
Core src/main/java/com/jsoniter
• Codegen packages: static_codegen, jsoniter_codegen, output.codegen…
• Runtime iterator packages: JsonIterator, IterImpl*, MapKeyDecoders, JsonIteratorPool.
• SPI: Config, Decoder, Encoder, Extension interfaces.
• any/: dynamic “Any” type support with lazy, wrapper variants.
• extra/: plug-in modules for compatibility and naming strategies.
• fuzzy/: tolerant decoder implementations.
• output/: streaming encoder, JsonStream, JsonStreamPool.
Tests
• Extensive unit tests under src/test for all modules and sample data.
• Core Runtime: JsonIterator, IterImpl*, MapKeyDecoders, JsonIteratorPool.
• Code Generation:
– StaticCodegen (compile-time generator).
– DynamicCodegen (runtime bytecode generator).
– jsoniter_codegen (annotation-driven source generator).
• Streaming API: JsonStream, JsonStreamPool, encoding/decoding impls.
• SPI & Extensions:
– SPI interfaces (Decoder, Encoder, Extension).
– extra/ modules (GsonCompatibilityMode, JacksonCompatibilityMode, etc.).
– fuzzy/ parsers.
– any/ “Any” API.
• Samples & Build:
– demo/ (Maven-driven CLI demo).
– android-demo/ (Android sample using Gradle).
• External Dependencies:
– JDK, ASM or equivalent bytecode libs (if used), annotation processors.
• Application code → JsonIterator (entry point) → IterImpl* → core byte array/buffer.
• Application code → Codegen module → generates encoder/decoder classes → classloader.
• JsonStream ↔ JsonStreamPool (object reuse), writes to OutputStream.
• SPI interfaces ← Extension implementations (extra/, fuzzy/).
• Samples invoke either static_codegen or dynamic_codegen to pre-generate or on-the-fly generate serializers.
c. Architectural Patterns & Principles
• Factory/Provider pattern: CodegenAccess, ReflectionDecoderFactory for selecting decoding strategy.
• SPI: decouples core from extension modules.
• Pooling: JsonIteratorPool, JsonStreamPool for object reuse and performance.
• Modular design: clear separation between parsing (iterators), serialization (streams), codegen, and extension.
d. Key Technologies & Frameworks
• Java 8+ (annotations, reflection).
• Maven (core build) and Gradle (android-demo).
• Annotation processing for code generation.
• Streaming I/O APIs.
• ASM or similar for dynamic class generation (implicit).
Essential Elements to Capture
• Core entry points (JsonIterator, JsonStream).
• Codegen paths (static vs dynamic vs annotation-based).
• SPI interfaces and sample extension modules.
• Consumer apps demonstrating integration.
• Object pools for performance.
• External I/O (InputStream, OutputStream).