Skip to content

Commit 08d2f3e

Browse files
Move BinaryData extensions to Core (Azure#18205)
* Move BinaryData extensions to Core * PR fb * Rename * SB test * update proj ref
1 parent 3f26709 commit 08d2f3e

File tree

14 files changed

+128
-125
lines changed

14 files changed

+128
-125
lines changed

sdk/core/Azure.Core.Experimental/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Release History
22

33
## 0.1.0-preview.8 (Unreleased)
4-
4+
- Methods from `SerializationExtensions` moved into `Azure.Core` package.
55

66
## 0.1.0-preview.7 (2020-10-28)
77

sdk/core/Azure.Core.Experimental/api/Azure.Core.Experimental.netstandard2.0.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
namespace Azure
2-
{
3-
public static partial class SerializationExtensions
4-
{
5-
public static System.BinaryData SerializeToBinaryData(this Azure.Core.Serialization.ObjectSerializer serializer, object? value, System.Type? inputType = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
6-
public static System.Threading.Tasks.ValueTask<System.BinaryData> SerializeToBinaryDataAsync(this Azure.Core.Serialization.ObjectSerializer serializer, object? value, System.Type? inputType = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
7-
public static System.Threading.Tasks.ValueTask<T?> ToObjectAsync<T>(this System.BinaryData data, Azure.Core.Serialization.ObjectSerializer serializer, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
8-
public static T? ToObject<T>(this System.BinaryData data, Azure.Core.Serialization.ObjectSerializer serializer, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
9-
}
10-
}
111
namespace Azure.Core
122
{
133
public partial class DynamicJson : System.Dynamic.IDynamicMetaObjectProvider

sdk/core/Azure.Core.Experimental/src/Azure.Core.Experimental.csproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@
1313
<ProjectReference Include="../../Azure.Core/src/Azure.Core.csproj" />
1414
</ItemGroup>
1515

16-
<ItemGroup>
17-
<PackageReference Include="System.Memory.Data" />
18-
</ItemGroup>
19-
2016
<ItemGroup>
2117
<Compile Include="$(AzureCoreSharedSources)Argument.cs" />
2218
<Compile Include="$(AzureCoreSharedSources)HashCodeBuilder.cs" />

sdk/core/Azure.Core.Experimental/src/Serialization/SerializationExtensions.cs

Lines changed: 0 additions & 91 deletions
This file was deleted.

sdk/core/Azure.Core/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## 1.9.0-beta.1 (Unreleased)
44

5+
## Added
6+
- Added Serialize overloads on `ObjectSerializer` that serialize to `BinaryData`.
7+
- Added AzureCoreExtensions containing extensions methods for `BinaryData` that allow deserializing with an `ObjectSerializer`.
8+
59
### Key Bug Fixes
610

711
- Avoid `ObjectDisposedException` when the request is cancelled during content upload over HTTPS.

sdk/core/Azure.Core/api/Azure.Core.net461.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ protected AsyncPageable(System.Threading.CancellationToken cancellationToken) {
1515
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
1616
public override string? ToString() { throw null; }
1717
}
18+
public static partial class AzureCoreExtensions
19+
{
20+
public static System.Threading.Tasks.ValueTask<T?> ToObjectAsync<T>(this System.BinaryData data, Azure.Core.Serialization.ObjectSerializer serializer, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
21+
public static T? ToObject<T>(this System.BinaryData data, Azure.Core.Serialization.ObjectSerializer serializer, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
22+
}
1823
public partial class AzureKeyCredential
1924
{
2025
public AzureKeyCredential(string key) { }
@@ -550,6 +555,8 @@ protected ObjectSerializer() { }
550555
public abstract object? Deserialize(System.IO.Stream stream, System.Type returnType, System.Threading.CancellationToken cancellationToken);
551556
public abstract System.Threading.Tasks.ValueTask<object?> DeserializeAsync(System.IO.Stream stream, System.Type returnType, System.Threading.CancellationToken cancellationToken);
552557
public abstract void Serialize(System.IO.Stream stream, object? value, System.Type inputType, System.Threading.CancellationToken cancellationToken);
558+
public virtual System.BinaryData Serialize(object? value, System.Type? inputType = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
553559
public abstract System.Threading.Tasks.ValueTask SerializeAsync(System.IO.Stream stream, object? value, System.Type inputType, System.Threading.CancellationToken cancellationToken);
560+
public virtual System.Threading.Tasks.ValueTask<System.BinaryData> SerializeAsync(object? value, System.Type? inputType = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
554561
}
555562
}

sdk/core/Azure.Core/api/Azure.Core.net5.0.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ protected AsyncPageable(System.Threading.CancellationToken cancellationToken) {
1515
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
1616
public override string? ToString() { throw null; }
1717
}
18+
public static partial class AzureCoreExtensions
19+
{
20+
public static System.Threading.Tasks.ValueTask<T?> ToObjectAsync<T>(this System.BinaryData data, Azure.Core.Serialization.ObjectSerializer serializer, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
21+
public static T? ToObject<T>(this System.BinaryData data, Azure.Core.Serialization.ObjectSerializer serializer, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
22+
}
1823
public partial class AzureKeyCredential
1924
{
2025
public AzureKeyCredential(string key) { }
@@ -550,6 +555,8 @@ protected ObjectSerializer() { }
550555
public abstract object? Deserialize(System.IO.Stream stream, System.Type returnType, System.Threading.CancellationToken cancellationToken);
551556
public abstract System.Threading.Tasks.ValueTask<object?> DeserializeAsync(System.IO.Stream stream, System.Type returnType, System.Threading.CancellationToken cancellationToken);
552557
public abstract void Serialize(System.IO.Stream stream, object? value, System.Type inputType, System.Threading.CancellationToken cancellationToken);
558+
public virtual System.BinaryData Serialize(object? value, System.Type? inputType = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
553559
public abstract System.Threading.Tasks.ValueTask SerializeAsync(System.IO.Stream stream, object? value, System.Type inputType, System.Threading.CancellationToken cancellationToken);
560+
public virtual System.Threading.Tasks.ValueTask<System.BinaryData> SerializeAsync(object? value, System.Type? inputType = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
554561
}
555562
}

sdk/core/Azure.Core/api/Azure.Core.netstandard2.0.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ protected AsyncPageable(System.Threading.CancellationToken cancellationToken) {
1515
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Never)]
1616
public override string? ToString() { throw null; }
1717
}
18+
public static partial class AzureCoreExtensions
19+
{
20+
public static System.Threading.Tasks.ValueTask<T?> ToObjectAsync<T>(this System.BinaryData data, Azure.Core.Serialization.ObjectSerializer serializer, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
21+
public static T? ToObject<T>(this System.BinaryData data, Azure.Core.Serialization.ObjectSerializer serializer, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
22+
}
1823
public partial class AzureKeyCredential
1924
{
2025
public AzureKeyCredential(string key) { }
@@ -550,6 +555,8 @@ protected ObjectSerializer() { }
550555
public abstract object? Deserialize(System.IO.Stream stream, System.Type returnType, System.Threading.CancellationToken cancellationToken);
551556
public abstract System.Threading.Tasks.ValueTask<object?> DeserializeAsync(System.IO.Stream stream, System.Type returnType, System.Threading.CancellationToken cancellationToken);
552557
public abstract void Serialize(System.IO.Stream stream, object? value, System.Type inputType, System.Threading.CancellationToken cancellationToken);
558+
public virtual System.BinaryData Serialize(object? value, System.Type? inputType = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
553559
public abstract System.Threading.Tasks.ValueTask SerializeAsync(System.IO.Stream stream, object? value, System.Type inputType, System.Threading.CancellationToken cancellationToken);
560+
public virtual System.Threading.Tasks.ValueTask<System.BinaryData> SerializeAsync(object? value, System.Type? inputType = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; }
554561
}
555562
}

sdk/core/Azure.Core/src/Azure.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<PackageReference Include="System.Numerics.Vectors" />
2121
<PackageReference Include="System.Threading.Tasks.Extensions" />
2222
<PackageReference Include="System.Text.Json" />
23+
<PackageReference Include="System.Memory.Data" />
2324
</ItemGroup>
2425

2526
<ItemGroup>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.Diagnostics.CodeAnalysis;
6+
using System.IO;
7+
using System.Threading;
8+
using System.Threading.Tasks;
9+
using Azure.Core;
10+
using Azure.Core.Pipeline;
11+
using Azure.Core.Serialization;
12+
13+
namespace Azure
14+
{
15+
/// <summary>
16+
/// Extensions that can be used for serialization.
17+
/// </summary>
18+
public static class AzureCoreExtensions
19+
{
20+
/// <summary>
21+
/// Converts the <see cref="BinaryData"/> to the specified type using
22+
/// the provided <see cref="ObjectSerializer"/>.
23+
/// </summary>
24+
/// <typeparam name="T">The type that the data should be
25+
/// converted to.</typeparam>
26+
/// <param name="data">The <see cref="BinaryData"/> instance to convert.</param>
27+
/// <param name="serializer">The serializer to use
28+
/// when deserializing the data.</param>
29+
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to use during deserialization.</param>
30+
///<returns>The data converted to the specified type.</returns>
31+
public static T? ToObject<T>(this BinaryData data, ObjectSerializer serializer, CancellationToken cancellationToken = default) =>
32+
(T?)serializer.Deserialize(data.ToStream(), typeof(T), cancellationToken);
33+
34+
/// <summary>
35+
/// Converts the <see cref="BinaryData"/> to the specified type using
36+
/// the provided <see cref="ObjectSerializer"/>.
37+
/// </summary>
38+
/// <typeparam name="T">The type that the data should be
39+
/// converted to.</typeparam>
40+
/// <param name="data">The <see cref="BinaryData"/> instance to convert.</param>
41+
/// <param name="serializer">The serializer to use
42+
/// when deserializing the data.</param>
43+
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to use during deserialization.</param>
44+
///<returns>The data converted to the specified type.</returns>
45+
public static async ValueTask<T?> ToObjectAsync<T>(this BinaryData data, ObjectSerializer serializer, CancellationToken cancellationToken = default) =>
46+
(T?)await serializer.DeserializeAsync(data.ToStream(), typeof(T), cancellationToken).ConfigureAwait(false);
47+
}
48+
}

0 commit comments

Comments
 (0)