diff --git a/sdk/core/Azure.Core.Experimental/api/Azure.Core.Experimental.netstandard2.0.cs b/sdk/core/Azure.Core.Experimental/api/Azure.Core.Experimental.netstandard2.0.cs index b9c08529bb56..9540ae753f98 100644 --- a/sdk/core/Azure.Core.Experimental/api/Azure.Core.Experimental.netstandard2.0.cs +++ b/sdk/core/Azure.Core.Experimental/api/Azure.Core.Experimental.netstandard2.0.cs @@ -112,16 +112,30 @@ public static partial class BinaryDataExtensions { public static dynamic ToDynamic(this System.BinaryData data) { throw null; } } + public abstract partial class DynamicData + { + protected DynamicData() { } + internal abstract void WriteTo(System.Text.Json.Utf8JsonWriter writer); + public static void WriteTo(System.Text.Json.Utf8JsonWriter writer, Azure.Core.Dynamic.DynamicData data) { } + } [System.Diagnostics.DebuggerDisplayAttribute("{DebuggerDisplay,nq}")] - public partial class JsonData : System.Dynamic.IDynamicMetaObjectProvider, System.IEquatable + public partial class JsonData : Azure.Core.Dynamic.DynamicData, System.Dynamic.IDynamicMetaObjectProvider, System.IEquatable { internal JsonData() { } public bool Equals(Azure.Core.Dynamic.JsonData other) { throw null; } public override bool Equals(object? obj) { throw null; } public override int GetHashCode() { throw null; } + public static bool operator ==(Azure.Core.Dynamic.JsonData? left, bool right) { throw null; } + public static bool operator ==(Azure.Core.Dynamic.JsonData? left, double right) { throw null; } public static bool operator ==(Azure.Core.Dynamic.JsonData? left, int right) { throw null; } + public static bool operator ==(Azure.Core.Dynamic.JsonData? left, long right) { throw null; } + public static bool operator ==(Azure.Core.Dynamic.JsonData? left, float right) { throw null; } public static bool operator ==(Azure.Core.Dynamic.JsonData? left, string? right) { throw null; } + public static bool operator ==(bool left, Azure.Core.Dynamic.JsonData? right) { throw null; } + public static bool operator ==(double left, Azure.Core.Dynamic.JsonData? right) { throw null; } public static bool operator ==(int left, Azure.Core.Dynamic.JsonData? right) { throw null; } + public static bool operator ==(long left, Azure.Core.Dynamic.JsonData? right) { throw null; } + public static bool operator ==(float left, Azure.Core.Dynamic.JsonData? right) { throw null; } public static bool operator ==(string? left, Azure.Core.Dynamic.JsonData? right) { throw null; } public static implicit operator bool (Azure.Core.Dynamic.JsonData json) { throw null; } public static implicit operator double (Azure.Core.Dynamic.JsonData json) { throw null; } @@ -134,9 +148,17 @@ internal JsonData() { } public static implicit operator float? (Azure.Core.Dynamic.JsonData json) { throw null; } public static implicit operator float (Azure.Core.Dynamic.JsonData json) { throw null; } public static implicit operator string (Azure.Core.Dynamic.JsonData json) { throw null; } + public static bool operator !=(Azure.Core.Dynamic.JsonData? left, bool right) { throw null; } + public static bool operator !=(Azure.Core.Dynamic.JsonData? left, double right) { throw null; } public static bool operator !=(Azure.Core.Dynamic.JsonData? left, int right) { throw null; } + public static bool operator !=(Azure.Core.Dynamic.JsonData? left, long right) { throw null; } + public static bool operator !=(Azure.Core.Dynamic.JsonData? left, float right) { throw null; } public static bool operator !=(Azure.Core.Dynamic.JsonData? left, string? right) { throw null; } + public static bool operator !=(bool left, Azure.Core.Dynamic.JsonData? right) { throw null; } + public static bool operator !=(double left, Azure.Core.Dynamic.JsonData? right) { throw null; } public static bool operator !=(int left, Azure.Core.Dynamic.JsonData? right) { throw null; } + public static bool operator !=(long left, Azure.Core.Dynamic.JsonData? right) { throw null; } + public static bool operator !=(float left, Azure.Core.Dynamic.JsonData? right) { throw null; } public static bool operator !=(string? left, Azure.Core.Dynamic.JsonData? right) { throw null; } System.Dynamic.DynamicMetaObject System.Dynamic.IDynamicMetaObjectProvider.GetMetaObject(System.Linq.Expressions.Expression parameter) { throw null; } public override string ToString() { throw null; } diff --git a/sdk/core/Azure.Core.Experimental/src/BinaryDataExtensions.cs b/sdk/core/Azure.Core.Experimental/src/BinaryDataExtensions.cs index a4aa920c03f6..355d8deac7a7 100644 --- a/sdk/core/Azure.Core.Experimental/src/BinaryDataExtensions.cs +++ b/sdk/core/Azure.Core.Experimental/src/BinaryDataExtensions.cs @@ -11,6 +11,7 @@ namespace Azure.Core.Dynamic public static class BinaryDataExtensions { /// + /// Return the content of the BinaryData as a dynamic type. /// public static dynamic ToDynamic(this BinaryData data) { diff --git a/sdk/core/Azure.Core.Experimental/src/DynamicData.cs b/sdk/core/Azure.Core.Experimental/src/DynamicData.cs new file mode 100644 index 000000000000..9b3c64b6561f --- /dev/null +++ b/sdk/core/Azure.Core.Experimental/src/DynamicData.cs @@ -0,0 +1,28 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +using System.Text.Json; + +namespace Azure.Core.Dynamic +{ + /// + /// A dynamic abstraction over content data. Deriving types are implemented + /// for a specific format, such as JSON or XML. + /// + /// This and related types are not intended to be mocked. + /// + public abstract class DynamicData + { + /// + /// Writes the data to the provided writer as a JSON value. + /// + /// The writer to which to write the document. + /// The dynamic data value to write. + public static void WriteTo(Utf8JsonWriter writer, DynamicData data) + { + data.WriteTo(writer); + } + + internal abstract void WriteTo(Utf8JsonWriter writer); + } +} diff --git a/sdk/core/Azure.Core.Experimental/src/JsonData.Operators.cs b/sdk/core/Azure.Core.Experimental/src/JsonData.Operators.cs index 8b2cbfbac0de..817a7c544bb3 100644 --- a/sdk/core/Azure.Core.Experimental/src/JsonData.Operators.cs +++ b/sdk/core/Azure.Core.Experimental/src/JsonData.Operators.cs @@ -49,7 +49,7 @@ public partial class JsonData : IDynamicMetaObjectProvider, IEquatable public static implicit operator double(JsonData json) => json.GetDouble(); /// - /// Converts the value to a + /// Converts the value to a or null. /// /// The value to convert. public static implicit operator bool?(JsonData json) => json.Kind == JsonValueKind.Null ? null : json.GetBoolean(); @@ -78,6 +78,164 @@ public partial class JsonData : IDynamicMetaObjectProvider, IEquatable /// The value to convert. public static implicit operator double?(JsonData json) => json.Kind == JsonValueKind.Null ? null : json.GetDouble(); + /// + /// Returns true if a has the same value as a given bool, + /// and false otherwise. + /// + /// The to compare. + /// The to compare. + /// True if the given JsonData represents the given bool, and false otherwise. + public static bool operator ==(JsonData? left, bool right) + { + if (left is null) + { + return false; + } + + return (left.Kind == JsonValueKind.False || left.Kind == JsonValueKind.True) && + ((bool)left) == right; + } + + /// + /// Returns false if a has the same value as a given bool, + /// and true otherwise. + /// + /// The to compare. + /// The to compare. + /// False if the given JsonData represents the given string, and false otherwise + public static bool operator !=(JsonData? left, bool right) => !(left == right); + + /// + /// Returns true if a has the same value as a given bool, + /// and false otherwise. + /// + /// The to compare. + /// The to compare. + /// True if the given JsonData represents the given bool, and false otherwise. + public static bool operator ==(bool left, JsonData? right) + { + if (right is null) + { + return false; + } + + return (right.Kind == JsonValueKind.False || right.Kind == JsonValueKind.True) && + ((bool)right) == left; + } + + /// + /// Returns false if a has the same value as a given bool, + /// and true otherwise. + /// + /// The to compare. + /// The to compare. + /// False if the given JsonData represents the given bool, and false otherwise + public static bool operator !=(bool left, JsonData? right) => !(left == right); + + /// + /// Returns true if a has the same value as a given int, + /// and false otherwise. + /// + /// The to compare. + /// The to compare. + /// True if the given JsonData represents the given int, and false otherwise. + public static bool operator ==(JsonData? left, int right) + { + if (left is null) + { + return false; + } + + return left.Kind == JsonValueKind.Number && ((int)left) == right; + } + + /// + /// Returns false if a has the same value as a given int, + /// and true otherwise. + /// + /// The to compare. + /// The to compare. + /// False if the given JsonData represents the given string, and false otherwise + public static bool operator !=(JsonData? left, int right) => !(left == right); + + /// + /// Returns true if a has the same value as a given int, + /// and false otherwise. + /// + /// The to compare. + /// The to compare. + /// True if the given JsonData represents the given int, and false otherwise. + public static bool operator ==(int left, JsonData? right) + { + if (right is null) + { + return false; + } + + return right.Kind == JsonValueKind.Number && ((int)right) == left; + } + + /// + /// Returns false if a has the same value as a given int, + /// and true otherwise. + /// + /// The to compare. + /// The to compare. + /// False if the given JsonData represents the given int, and false otherwise + public static bool operator !=(int left, JsonData? right) => !(left == right); + + /// + /// Returns true if a has the same value as a given long, + /// and false otherwise. + /// + /// The to compare. + /// The to compare. + /// True if the given JsonData represents the given long, and false otherwise. + public static bool operator ==(JsonData? left, long right) + { + if (left is null) + { + return false; + } + + return left.Kind == JsonValueKind.Number && ((long)left) == right; + } + + /// + /// Returns false if a has the same value as a given long, + /// and true otherwise. + /// + /// The to compare. + /// The to compare. + /// False if the given JsonData represents the given string, and false otherwise + public static bool operator !=(JsonData? left, long right) => !(left == right); + + /// + /// Returns true if a has the same value as a given long, + /// and false otherwise. + /// + /// The to compare. + /// The to compare. + /// True if the given JsonData represents the given long, and false otherwise. + public static bool operator ==(long left, JsonData? right) + { + if (right is null) + { + return false; + } + + return right.Kind == JsonValueKind.Number && ((long)right) == left; + } + + /// + /// Returns false if a has the same value as a given long, + /// and true otherwise. + /// + /// The to compare. + /// The to compare. + /// False if the given JsonData represents the given long, and false otherwise + public static bool operator !=(long left, JsonData? right) => !(left == right); + /// /// Returns true if a has the same value as a given string, /// and false otherwise. @@ -141,55 +299,107 @@ public partial class JsonData : IDynamicMetaObjectProvider, IEquatable public static bool operator !=(string? left, JsonData? right) => !(left == right); /// - /// Returns true if a has the same value as a given int, + /// Returns true if a has the same value as a given float, /// and false otherwise. /// /// The to compare. - /// The to compare. - /// True if the given JsonData represents the given int, and false otherwise. - public static bool operator ==(JsonData? left, int right) + /// The to compare. + /// True if the given JsonData represents the given float, and false otherwise. + public static bool operator ==(JsonData? left, float right) { if (left is null) { return false; } - return left.Kind == JsonValueKind.Number && ((int)left) == right; + return left.Kind == JsonValueKind.Number && ((float)left) == right; } /// - /// Returns false if a has the same value as a given int, + /// Returns false if a has the same value as a given float, /// and true otherwise. /// /// The to compare. - /// The to compare. + /// The to compare. /// False if the given JsonData represents the given string, and false otherwise - public static bool operator !=(JsonData? left, int right) => !(left == right); + public static bool operator !=(JsonData? left, float right) => !(left == right); /// - /// Returns true if a has the same value as a given int, + /// Returns true if a has the same value as a given float, /// and false otherwise. /// - /// The to compare. + /// The to compare. /// The to compare. - /// True if the given JsonData represents the given int, and false otherwise. - public static bool operator ==(int left, JsonData? right) + /// True if the given JsonData represents the given float, and false otherwise. + public static bool operator ==(float left, JsonData? right) { if (right is null) { return false; } - return right.Kind == JsonValueKind.Number && ((int)right) == left; + return right.Kind == JsonValueKind.Number && ((float)right) == left; } /// - /// Returns false if a has the same value as a given int, + /// Returns false if a has the same value as a given float, /// and true otherwise. /// - /// The to compare. + /// The to compare. /// The to compare. - /// False if the given JsonData represents the given int, and false otherwise - public static bool operator !=(int left, JsonData? right) => !(left == right); + /// False if the given JsonData represents the given float, and false otherwise + public static bool operator !=(float left, JsonData? right) => !(left == right); + + /// + /// Returns true if a has the same value as a given double, + /// and false otherwise. + /// + /// The to compare. + /// The to compare. + /// True if the given JsonData represents the given double, and false otherwise. + public static bool operator ==(JsonData? left, double right) + { + if (left is null) + { + return false; + } + + return left.Kind == JsonValueKind.Number && ((double)left) == right; + } + + /// + /// Returns false if a has the same value as a given double, + /// and true otherwise. + /// + /// The to compare. + /// The to compare. + /// False if the given JsonData represents the given string, and false otherwise + public static bool operator !=(JsonData? left, double right) => !(left == right); + + /// + /// Returns true if a has the same value as a given double, + /// and false otherwise. + /// + /// The to compare. + /// The to compare. + /// True if the given JsonData represents the given double, and false otherwise. + public static bool operator ==(double left, JsonData? right) + { + if (right is null) + { + return false; + } + + return right.Kind == JsonValueKind.Number && ((double)right) == left; + } + + /// + /// Returns false if a has the same value as a given double, + /// and true otherwise. + /// + /// The to compare. + /// The to compare. + /// False if the given JsonData represents the given double, and false otherwise + public static bool operator !=(double left, JsonData? right) => !(left == right); } } diff --git a/sdk/core/Azure.Core.Experimental/src/JsonData.cs b/sdk/core/Azure.Core.Experimental/src/JsonData.cs index 3c4e168d5f66..58b283f9ef0c 100644 --- a/sdk/core/Azure.Core.Experimental/src/JsonData.cs +++ b/sdk/core/Azure.Core.Experimental/src/JsonData.cs @@ -23,7 +23,7 @@ namespace Azure.Core.Dynamic [DebuggerDisplay("{DebuggerDisplay,nq}")] [DebuggerTypeProxy(typeof(JsonDataDebuggerProxy))] [JsonConverter(typeof(JsonConverter))] - public partial class JsonData : IDynamicMetaObjectProvider, IEquatable + public partial class JsonData : DynamicData, IDynamicMetaObjectProvider, IEquatable { private readonly JsonValueKind _kind; private Dictionary? _objectRepresentation; @@ -33,9 +33,9 @@ public partial class JsonData : IDynamicMetaObjectProvider, IEquatable private static readonly JsonSerializerOptions DefaultJsonSerializerOptions = new JsonSerializerOptions(); /// - /// Parses a UTF8 encoded string representing a single JSON value into a . + /// Parses a UTF-8 encoded string representing a single JSON value into a . /// - /// A UTF8 encoded string representing a JSON value. + /// A UTF-8 encoded string representing a JSON value. /// A representation of the value. internal static JsonData Parse(BinaryData utf8Json) { @@ -345,7 +345,7 @@ private float GetFloat() private bool GetBoolean() => (bool)EnsureValue()!; - private void WriteTo(Utf8JsonWriter writer) + internal override void WriteTo(Utf8JsonWriter writer) { switch (_kind) { diff --git a/sdk/core/Azure.Core.Experimental/src/Properties/AssemblyInfo.cs b/sdk/core/Azure.Core.Experimental/src/Properties/AssemblyInfo.cs index 9255d39a7424..a0f33f09c81f 100644 --- a/sdk/core/Azure.Core.Experimental/src/Properties/AssemblyInfo.cs +++ b/sdk/core/Azure.Core.Experimental/src/Properties/AssemblyInfo.cs @@ -6,6 +6,4 @@ [assembly: InternalsVisibleTo("Azure.Core.Experimental.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100d15ddcb29688295338af4b7686603fe614abd555e09efba8fb88ee09e1f7b1ccaeed2e8f823fa9eef3fdd60217fc012ea67d2479751a0b8c087a4185541b851bd8b16f8d91b840e51b1cb0ba6fe647997e57429265e85ef62d565db50a69ae1647d54d7bd855e4db3d8a91510e5bcbd0edfbbecaa20a7bd9ae74593daa7b11b4")] -[assembly: SuppressMessage("Usage", "AZC0014:Avoid using banned types in public API", Justification = "Azure.Core.Experimental ships adapters for System.Text.Json", Scope = "type", Target = "~T:Azure.Core.JsonObjectSerializer")] -[assembly: SuppressMessage("Usage", "AZC0014:Avoid using banned types in public API", Justification = "Azure.Core.Experimental ships adapters for System.Text.Json", Scope = "type", Target = "~T:Azure.Core.GeoJson.GeoJsonConverter")] -[assembly: SuppressMessage("Usage", "AZC0014:Avoid using banned types in public API", Justification = "Azure.Core.Experimental ships adapters for System.Text.Json", Scope = "type", Target = "~T:Azure.Core.DynamicJson")] +[assembly: SuppressMessage("Usage", "AZC0014:Avoid using banned types in public API", Justification = "Azure.Core.Experimental ships adapters for System.Text.Json", Scope = "type", Target = "~T:Azure.Core.Dynamic.DynamicData")] diff --git a/sdk/core/Azure.Core.Experimental/tests/public/JsonDataPublicMutableTests.cs b/sdk/core/Azure.Core.Experimental/tests/public/JsonDataPublicMutableTests.cs index a97ce0f1e39a..7400251ff6da 100644 --- a/sdk/core/Azure.Core.Experimental/tests/public/JsonDataPublicMutableTests.cs +++ b/sdk/core/Azure.Core.Experimental/tests/public/JsonDataPublicMutableTests.cs @@ -34,7 +34,7 @@ public void ExistingObjectPropertiesCanBeAssigned() [TestCaseSource(nameof(PrimitiveValues))] public void NewObjectPropertiesCanBeAssignedWithPrimitive(T value, string expected) { - dynamic json = new BinaryData("{}").ToDynamic(); + dynamic json = JsonDataTestHelpers.CreateEmpty(); json.a = value; Assert.AreEqual(json.ToString(), "{\"a\":" + expected + "}"); @@ -51,7 +51,7 @@ public void PrimitiveValuesCanBeParsedDirectly(T value, string expected) [Test] public void NewObjectPropertiesCanBeAssignedWithArrays() { - dynamic json = new BinaryData("{}").ToDynamic(); + dynamic json = JsonDataTestHelpers.CreateEmpty(); json.a = new object[] { 1, 2, null, "string" }; Assert.AreEqual(json.ToString(), "{\"a\":[1,2,null,\"string\"]}"); @@ -60,7 +60,7 @@ public void NewObjectPropertiesCanBeAssignedWithArrays() [Test] public void NewObjectPropertiesCanBeAssignedWithObject() { - var json = new BinaryData("{}").ToDynamic(); + var json = JsonDataTestHelpers.CreateEmpty(); json.a = new { b = 2 }; Assert.AreEqual(json.ToString(), "{\"a\":{\"b\":2}}"); @@ -69,8 +69,8 @@ public void NewObjectPropertiesCanBeAssignedWithObject() [Test] public void NewObjectPropertiesCanBeAssignedWithObjectIndirectly() { - dynamic json = new BinaryData("{}").ToDynamic(); - dynamic anotherJson = new BinaryData("{}").ToDynamic(); + dynamic json = JsonDataTestHelpers.CreateEmpty(); + dynamic anotherJson = JsonDataTestHelpers.CreateEmpty(); json.a = anotherJson; anotherJson.b = 2; @@ -80,7 +80,7 @@ public void NewObjectPropertiesCanBeAssignedWithObjectIndirectly() [Test] public void NewObjectPropertiesCanBeAssignedWithSerializedObject() { - var json = new BinaryData("{}").ToDynamic(); + var json = JsonDataTestHelpers.CreateEmpty(); json.a = new GeoPoint(1, 2); Assert.AreEqual("{\"a\":{\"type\":\"Point\",\"coordinates\":[1,2]}}", json.ToString()); diff --git a/sdk/core/Azure.Core.Experimental/tests/public/JsonDataPublicTests.cs b/sdk/core/Azure.Core.Experimental/tests/public/JsonDataPublicTests.cs index 9b8b0f9de9a7..37a9506afbe0 100644 --- a/sdk/core/Azure.Core.Experimental/tests/public/JsonDataPublicTests.cs +++ b/sdk/core/Azure.Core.Experimental/tests/public/JsonDataPublicTests.cs @@ -5,7 +5,10 @@ using System.Collections.Generic; using System.Globalization; using Azure.Core.Dynamic; +using System.Text.Json; using NUnit.Framework; +using System.IO; +using System.Threading.Tasks; namespace Azure.Core.Tests.Public { @@ -343,7 +346,24 @@ public void EqualsForObjectsAndArrays() } [Test] - public void OperatorEqualsForInt() + public void OperatorEqualsForBool() + { + dynamic trueJson = new BinaryData("{ \"value\": true }").ToDynamic().value; + dynamic falseJson = new BinaryData("{ \"value\": false }").ToDynamic().value; + + Assert.IsTrue(trueJson == true); + Assert.IsTrue(true == trueJson); + Assert.IsFalse(trueJson != true); + Assert.IsFalse(true != trueJson); + + Assert.IsFalse(falseJson == true); + Assert.IsFalse(true == falseJson); + Assert.IsTrue(falseJson != true); + Assert.IsTrue(true != falseJson); + } + + [Test] + public void OperatorEqualsForInt32() { dynamic fiveJson = new BinaryData("{ \"value\": 5 }").ToDynamic().value; dynamic sixJson = new BinaryData("{ \"value\": 6 }").ToDynamic().value; @@ -359,6 +379,63 @@ public void OperatorEqualsForInt() Assert.IsTrue(5 != sixJson); } + [Test] + public void OperatorEqualsForLong() + { + long max = long.MaxValue; + long min = long.MinValue; + + dynamic maxJson = new BinaryData($"{{ \"value\": { max } }}").ToDynamic().value; + dynamic minJson = new BinaryData($"{{ \"value\": { min } }}").ToDynamic().value; + + Assert.IsTrue(maxJson == max); + Assert.IsTrue(max == maxJson); + Assert.IsFalse(maxJson != max); + Assert.IsFalse(max != maxJson); + + Assert.IsFalse(minJson == max); + Assert.IsFalse(max == minJson); + Assert.IsTrue(minJson != max); + Assert.IsTrue(max != minJson); + } + + [Test] + public void OperatorEqualsForFloat() + { + float half = 0.5f; + + dynamic halfJson = new BinaryData("{ \"value\": 0.5 }").ToDynamic().value; + dynamic fourthJson = new BinaryData("{ \"value\": 0.25 }").ToDynamic().value; + + Assert.IsTrue(halfJson == half); + Assert.IsTrue(half == halfJson); + Assert.IsFalse(halfJson != half); + Assert.IsFalse(half != halfJson); + + Assert.IsFalse(fourthJson == half); + Assert.IsFalse(half == fourthJson); + Assert.IsTrue(fourthJson != half); + Assert.IsTrue(half != fourthJson); + } + [Test] + public void OperatorEqualsForDouble() + { + double half = 0.5; + + dynamic halfJson = new BinaryData("{ \"value\": 0.5 }").ToDynamic().value; + dynamic fourthJson = new BinaryData("{ \"value\": 0.25 }").ToDynamic().value; + + Assert.IsTrue(halfJson == half); + Assert.IsTrue(half == halfJson); + Assert.IsFalse(halfJson != half); + Assert.IsFalse(half != halfJson); + + Assert.IsFalse(fourthJson == half); + Assert.IsFalse(half == fourthJson); + Assert.IsTrue(fourthJson != half); + Assert.IsTrue(half != fourthJson); + } + [Test] public void OperatorEqualsForString() { @@ -389,5 +466,50 @@ public void EqualsForStringNUnit() Assert.That(value, Is.EqualTo("foo")); Assert.That("foo", Is.EqualTo(value)); } + + [Test] + public async Task CanWriteToStream() + { + // Arrange + dynamic json = new BinaryData("{ \"Message\": \"Hi!\", \"Number\": 5 }").ToDynamic(); + + // Act + using var stream = new MemoryStream(); + using (var writer = new Utf8JsonWriter(stream)) + { + DynamicData.WriteTo(writer, json); + } + + // Assert + + // Deserialize to model type to validate value was correctly written to stream. + stream.Position = 0; + + var model = (SampleModel)await JsonSerializer.DeserializeAsync(stream, typeof(SampleModel)); + + Assert.AreEqual("Hi!", model.Message); + Assert.AreEqual(5, model.Number); + } + + [Test] + public async Task CanWriteToStream_JsonSerializer() + { + // Arrange + dynamic json = new BinaryData("{ \"Message\": \"Hi!\", \"Number\": 5 }").ToDynamic(); + + // Act + using var stream = new MemoryStream(); + await JsonSerializer.SerializeAsync(stream, json); + + // Assert + + // Deserialize to model type to validate value was correctly written to stream. + stream.Position = 0; + + var model = (SampleModel)await JsonSerializer.DeserializeAsync(stream, typeof(SampleModel)); + + Assert.AreEqual("Hi!", model.Message); + Assert.AreEqual(5, model.Number); + } } } diff --git a/sdk/core/Azure.Core.Experimental/tests/public/JsonDataTestHelpers.cs b/sdk/core/Azure.Core.Experimental/tests/public/JsonDataTestHelpers.cs index 4ac0da4aecca..66f0a14e7a30 100644 --- a/sdk/core/Azure.Core.Experimental/tests/public/JsonDataTestHelpers.cs +++ b/sdk/core/Azure.Core.Experimental/tests/public/JsonDataTestHelpers.cs @@ -8,6 +8,11 @@ namespace Azure.Core.Tests.Public { public class JsonDataTestHelpers { + public static dynamic CreateEmpty() + { + return BinaryData.FromString("{}").ToDynamic(); + } + public static dynamic CreateFromJson(string json) { return BinaryData.FromString(json).ToDynamic();