< Summary

Class:Azure.Core.Utf8JsonWriterExtensions
Assembly:Azure.Analytics.Synapse.Artifacts
File(s):C:\Git\azure-sdk-for-net\sdk\core\Azure.Core\src\Shared\AutoRest\Utf8JsonWriterExtensions.cs
Covered lines:0
Uncovered lines:49
Coverable lines:49
Total lines:114
Line coverage:0% (0 of 49)
Covered branches:0
Total branches:40
Branch coverage:0% (0 of 40)

Metrics

MethodCyclomatic complexity Line coverage Branch coverage
WriteStringValue(...)-0%100%
WriteStringValue(...)-0%100%
WriteStringValue(...)-0%100%
WriteObjectValue(...)-0%100%
WriteBase64StringValue(...)-0%0%
WriteNumberValue(...)-0%0%
WriteObjectValue(...)-0%0%

File(s)

C:\Git\azure-sdk-for-net\sdk\core\Azure.Core\src\Shared\AutoRest\Utf8JsonWriterExtensions.cs

#LineLine coverage
 1// Copyright (c) Microsoft Corporation. All rights reserved.
 2// Licensed under the MIT License.
 3
 4#nullable enable
 5
 6using System;
 7using System.Collections.Generic;
 8using System.Globalization;
 9using System.Text.Json;
 10
 11namespace Azure.Core
 12{
 13    internal static class Utf8JsonWriterExtensions
 14    {
 15        public static void WriteStringValue(this Utf8JsonWriter writer, DateTimeOffset value, string format) =>
 016            writer.WriteStringValue(TypeFormatters.ToString(value, format));
 17
 18        public static void WriteStringValue(this Utf8JsonWriter writer, TimeSpan value, string format) =>
 019            writer.WriteStringValue(TypeFormatters.ToString(value, format));
 20
 21        public static void WriteStringValue(this Utf8JsonWriter writer, char value) =>
 022            writer.WriteStringValue(value.ToString(CultureInfo.InvariantCulture));
 23
 24        public static void WriteObjectValue(this Utf8JsonWriter writer, IUtf8JsonSerializable value)
 25        {
 026            value.Write(writer);
 027        }
 28
 29        public static void WriteBase64StringValue(this Utf8JsonWriter writer, byte[] value, string format)
 30        {
 31            switch (format)
 32            {
 33                case "U":
 034                    writer.WriteStringValue(TypeFormatters.ToBase64UrlString(value));
 035                    break;
 36                default:
 037                    throw new ArgumentException($"Format is not supported: '{format}'", nameof(format));
 38            }
 39        }
 40
 41        public static void WriteNumberValue(this Utf8JsonWriter writer, DateTimeOffset value, string format)
 42        {
 043            if (format != "U") throw new ArgumentOutOfRangeException(format, "Only 'U' format is supported when writing 
 44
 045            writer.WriteNumberValue(value.ToUnixTimeSeconds());
 046        }
 47
 48        public static void WriteObjectValue(this Utf8JsonWriter writer, object value)
 49        {
 50            switch (value)
 51            {
 52                case null:
 053                    writer.WriteNullValue();
 054                    break;
 55                case IUtf8JsonSerializable serializable:
 056                    writer.WriteObjectValue(serializable);
 057                    break;
 58                case byte[] bytes:
 059                    writer.WriteStringValue(Convert.ToBase64String(bytes));
 060                    break;
 61                case int i:
 062                    writer.WriteNumberValue(i);
 063                    break;
 64                case decimal d:
 065                    writer.WriteNumberValue(d);
 066                    break;
 67                case double d:
 068                    writer.WriteNumberValue(d);
 069                    break;
 70                case float f:
 071                    writer.WriteNumberValue(f);
 072                    break;
 73                case long l:
 074                    writer.WriteNumberValue(l);
 075                    break;
 76                case string s:
 077                    writer.WriteStringValue(s);
 078                    break;
 79                case bool b:
 080                    writer.WriteBooleanValue(b);
 081                    break;
 82                case Guid g:
 083                    writer.WriteStringValue(g);
 084                    break;
 85                case DateTimeOffset dateTimeOffset:
 086                    writer.WriteStringValue(dateTimeOffset,"O");
 087                    break;
 88                case DateTime dateTime:
 089                    writer.WriteStringValue(dateTime, "O");
 090                    break;
 91                case IEnumerable<KeyValuePair<string, object>> enumerable:
 092                    writer.WriteStartObject();
 093                    foreach (KeyValuePair<string, object> pair in enumerable)
 94                    {
 095                        writer.WritePropertyName(pair.Key);
 096                        writer.WriteObjectValue(pair.Value);
 97                    }
 098                    writer.WriteEndObject();
 099                    break;
 100                case IEnumerable<object> objectEnumerable:
 0101                    writer.WriteStartArray();
 0102                    foreach (object item in objectEnumerable)
 103                    {
 0104                        writer.WriteObjectValue(item);
 105                    }
 0106                    writer.WriteEndArray();
 0107                    break;
 108
 109                default:
 0110                    throw new NotSupportedException("Not supported type " + value.GetType());
 111            }
 112        }
 113    }
 114}