Class ChainExporter
Provides three static operations that satisfy FR-SER-02 and FR-SER-03:
toJson(Blockchain)— serializes all blocks to a JSON array string using the sharedBlockSerializer.MAPPERinstance, preserving full Jackson polymorphism forTransactionsubtypes (@JsonTypeInfo).fromJson(String, BlockchainStorage)— deserializes a JSON array produced bytoJsonand persists each block to the supplied storage backend after hash-integrity verification (NFR-SEC-03). Intended for administrative chain import and fast node bootstrap without network sync.toCsv(Blockchain)— flattens the chain to one RFC-4180 CSV row per transaction across all blocks, suitable for audit exports and analytics pipelines.
This class is placed in blockchain-storage rather than blockchain-core
because it depends on BlockSerializer.MAPPER — a Jackson ObjectMapper that
is already a mandatory (non-optional) dependency of the storage module. Placing it here
keeps blockchain-core free of mandatory Jackson transitive dependencies
(design.md §7.1).
Thread safety
All methods are stateless and thread-safe. The underlying BlockSerializer.MAPPER
is thread-safe once configured.
Usage
// Export
String json = ChainExporter.toJson(node.getChain());
String csv = ChainExporter.toCsv(node.getChain());
// Import into a fresh storage backend
InMemoryStorage fresh = new InMemoryStorage();
ChainExporter.fromJson(json, fresh);
- Since:
- 1.0.0
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic voidfromJson(String json, BlockchainStorage storage) Deserializes a JSON array produced bytoJson(com.privatechain.core.builder.Blockchain)and persists each block to the supplied storage backend (FR-SER-02).static StringtoCsv(Blockchain chain) Exports the blockchain as a flat RFC-4180 CSV string for audit and analytics use (FR-SER-03).static StringtoJson(Blockchain chain) Serializes the entire blockchain to a compact JSON array string (FR-SER-02).
-
Method Details
-
toJson
Serializes the entire blockchain to a compact JSON array string (FR-SER-02).Each element of the array is the canonical JSON representation of one
Block, produced using the sameBlockSerializer.MAPPERshared by all persistent storage implementations. The resulting string can be restored viafromJson(String, BlockchainStorage).Transaction polymorphism is fully preserved: each transaction object carries its concrete class name in the
_typefield (Jackson@JsonTypeInfo), so all subtype-specific fields survive the round-trip without registration (AC-09).- Parameters:
chain- the blockchain to export (non-null)- Returns:
- compact JSON array string; never null or blank
- Throws:
NullPointerException- ifchainis nullBlockchainException- if Jackson serialization fails
-
fromJson
Deserializes a JSON array produced bytoJson(com.privatechain.core.builder.Blockchain)and persists each block to the supplied storage backend (FR-SER-02).Each deserialized block is hash-verified via
Block.isHashValid()before being saved to the storage. A hash mismatch causes aBlockValidationExceptionand aborts the import immediately, leaving already-saved blocks in the storage. Callers that require transactional semantics should pass an empty storage and callBlockchainStorage.deleteAll()on failure.Note: This method bypasses
ConsensusEnginevalidation intentionally. Chain import is an administrative / bootstrap operation and must only be used with trusted chain dumps. Normal block acceptance must go throughBlockchain.addBlock(), which enforces consensus rules.- Parameters:
json- a JSON array string produced bytoJson(com.privatechain.core.builder.Blockchain)(non-null, non-blank)storage- the backend to write the imported blocks into (non-null)- Throws:
NullPointerException- ifjsonorstorageis nullIllegalArgumentException- ifjsonis blankBlockValidationException- if any block fails hash-integrity verificationBlockchainException- if JSON parsing fails
-
toCsv
Exports the blockchain as a flat RFC-4180 CSV string for audit and analytics use (FR-SER-03).The output contains one row per transaction across all blocks. The first row is always the header. If the chain contains no transactions at all, only the header row is returned. String fields that contain a comma (
,) are automatically wrapped in double-quotes per RFC-4180 §2.6.Columns:
blockIndex— zero-based block position in the chainblockHash— full hex SHA-256 hash of the blocktxId— transaction UUIDsenderAddress— sender's blockchain addressreceiverAddress— receiver's blockchain addressamount— token amount (BigDecimal.toPlainString())timestamp— ISO-8601 UTC timestamp of the transactiontxType— simple class name of the concreteTransactionsubtype
- Parameters:
chain- the blockchain to export (non-null)- Returns:
- RFC-4180 CSV string with a header row; never null; never empty (always has header)
- Throws:
NullPointerException- ifchainis null
-