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
16 changes: 7 additions & 9 deletions sdk/core/Azure.Core/samples/Serialization.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,17 +109,15 @@ DogListProperty dog = JsonSerializer.Deserialize<DogListProperty>(json);
```

## Using static deserializer

Serialization

```C# Snippet:Static_Serialize
//TODO
```

Deserialization
Serialize would use the Try/Do examples from above. We would use Interface form the Serializable but potentially have static method for Deserialize.
When using Static Deserialize, an empty Model does not have to be created first as we can deserialize directly into a new instance.

```C# Snippet:Static_Deserialize
//TODO
SerializableOptions options = new SerializableOptions() { IgnoreReadOnlyProperties = false, IgnoreAdditionalProperties = false };
string serviceResponse =
"{\"latinName\":\"Animalia\",\"weight\":2.3,\"name\":\"Rabbit\",\"isHungry\":false,\"numberOfLegs\":4}";

Animal model = Animal.StaticDeserialize(new MemoryStream(Encoding.UTF8.GetBytes(serviceResponse)), options: options);
```


8 changes: 8 additions & 0 deletions sdk/core/Azure.Core/tests/ModelSerializationTests/Animal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,5 +170,13 @@ public void Serialize(Stream stream, SerializableOptions options = default)
writer.Flush();
}
#endregion

#region PublicStaticDeserializer
public static Animal StaticDeserialize(Stream stream, SerializableOptions options = default)
{
JsonDocument jsonDocument = JsonDocument.Parse(stream);
return DeserializeAnimal(jsonDocument.RootElement, options ?? new SerializableOptions());
}
#endregion
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Azure;
using Azure.Core.Tests;
using Azure.Core.Tests.ModelSerializationTests;
using NUnit.Framework;

namespace Azure.Core.Tests.ModelSerializationTests
{
public class StaticDeserializerTests
{
private readonly SerializableOptions _wireOptions = new SerializableOptions { IgnoreReadOnlyProperties = false };
private readonly SerializableOptions _objectOptions = new SerializableOptions();

[TestCase(true, true)]
[TestCase(true, false)]
[TestCase(false, true)]
[TestCase(false, false)]
public void CanRoundTripFutureVersionWithoutLoss(bool ignoreReadOnly, bool ignoreUnknown)
{
Stream stream = new MemoryStream();
string serviceResponse =
"{\"latinName\":\"Animalia\",\"weight\":2.3,\"name\":\"Rabbit\",\"isHungry\":false,\"numberOfLegs\":4}";

StringBuilder expectedSerialized = new StringBuilder("{");
if (!ignoreReadOnly)
{
expectedSerialized.Append("\"latinName\":\"Animalia\",");
}
expectedSerialized.Append("\"name\":\"Rabbit\",");
expectedSerialized.Append("\"isHungry\":false,");
expectedSerialized.Append("\"weight\":2.3");
if (!ignoreUnknown)
{
expectedSerialized.Append(",\"numberOfLegs\":4");
}
expectedSerialized.Append("}");
var expectedSerializedString = expectedSerialized.ToString();

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

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

if (!ignoreReadOnly)
{
Assert.That(model.LatinName, Is.EqualTo("Animalia"));
}
Assert.That(model.Name, Is.EqualTo("Rabbit"));
Assert.IsFalse(model.IsHungry);
#if NET6_0_OR_GREATER
Assert.That(model.Weight, Is.EqualTo(2.3));
#endif

if (!ignoreUnknown)
{
var additionalProperties = typeof(Animal).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 = Animal.StaticDeserialize(new MemoryStream(Encoding.UTF8.GetBytes(roundTrip)), options: options);
VerifyModels.CheckAnimals(model, model2, options);
}
}
}
15 changes: 5 additions & 10 deletions sdk/core/Azure.Core/tests/samples/SerializationSamples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,21 +128,16 @@ public void StjDeserialize()
#endregion
}

[Test]
[Ignore("Only verifying that the sample builds")]
public void StaticSerialize()
{
#region Snippet:Static_Serialize
//TODO
#endregion
}

[Test]
[Ignore("Only verifying that the sample builds")]
public void StaticDeserialize()
{
#region Snippet:Static_Deserialize
//TODO
SerializableOptions options = new SerializableOptions() { IgnoreReadOnlyProperties = false, IgnoreAdditionalProperties = false };
string serviceResponse =
"{\"latinName\":\"Animalia\",\"weight\":2.3,\"name\":\"Rabbit\",\"isHungry\":false,\"numberOfLegs\":4}";

Animal model = Animal.StaticDeserialize(new MemoryStream(Encoding.UTF8.GetBytes(serviceResponse)), options: options);
#endregion
}
}
Expand Down