forked from dotnet-architecture/eShopOnWeb
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Inspired by dotnet show (dotnet-architecture#386)
* Changed the order of the projects, Web first to infer as startup. * Added encapsulated JSON serialization - and moved to System.Text.Json * Refactored a few minor updates out Co-authored-by: Eric Fleming <eric-fleming18@hotmail.com>
- Loading branch information
1 parent
70a919e
commit 0af21d2
Showing
8 changed files
with
105 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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 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 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,18 @@ | ||
using System.Text.Json; | ||
|
||
namespace Microsoft.eShopWeb | ||
{ | ||
public static class JsonExtensions | ||
{ | ||
private static readonly JsonSerializerOptions _jsonOptions = new JsonSerializerOptions | ||
{ | ||
PropertyNameCaseInsensitive = true | ||
}; | ||
|
||
public static T FromJson<T>(this string json) => | ||
JsonSerializer.Deserialize<T>(json, _jsonOptions); | ||
|
||
public static string ToJson<T>(this T obj) => | ||
JsonSerializer.Serialize<T>(obj, _jsonOptions); | ||
} | ||
} |
This file contains 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 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
36 changes: 36 additions & 0 deletions
36
tests/UnitTests/ApplicationCore/Extensions/JsonExtensions.cs
This file contains 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,36 @@ | ||
using Xunit; | ||
|
||
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Extensions | ||
{ | ||
public class JsonExtensions | ||
{ | ||
[Fact] | ||
public void CorrectlySerializesAndDeserializesObject() | ||
{ | ||
var testParent = new TestParent | ||
{ | ||
Id = 7, | ||
Name = "Test name", | ||
Children = new[] | ||
{ | ||
new TestChild(), | ||
new TestChild(), | ||
new TestChild() | ||
} | ||
}; | ||
|
||
var json = testParent.ToJson(); | ||
var result = json.FromJson<TestParent>(); | ||
Assert.Equal(testParent, result); | ||
} | ||
|
||
[ | ||
Theory, | ||
InlineData("{ \"id\": 9, \"name\": \"Another test\" }", 9, "Another test"), | ||
InlineData("{ \"id\": 3124, \"name\": \"Test Value 1\" }", 3124, "Test Value 1"), | ||
] | ||
public void CorrectlyDeserializesJson(string json, int expectedId, string expectedName) => | ||
Assert.Equal(new TestParent { Id = expectedId, Name = expectedName }, json.FromJson<TestParent>()); | ||
|
||
} | ||
} |
This file contains 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,17 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Extensions | ||
{ | ||
[DebuggerDisplay("Id={Id}, Date={Date}")] | ||
public class TestChild : IEquatable<TestChild> | ||
{ | ||
public Guid Id { get; set; } = Guid.NewGuid(); | ||
|
||
public DateTime Date { get; set; } = DateTime.UtcNow; | ||
|
||
public bool Equals([AllowNull] TestChild other) => | ||
other?.Date == Date && other?.Id == Id; | ||
} | ||
} |
This file contains 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,21 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Linq; | ||
|
||
namespace Microsoft.eShopWeb.UnitTests.ApplicationCore.Extensions | ||
{ | ||
public class TestParent : IEquatable<TestParent> | ||
{ | ||
public int Id { get; set; } | ||
|
||
public string Name { get; set; } | ||
|
||
public IEnumerable<TestChild> Children { get; set; } | ||
|
||
public bool Equals([AllowNull] TestParent other) => | ||
other?.Id == Id && other?.Name == Name && | ||
(other?.Children is null && Children is null || | ||
(other?.Children?.Zip(Children)?.All(t => t.First?.Equals(t.Second) ?? false) ?? false)); | ||
} | ||
} |