| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.IO; |
| | 7 | | using System.Text.Json; |
| | 8 | | using System.Threading; |
| | 9 | | using Azure.Core; |
| | 10 | | using Azure.Core.Serialization; |
| | 11 | |
|
| | 12 | | namespace Azure.Messaging.EventGrid |
| | 13 | | { |
| | 14 | | /// <summary> |
| | 15 | | /// UTF-8 JSON-serializable wrapper for objects such as custom schema events. |
| | 16 | | /// Takes a custom ObjectSerializer to use when writing the object as JSON text. |
| | 17 | | /// </summary> |
| | 18 | | internal class CustomModelSerializer : IUtf8JsonSerializable |
| | 19 | | { |
| | 20 | | public object _payload; |
| | 21 | | public CancellationToken _cancellationToken; |
| | 22 | | public ObjectSerializer _serializer; |
| | 23 | |
|
| | 24 | | /// <summary> |
| | 25 | | /// Initializes an instance of the CustomModelSerializer class. |
| | 26 | | /// </summary> |
| | 27 | | /// <param name="payload"> |
| | 28 | | /// Object that can represent an event with a custom schema, or additional properties |
| | 29 | | /// added to the event envelope. |
| | 30 | | /// </param> |
| | 31 | | /// <param name="serializer"> |
| | 32 | | /// Custom ObjectSerializer to use when writing the object as JSON text. |
| | 33 | | /// </param> |
| | 34 | | /// <param name="cancellationToken"> The cancellation token to use. </param> |
| 160 | 35 | | public CustomModelSerializer(object payload, ObjectSerializer serializer, CancellationToken cancellationToken) |
| | 36 | | { |
| 160 | 37 | | _payload = payload; |
| 160 | 38 | | _serializer = serializer; |
| 160 | 39 | | _cancellationToken = cancellationToken; |
| 160 | 40 | | } |
| | 41 | | public void Write(Utf8JsonWriter writer) |
| | 42 | | { |
| 160 | 43 | | var stream = new MemoryStream(); |
| 160 | 44 | | _serializer.Serialize(stream, _payload, _payload.GetType(), _cancellationToken); |
| 160 | 45 | | stream.Seek(0, SeekOrigin.Begin); |
| 160 | 46 | | JsonDocument.Parse(stream).WriteTo(writer); |
| 160 | 47 | | } |
| | 48 | | } |
| | 49 | | } |