| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | #nullable enable |
| | 5 | |
|
| | 6 | | using System; |
| | 7 | | using System.Collections.Generic; |
| | 8 | | using System.Globalization; |
| | 9 | | using System.Text.Json; |
| | 10 | |
|
| | 11 | | namespace Azure.Core |
| | 12 | | { |
| | 13 | | internal static class Utf8JsonWriterExtensions |
| | 14 | | { |
| | 15 | | public static void WriteStringValue(this Utf8JsonWriter writer, DateTimeOffset value, string format) => |
| 0 | 16 | | writer.WriteStringValue(TypeFormatters.ToString(value, format)); |
| | 17 | |
|
| | 18 | | public static void WriteStringValue(this Utf8JsonWriter writer, TimeSpan value, string format) => |
| 0 | 19 | | writer.WriteStringValue(TypeFormatters.ToString(value, format)); |
| | 20 | |
|
| | 21 | | public static void WriteStringValue(this Utf8JsonWriter writer, char value) => |
| 0 | 22 | | writer.WriteStringValue(value.ToString(CultureInfo.InvariantCulture)); |
| | 23 | |
|
| | 24 | | public static void WriteObjectValue(this Utf8JsonWriter writer, IUtf8JsonSerializable value) |
| | 25 | | { |
| 148 | 26 | | value.Write(writer); |
| 148 | 27 | | } |
| | 28 | |
|
| | 29 | | public static void WriteBase64StringValue(this Utf8JsonWriter writer, byte[] value, string format) |
| | 30 | | { |
| | 31 | | switch (format) |
| | 32 | | { |
| | 33 | | case "U": |
| 0 | 34 | | writer.WriteStringValue(TypeFormatters.ToBase64UrlString(value)); |
| 0 | 35 | | break; |
| | 36 | | default: |
| 0 | 37 | | 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 | | { |
| 0 | 43 | | if (format != "U") throw new ArgumentOutOfRangeException(format, "Only 'U' format is supported when writing |
| | 44 | |
|
| 0 | 45 | | writer.WriteNumberValue(value.ToUnixTimeSeconds()); |
| 0 | 46 | | } |
| | 47 | |
|
| | 48 | | public static void WriteObjectValue(this Utf8JsonWriter writer, object value) |
| | 49 | | { |
| | 50 | | switch (value) |
| | 51 | | { |
| | 52 | | case null: |
| 0 | 53 | | writer.WriteNullValue(); |
| 0 | 54 | | break; |
| | 55 | | case IUtf8JsonSerializable serializable: |
| 0 | 56 | | writer.WriteObjectValue(serializable); |
| 0 | 57 | | break; |
| | 58 | | case byte[] bytes: |
| 0 | 59 | | writer.WriteStringValue(Convert.ToBase64String(bytes)); |
| 0 | 60 | | break; |
| | 61 | | case int i: |
| 0 | 62 | | writer.WriteNumberValue(i); |
| 0 | 63 | | break; |
| | 64 | | case decimal d: |
| 0 | 65 | | writer.WriteNumberValue(d); |
| 0 | 66 | | break; |
| | 67 | | case double d: |
| 0 | 68 | | writer.WriteNumberValue(d); |
| 0 | 69 | | break; |
| | 70 | | case float f: |
| 0 | 71 | | writer.WriteNumberValue(f); |
| 0 | 72 | | break; |
| | 73 | | case long l: |
| 0 | 74 | | writer.WriteNumberValue(l); |
| 0 | 75 | | break; |
| | 76 | | case string s: |
| 0 | 77 | | writer.WriteStringValue(s); |
| 0 | 78 | | break; |
| | 79 | | case bool b: |
| 0 | 80 | | writer.WriteBooleanValue(b); |
| 0 | 81 | | break; |
| | 82 | | case Guid g: |
| 0 | 83 | | writer.WriteStringValue(g); |
| 0 | 84 | | break; |
| | 85 | | case DateTimeOffset dateTimeOffset: |
| 0 | 86 | | writer.WriteStringValue(dateTimeOffset,"O"); |
| 0 | 87 | | break; |
| | 88 | | case DateTime dateTime: |
| 0 | 89 | | writer.WriteStringValue(dateTime, "O"); |
| 0 | 90 | | break; |
| | 91 | | case IEnumerable<KeyValuePair<string, object>> enumerable: |
| 0 | 92 | | writer.WriteStartObject(); |
| 0 | 93 | | foreach (KeyValuePair<string, object> pair in enumerable) |
| | 94 | | { |
| 0 | 95 | | writer.WritePropertyName(pair.Key); |
| 0 | 96 | | writer.WriteObjectValue(pair.Value); |
| | 97 | | } |
| 0 | 98 | | writer.WriteEndObject(); |
| 0 | 99 | | break; |
| | 100 | | case IEnumerable<object> objectEnumerable: |
| 0 | 101 | | writer.WriteStartArray(); |
| 0 | 102 | | foreach (object item in objectEnumerable) |
| | 103 | | { |
| 0 | 104 | | writer.WriteObjectValue(item); |
| | 105 | | } |
| 0 | 106 | | writer.WriteEndArray(); |
| 0 | 107 | | break; |
| | 108 | |
|
| | 109 | | default: |
| 0 | 110 | | throw new NotSupportedException("Not supported type " + value.GetType()); |
| | 111 | | } |
| | 112 | | } |
| | 113 | | } |
| | 114 | | } |