< Summary

Class:Azure.Messaging.EventGrid.CustomModelSerializer
Assembly:Azure.Messaging.EventGrid
File(s):C:\Git\azure-sdk-for-net\sdk\eventgrid\Azure.Messaging.EventGrid\src\Customization\CustomModelSerializer.cs
Covered lines:10
Uncovered lines:0
Coverable lines:10
Total lines:49
Line coverage:100% (10 of 10)
Covered branches:0
Total branches:0

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
.ctor(...)-100%100%
Write(...)-100%100%

File(s)

C:\Git\azure-sdk-for-net\sdk\eventgrid\Azure.Messaging.EventGrid\src\Customization\CustomModelSerializer.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4using System;
 5using System.Collections.Generic;
 6using System.IO;
 7using System.Text.Json;
 8using System.Threading;
 9using Azure.Core;
 10using Azure.Core.Serialization;
 11
 12namespace 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>
 16035        public CustomModelSerializer(object payload, ObjectSerializer serializer, CancellationToken cancellationToken)
 36        {
 16037            _payload = payload;
 16038            _serializer = serializer;
 16039            _cancellationToken = cancellationToken;
 16040        }
 41        public void Write(Utf8JsonWriter writer)
 42        {
 16043            var stream = new MemoryStream();
 16044            _serializer.Serialize(stream, _payload, _payload.GetType(), _cancellationToken);
 16045            stream.Seek(0, SeekOrigin.Begin);
 16046            JsonDocument.Parse(stream).WriteTo(writer);
 16047        }
 48    }
 49}

Methods/Properties

.ctor(...)
Write(...)