Class CloudEventSerializer
- Namespace
- Excalibur.Dispatch.CloudEvents
- Assembly
- Excalibur.Dispatch.dll
Carries the CloudEvents serialization decision — reflection or source-generated — as a resolved dependency, so every transport adapter shares one decided configuration instead of each making the choice again.
public sealed class CloudEventSerializer
- Inheritance
-
CloudEventSerializer
- Inherited Members
- Extension Methods
Remarks
Why this type exists. Each transport adapter previously constructed a parameterless JsonEventFormatter of its own, which silently ignored the resolver the consumer supplied at registration and serialized payloads reflectively. That is why publishing a CloudEvent was not ahead-of-time publishable on any transport: not because the format needed rewriting, but because the formatter was being built without the metadata the consumer had already supplied.
Why it is a dependency rather than a branch. Deciding reflection-versus-source-generated inside each adapter would require every adapter to carry a trimming annotation, which propagates to the consumer's own build. Deciding it once, at registration, lets the decision travel as data: the registration that chose reflection carries the annotation, and nothing downstream needs one. The adapters receive something already decided, so there is no precondition left for them to establish.
Constructors
CloudEventSerializer(JsonSerializerOptions)
Initializes a new instance of the CloudEventSerializer class.
public CloudEventSerializer(JsonSerializerOptions options)
Parameters
optionsJsonSerializerOptionsThe decided serializer options, from CreateOptions(IJsonTypeInfoResolver) or CreateReflectionOptions().
Exceptions
- ArgumentNullException
Thrown when
optionsis null.
Properties
Formatter
Gets the CloudEvents formatter for structured-mode encoding and decoding, built over Options so the consumer's payload metadata is actually used.
public CloudEventFormatter Formatter { get; }
Property Value
Options
Gets the decided serializer options.
public JsonSerializerOptions Options { get; }
Property Value
Methods
CreateWithMetadata(JsonSerializerOptions)
Creates a serializer whose payload formatter uses metadata from the supplied options explicitly.
public static CloudEventSerializer CreateWithMetadata(JsonSerializerOptions options)
Parameters
optionsJsonSerializerOptionsThe same options used for the envelope and payload, with an explicit resolver.
Returns
- CloudEventSerializer
A serializer retaining the supplied options and their converters.
Remarks
This does not add a reflection fallback or certify arbitrary consumer converters for Native AOT. The resolver must cover the payloads published through it. The legacy constructor retains the SDK formatter.
Exceptions
- ArgumentNullException
optionsis null.- ArgumentException
The supplied options do not have an explicit resolver.
SerializePayload(object)
Serializes a CloudEvent payload to JSON text through the decided resolver.
public string SerializePayload(object data)
Parameters
dataobjectThe payload to serialize.
Returns
- string
The payload as JSON text.
Remarks
This replaces the parameterless JsonSerializer.Serialize(object) overload that each
adapter previously reached for as the fallback arm of its own binary-mode switch. That overload
is reflective, so it required a trimming annotation on the adapter and therefore on the
consumer; routing through a JsonTypeInfo obtained from the decided options needs
none, and produces identical JSON.
When the resolver does not cover data's type this throws
NotSupportedException naming that type, which is the diagnostic a consumer needs
and is the same failure the formatter itself produces. It fails loudly rather than emitting a
rendered type name as the payload.
Exceptions
- ArgumentNullException
Thrown when
datais null.- NotSupportedException
Thrown when the configured resolver provides no metadata for the payload's type.
SerializePayloadToUtf8Bytes(object)
Serializes a CloudEvent payload to UTF-8 bytes through the decided resolver.
public byte[] SerializePayloadToUtf8Bytes(object data)
Parameters
dataobjectThe payload to serialize.
Returns
- byte[]
The payload as UTF-8 encoded JSON.
Exceptions
- ArgumentNullException
Thrown when
datais null.- NotSupportedException
Thrown when the configured resolver provides no metadata for the payload's type.