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:
parent
fde9413681
commit
47f1489cbb
@ -14,6 +14,7 @@
|
|||||||
- Added `IEnumerable<T>.Shuffled([Random])`, which wraps `IList<T>.Shuffle([Random])` and returns the result
|
- Added `IEnumerable<T>.Shuffled([Random])`, which wraps `IList<T>.Shuffle([Random])` and returns the result
|
||||||
- Added `T.AsArray()`
|
- Added `T.AsArray()`
|
||||||
- Added `T.AsEnumerable()`
|
- Added `T.AsEnumerable()`
|
||||||
|
- Added `T.ToJson([JsonSerializerOptions])`
|
||||||
- Added `T[].AsReadOnly()`
|
- Added `T[].AsReadOnly()`
|
||||||
- Added `T[].Clear([Range])` and `T[].Clear(int, int)`
|
- Added `T[].Clear([Range])` and `T[].Clear(int, int)`
|
||||||
- Added `T[].Fill(T)` and `T[].Fill(T, int, int)`
|
- Added `T[].Fill(T)` and `T[].Fill(T, int, int)`
|
||||||
@ -138,6 +139,7 @@
|
|||||||
- Added `Stream.Write(uint, [Endian])`
|
- Added `Stream.Write(uint, [Endian])`
|
||||||
- Added `Stream.Write(ulong, [Endian])`
|
- Added `Stream.Write(ulong, [Endian])`
|
||||||
- Added `string.IsPalindrome()`
|
- Added `string.IsPalindrome()`
|
||||||
|
- Added `string.FromJson([JsonSerializerOptions])`
|
||||||
- Added `TimeSpanParser.TryParse` which supersedes `TimeSpanParser.Parse`
|
- Added `TimeSpanParser.TryParse` which supersedes `TimeSpanParser.Parse`
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
|
26
X10D.Tests/src/Text/CoreTests.cs
Normal file
26
X10D.Tests/src/Text/CoreTests.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
29
X10D.Tests/src/Text/StringTests.cs
Normal file
29
X10D.Tests/src/Text/StringTests.cs
Normal 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]);
|
||||||
|
}
|
||||||
|
}
|
@ -1,4 +1,4 @@
|
|||||||
namespace X10D.Core;
|
namespace X10D.Core;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Extension methods which apply to all types.
|
/// Extension methods which apply to all types.
|
||||||
|
21
X10D/src/Text/Extensions.cs
Normal file
21
X10D/src/Text/Extensions.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
23
X10D/src/Text/StringExtensions.cs
Normal file
23
X10D/src/Text/StringExtensions.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user