-
Notifications
You must be signed in to change notification settings - Fork 5.1k
[Azure.Core] Add NewtonSoft Json example #36453
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nisha-bhatia
merged 7 commits into
Azure:feature/updateSerializer
from
nisha-bhatia:addNewtonSoft
May 30, 2023
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
sdk/core/Azure.Core/tests/ModelSerializationTests/NewtonSoftTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
| using Azure.Core.Serialization; | ||
| using NUnit.Framework; | ||
| using Newtonsoft.Json; | ||
|
|
||
| namespace Azure.Core.Tests.ModelSerializationTests | ||
| { | ||
| internal class NewtonSoftTests | ||
| { | ||
| private readonly SerializableOptions _wireOptions = new SerializableOptions { IgnoreReadOnlyProperties = false }; | ||
| private readonly SerializableOptions _objectOptions = new SerializableOptions(); | ||
|
|
||
| [TestCase(true)] | ||
| [TestCase(false)] | ||
| public void CanRoundTripFutureVersionWithoutLoss(bool ignoreReadOnly) | ||
| { | ||
| Stream stream = new MemoryStream(); | ||
| string serviceResponse = | ||
| "{\"latinName\":\"Animalia\",\"weight\":2.3,\"name\":\"Rabbit\",\"isHungry\":false}"; | ||
|
|
||
| StringBuilder expectedSerialized = new StringBuilder("{"); | ||
| expectedSerialized.Append("\"IsHungry\":false,"); | ||
| expectedSerialized.Append("\"Weight\":2.3,"); | ||
| if (!ignoreReadOnly) | ||
| { | ||
| expectedSerialized.Append("\"LatinName\":\"Animalia\","); | ||
| } | ||
| expectedSerialized.Append("\"Name\":\"Rabbit\""); | ||
| expectedSerialized.Append("}"); | ||
| var expectedSerializedString = expectedSerialized.ToString(); | ||
|
|
||
| SerializableOptions options = new SerializableOptions() { IgnoreReadOnlyProperties = ignoreReadOnly }; | ||
|
|
||
| if (ignoreReadOnly) | ||
| { | ||
| JsonSerializerSettings settings = new JsonSerializerSettings | ||
| { | ||
| ContractResolver = new IgnoreReadOnlyPropertiesResolver() | ||
| }; | ||
| options.Serializer = new NewtonsoftJsonObjectSerializer(settings); | ||
| } | ||
| else | ||
| options.Serializer = new NewtonsoftJsonObjectSerializer(); | ||
|
|
||
| var model = ModelSerializer.Deserialize<Animal>(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 | ||
|
|
||
| stream = ModelSerializer.Serialize<Animal>(model, options); | ||
| stream.Position = 0; | ||
| string roundTrip = new StreamReader(stream).ReadToEnd(); | ||
|
|
||
| #if NET6_0_OR_GREATER | ||
| Assert.That(roundTrip, Is.EqualTo(expectedSerializedString)); | ||
| #endif | ||
|
|
||
| var model2 = ModelSerializer.Deserialize<Animal>(new MemoryStream(Encoding.UTF8.GetBytes(roundTrip)), options: options); | ||
| VerifyModels.CheckAnimals(model, model2, options); | ||
| } | ||
|
|
||
| // Generate a class that implements the NewtonSoft default contract resolver so that ReadOnly properties are not serialized | ||
| // This is used to verify that the ReadOnly properties are not serialized when IgnoreReadOnlyProperties is set to true | ||
| private class IgnoreReadOnlyPropertiesResolver : Newtonsoft.Json.Serialization.DefaultContractResolver | ||
| { | ||
| protected override Newtonsoft.Json.Serialization.JsonProperty CreateProperty(System.Reflection.MemberInfo member, MemberSerialization memberSerialization) | ||
| { | ||
| Newtonsoft.Json.Serialization.JsonProperty property = base.CreateProperty(member, memberSerialization); | ||
|
|
||
| if (!property.Writable) | ||
| { | ||
| property.ShouldSerialize = obj => false; | ||
| } | ||
|
|
||
| return property; | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.