Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions sdk/core/Azure.Core/api/Azure.Core.net461.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ public HttpAuthorization(string scheme, string parameter) { }
}
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);
}
Expand Down
2 changes: 2 additions & 0 deletions sdk/core/Azure.Core/api/Azure.Core.net5.0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ public HttpAuthorization(string scheme, string parameter) { }
}
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);
}
Expand Down
2 changes: 2 additions & 0 deletions sdk/core/Azure.Core/api/Azure.Core.net6.0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ public HttpAuthorization(string scheme, string parameter) { }
}
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);
}
Expand Down
2 changes: 2 additions & 0 deletions sdk/core/Azure.Core/api/Azure.Core.netcoreapp2.1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ public HttpAuthorization(string scheme, string parameter) { }
}
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);
}
Expand Down
2 changes: 2 additions & 0 deletions sdk/core/Azure.Core/api/Azure.Core.netstandard2.0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ public HttpAuthorization(string scheme, string parameter) { }
}
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);
}
Expand Down
14 changes: 14 additions & 0 deletions sdk/core/Azure.Core/src/IJsonSerializable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,19 @@ public interface IJsonSerializable
/// <param name="options"></param>
/// <returns></returns>
bool TryDeserialize(Stream stream, out long bytesConsumed, SerializableOptions? options = default);

Comment thread
nisha-bhatia marked this conversation as resolved.
/// <summary>
/// TODO
/// </summary>
/// <param name="stream"></param>
/// <param name="options"></param>
void Serialize(Stream stream, SerializableOptions? options = default);
Comment thread
nisha-bhatia marked this conversation as resolved.

/// <summary>
/// TODO
/// </summary>
/// <param name="stream"></param>
/// <param name="options"></param>
void Deserialize(Stream stream, SerializableOptions? options = default);
}
}
40 changes: 25 additions & 15 deletions sdk/core/Azure.Core/tests/ModelSerializationTests/Animal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,7 @@ public bool TryDeserialize(Stream stream, out long bytesConsumed, SerializableOp
bytesConsumed = 0;
try
{
JsonDocument jsonDocument = JsonDocument.Parse(stream);
var model = DeserializeAnimal(jsonDocument.RootElement, options ?? new SerializableOptions());
this.LatinName = model.LatinName;
this.Weight = model.Weight;
this.IsHungry = model.IsHungry;
this.Name = model.Name;
this.RawData = model.RawData;
Deserialize(stream, options);
bytesConsumed = stream.Length;
return true;
}
Expand All @@ -143,14 +137,7 @@ public bool TrySerialize(Stream stream, out long bytesWritten, SerializableOptio
bytesWritten = 0;
try
{
JsonWriterOptions jsonWriterOptions = new JsonWriterOptions();
if (options.PrettyPrint)
{
jsonWriterOptions.Indented = true;
}
Utf8JsonWriter writer = new Utf8JsonWriter(stream, jsonWriterOptions);
((IUtf8JsonSerializable)this).Write(writer, options ?? new SerializableOptions());
writer.Flush();
Serialize(stream, options);
bytesWritten = stream.Length;
return true;
}
Expand All @@ -159,6 +146,29 @@ public bool TrySerialize(Stream stream, out long bytesWritten, SerializableOptio
return false;
}
}

public void Deserialize(Stream stream, SerializableOptions options = default)
Comment thread
nisha-bhatia marked this conversation as resolved.
{
JsonDocument jsonDocument = JsonDocument.Parse(stream);
var model = DeserializeAnimal(jsonDocument.RootElement, options ?? new SerializableOptions());
this.LatinName = model.LatinName;
this.Weight = model.Weight;
this.IsHungry = model.IsHungry;
this.Name = model.Name;
this.RawData = model.RawData;
}

public void Serialize(Stream stream, SerializableOptions options = default)
{
JsonWriterOptions jsonWriterOptions = new JsonWriterOptions();
if (options.PrettyPrint)
{
jsonWriterOptions.Indented = true;
}
Utf8JsonWriter writer = new Utf8JsonWriter(stream, jsonWriterOptions);
((IUtf8JsonSerializable)this).Write(writer, options ?? new SerializableOptions());
writer.Flush();
}
#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,7 @@ internal static CatReadOnlyProperty DeserializeCatReadOnlyProperty(JsonElement e
bytesConsumed = 0;
try
{
JsonDocument jsonDocument = JsonDocument.Parse(stream);
var model = DeserializeCatReadOnlyProperty(jsonDocument.RootElement, options ?? new SerializableOptions());
this.Weight = model.Weight;
this.IsHungry = model.IsHungry;
this.HasWhiskers = model.HasWhiskers;
this.IsHungry = model.IsHungry;
this.RawData = model.RawData;
Deserialize(stream, options);
bytesConsumed = stream.Length;
return true;
}
Expand All @@ -133,14 +127,7 @@ internal static CatReadOnlyProperty DeserializeCatReadOnlyProperty(JsonElement e
bytesWritten = 0;
try
{
JsonWriterOptions jsonWriterOptions = new JsonWriterOptions();
if (options.PrettyPrint)
{
jsonWriterOptions.Indented = true;
}
Utf8JsonWriter writer = new Utf8JsonWriter(stream, jsonWriterOptions);
((IUtf8JsonSerializable)this).Write(writer, options ?? new SerializableOptions());
writer.Flush();
Serialize(stream, options);
bytesWritten = (int)stream.Length;
return true;
}
Expand All @@ -149,6 +136,29 @@ internal static CatReadOnlyProperty DeserializeCatReadOnlyProperty(JsonElement e
return false;
}
}

public new void Deserialize(Stream stream, SerializableOptions options = default)
{
JsonDocument jsonDocument = JsonDocument.Parse(stream);
var model = DeserializeCatReadOnlyProperty(jsonDocument.RootElement, options ?? new SerializableOptions());
this.Weight = model.Weight;
this.IsHungry = model.IsHungry;
this.HasWhiskers = model.HasWhiskers;
this.IsHungry = model.IsHungry;
this.RawData = model.RawData;
}

public new void Serialize(Stream stream, SerializableOptions options = default)
{
JsonWriterOptions jsonWriterOptions = new JsonWriterOptions();
if (options.PrettyPrint)
{
jsonWriterOptions.Indented = true;
}
Utf8JsonWriter writer = new Utf8JsonWriter(stream, jsonWriterOptions);
((IUtf8JsonSerializable)this).Write(writer, options ?? new SerializableOptions());
writer.Flush();
}
#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,7 @@ internal static DogListProperty DeserializeDogListProperty(JsonElement element,
bytesConsumed = 0;
try
{
JsonDocument jsonDocument = JsonDocument.Parse(stream);
var model = DeserializeDogListProperty(jsonDocument.RootElement, options ?? new SerializableOptions());
this.Name = model.Name;
this.Weight = model.Weight;
this.IsHungry = model.IsHungry;
this.FoodConsumed = model.FoodConsumed;
this.RawData = model.RawData;
Deserialize(stream, options);
bytesConsumed = stream.Length;
return true;
}
Expand All @@ -144,14 +138,7 @@ internal static DogListProperty DeserializeDogListProperty(JsonElement element,
bytesWritten = 0;
try
{
JsonWriterOptions jsonWriterOptions = new JsonWriterOptions();
if (options.PrettyPrint)
{
jsonWriterOptions.Indented = true;
}
Utf8JsonWriter writer = new Utf8JsonWriter(stream, jsonWriterOptions);
((IUtf8JsonSerializable)this).Write(writer, options ?? new SerializableOptions());
writer.Flush();
Serialize(stream, options);
bytesWritten = (int)stream.Length;
return true;
}
Expand All @@ -160,6 +147,29 @@ internal static DogListProperty DeserializeDogListProperty(JsonElement element,
return false;
}
}

public new void Deserialize(Stream stream, SerializableOptions options = default)
{
JsonDocument jsonDocument = JsonDocument.Parse(stream);
var model = DeserializeDogListProperty(jsonDocument.RootElement, options ?? new SerializableOptions());
this.Name = model.Name;
this.Weight = model.Weight;
this.IsHungry = model.IsHungry;
this.FoodConsumed = model.FoodConsumed;
this.RawData = model.RawData;
}

public new void Serialize(Stream stream, SerializableOptions options = default)
{
JsonWriterOptions jsonWriterOptions = new JsonWriterOptions();
if (options.PrettyPrint)
{
jsonWriterOptions.Indented = true;
}
Utf8JsonWriter writer = new Utf8JsonWriter(stream, jsonWriterOptions);
((IUtf8JsonSerializable)this).Write(writer, options ?? new SerializableOptions());
writer.Flush();
}
#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ public void CanRoundTripFutureVersionWithoutLoss(bool ignoreReadOnly, bool ignor
}
Assert.That(model.Name, Is.EqualTo("Doggo"));
Assert.IsFalse(model.IsHungry);
#if NET6_0_OR_GREATER
Assert.That(model.Weight, Is.EqualTo(1.1));
#endif
Assert.That(model.FoodConsumed, Is.EqualTo(new List<string> { "kibble", "egg", "peanut butter" }));

if (!ignoreUnknown)
Expand All @@ -68,17 +70,84 @@ public void CanRoundTripFutureVersionWithoutLoss(bool ignoreReadOnly, bool ignor
model.TrySerialize(stream, out var bytesWritten, options: options);
stream.Position = 0;
string roundTrip = new StreamReader(stream).ReadToEnd();

#if NET6_0_OR_GREATER
Assert.That(roundTrip, Is.EqualTo(expectedSerializedString));
Assert.That(expectedSerialized.Length, Is.EqualTo(bytesWritten));
#endif

var model2 = new DogListProperty();
model2.TryDeserialize(new MemoryStream(Encoding.UTF8.GetBytes(roundTrip)), out bytesConsumed, options: options);
VerifyModels.CheckDogs(model, model2, options);
}

[TestCase(true, true)]
[TestCase(true, false)]
[TestCase(false, true)]
[TestCase(false, false)]
public void CanRoundTripNoTry(bool ignoreReadOnly, bool ignoreUnknown)
{
Stream stream = new MemoryStream();
string serviceResponse =
"{\"latinName\":\"Animalia\",\"weight\":1.1,\"name\":\"Doggo\",\"isHungry\":false,\"foodConsumed\":[\"kibble\",\"egg\",\"peanut butter\"], \"numberOfLegs\":4}";

StringBuilder expectedSerialized = new StringBuilder("{");
if (!ignoreReadOnly)
{
expectedSerialized.Append("\"latinName\":\"Animalia\",");
}
expectedSerialized.Append("\"name\":\"Doggo\",");
expectedSerialized.Append("\"isHungry\":false,");
expectedSerialized.Append("\"weight\":1.1,");
expectedSerialized.Append("\"foodConsumed\":[\"kibble\",\"egg\",\"peanut butter\"]");
if (!ignoreUnknown)
{
expectedSerialized.Append(",\"numberOfLegs\":4");
}
expectedSerialized.Append("}");
var expectedSerializedString = expectedSerialized.ToString();

SerializableOptions options = new SerializableOptions() { IgnoreReadOnlyProperties = ignoreReadOnly, IgnoreAdditionalProperties = ignoreUnknown };

var model = new DogListProperty();
model.Deserialize(new MemoryStream(Encoding.UTF8.GetBytes(serviceResponse)), options: options);

if (!ignoreReadOnly)
{
Assert.That(model.LatinName, Is.EqualTo("Animalia"));
}
Assert.That(model.Name, Is.EqualTo("Doggo"));
Assert.IsFalse(model.IsHungry);
#if NET6_0_OR_GREATER
Assert.That(model.Weight, Is.EqualTo(1.1));
#endif
Assert.That(model.FoodConsumed, Is.EqualTo(new List<string> { "kibble", "egg", "peanut butter" }));

if (!ignoreUnknown)
{
var additionalProperties = typeof(DogListProperty).GetProperty("RawData", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).GetValue(model) as Dictionary<string, BinaryData>;
Assert.AreEqual(1, additionalProperties.Count);
Assert.IsTrue(additionalProperties.ContainsKey("numberOfLegs"));
Assert.IsTrue(additionalProperties["numberOfLegs"].ToString() == "4");
}

model.Serialize(stream, options: options);
stream.Position = 0;
string roundTrip = new StreamReader(stream).ReadToEnd();

#if NET6_0_OR_GREATER
Assert.That(roundTrip, Is.EqualTo(expectedSerializedString));
#endif

var model2 = new DogListProperty();
model2.Deserialize(new MemoryStream(Encoding.UTF8.GetBytes(roundTrip)), options: options);
VerifyModels.CheckDogs(model, model2, options);
}

[Test]
public void PrettyPrint()
{
#if NET6_0_OR_GREATER
DogListProperty model = new DogListProperty("Doggo");

Stream stream = new MemoryStream();
Expand All @@ -100,6 +169,7 @@ public void PrettyPrint()
""";

Assert.AreEqual(VerifyModels.NormalizeNewLines(expectedJson), VerifyModels.NormalizeNewLines(actualJson));
#endif
}
}
}
Loading