/
AzureCoreExtensions.cs
144 lines (135 loc) · 7.06 KB
/
AzureCoreExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Azure.Core.Json;
using Azure.Core.Serialization;
namespace Azure
{
/// <summary>
/// Extensions that can be used for serialization.
/// </summary>
public static class AzureCoreExtensions
{
/// <summary>
/// Converts the <see cref="BinaryData"/> to the specified type using
/// the provided <see cref="ObjectSerializer"/>.
/// </summary>
/// <typeparam name="T">The type that the data should be
/// converted to.</typeparam>
/// <param name="data">The <see cref="BinaryData"/> instance to convert.</param>
/// <param name="serializer">The serializer to use
/// when deserializing the data.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to use during deserialization.</param>
///<returns>The data converted to the specified type.</returns>
public static T? ToObject<T>(this BinaryData data, ObjectSerializer serializer, CancellationToken cancellationToken = default) =>
(T?)serializer.Deserialize(data.ToStream(), typeof(T), cancellationToken);
/// <summary>
/// Converts the <see cref="BinaryData"/> to the specified type using
/// the provided <see cref="ObjectSerializer"/>.
/// </summary>
/// <typeparam name="T">The type that the data should be
/// converted to.</typeparam>
/// <param name="data">The <see cref="BinaryData"/> instance to convert.</param>
/// <param name="serializer">The serializer to use
/// when deserializing the data.</param>
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to use during deserialization.</param>
///<returns>The data converted to the specified type.</returns>
public static async ValueTask<T?> ToObjectAsync<T>(this BinaryData data, ObjectSerializer serializer, CancellationToken cancellationToken = default) =>
(T?)await serializer.DeserializeAsync(data.ToStream(), typeof(T), cancellationToken).ConfigureAwait(false);
/// <summary>
/// Converts the json value represented by <see cref="BinaryData"/> to an object of a specific type.
/// </summary>
/// <param name="data">The <see cref="BinaryData"/> instance to convert.</param>
/// <returns> The object value of the json value.
/// If the object contains a primitive type such as string, int, double, bool, or null literal, it returns that type.
/// Otherwise, it returns either an object[] or Dictionary<string, object>.
/// Each value in the key value pair or list will also be converted into a primitive or another complex type recursively.
/// </returns>
public static object? ToObjectFromJson(this BinaryData data)
{
JsonElement element = data.ToObjectFromJson<JsonElement>();
return element.GetObject();
}
/// <summary>
/// Return the content of the BinaryData as a dynamic type. Please see https://aka.ms/azsdk/net/dynamiccontent for details.
/// </summary>
public static dynamic ToDynamicFromJson(this BinaryData utf8Json)
{
DynamicDataOptions options = new DynamicDataOptions();
return utf8Json.ToDynamicFromJson(options);
}
/// <summary>
/// Return the content of the BinaryData as a dynamic type. Please see https://aka.ms/azsdk/net/dynamiccontent for details.
/// <paramref name="propertyNameFormat">The format of property names in the JSON content.
/// This value indicates to the dynamic type that it can convert property names on the returned value to this format in the underlying JSON.
/// Please see https://aka.ms/azsdk/net/dynamiccontent#use-c-naming-conventions for details.
/// </paramref>
/// <paramref name="dateTimeFormat">The standard format specifier to pass when serializing DateTime and DateTimeOffset values in the JSON content.
/// To serialize to unix time, pass the value <code>"x"</code> and
/// see <see href="https://learn.microsoft.com/dotnet/standard/base-types/standard-date-and-time-format-strings">https://learn.microsoft.com/dotnet/standard/base-types/standard-date-and-time-format-strings#table-of-format-specifiers</see> for other well known values.
/// </paramref>
/// </summary>
public static dynamic ToDynamicFromJson(this BinaryData utf8Json, JsonPropertyNames propertyNameFormat, string dateTimeFormat = DynamicData.RoundTripFormat)
{
DynamicDataOptions options = new DynamicDataOptions()
{
PropertyNameFormat = propertyNameFormat,
DateTimeFormat = dateTimeFormat
};
return utf8Json.ToDynamicFromJson(options);
}
/// <summary>
/// Return the content of the BinaryData as a dynamic type.
/// </summary>
internal static dynamic ToDynamicFromJson(this BinaryData utf8Json, DynamicDataOptions options)
{
MutableJsonDocument mdoc = MutableJsonDocument.Parse(utf8Json, DynamicDataOptions.ToSerializerOptions(options));
return new DynamicData(mdoc.RootElement, options);
}
private static object? GetObject(in this JsonElement element)
{
switch (element.ValueKind)
{
case JsonValueKind.String:
return element.GetString();
case JsonValueKind.Number:
if (element.TryGetInt32(out int intValue))
{
return intValue;
}
if (element.TryGetInt64(out long longValue))
{
return longValue;
}
return element.GetDouble();
case JsonValueKind.True:
return true;
case JsonValueKind.False:
return false;
case JsonValueKind.Undefined:
case JsonValueKind.Null:
return null;
case JsonValueKind.Object:
var dictionary = new Dictionary<string, object?>();
foreach (JsonProperty jsonProperty in element.EnumerateObject())
{
dictionary.Add(jsonProperty.Name, jsonProperty.Value.GetObject());
}
return dictionary;
case JsonValueKind.Array:
var list = new List<object?>();
foreach (JsonElement item in element.EnumerateArray())
{
list.Add(item.GetObject());
}
return list.ToArray();
default:
throw new NotSupportedException("Not supported value kind " + element.ValueKind);
}
}
}
}