Class ClientSerializer
Replaces the previous use of String.valueOf() which is semantically lossy:
distinct objects with identical toString() would collide (e.g., an Integer and a
Long with the same numeric value, or locale-dependent decimal formatting).
This serializer produces deterministic string representations for supported types by encoding a single-character type tag followed by the value. The type tag ensures that values of different types never collide, even if their string representations are identical.
Type Tags
S: String I: Integer L: Long D: Double F: Float H: Short Y: Byte C: Character B: Boolean G: BigDecimal J: BigInteger U: UUID A: byte[] N: null O: legacy unsupported object payload
Thread Safety
This class is stateless and thread-safe. All methods are static.- Since:
- 1.1
-
Method Summary
Modifier and TypeMethodDescriptionstatic @Nullable Objectdeserialize(@Nullable String tagged) Deserialize a type-tagged string back to its original object.static @Nullable ObjectdeserializeFromBytes(byte @Nullable [] bytes) Deserialize a UTF-8 byte array back to its original object.static @Nullable ObjectdeserializeWithLegacyFallback(@Nullable String value) Deserialize a value through the current wire decoder.static booleanisLegacyFormat(String value) Check if a serialized string uses the legacyString.valueOf()format (no type tag prefix).static StringSerialize an object to a type-tagged string representation.static byte[]serializeToBytes(@Nullable Object obj) Serialize an object to a UTF-8 byte array suitable for wire transmission.
-
Method Details
-
serialize
Serialize an object to a type-tagged string representation.The result is deterministic, locale-independent, and round-trippable via
deserialize(String)for supported types. Different types with the same string representation will produce different serialized forms (e.g., Integer(42) vs Long(42L)). Unsupported types are rejected instead of being serialized lossy.- Parameters:
obj- the object to serialize (may be null)- Returns:
- a type-tagged string, or the null sentinel "N" if obj is null
- Throws:
SerializationException- ifobjhas an unsupported type
-
deserialize
Deserialize a type-tagged string back to its original object.This is the inverse of
serialize(Object)for supported tagged values. The type tag determines the target type, ensuring type fidelity across serialization boundaries.- Parameters:
tagged- the type-tagged string produced byserialize(Object)- Returns:
- the deserialized object, or null if the input represents null
- Throws:
IllegalArgumentException- if the tagged string has a malformed supported payload or an unknown type tagSerializationException- if the tagged string uses the unsupported legacy object payload tag
-
serializeToBytes
Serialize an object to a UTF-8 byte array suitable for wire transmission.Convenience method that combines
serialize(Object)with UTF-8 encoding, matching the existing wire protocol format used byLoomClient.- Parameters:
obj- the object to serialize (may be null)- Returns:
- UTF-8 encoded bytes of the type-tagged string
-
deserializeFromBytes
Deserialize a UTF-8 byte array back to its original object.Convenience method that combines UTF-8 decoding with
deserialize(String).- Parameters:
bytes- the UTF-8 encoded type-tagged string (may be null or empty)- Returns:
- the deserialized object, or null if input is null/empty
-
isLegacyFormat
Check if a serialized string uses the legacyString.valueOf()format (no type tag prefix).Used for backward compatibility: if existing data was stored with the old format, it can be detected and handled gracefully.
- Parameters:
value- the stored string to check (must not be null)- Returns:
- true if the value appears to be in legacy (untagged) format
-
deserializeWithLegacyFallback
Deserialize a value through the current wire decoder.The method name is retained for existing callers, but LoomCache is pre-production and no longer accepts unframed legacy payloads.
- Parameters:
value- the stored value to deserialize (may be null)- Returns:
- the deserialized object, or null
-