diff --git a/sdk/core/Azure.Core/api/Azure.Core.net461.cs b/sdk/core/Azure.Core/api/Azure.Core.net461.cs index 3de8fe7ed73a..09f8421a1b0d 100644 --- a/sdk/core/Azure.Core/api/Azure.Core.net461.cs +++ b/sdk/core/Azure.Core/api/Azure.Core.net461.cs @@ -89,13 +89,6 @@ public HttpAuthorization(string scheme, string parameter) { } public static bool operator !=(Azure.HttpRange left, Azure.HttpRange right) { throw null; } public override string ToString() { throw null; } } - public partial interface IJsonSerializable - { - void Deserialize(System.IO.Stream stream, Azure.SerializableOptions? options = null); - void Serialize(System.IO.Stream stream, Azure.SerializableOptions? options = null); - bool TryDeserialize(System.IO.Stream stream, out long bytesConsumed, Azure.SerializableOptions? options = null); - bool TrySerialize(System.IO.Stream stream, out long bytesWritten, Azure.SerializableOptions? options = null); - } public partial class JsonPatchDocument { public JsonPatchDocument() { } @@ -278,13 +271,6 @@ protected Response() { } public override int GetHashCode() { throw null; } public static implicit operator T (Azure.Response response) { throw null; } } - public partial class SerializableOptions - { - public SerializableOptions() { } - public bool IgnoreAdditionalProperties { get { throw null; } set { } } - public bool IgnoreReadOnlyProperties { get { throw null; } set { } } - public bool PrettyPrint { get { throw null; } set { } } - } public partial class SyncAsyncEventArgs : System.EventArgs { public SyncAsyncEventArgs(bool isRunningSynchronously, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { } @@ -1078,6 +1064,13 @@ public ServerCertificateCustomValidationArgs(System.Security.Cryptography.X509Ce } namespace Azure.Core.Serialization { + public partial interface IJsonSerializable + { + void Deserialize(System.IO.Stream stream, Azure.Core.Serialization.SerializableOptions? options = null); + void Serialize(System.IO.Stream stream, Azure.Core.Serialization.SerializableOptions? options = null); + bool TryDeserialize(System.IO.Stream stream, out long bytesConsumed, Azure.Core.Serialization.SerializableOptions? options = null); + bool TrySerialize(System.IO.Stream stream, out long bytesWritten, Azure.Core.Serialization.SerializableOptions? options = null); + } public partial interface IMemberNameConverter { string? ConvertMemberName(System.Reflection.MemberInfo member); @@ -1095,6 +1088,12 @@ public override void Serialize(System.IO.Stream stream, object? value, System.Ty public override System.Threading.Tasks.ValueTask SerializeAsync(System.IO.Stream stream, object? value, System.Type inputType, System.Threading.CancellationToken cancellationToken) { throw null; } public override System.Threading.Tasks.ValueTask SerializeAsync(object? value, System.Type? inputType = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } } + public static partial class ModelSerializer + { + public static T Deserialize(System.IO.Stream stream, Azure.Core.Serialization.SerializableOptions? options = null) where T : Azure.Core.Serialization.IJsonSerializable, new() { throw null; } + public static T Deserialize(string json, Azure.Core.Serialization.SerializableOptions? options = null) where T : Azure.Core.Serialization.IJsonSerializable, new() { throw null; } + public static System.IO.Stream Serialize(T model, Azure.Core.Serialization.SerializableOptions? options = null) where T : Azure.Core.Serialization.IJsonSerializable, new() { throw null; } + } public abstract partial class ObjectSerializer { protected ObjectSerializer() { } @@ -1105,6 +1104,13 @@ protected ObjectSerializer() { } public abstract System.Threading.Tasks.ValueTask SerializeAsync(System.IO.Stream stream, object? value, System.Type inputType, System.Threading.CancellationToken cancellationToken); public virtual System.Threading.Tasks.ValueTask SerializeAsync(object? value, System.Type? inputType = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } } + public partial class SerializableOptions + { + public SerializableOptions() { } + public bool IgnoreAdditionalProperties { get { throw null; } set { } } + public bool IgnoreReadOnlyProperties { get { throw null; } set { } } + public bool PrettyPrint { get { throw null; } set { } } + } } namespace Azure.Messaging { diff --git a/sdk/core/Azure.Core/api/Azure.Core.net5.0.cs b/sdk/core/Azure.Core/api/Azure.Core.net5.0.cs index 0f4e3f5d08bc..e6a9068c396c 100644 --- a/sdk/core/Azure.Core/api/Azure.Core.net5.0.cs +++ b/sdk/core/Azure.Core/api/Azure.Core.net5.0.cs @@ -89,13 +89,6 @@ public HttpAuthorization(string scheme, string parameter) { } public static bool operator !=(Azure.HttpRange left, Azure.HttpRange right) { throw null; } public override string ToString() { throw null; } } - public partial interface IJsonSerializable - { - void Deserialize(System.IO.Stream stream, Azure.SerializableOptions? options = null); - void Serialize(System.IO.Stream stream, Azure.SerializableOptions? options = null); - bool TryDeserialize(System.IO.Stream stream, out long bytesConsumed, Azure.SerializableOptions? options = null); - bool TrySerialize(System.IO.Stream stream, out long bytesWritten, Azure.SerializableOptions? options = null); - } public partial class JsonPatchDocument { public JsonPatchDocument() { } @@ -278,13 +271,6 @@ protected Response() { } public override int GetHashCode() { throw null; } public static implicit operator T (Azure.Response response) { throw null; } } - public partial class SerializableOptions - { - public SerializableOptions() { } - public bool IgnoreAdditionalProperties { get { throw null; } set { } } - public bool IgnoreReadOnlyProperties { get { throw null; } set { } } - public bool PrettyPrint { get { throw null; } set { } } - } public partial class SyncAsyncEventArgs : System.EventArgs { public SyncAsyncEventArgs(bool isRunningSynchronously, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { } @@ -1078,6 +1064,13 @@ public ServerCertificateCustomValidationArgs(System.Security.Cryptography.X509Ce } namespace Azure.Core.Serialization { + public partial interface IJsonSerializable + { + void Deserialize(System.IO.Stream stream, Azure.Core.Serialization.SerializableOptions? options = null); + void Serialize(System.IO.Stream stream, Azure.Core.Serialization.SerializableOptions? options = null); + bool TryDeserialize(System.IO.Stream stream, out long bytesConsumed, Azure.Core.Serialization.SerializableOptions? options = null); + bool TrySerialize(System.IO.Stream stream, out long bytesWritten, Azure.Core.Serialization.SerializableOptions? options = null); + } public partial interface IMemberNameConverter { string? ConvertMemberName(System.Reflection.MemberInfo member); @@ -1095,6 +1088,12 @@ public override void Serialize(System.IO.Stream stream, object? value, System.Ty public override System.Threading.Tasks.ValueTask SerializeAsync(System.IO.Stream stream, object? value, System.Type inputType, System.Threading.CancellationToken cancellationToken) { throw null; } public override System.Threading.Tasks.ValueTask SerializeAsync(object? value, System.Type? inputType = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } } + public static partial class ModelSerializer + { + public static T Deserialize(System.IO.Stream stream, Azure.Core.Serialization.SerializableOptions? options = null) where T : Azure.Core.Serialization.IJsonSerializable, new() { throw null; } + public static T Deserialize(string json, Azure.Core.Serialization.SerializableOptions? options = null) where T : Azure.Core.Serialization.IJsonSerializable, new() { throw null; } + public static System.IO.Stream Serialize(T model, Azure.Core.Serialization.SerializableOptions? options = null) where T : Azure.Core.Serialization.IJsonSerializable, new() { throw null; } + } public abstract partial class ObjectSerializer { protected ObjectSerializer() { } @@ -1105,6 +1104,13 @@ protected ObjectSerializer() { } public abstract System.Threading.Tasks.ValueTask SerializeAsync(System.IO.Stream stream, object? value, System.Type inputType, System.Threading.CancellationToken cancellationToken); public virtual System.Threading.Tasks.ValueTask SerializeAsync(object? value, System.Type? inputType = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } } + public partial class SerializableOptions + { + public SerializableOptions() { } + public bool IgnoreAdditionalProperties { get { throw null; } set { } } + public bool IgnoreReadOnlyProperties { get { throw null; } set { } } + public bool PrettyPrint { get { throw null; } set { } } + } } namespace Azure.Messaging { diff --git a/sdk/core/Azure.Core/api/Azure.Core.net6.0.cs b/sdk/core/Azure.Core/api/Azure.Core.net6.0.cs index 0f4e3f5d08bc..e6a9068c396c 100644 --- a/sdk/core/Azure.Core/api/Azure.Core.net6.0.cs +++ b/sdk/core/Azure.Core/api/Azure.Core.net6.0.cs @@ -89,13 +89,6 @@ public HttpAuthorization(string scheme, string parameter) { } public static bool operator !=(Azure.HttpRange left, Azure.HttpRange right) { throw null; } public override string ToString() { throw null; } } - public partial interface IJsonSerializable - { - void Deserialize(System.IO.Stream stream, Azure.SerializableOptions? options = null); - void Serialize(System.IO.Stream stream, Azure.SerializableOptions? options = null); - bool TryDeserialize(System.IO.Stream stream, out long bytesConsumed, Azure.SerializableOptions? options = null); - bool TrySerialize(System.IO.Stream stream, out long bytesWritten, Azure.SerializableOptions? options = null); - } public partial class JsonPatchDocument { public JsonPatchDocument() { } @@ -278,13 +271,6 @@ protected Response() { } public override int GetHashCode() { throw null; } public static implicit operator T (Azure.Response response) { throw null; } } - public partial class SerializableOptions - { - public SerializableOptions() { } - public bool IgnoreAdditionalProperties { get { throw null; } set { } } - public bool IgnoreReadOnlyProperties { get { throw null; } set { } } - public bool PrettyPrint { get { throw null; } set { } } - } public partial class SyncAsyncEventArgs : System.EventArgs { public SyncAsyncEventArgs(bool isRunningSynchronously, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { } @@ -1078,6 +1064,13 @@ public ServerCertificateCustomValidationArgs(System.Security.Cryptography.X509Ce } namespace Azure.Core.Serialization { + public partial interface IJsonSerializable + { + void Deserialize(System.IO.Stream stream, Azure.Core.Serialization.SerializableOptions? options = null); + void Serialize(System.IO.Stream stream, Azure.Core.Serialization.SerializableOptions? options = null); + bool TryDeserialize(System.IO.Stream stream, out long bytesConsumed, Azure.Core.Serialization.SerializableOptions? options = null); + bool TrySerialize(System.IO.Stream stream, out long bytesWritten, Azure.Core.Serialization.SerializableOptions? options = null); + } public partial interface IMemberNameConverter { string? ConvertMemberName(System.Reflection.MemberInfo member); @@ -1095,6 +1088,12 @@ public override void Serialize(System.IO.Stream stream, object? value, System.Ty public override System.Threading.Tasks.ValueTask SerializeAsync(System.IO.Stream stream, object? value, System.Type inputType, System.Threading.CancellationToken cancellationToken) { throw null; } public override System.Threading.Tasks.ValueTask SerializeAsync(object? value, System.Type? inputType = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } } + public static partial class ModelSerializer + { + public static T Deserialize(System.IO.Stream stream, Azure.Core.Serialization.SerializableOptions? options = null) where T : Azure.Core.Serialization.IJsonSerializable, new() { throw null; } + public static T Deserialize(string json, Azure.Core.Serialization.SerializableOptions? options = null) where T : Azure.Core.Serialization.IJsonSerializable, new() { throw null; } + public static System.IO.Stream Serialize(T model, Azure.Core.Serialization.SerializableOptions? options = null) where T : Azure.Core.Serialization.IJsonSerializable, new() { throw null; } + } public abstract partial class ObjectSerializer { protected ObjectSerializer() { } @@ -1105,6 +1104,13 @@ protected ObjectSerializer() { } public abstract System.Threading.Tasks.ValueTask SerializeAsync(System.IO.Stream stream, object? value, System.Type inputType, System.Threading.CancellationToken cancellationToken); public virtual System.Threading.Tasks.ValueTask SerializeAsync(object? value, System.Type? inputType = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } } + public partial class SerializableOptions + { + public SerializableOptions() { } + public bool IgnoreAdditionalProperties { get { throw null; } set { } } + public bool IgnoreReadOnlyProperties { get { throw null; } set { } } + public bool PrettyPrint { get { throw null; } set { } } + } } namespace Azure.Messaging { diff --git a/sdk/core/Azure.Core/api/Azure.Core.netcoreapp2.1.cs b/sdk/core/Azure.Core/api/Azure.Core.netcoreapp2.1.cs index 3de8fe7ed73a..09f8421a1b0d 100644 --- a/sdk/core/Azure.Core/api/Azure.Core.netcoreapp2.1.cs +++ b/sdk/core/Azure.Core/api/Azure.Core.netcoreapp2.1.cs @@ -89,13 +89,6 @@ public HttpAuthorization(string scheme, string parameter) { } public static bool operator !=(Azure.HttpRange left, Azure.HttpRange right) { throw null; } public override string ToString() { throw null; } } - public partial interface IJsonSerializable - { - void Deserialize(System.IO.Stream stream, Azure.SerializableOptions? options = null); - void Serialize(System.IO.Stream stream, Azure.SerializableOptions? options = null); - bool TryDeserialize(System.IO.Stream stream, out long bytesConsumed, Azure.SerializableOptions? options = null); - bool TrySerialize(System.IO.Stream stream, out long bytesWritten, Azure.SerializableOptions? options = null); - } public partial class JsonPatchDocument { public JsonPatchDocument() { } @@ -278,13 +271,6 @@ protected Response() { } public override int GetHashCode() { throw null; } public static implicit operator T (Azure.Response response) { throw null; } } - public partial class SerializableOptions - { - public SerializableOptions() { } - public bool IgnoreAdditionalProperties { get { throw null; } set { } } - public bool IgnoreReadOnlyProperties { get { throw null; } set { } } - public bool PrettyPrint { get { throw null; } set { } } - } public partial class SyncAsyncEventArgs : System.EventArgs { public SyncAsyncEventArgs(bool isRunningSynchronously, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { } @@ -1078,6 +1064,13 @@ public ServerCertificateCustomValidationArgs(System.Security.Cryptography.X509Ce } namespace Azure.Core.Serialization { + public partial interface IJsonSerializable + { + void Deserialize(System.IO.Stream stream, Azure.Core.Serialization.SerializableOptions? options = null); + void Serialize(System.IO.Stream stream, Azure.Core.Serialization.SerializableOptions? options = null); + bool TryDeserialize(System.IO.Stream stream, out long bytesConsumed, Azure.Core.Serialization.SerializableOptions? options = null); + bool TrySerialize(System.IO.Stream stream, out long bytesWritten, Azure.Core.Serialization.SerializableOptions? options = null); + } public partial interface IMemberNameConverter { string? ConvertMemberName(System.Reflection.MemberInfo member); @@ -1095,6 +1088,12 @@ public override void Serialize(System.IO.Stream stream, object? value, System.Ty public override System.Threading.Tasks.ValueTask SerializeAsync(System.IO.Stream stream, object? value, System.Type inputType, System.Threading.CancellationToken cancellationToken) { throw null; } public override System.Threading.Tasks.ValueTask SerializeAsync(object? value, System.Type? inputType = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } } + public static partial class ModelSerializer + { + public static T Deserialize(System.IO.Stream stream, Azure.Core.Serialization.SerializableOptions? options = null) where T : Azure.Core.Serialization.IJsonSerializable, new() { throw null; } + public static T Deserialize(string json, Azure.Core.Serialization.SerializableOptions? options = null) where T : Azure.Core.Serialization.IJsonSerializable, new() { throw null; } + public static System.IO.Stream Serialize(T model, Azure.Core.Serialization.SerializableOptions? options = null) where T : Azure.Core.Serialization.IJsonSerializable, new() { throw null; } + } public abstract partial class ObjectSerializer { protected ObjectSerializer() { } @@ -1105,6 +1104,13 @@ protected ObjectSerializer() { } public abstract System.Threading.Tasks.ValueTask SerializeAsync(System.IO.Stream stream, object? value, System.Type inputType, System.Threading.CancellationToken cancellationToken); public virtual System.Threading.Tasks.ValueTask SerializeAsync(object? value, System.Type? inputType = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } } + public partial class SerializableOptions + { + public SerializableOptions() { } + public bool IgnoreAdditionalProperties { get { throw null; } set { } } + public bool IgnoreReadOnlyProperties { get { throw null; } set { } } + public bool PrettyPrint { get { throw null; } set { } } + } } namespace Azure.Messaging { diff --git a/sdk/core/Azure.Core/api/Azure.Core.netstandard2.0.cs b/sdk/core/Azure.Core/api/Azure.Core.netstandard2.0.cs index 3de8fe7ed73a..09f8421a1b0d 100644 --- a/sdk/core/Azure.Core/api/Azure.Core.netstandard2.0.cs +++ b/sdk/core/Azure.Core/api/Azure.Core.netstandard2.0.cs @@ -89,13 +89,6 @@ public HttpAuthorization(string scheme, string parameter) { } public static bool operator !=(Azure.HttpRange left, Azure.HttpRange right) { throw null; } public override string ToString() { throw null; } } - public partial interface IJsonSerializable - { - void Deserialize(System.IO.Stream stream, Azure.SerializableOptions? options = null); - void Serialize(System.IO.Stream stream, Azure.SerializableOptions? options = null); - bool TryDeserialize(System.IO.Stream stream, out long bytesConsumed, Azure.SerializableOptions? options = null); - bool TrySerialize(System.IO.Stream stream, out long bytesWritten, Azure.SerializableOptions? options = null); - } public partial class JsonPatchDocument { public JsonPatchDocument() { } @@ -278,13 +271,6 @@ protected Response() { } public override int GetHashCode() { throw null; } public static implicit operator T (Azure.Response response) { throw null; } } - public partial class SerializableOptions - { - public SerializableOptions() { } - public bool IgnoreAdditionalProperties { get { throw null; } set { } } - public bool IgnoreReadOnlyProperties { get { throw null; } set { } } - public bool PrettyPrint { get { throw null; } set { } } - } public partial class SyncAsyncEventArgs : System.EventArgs { public SyncAsyncEventArgs(bool isRunningSynchronously, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { } @@ -1078,6 +1064,13 @@ public ServerCertificateCustomValidationArgs(System.Security.Cryptography.X509Ce } namespace Azure.Core.Serialization { + public partial interface IJsonSerializable + { + void Deserialize(System.IO.Stream stream, Azure.Core.Serialization.SerializableOptions? options = null); + void Serialize(System.IO.Stream stream, Azure.Core.Serialization.SerializableOptions? options = null); + bool TryDeserialize(System.IO.Stream stream, out long bytesConsumed, Azure.Core.Serialization.SerializableOptions? options = null); + bool TrySerialize(System.IO.Stream stream, out long bytesWritten, Azure.Core.Serialization.SerializableOptions? options = null); + } public partial interface IMemberNameConverter { string? ConvertMemberName(System.Reflection.MemberInfo member); @@ -1095,6 +1088,12 @@ public override void Serialize(System.IO.Stream stream, object? value, System.Ty public override System.Threading.Tasks.ValueTask SerializeAsync(System.IO.Stream stream, object? value, System.Type inputType, System.Threading.CancellationToken cancellationToken) { throw null; } public override System.Threading.Tasks.ValueTask SerializeAsync(object? value, System.Type? inputType = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } } + public static partial class ModelSerializer + { + public static T Deserialize(System.IO.Stream stream, Azure.Core.Serialization.SerializableOptions? options = null) where T : Azure.Core.Serialization.IJsonSerializable, new() { throw null; } + public static T Deserialize(string json, Azure.Core.Serialization.SerializableOptions? options = null) where T : Azure.Core.Serialization.IJsonSerializable, new() { throw null; } + public static System.IO.Stream Serialize(T model, Azure.Core.Serialization.SerializableOptions? options = null) where T : Azure.Core.Serialization.IJsonSerializable, new() { throw null; } + } public abstract partial class ObjectSerializer { protected ObjectSerializer() { } @@ -1105,6 +1104,13 @@ protected ObjectSerializer() { } public abstract System.Threading.Tasks.ValueTask SerializeAsync(System.IO.Stream stream, object? value, System.Type inputType, System.Threading.CancellationToken cancellationToken); public virtual System.Threading.Tasks.ValueTask SerializeAsync(object? value, System.Type? inputType = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) { throw null; } } + public partial class SerializableOptions + { + public SerializableOptions() { } + public bool IgnoreAdditionalProperties { get { throw null; } set { } } + public bool IgnoreReadOnlyProperties { get { throw null; } set { } } + public bool PrettyPrint { get { throw null; } set { } } + } } namespace Azure.Messaging { diff --git a/sdk/core/Azure.Core/samples/Serialization.md b/sdk/core/Azure.Core/samples/Serialization.md index cb3d696f919c..ec6de802c393 100644 --- a/sdk/core/Azure.Core/samples/Serialization.md +++ b/sdk/core/Azure.Core/samples/Serialization.md @@ -98,14 +98,24 @@ DogListProperty dog = new DogListProperty Weight = 1.1, FoodConsumed = { "kibble", "egg", "peanut butter" }, }; + +//stj example string json = JsonSerializer.Serialize(dog); + +//modelSerializer example +Stream stream = ModelSerializer.Serialize(dog); ``` Deserialization ```C# Snippet:Stj_Deserialize string json = "{\"latinName\":\"Animalia\",\"weight\":1.1,\"name\":\"Doggo\",\"isHungry\":false,\"foodConsumed\":[\"kibble\",\"egg\",\"peanut butter\"],\"numberOfLegs\":4}"; + +//stj example DogListProperty dog = JsonSerializer.Deserialize(json); + +//modelSerializer example +DogListProperty dog2 = ModelSerializer.Deserialize(json); ``` ## Using static deserializer diff --git a/sdk/core/Azure.Core/src/IJsonSerializable.cs b/sdk/core/Azure.Core/src/Serialization/IJsonSerializable.cs similarity index 97% rename from sdk/core/Azure.Core/src/IJsonSerializable.cs rename to sdk/core/Azure.Core/src/Serialization/IJsonSerializable.cs index be6686354ad0..9205e3e92df7 100644 --- a/sdk/core/Azure.Core/src/IJsonSerializable.cs +++ b/sdk/core/Azure.Core/src/Serialization/IJsonSerializable.cs @@ -4,7 +4,7 @@ using System; using System.IO; -namespace Azure +namespace Azure.Core.Serialization { /// /// TODO diff --git a/sdk/core/Azure.Core/src/Serialization/ModelSerializer.cs b/sdk/core/Azure.Core/src/Serialization/ModelSerializer.cs new file mode 100644 index 000000000000..b6e780335cfb --- /dev/null +++ b/sdk/core/Azure.Core/src/Serialization/ModelSerializer.cs @@ -0,0 +1,61 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System.IO; + +namespace Azure.Core.Serialization +{ + /// + /// Serializer class for Azure models. + /// + public static class ModelSerializer + { + /// + /// Serailize a model. + /// + /// + /// + /// + /// + public static Stream Serialize(T model, SerializableOptions? options = default) where T : IJsonSerializable, new() + { + IJsonSerializable serializable = (model ??= new T()) as IJsonSerializable; + Stream stream = new MemoryStream(); + serializable.Serialize(stream, options); + return stream; + } + + /// + /// Deserialize a model. + /// + /// + /// + /// + /// + public static T Deserialize(Stream stream, SerializableOptions? options = default) where T : IJsonSerializable, new() + { + IJsonSerializable serializable = new T(); + serializable.Deserialize(stream, options); + return (T)serializable; + } + + /// + /// Deserialize a model. + /// + /// + /// + /// + /// + public static T Deserialize(string json, SerializableOptions? options = default) where T : IJsonSerializable, new() + { + using Stream stream = new MemoryStream(); + using StreamWriter writer = new StreamWriter(stream); + writer.Write(json); + stream.Position = 0; + + IJsonSerializable serializable = new T(); + serializable.Deserialize(stream, options); + return (T)serializable; + } + } +} diff --git a/sdk/core/Azure.Core/src/SerializableOptions.cs b/sdk/core/Azure.Core/src/Serialization/SerializableOptions.cs similarity index 95% rename from sdk/core/Azure.Core/src/SerializableOptions.cs rename to sdk/core/Azure.Core/src/Serialization/SerializableOptions.cs index cd00da019dca..71e274375b3a 100644 --- a/sdk/core/Azure.Core/src/SerializableOptions.cs +++ b/sdk/core/Azure.Core/src/Serialization/SerializableOptions.cs @@ -1,7 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. -namespace Azure +namespace Azure.Core.Serialization { /// /// Provides the client options for serializing models. diff --git a/sdk/core/Azure.Core/tests/ModelSerializationTests/Animal.cs b/sdk/core/Azure.Core/tests/ModelSerializationTests/Animal.cs index 08e62e3c3d19..a4612d4257ed 100644 --- a/sdk/core/Azure.Core/tests/ModelSerializationTests/Animal.cs +++ b/sdk/core/Azure.Core/tests/ModelSerializationTests/Animal.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.IO; using System.Text.Json; +using Azure.Core.Serialization; namespace Azure.Core.Tests.ModelSerializationTests { diff --git a/sdk/core/Azure.Core/tests/ModelSerializationTests/CatReadOnlyProperty.cs b/sdk/core/Azure.Core/tests/ModelSerializationTests/CatReadOnlyProperty.cs index b6b197091c44..aa949de67c6e 100644 --- a/sdk/core/Azure.Core/tests/ModelSerializationTests/CatReadOnlyProperty.cs +++ b/sdk/core/Azure.Core/tests/ModelSerializationTests/CatReadOnlyProperty.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.IO; using System.Text.Json; +using Azure.Core.Serialization; namespace Azure.Core.Tests.ModelSerializationTests { diff --git a/sdk/core/Azure.Core/tests/ModelSerializationTests/DogListProperty.cs b/sdk/core/Azure.Core/tests/ModelSerializationTests/DogListProperty.cs index e2261644bf13..19c6943b6542 100644 --- a/sdk/core/Azure.Core/tests/ModelSerializationTests/DogListProperty.cs +++ b/sdk/core/Azure.Core/tests/ModelSerializationTests/DogListProperty.cs @@ -7,6 +7,7 @@ using System.Linq; using System.Text.Json; using System.Text.Json.Serialization; +using Azure.Core.Serialization; namespace Azure.Core.Tests.ModelSerializationTests { diff --git a/sdk/core/Azure.Core/tests/ModelSerializationTests/IUtf8JsonSerializable.cs b/sdk/core/Azure.Core/tests/ModelSerializationTests/IUtf8JsonSerializable.cs index 7341cb8d88fa..2837f7a70218 100644 --- a/sdk/core/Azure.Core/tests/ModelSerializationTests/IUtf8JsonSerializable.cs +++ b/sdk/core/Azure.Core/tests/ModelSerializationTests/IUtf8JsonSerializable.cs @@ -2,6 +2,7 @@ // Licensed under the MIT License. using System.Text.Json; +using Azure.Core.Serialization; namespace Azure.Core.Tests.ModelSerializationTests { diff --git a/sdk/core/Azure.Core/tests/ModelSerializationTests/ListPropertyTests.cs b/sdk/core/Azure.Core/tests/ModelSerializationTests/ListPropertyTests.cs index 734d55265a25..f4438f966edf 100644 --- a/sdk/core/Azure.Core/tests/ModelSerializationTests/ListPropertyTests.cs +++ b/sdk/core/Azure.Core/tests/ModelSerializationTests/ListPropertyTests.cs @@ -6,6 +6,7 @@ using System.IO; using System.Text; using Azure; +using Azure.Core.Serialization; using Azure.Core.Tests; using Azure.Core.Tests.ModelSerializationTests; using NUnit.Framework; diff --git a/sdk/core/Azure.Core/tests/ModelSerializationTests/ReadOnlyPropertyTests.cs b/sdk/core/Azure.Core/tests/ModelSerializationTests/ReadOnlyPropertyTests.cs index 756ca60b884a..bf6bea5f6fb1 100644 --- a/sdk/core/Azure.Core/tests/ModelSerializationTests/ReadOnlyPropertyTests.cs +++ b/sdk/core/Azure.Core/tests/ModelSerializationTests/ReadOnlyPropertyTests.cs @@ -6,6 +6,7 @@ using System.IO; using System.Text; using Azure; +using Azure.Core.Serialization; using Azure.Core.Tests; using Azure.Core.Tests.ModelSerializationTests; using NUnit.Framework; diff --git a/sdk/core/Azure.Core/tests/ModelSerializationTests/StaticDeserializerTests.cs b/sdk/core/Azure.Core/tests/ModelSerializationTests/StaticDeserializerTests.cs index feb1c19ed71a..455b470ca26e 100644 --- a/sdk/core/Azure.Core/tests/ModelSerializationTests/StaticDeserializerTests.cs +++ b/sdk/core/Azure.Core/tests/ModelSerializationTests/StaticDeserializerTests.cs @@ -6,6 +6,7 @@ using System.IO; using System.Text; using Azure; +using Azure.Core.Serialization; using Azure.Core.Tests; using Azure.Core.Tests.ModelSerializationTests; using NUnit.Framework; diff --git a/sdk/core/Azure.Core/tests/ModelSerializationTests/VerifyModels.cs b/sdk/core/Azure.Core/tests/ModelSerializationTests/VerifyModels.cs index 7811e13dc71b..2f6780b6fbb9 100644 --- a/sdk/core/Azure.Core/tests/ModelSerializationTests/VerifyModels.cs +++ b/sdk/core/Azure.Core/tests/ModelSerializationTests/VerifyModels.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; +using Azure.Core.Serialization; using NUnit.Framework; namespace Azure.Core.Tests.ModelSerializationTests diff --git a/sdk/core/Azure.Core/tests/samples/SerializationSamples.cs b/sdk/core/Azure.Core/tests/samples/SerializationSamples.cs index adb24beba439..89121197a2fe 100644 --- a/sdk/core/Azure.Core/tests/samples/SerializationSamples.cs +++ b/sdk/core/Azure.Core/tests/samples/SerializationSamples.cs @@ -9,6 +9,7 @@ using System.Text.Json; using System.Threading.Tasks; using Azure.Core.Experimental.Tests; +using Azure.Core.Serialization; using Azure.Core.TestFramework; using Azure.Core.Tests.ModelSerializationTests; using Azure.Identity; @@ -114,7 +115,12 @@ public void StjSerialize() Weight = 1.1, FoodConsumed = { "kibble", "egg", "peanut butter" }, }; + + //stj example string json = JsonSerializer.Serialize(dog); + + //modelSerializer example + Stream stream = ModelSerializer.Serialize(dog); #endregion } @@ -124,7 +130,12 @@ public void StjDeserialize() { #region Snippet:Stj_Deserialize string json = "{\"latinName\":\"Animalia\",\"weight\":1.1,\"name\":\"Doggo\",\"isHungry\":false,\"foodConsumed\":[\"kibble\",\"egg\",\"peanut butter\"],\"numberOfLegs\":4}"; + + //stj example DogListProperty dog = JsonSerializer.Deserialize(json); + + //modelSerializer example + DogListProperty dog2 = ModelSerializer.Deserialize(json); #endregion }