Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add System.Text.Json converter for EventGridEvent #22295

Merged
merged 5 commits into from
Jun 29, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@

using System;
using System.Text.Json;
using System.Text.Json.Serialization;
using Azure.Core;
using Azure.Messaging.EventGrid.Models;

namespace Azure.Messaging.EventGrid
{
/// <summary> Properties of an event published to an Event Grid topic using the EventGrid Schema. </summary>
[JsonConverter(typeof(EventGridEventConverter))]
public class EventGridEvent
{
/// <summary> Initializes a new instance of <see cref="EventGridEvent"/>. </summary>
Expand Down Expand Up @@ -142,16 +144,11 @@ public static EventGridEvent[] ParseMany(BinaryData json)
if (requestDocument.RootElement.ValueKind == JsonValueKind.Object)
{
egEvents = new EventGridEvent[1];
egEvents[0] = (new EventGridEvent(EventGridEventInternal.DeserializeEventGridEventInternal(requestDocument.RootElement)));
egEvents[0] = JsonSerializer.Deserialize<EventGridEvent>(json.ToMemory().Span);
}
else if (requestDocument.RootElement.ValueKind == JsonValueKind.Array)
{
egEvents = new EventGridEvent[requestDocument.RootElement.GetArrayLength()];
int i = 0;
foreach (JsonElement property in requestDocument.RootElement.EnumerateArray())
{
egEvents[i++] = new EventGridEvent(EventGridEventInternal.DeserializeEventGridEventInternal(property));
}
egEvents = JsonSerializer.Deserialize<EventGridEvent[]>(json.ToMemory().Span);
}
return egEvents ?? Array.Empty<EventGridEvent>();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Text.Json;
using System.Text.Json.Serialization;
using Azure.Core;
using Azure.Messaging.EventGrid.Models;

namespace Azure.Messaging.EventGrid
{
/// <summary>
/// A custom converter that attributes the <see cref="EventGridEvent"/> type.
/// This allows System.Text.Json to serialize and deserialize EventGridEvent by default.
/// </summary>
internal class EventGridEventConverter : JsonConverter<EventGridEvent>
Copy link
Member Author

@JoshLove-msft JoshLove-msft Jun 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is covered by existing tests after switching the Parse and Publish methods to use this.

{
/// <summary>
/// Gets or sets the serializer to use for the data portion of the <see cref="EventGridEvent"/>. If not specified,
/// JsonObjectSerializer is used.
/// </summary>
/// <inheritdoc cref="JsonConverter{CloudEvent}.Read(ref Utf8JsonReader, Type, JsonSerializerOptions)"/>
public override EventGridEvent Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
JsonDocument requestDocument = JsonDocument.ParseValue(ref reader);
return new EventGridEvent(EventGridEventInternal.DeserializeEventGridEventInternal(requestDocument.RootElement));
}

/// <inheritdoc cref="JsonConverter{EventGridEvent}.Write(Utf8JsonWriter, EventGridEvent, JsonSerializerOptions)"/>
public override void Write(Utf8JsonWriter writer, EventGridEvent value, JsonSerializerOptions options)
{
JsonDocument data = JsonDocument.Parse(value.Data.ToStream());
var eventGridEventInternal = new EventGridEventInternal(
value.Id,
value.Subject,
data.RootElement,
value.EventType,
value.EventTime,
value.DataVersion)
{
Topic = value.Topic
};
((IUtf8JsonSerializable) eventGridEventInternal).Write(writer);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using System.Threading.Tasks;
using Azure.Core;
using Azure.Core.Pipeline;
using Azure.Messaging.EventGrid.Models;

namespace Azure.Messaging.EventGrid
{
Expand Down Expand Up @@ -160,26 +159,9 @@ private async Task<Response> SendEventsInternal(IEnumerable<EventGridEvent> even

using HttpMessage message = _pipeline.CreateMessage();
Request request = CreateEventRequest(message, "application/json");
var content = new Utf8JsonRequestContent();
content.JsonWriter.WriteStartArray();
foreach (EventGridEvent egEvent in events)
{
JsonDocument data = JsonDocument.Parse(egEvent.Data.ToStream());
EventGridEventInternal newEGEvent = new EventGridEventInternal(
egEvent.Id,
egEvent.Subject,
data.RootElement,
egEvent.EventType,
egEvent.EventTime,
egEvent.DataVersion)
{
Topic = egEvent.Topic
};
content.JsonWriter.WriteObjectValue(newEGEvent);
}

content.JsonWriter.WriteEndArray();
request.Content = content;
// leverage custom converter for EventGridEvent
request.Content = RequestContent.Create(JsonSerializer.Serialize(events));

if (async)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public async Task RespectsPortFromUriSendingEventGridEvents()

private static List<EventGridEvent> DeserializeRequest(Request request)
{
var content = request.Content as Utf8JsonRequestContent;
var content = request.Content;
var stream = new MemoryStream();
content.WriteTo(stream, CancellationToken.None);
stream.Position = 0;
Expand Down