Table of Contents

Class JsonCloudEventCodec

Namespace
Virtufin.Worker.DevKit
Assembly
Virtufin.Worker.DevKit.dll

Default JSON codec for WorkerBase<TInput, TOutput>. Decodes Data into a JsonNode and encodes a JsonObject response payload back into JSON bytes with application/json content type.

AOT/trim-safety: every decode branch works on input that is already JSON -- string passes through, JsonElement and JsonDocument yield their raw text, and a JsonNode writes itself. None of them reach the reflection-based serializer, so this type is clean under PublishAot=true. An unsupported runtime type throws NotSupportedException instead of falling back to reflection: a DefaultJsonTypeInfoResolver fallback is itself flagged IL2026/IL3050 and would fail inside a NativeAOT worker, so failing loudly at the boundary beats failing obscurely at runtime.

public sealed class JsonCloudEventCodec : ICloudEventCodec<JsonNode, JsonObject>
Inheritance
JsonCloudEventCodec
Implements
Inherited Members

Fields

ContentType

CloudEvents JSON content type.

public const string ContentType = "application/json"

Field Value

string

Methods

DecodePayload(CloudEvent)

Decode the CloudEvent's Data payload into the worker's typed input representation. Implementations may read DataContentType to decide the parser.

public JsonNode DecodePayload(CloudEvent input)

Parameters

input CloudEvent

Returns

JsonNode

Remarks

A JSON literal null -- including a CloudEvent whose Data is itself null -- parses successfully, but Parse(string, JsonNodeOptions?, JsonDocumentOptions) returns a C# null reference for it (there is no "JSON null" node type in System.Text.Json.Nodes). That is a valid, empty payload, not a parse failure, so it's represented as an empty JsonObject rather than throwing. Only a genuine parse error (malformed JSON) throws InvalidOperationException.

EncodeError(string)

Encode an error message into a (data, contentType) tuple for error responses. Default convention mirrors the response shape.

public (object data, string contentType) EncodeError(string message)

Parameters

message string

Returns

(object data, string contentType)

EncodeResponse(JsonObject)

Encode the worker's typed response into a (data, contentType) tuple suitable for assignment to Data and DataContentType.

public (object data, string contentType) EncodeResponse(JsonObject payload)

Parameters

payload JsonObject

Returns

(object data, string contentType)