Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions JsonHelper.Net.Test/JTokenExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,25 @@ public async Task SelectListWithStringsTest(string json, string path, params str
Assert.That(result, Is.EquivalentTo(items));
}
/// <summary>
/// Array selection test
/// </summary>
[TestCase("{\"field\": [\"a\",\"b\",\"c\"]}", "$.field", "a", "b", "c")]
public async Task SelectArrayWithStringsTest(string json, string path, params string[] items) {
await Console.Out.WriteLineAsync(json);
await Console.Out.WriteLineAsync(path);
await Console.Out.WriteLineAsync(items.Length.ToString());
foreach (var item in items) {
await Console.Out.WriteLineAsync(item);
}
var jtoken = JToken.Parse(json);
var result = jtoken.SelectArray<string>(path);
Assert.That(result, Is.Not.Null);
foreach (var item in result!) {
await Console.Out.WriteLineAsync(item);
}
Assert.That(result, Is.EquivalentTo(items));
}
/// <summary>
/// Date selection test
/// </summary>
[TestCase("{\"field\": \"2024-01-05T00:00:00\"}", "$.field", "2024-01-05T00:00:00.0000000")]
Expand Down
19 changes: 19 additions & 0 deletions JsonHelper.Net.Test/JsonHelperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,25 @@ public async Task SelectListWithStringsTest(string json, string path, params str
Assert.That(result, Is.EquivalentTo(items));
}
/// <summary>
/// Array selection test
/// </summary>
[TestCase("{\"field\": [\"a\",\"b\",\"c\"]}", "$.field", "a", "b", "c")]
public async Task SelectArrayWithStringsTest(string json, string path, params string[] items) {
await Console.Out.WriteLineAsync(json);
await Console.Out.WriteLineAsync(path);
await Console.Out.WriteLineAsync(items.Length.ToString());
foreach (var item in items) {
await Console.Out.WriteLineAsync(item);
}
var jtoken = JToken.Parse(json);
var result = JsonHelper.SelectArray<string>(jtoken, path);
Assert.That(result, Is.Not.Null);
foreach (var item in result!) {
await Console.Out.WriteLineAsync(item);
}
Assert.That(result, Is.EquivalentTo(items));
}
/// <summary>
/// Date selection test
/// </summary>
[TestCase("{\"field\": \"2024-01-05T00:00:00\"}", "$.field", "2024-01-05T00:00:00.0000000")]
Expand Down
22 changes: 22 additions & 0 deletions JsonHelper.Net/JTokenExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,28 @@ public static List<T> SelectListOrThrow<T>(this JToken json, string path) {
return token;
}
/// <summary>
/// Select <see cref="T[]"/>
/// </summary>
/// <param name="json">Initial json token</param>
/// <param name="path">JSON path</param>
/// <returns>Resolved <see cref="T[]"/></returns>
public static T[]? SelectArray<T>(this JToken json, string path) {
return JsonHelper.SelectArray<T>(json, path);
}
/// <summary>
/// Select <see cref="T[]"/> or throw <see cref="JsonHelperException"/>
/// </summary>
/// <param name="json">Initial json token</param>
/// <param name="path">JSON path</param>
/// <returns>Resolved <see cref="T[]"/></returns>
public static T[] SelectArrayOrThrow<T>(this JToken json, string path) {
var token = SelectArray<T>(json, path);
if (token == null) {
throw new JsonHelperException("Failed to select JToken: token is null");
}
return token;
}
/// <summary>
/// Select <see cref="DateTime"/>
/// </summary>
/// <param name="json">Initial json token</param>
Expand Down
28 changes: 27 additions & 1 deletion JsonHelper.Net/JsonHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,33 @@ public static Guid SelectGuidOrThrow(JToken json, string path) {
return value.Value;
}
/// <summary>
/// Select <see cref="DateTime"/>
/// Select <see cref="T[]"/>
/// </summary>
/// <param name="json">Initial json token</param>
/// <param name="path">JSON path</param>
/// <returns>Resolved <see cref="T[]"/></returns>
public static T[]? SelectArray<T>(JToken json, string path) {
var token = Select(json, path, JTokenType.Array);
if (token == null) {
return null;
}
return token.ToObject<T[]>();
}
/// <summary>
/// Select <see cref="T[]"/> or throw <see cref="JsonHelperException"/>
/// </summary>
/// <param name="json">Initial json token</param>
/// <param name="path">JSON path</param>
/// <returns>Resolved <see cref="T[]"/></returns>
public static T[] SelectArrayOrThrow<T>(JToken json, string path) {
var value = SelectArray<T>(json, path);
if (value == null) {
throw new JsonHelperException($"Failed to select an array at {path}");
}
return value;
}
/// <summary>
/// Select <see cref="DateTime"/>
/// </summary>
/// <param name="json">Initial json token</param>
/// <param name="path">JSON Path</param>
Expand Down
Loading