From 47f1489cbbf29bb0662300dd9d65ce3d18e98f62 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Fri, 22 Apr 2022 09:42:37 +0100 Subject: [PATCH] Add string.FromJson T.ToJson --- CHANGELOG.md | 2 ++ X10D.Tests/src/Text/CoreTests.cs | 26 ++++++++++++++++++++++++++ X10D.Tests/src/Text/StringTests.cs | 29 +++++++++++++++++++++++++++++ X10D/src/Core/Extensions.cs | 2 +- X10D/src/Text/Extensions.cs | 21 +++++++++++++++++++++ X10D/src/Text/StringExtensions.cs | 23 +++++++++++++++++++++++ 6 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 X10D.Tests/src/Text/CoreTests.cs create mode 100644 X10D.Tests/src/Text/StringTests.cs create mode 100644 X10D/src/Text/Extensions.cs create mode 100644 X10D/src/Text/StringExtensions.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index 7dc6022..fa4bb2c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ - Added `IEnumerable.Shuffled([Random])`, which wraps `IList.Shuffle([Random])` and returns the result - Added `T.AsArray()` - Added `T.AsEnumerable()` +- Added `T.ToJson([JsonSerializerOptions])` - Added `T[].AsReadOnly()` - Added `T[].Clear([Range])` and `T[].Clear(int, int)` - Added `T[].Fill(T)` and `T[].Fill(T, int, int)` @@ -138,6 +139,7 @@ - Added `Stream.Write(uint, [Endian])` - Added `Stream.Write(ulong, [Endian])` - Added `string.IsPalindrome()` +- Added `string.FromJson([JsonSerializerOptions])` - Added `TimeSpanParser.TryParse` which supersedes `TimeSpanParser.Parse` ### Changed diff --git a/X10D.Tests/src/Text/CoreTests.cs b/X10D.Tests/src/Text/CoreTests.cs new file mode 100644 index 0000000..f6d9aea --- /dev/null +++ b/X10D.Tests/src/Text/CoreTests.cs @@ -0,0 +1,26 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using X10D.Text; + +namespace X10D.Tests.Text; + +[TestClass] +public class CoreTests +{ + [TestMethod] + public void ToJsonShouldNotBeEmpty() + { + object? obj = null; + string json = obj.ToJson(); + Assert.IsFalse(string.IsNullOrEmpty(json)); + } + + [TestMethod] + public void ToJsonShouldDeserializeEquivalent() + { + int[] source = Enumerable.Range(1, 100).ToArray(); + string json = source.ToJson(); + int[]? target = json.FromJson(); + CollectionAssert.AreEqual(source, target); + CollectionAssert.AreEquivalent(source, target); + } +} diff --git a/X10D.Tests/src/Text/StringTests.cs b/X10D.Tests/src/Text/StringTests.cs new file mode 100644 index 0000000..794ceeb --- /dev/null +++ b/X10D.Tests/src/Text/StringTests.cs @@ -0,0 +1,29 @@ +using System.Text.Json.Serialization; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using X10D.Text; + +namespace X10D.Tests.Text; + +[TestClass] +public class StringTests +{ + private struct SampleStructure + { + [JsonPropertyName("values")] + public int[] Values { get; set; } + } + + [TestMethod] + public void FromJsonShouldDeserializeCorrectly() + { + const string json = "{\"values\": [1, 2, 3]}"; + var target = json.FromJson(); + Assert.IsInstanceOfType(target, typeof(SampleStructure)); + Assert.IsNotNull(target); + Assert.IsNotNull(target.Values); + Assert.AreEqual(3, target.Values.Length); + Assert.AreEqual(1, target.Values[0]); + Assert.AreEqual(2, target.Values[1]); + Assert.AreEqual(3, target.Values[2]); + } +} diff --git a/X10D/src/Core/Extensions.cs b/X10D/src/Core/Extensions.cs index d2cbc49..ff51a8a 100644 --- a/X10D/src/Core/Extensions.cs +++ b/X10D/src/Core/Extensions.cs @@ -1,4 +1,4 @@ -namespace X10D.Core; +namespace X10D.Core; /// /// Extension methods which apply to all types. diff --git a/X10D/src/Text/Extensions.cs b/X10D/src/Text/Extensions.cs new file mode 100644 index 0000000..7cc6021 --- /dev/null +++ b/X10D/src/Text/Extensions.cs @@ -0,0 +1,21 @@ +using System.Text.Json; + +namespace X10D.Text; + +/// +/// Text-related extension methods for all types. +/// +public static class Extensions +{ + /// + /// Returns a JSON string representation of the specified value. + /// + /// The value to convert. + /// The JSON serialization options. + /// The type of the value to convert. + /// A JSON string representing the object. + public static string ToJson(this T value, JsonSerializerOptions? options = null) + { + return JsonSerializer.Serialize(value, options); + } +} diff --git a/X10D/src/Text/StringExtensions.cs b/X10D/src/Text/StringExtensions.cs new file mode 100644 index 0000000..f4fd6fb --- /dev/null +++ b/X10D/src/Text/StringExtensions.cs @@ -0,0 +1,23 @@ +using System.Text.Json; + +namespace X10D.Text; + +/// +/// Text-related extension methods for . +/// +public static class StringExtensions +{ + /// + /// Returns an object from the specified JSON string. + /// + /// The JSON to convert. + /// The JSON serialization options. + /// The type of the value to deserialize. + /// + /// An object constructed from the JSON string, or if deserialization could not be performed. + /// + public static T? FromJson(this string value, JsonSerializerOptions? options = null) + { + return JsonSerializer.Deserialize(value, options); + } +}