1
0
mirror of https://github.com/oliverbooth/X10D synced 2024-11-09 23:25:43 +00:00

Add string.FromJson T.ToJson

This commit is contained in:
Oliver Booth 2022-04-22 09:42:37 +01:00
parent fde9413681
commit 47f1489cbb
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
6 changed files with 102 additions and 1 deletions

View File

@ -14,6 +14,7 @@
- Added `IEnumerable<T>.Shuffled([Random])`, which wraps `IList<T>.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

View File

@ -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<int[]>();
CollectionAssert.AreEqual(source, target);
CollectionAssert.AreEquivalent(source, target);
}
}

View File

@ -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<SampleStructure>();
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]);
}
}

View File

@ -1,4 +1,4 @@
namespace X10D.Core;
namespace X10D.Core;
/// <summary>
/// Extension methods which apply to all types.

View File

@ -0,0 +1,21 @@
using System.Text.Json;
namespace X10D.Text;
/// <summary>
/// Text-related extension methods for all types.
/// </summary>
public static class Extensions
{
/// <summary>
/// Returns a JSON string representation of the specified value.
/// </summary>
/// <param name="value">The value to convert.</param>
/// <param name="options">The JSON serialization options.</param>
/// <typeparam name="T">The type of the value to convert.</typeparam>
/// <returns>A JSON string representing the object.</returns>
public static string ToJson<T>(this T value, JsonSerializerOptions? options = null)
{
return JsonSerializer.Serialize(value, options);
}
}

View File

@ -0,0 +1,23 @@
using System.Text.Json;
namespace X10D.Text;
/// <summary>
/// Text-related extension methods for <see cref="string" />.
/// </summary>
public static class StringExtensions
{
/// <summary>
/// Returns an object from the specified JSON string.
/// </summary>
/// <param name="value">The JSON to convert.</param>
/// <param name="options">The JSON serialization options.</param>
/// <typeparam name="T">The type of the value to deserialize.</typeparam>
/// <returns>
/// An object constructed from the JSON string, or <see langword="null" /> if deserialization could not be performed.
/// </returns>
public static T? FromJson<T>(this string value, JsonSerializerOptions? options = null)
{
return JsonSerializer.Deserialize<T>(value, options);
}
}