// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
using System.Text.Json.Serialization;
using Azure.Core;
using Azure.Core.Serialization;
namespace Azure.Messaging
{
/// Represents a CloudEvent conforming to the 1.0 schema. This type has built-in serialization using System.Text.Json.
[JsonConverter(typeof(CloudEventConverter))]
[RequiresUnreferencedCode(DynamicData.SerializationRequiresUnreferencedCode)]
[RequiresDynamicCode(DynamicData.SerializationRequiresUnreferencedCode)]
public class CloudEvent
{
/// Initializes a new instance of the class.
/// Identifies the context in which an event happened. The combination of id and source must be unique for each distinct event.
/// Type of event related to the originating occurrence. For example, "Contoso.Items.ItemReceived".
/// Event data specific to the event type.
/// The type to use when serializing the data.
/// If not specified, will be used on .
///
/// or was null.
///
public CloudEvent(string source, string type, object? jsonSerializableData, Type? dataSerializationType = default)
{
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, type: dataSerializationType ?? jsonSerializableData?.GetType());
SpecVersion = "1.0";
}
/// Initializes a new instance of the class using binary event data.
/// Identifies the context in which an event happened. The combination of id and source must be unique for each distinct event.
/// Type of event related to the originating occurrence. For example, "Contoso.Items.ItemReceived".
/// Binary event data specific to the event type.
/// Content type of the payload. A content type different from "application/json" should be specified if payload is not JSON.
/// The format that the data of a should be sent in
/// when using the JSON envelope format.
///
/// or was null.
///
public CloudEvent(string source, string type, BinaryData? data, 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() { }
///
/// Gets or sets the event data as . Using BinaryData,
/// one can deserialize the payload into rich data, or access the raw JSON data using .
///
public BinaryData? Data { get; set; }
///
/// Gets or sets an identifier for the event. The combination of and must be unique for each distinct event.
/// If not explicitly set, this will default to a .
///
public string Id
{
get
{
return _id!;
}
set
{
Argument.AssertNotNull(value, nameof(value));
_id = value;
}
}
private string? _id;
internal CloudEventDataFormat DataFormat { get; set; }
/// Gets or sets the context in which an event happened. The combination of
/// and must be unique for each distinct event.
public string Source
{
get
{
return _source!;
}
set
{
Argument.AssertNotNull(value, nameof(value));
_source = value;
}
}
private string? _source;
/// Gets or sets the type of event related to the originating occurrence.
public string Type
{
get
{
return _type!;
}
set
{
Argument.AssertNotNull(value, nameof(value));
_type = value;
}
}
private string? _type;
///
/// The spec version of the cloud event.
///
internal string? SpecVersion { get; set; }
///
/// Gets or sets the time (in UTC) the event was generated, in RFC3339 format.
/// If not explicitly set, this will default to the time that the event is constructed.
///
public DateTimeOffset? Time { get; set; } = DateTimeOffset.UtcNow;
/// Gets or sets the schema that the data adheres to.
public string? DataSchema { get; set; }
/// Gets or sets the content type of the data.
public string? DataContentType { get; set; }
internal Type? DataSerializationType { get; }
/// Gets or sets the subject of the event in the context of the event producer (identified by source).
public string? Subject { get; set; }
///
/// Gets extension attributes that can be additionally added to the CloudEvent envelope.
///
public IDictionary ExtensionAttributes { get; } = new CloudEventExtensionAttributes();
///
/// Given JSON-encoded events, parses the event envelope and returns an array of CloudEvents.
/// If the specified event is not valid JSON an exception is thrown.
/// By default, if the event is missing required properties, an exception is thrown though this can be relaxed
/// by setting the parameter.
///
/// An instance of containing the JSON for one or more CloudEvents.
/// Set to to allow missing or invalid properties to still parse into a CloudEvent.
/// In particular, by setting strict to , the source, id, specversion and type properties are no longer required
/// to be present in the JSON. Additionally, the casing requirements of the extension attribute names are relaxed.
///
/// An array of instances.
public static CloudEvent[] ParseMany(BinaryData json, bool skipValidation = false)
{
Argument.AssertNotNull(json, nameof(json));
CloudEvent[]? cloudEvents = null;
JsonDocument requestDocument = JsonDocument.Parse(json);
// Parse JsonElement into separate events, deserialize event envelope properties
if (requestDocument.RootElement.ValueKind == JsonValueKind.Object)
{
cloudEvents = new CloudEvent[1];
cloudEvents[0] = CloudEventConverter.DeserializeCloudEvent(requestDocument.RootElement, skipValidation);
}
else if (requestDocument.RootElement.ValueKind == JsonValueKind.Array)
{
cloudEvents = new CloudEvent[requestDocument.RootElement.GetArrayLength()];
int i = 0;
foreach (JsonElement property in requestDocument.RootElement.EnumerateArray())
{
cloudEvents[i++] = CloudEventConverter.DeserializeCloudEvent(property, skipValidation);
}
}
return cloudEvents ?? Array.Empty();
}
///
/// Given a single JSON-encoded event, parses the event envelope and returns a .
/// If the specified event is not valid JSON an exception is thrown.
/// By default, if the event is missing required properties, an exception is thrown though this can be relaxed
/// by setting the parameter.
///
/// An instance of containing the JSON for the CloudEvent.
/// Set to to allow missing or invalid properties to still parse into a CloudEvent.
/// In particular, by setting strict to , the source, id, specversion and type properties are no longer required
/// to be present in the JSON. Additionally, the casing requirements of the extension attribute names are relaxed.
///
/// A .
///
/// contained multiple events. should be used instead.
///
public static CloudEvent? Parse(BinaryData json, bool skipValidation = false)
{
Argument.AssertNotNull(json, nameof(json));
using JsonDocument requestDocument = JsonDocument.Parse(json);
CloudEvent? cloudEvent = null;
if (requestDocument.RootElement.ValueKind == JsonValueKind.Object)
{
cloudEvent = CloudEventConverter.DeserializeCloudEvent(requestDocument.RootElement, skipValidation);
}
else if (requestDocument.RootElement.ValueKind == JsonValueKind.Array)
{
if (requestDocument.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 {nameof(ParseMany)} overload.");
}
foreach (JsonElement property in requestDocument.RootElement.EnumerateArray())
{
cloudEvent = CloudEventConverter.DeserializeCloudEvent(property, skipValidation);
break;
}
}
return cloudEvent;
}
}
}