Class BlockSerializer
Block objects.
Used internally by all persistent storage implementations (LevelDBStorage,
RocksDBStorage, FileSystemStorage) to convert blocks to / from their
JSON wire format. Centralizing serialization here ensures that all implementations
use an identical format, making cross-implementation round-trips lossless.
Jackson configuration
JavaTimeModule— serializesInstantas ISO-8601 strings, not numeric timestamp arrays, whenWRITE_DATES_AS_TIMESTAMPSis disabled.DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIESis disabled so that convenience getters onBlock(e.g.getMerkleRoot(), which delegates toheader.merkleRoot()) do not cause deserialization failures. Jackson serializes every public getter as a JSON field; those derived fields have no corresponding constructor parameter inBlock's@JsonCreatorand would otherwise trigger "Unrecognized field" errors. Disabling this feature is safe becauseverifyHash(Block)provides an independent integrity check after every deserialization (NFR-SEC-03).- Getter-based serialization is suppressed (
PropertyAccessor.GETTERandIS_GETTERset toNONE) so only@JsonProperty-annotated constructor parameters are written to JSON. This prevents derived convenience getters such asgetMerkleRoot()from producing redundant top-level fields, keeping stored JSON canonical and eliminating the round-trip mismatch at the source rather than merely tolerating it on the read path. - Polymorphic
Transactionsubtypes are handled by Jackson's@JsonTypeInfoannotation on the abstractTransactionbase class (design §7.5, FR-SER-01).
- Since:
- 1.0.0
-
Method Summary
Modifier and TypeMethodDescriptionstatic BlockfromBytes(byte[] bytes) Deserializes aBlockfrom a raw UTF-8 JSON byte array.static BlockDeserializes aBlockfrom a UTF-8 JSON string.static byte[]Serializes aBlockto its canonical UTF-8 JSON byte representation.static StringSerializes aBlockto a UTF-8 JSON string.
-
Method Details
-
toBytes
Serializes aBlockto its canonical UTF-8 JSON byte representation.Compact JSON (no pretty-printing) minimizes storage footprint, particularly in LevelDB and RocksDB where values are raw byte arrays.
- Parameters:
block- the block to serialize (non-null)- Returns:
- UTF-8 JSON byte array (non-null)
- Throws:
BlockValidationException- if Jackson serialization failsNullPointerException- ifblockis null
-
toJson
Serializes aBlockto a UTF-8 JSON string.Convenience wrapper around
toBytes(Block)for storage backends that work withStringvalues (e.g.,FileSystemStorage).- Parameters:
block- the block to serialize (non-null)- Returns:
- JSON string (non-null)
- Throws:
BlockValidationException- if Jackson serialization failsNullPointerException- ifblockis null
-
fromBytes
Deserializes aBlockfrom a raw UTF-8 JSON byte array.After deserialization, the block's hash is recomputed and compared against the stored
hashfield to detect storage corruption (NFR-SEC-03). A mismatch causes aBlockValidationException.- Parameters:
bytes- raw UTF-8 JSON bytes (non-null, non-empty)- Returns:
- the deserialized and hash-verified
Block - Throws:
BlockValidationException- if deserialization fails or hash is corruptNullPointerException- ifbytesis null
-
fromJson
Deserializes aBlockfrom a UTF-8 JSON string.Convenience wrapper around
fromBytes(byte[])for storage backends that readStringvalues (e.g.,FileSystemStorage).- Parameters:
json- UTF-8 JSON string (non-null, non-blank)- Returns:
- the deserialized and hash-verified
Block - Throws:
BlockValidationException- if deserialization fails or hash is corruptNullPointerException- ifjsonis null
-