CloudEvent
Represents a CloudEvent conforming to the 1.0 schema. This type has built-in serialization using System.Text.Json.
using Azure.Core;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace Azure.Messaging
{
[System.Runtime.CompilerServices.NullableContext(2)]
[System.Runtime.CompilerServices.Nullable(0)]
[JsonConverter(typeof(CloudEventConverter))]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("This utilizes reflection-based JSON serialization and deserialization which is not compatible with trimming.")]
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("This utilizes reflection-based JSON serialization and deserialization which is not compatible with trimming.")]
public class CloudEvent
{
private string _id;
private string _source;
private string _type;
public BinaryData Data { get; set; }
[System.Runtime.CompilerServices.Nullable(1)]
public string Id {
[System.Runtime.CompilerServices.NullableContext(1)]
get {
return _id;
}
[System.Runtime.CompilerServices.NullableContext(1)]
set {
Argument.AssertNotNull(value, "value");
_id = value;
}
}
internal CloudEventDataFormat DataFormat { get; set; }
[System.Runtime.CompilerServices.Nullable(1)]
public string Source {
[System.Runtime.CompilerServices.NullableContext(1)]
get {
return _source;
}
[System.Runtime.CompilerServices.NullableContext(1)]
set {
Argument.AssertNotNull(value, "value");
_source = value;
}
}
[System.Runtime.CompilerServices.Nullable(1)]
public string Type {
[System.Runtime.CompilerServices.NullableContext(1)]
get {
return _type;
}
[System.Runtime.CompilerServices.NullableContext(1)]
set {
Argument.AssertNotNull(value, "value");
_type = value;
}
}
internal string SpecVersion { get; set; }
public DateTimeOffset? Time { get; set; } = DateTimeOffset.UtcNow;
public string DataSchema { get; set; }
public string DataContentType { get; set; }
internal Type DataSerializationType { get; }
public string Subject { get; set; }
[System.Runtime.CompilerServices.Nullable(1)]
[field: System.Runtime.CompilerServices.Nullable(1)]
public IDictionary<string, object> ExtensionAttributes {
[System.Runtime.CompilerServices.NullableContext(1)]
get;
} = new CloudEventExtensionAttributes<string, object>();
[System.Runtime.CompilerServices.NullableContext(1)]
public CloudEvent(string source, string type, [System.Runtime.CompilerServices.Nullable(2)] object jsonSerializableData, [System.Runtime.CompilerServices.Nullable(2)] Type dataSerializationType = null)
{
if (jsonSerializableData is BinaryData)
throw new InvalidOperationException("This constructor does not support BinaryData. Use the constructor that takes a BinaryData instance.");
Source = source;
Type = type;
Id = Guid.NewGuid().ToString();
DataFormat = CloudEventDataFormat.Json;
Data = new BinaryData(jsonSerializableData, (JsonSerializerOptions)null, dataSerializationType ?? jsonSerializableData?.GetType());
SpecVersion = "1.0";
}
[System.Runtime.CompilerServices.NullableContext(1)]
public CloudEvent(string source, string type, [System.Runtime.CompilerServices.Nullable(2)] BinaryData data, [System.Runtime.CompilerServices.Nullable(2)] string dataContentType, CloudEventDataFormat dataFormat = CloudEventDataFormat.Binary)
{
Source = source;
Type = type;
DataContentType = dataContentType;
Id = Guid.NewGuid().ToString();
DataFormat = dataFormat;
Data = data;
SpecVersion = "1.0";
}
internal CloudEvent()
{
}
[System.Runtime.CompilerServices.NullableContext(1)]
public static CloudEvent[] ParseMany(BinaryData json, bool skipValidation = false)
{
Argument.AssertNotNull<BinaryData>(json, "json");
CloudEvent[] array = null;
JsonDocument jsonDocument = JsonDocument.Parse(BinaryData.op_Implicit(json), default(JsonDocumentOptions));
JsonElement rootElement = jsonDocument.RootElement;
if (rootElement.ValueKind == JsonValueKind.Object)
array = new CloudEvent[1] {
CloudEventConverter.DeserializeCloudEvent(jsonDocument.RootElement, skipValidation)
};
else {
rootElement = jsonDocument.RootElement;
if (rootElement.ValueKind == JsonValueKind.Array) {
rootElement = jsonDocument.RootElement;
array = new CloudEvent[rootElement.GetArrayLength()];
int num = 0;
rootElement = jsonDocument.RootElement;
foreach (JsonElement item in rootElement.EnumerateArray()) {
array[num++] = CloudEventConverter.DeserializeCloudEvent(item, skipValidation);
}
}
}
return array ?? Array.Empty<CloudEvent>();
}
[System.Runtime.CompilerServices.NullableContext(1)]
[return: System.Runtime.CompilerServices.Nullable(2)]
public static CloudEvent Parse(BinaryData json, bool skipValidation = false)
{
Argument.AssertNotNull<BinaryData>(json, "json");
using (JsonDocument jsonDocument = JsonDocument.Parse(BinaryData.op_Implicit(json), default(JsonDocumentOptions))) {
CloudEvent result = null;
JsonElement rootElement = jsonDocument.RootElement;
if (rootElement.ValueKind == JsonValueKind.Object)
result = CloudEventConverter.DeserializeCloudEvent(jsonDocument.RootElement, skipValidation);
else {
rootElement = jsonDocument.RootElement;
if (rootElement.ValueKind == JsonValueKind.Array) {
rootElement = jsonDocument.RootElement;
if (rootElement.GetArrayLength() > 1)
throw new ArgumentException("The BinaryData instance contains JSON from multiple cloud events. This method should only be used with BinaryData containing a single cloud event. " + Environment.NewLine + "To parse multiple events, use the ParseMany overload.");
rootElement = jsonDocument.RootElement;
using (JsonElement.ArrayEnumerator arrayEnumerator = rootElement.EnumerateArray().GetEnumerator()) {
if (arrayEnumerator.MoveNext())
result = CloudEventConverter.DeserializeCloudEvent(arrayEnumerator.Current, skipValidation);
}
}
}
return result;
}
}
}
}