Class JsonEventSerializer
JSON-based event serializer implementation.
public sealed class JsonEventSerializer : IEventSerializer
- Inheritance
-
JsonEventSerializer
- Implements
- Inherited Members
- Extension Methods
Constructors
JsonEventSerializer(IEventTypeRegistry?, JsonSerializerOptions?, bool)
Initializes a new instance of the JsonEventSerializer class that consults a consumer-registered event-type registry before any assembly scan.
[RequiresUnreferencedCode("JSON serialization may reference types not preserved during trimming. Use AotJsonEventSerializer for an AOT-safe path.")]
[RequiresDynamicCode("JSON serialization may require dynamic code generation which is not compatible with AOT compilation.")]
public JsonEventSerializer(IEventTypeRegistry? registry, JsonSerializerOptions? options = null, bool allowAssemblyScan = false)
Parameters
registryIEventTypeRegistryOptional event-type registry (populated via
AddEventTypes<T>()). When provided, ResolveType(string) resolves registered types independently of the scan, so the secure default (allowAssemblyScanfalse) is usable for event sourcing while an unregistered/attacker-chosen type stays unresolvable without the opt-in scan.optionsJsonSerializerOptionsOptional JSON serializer options to use.
allowAssemblyScanboolSee the public constructor.
JsonEventSerializer(JsonSerializerOptions?, bool)
Initializes a new instance of the JsonEventSerializer class.
[RequiresUnreferencedCode("JSON serialization may reference types not preserved during trimming. Use AotJsonEventSerializer for an AOT-safe path.")]
[RequiresDynamicCode("JSON serialization may require dynamic code generation which is not compatible with AOT compilation.")]
public JsonEventSerializer(JsonSerializerOptions? options = null, bool allowAssemblyScan = false)
Parameters
optionsJsonSerializerOptionsOptional JSON serializer options to use.
allowAssemblyScanboolWhen true, an unregistered type name is resolved by scanning all loaded assemblies. This is an explicit, trusted-environment opt-in: the unbounded scan can resolve an attacker-chosen (gadget-chain) type, so it is false by default and an unknown type name is rejected with UnknownEventTypeException. The flag is symmetric with the reflection/trim opt-in this constructor already represents.
Remarks
This reflection-based serializer is the trim/AOT opt-in gate: constructing it surfaces the
IL2026/IL3050 warnings. Use AotJsonEventSerializer for an AOT-safe path. The
IEventSerializer interface itself is intentionally annotation-free (clean contract).
Methods
DeserializeEvent(byte[], Type)
Deserializes an event from bytes.
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "Event deserialization requires runtime type resolution for polymorphic event types - reflection is intentional; opt-in enforced at the constructor.")]
[UnconditionalSuppressMessage("AOT", "IL3050:RequiresDynamicCode", Justification = "Reflection-based deserialization is intentional; the AOT opt-in is enforced at the constructor.")]
public IDomainEvent DeserializeEvent(byte[] data, Type eventType)
Parameters
Returns
- IDomainEvent
The deserialized event.
GetTypeName(Type)
Gets the type name for serialization.
public string GetTypeName(Type type)
Parameters
typeTypeThe type to get the name for.
Returns
- string
Type name for storage.
ResolveType(string)
Resolves a type from its stored name.
[UnconditionalSuppressMessage("Trimming", "IL2057:Unrecognized value passed to the parameter of method. It's not possible to guarantee the availability of the target type.", Justification = "Type resolution from strings is required for event sourcing polymorphic event deserialization. Event types are preserved through event store infrastructure.")]
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "Assembly scanning for type resolution is intentional; the trim opt-in is enforced at the constructor.")]
public Type ResolveType(string typeName)
Parameters
typeNamestringThe stored type name.
Returns
- Type
The resolved type.
SerializeEvent(IDomainEvent)
Serializes an event to bytes.
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "Reflection-based serialization is intentional; the trim opt-in is enforced at the constructor.")]
[UnconditionalSuppressMessage("AOT", "IL3050:RequiresDynamicCode", Justification = "Reflection-based serialization is intentional; the AOT opt-in is enforced at the constructor.")]
public byte[] SerializeEvent(IDomainEvent domainEvent)
Parameters
domainEventIDomainEventThe event to serialize.
Returns
- byte[]
Serialized event data.