Skip to content

Commit 63c9245

Browse files
committed
Fixed misc issues when getting values from JArray
The idea behind these methods is that they should to call, meaning if you GetInt32(0) and the item isn't found, or the value cannot be converted to a 32-bit integer, the method should return a default value instead of throwing an exception. As this wasn't the case, the implementation has been updated to reflect this. - added check for min and max values - no longer tries to cast values - added additional GetType and TryGetType extension methods - added lots of unit tests - etc.
1 parent a01b5f1 commit 63c9245

36 files changed

+4855
-371
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
using System.Diagnostics.CodeAnalysis;
2+
using Newtonsoft.Json.Linq;
3+
using static Skybrud.Essentials.Json.Newtonsoft.Parsing.JsonTokenUtils;
4+
5+
namespace Skybrud.Essentials.Json.Newtonsoft.Extensions;
6+
7+
public static partial class NewtonsoftJsonArrayExtensions {
8+
9+
/// <summary>
10+
/// Returns the <see cref="bool"/> value of the item at the specified <paramref name="index"/> in the array. If an
11+
/// item at <paramref name="index"/> isn&apos;t found, or the value cannot be converted to a <see cref="bool"/>,
12+
/// <c>0</c> is returned instead.
13+
/// </summary>
14+
/// <param name="array">The parent array.</param>
15+
/// <param name="index">The index of the item.</param>
16+
/// <returns>An instance of <see cref="bool"/> if successful; otherwise, <see langword="false"/>.</returns>
17+
public static bool GetBoolean(this JArray? array, int index) {
18+
return TryParseBoolean(GetToken(array, index), out bool result) && result;
19+
}
20+
21+
/// <summary>
22+
/// Returns the <see cref="bool"/> value of the item at the specified <paramref name="index"/> in the array. If an
23+
/// item at <paramref name="index"/> isn&apos;t found, or the value cannot be converted to a <see cref="bool"/>,
24+
/// <paramref name="fallback"/> is returned instead.
25+
/// </summary>
26+
/// <param name="array">The parent array.</param>
27+
/// <param name="index">The index of the item.</param>
28+
/// <param name="fallback">The fallback value.</param>
29+
/// <returns>An instance of <see cref="bool"/> if successful; otherwise, <paramref name="fallback"/>.</returns>
30+
public static bool? GetBoolean(this JArray? array, int index, bool fallback) {
31+
return TryParseBoolean(GetToken(array, index), out bool result) ? result : fallback;
32+
}
33+
34+
/// <summary>
35+
/// Returns the <see cref="bool"/> value of the item at the specified <paramref name="index"/> in the array. If an
36+
/// item at <paramref name="index"/> isn&apos;t found, or the value cannot be converted to a <see cref="bool"/>,
37+
/// <see langword="null"/> is returned instead.
38+
/// </summary>
39+
/// <param name="array">The parent array.</param>
40+
/// <param name="index">The index of the item.</param>
41+
/// <returns>An instance of <see cref="bool"/> if successful; otherwise, <see langword="null"/>.</returns>
42+
public static bool? GetBooleanOrNull(this JArray? array, int index) {
43+
return TryParseBoolean(GetToken(array, index), out bool result) ? result : null;
44+
}
45+
46+
/// <summary>
47+
/// Returns the <see cref="bool"/> value of the token matching the specified <paramref name="path"/>. If a
48+
/// matching token isn&apos;t found, or the value cannot be converted to a <see cref="bool"/>,
49+
/// <see langword="false"/> is returned instead.
50+
/// </summary>
51+
/// <param name="array">The parent array.</param>
52+
/// <param name="path">A <see cref="string"/> that contains a JPath expression.</param>
53+
/// <returns>An instance of <see cref="bool"/> if successful; otherwise, <see langword="false"/>.</returns>
54+
public static bool GetBooleanByPath(this JArray? array, string path) {
55+
return ParseBoolean(array?.SelectToken(path));
56+
}
57+
58+
/// <summary>
59+
/// Returns the <see cref="bool"/> value of the token matching the specified <paramref name="path"/>. If a
60+
/// matching token isn&apos;t found, or the value cannot be converted to a <see cref="bool"/>, <paramref name="fallback"/> is
61+
/// returned instead.
62+
/// </summary>
63+
/// <param name="array">The parent array.</param>
64+
/// <param name="path">A <see cref="string"/> that contains a JPath expression.</param>
65+
/// <param name="fallback">The fallback value.</param>
66+
/// <returns>An instance of <see cref="bool"/> if successful; otherwise, <paramref name="fallback"/>.</returns>
67+
public static bool GetBooleanByPath(this JArray? array, string path, bool fallback) {
68+
return ParseBoolean(array?.SelectToken(path), fallback);
69+
}
70+
71+
/// <summary>
72+
/// Returns the <see cref="bool"/> value of the token matching the specified <paramref name="path"/>. If a
73+
/// matching token isn&apos;t found, or the value cannot be converted to a <see cref="bool"/>, <see langword="null"/> is
74+
/// returned instead.
75+
/// </summary>
76+
/// <param name="array">The parent array.</param>
77+
/// <param name="path">A <see cref="string"/> that contains a JPath expression.</param>
78+
/// <returns>An instance of <see cref="bool"/> if successful; otherwise, <see langword="null"/>.</returns>
79+
public static bool? GetBooleanByPathOrNull(this JArray? array, string path) {
80+
return ParseBooleanOrNull(array?.SelectToken(path));
81+
}
82+
83+
/// <summary>
84+
/// Attempts to get the <see cref="bool"/> value of the item at the specified <paramref name="index"/> in the array.
85+
/// </summary>
86+
/// <param name="array">The parent array.</param>
87+
/// <param name="index">The index of the item.</param>
88+
/// <param name="result">When this method returns, holds the <see cref="bool"/> value if successful; otherwise, <see langword="false"/>.</param>
89+
/// <returns><see langword="true"/> if a matching item is found, and the value matches a <see cref="bool"/>; otherwise, <see langword="false"/>.</returns>
90+
public static bool TryGetBoolean(this JArray? array, int index, out bool result) {
91+
return TryParseBoolean(GetToken(array, index), out result);
92+
}
93+
94+
/// <summary>
95+
/// Attempts to get the <see cref="bool"/> value of the item at the specified <paramref name="index"/> in the array.
96+
/// </summary>
97+
/// <param name="array">The parent array.</param>
98+
/// <param name="index">The index of the item.</param>
99+
/// <param name="result">When this method returns, holds the <see cref="bool"/> value if successful; otherwise, <see langword="null"/>.</param>
100+
/// <returns><see langword="true"/> if a matching item is found, and the value matches a <see cref="bool"/>; otherwise, <see langword="null"/>.</returns>
101+
public static bool TryGetBoolean(this JArray? array, int index, [NotNullWhen(true)] out bool? result) {
102+
return TryParseBoolean(GetToken(array, index), out result);
103+
}
104+
105+
/// <summary>
106+
/// Attempts to get the <see cref="bool"/> value of the token matching the specified <paramref name="path"/>.
107+
/// </summary>
108+
/// <param name="array">The parent array.</param>
109+
/// <param name="path">A <see cref="string"/> that contains a JPath expression.</param>
110+
/// <param name="result">When this method returns, holds the <see cref="bool"/> value if successful; otherwise, <see langword="false"/>.</param>
111+
/// <returns><see langword="true"/> if a matching token is found, and the value matches a <see cref="bool"/>; otherwise, <see langword="false"/>.</returns>
112+
public static bool TryGetBooleanByPath(this JArray? array, string path, out bool result) {
113+
return TryParseBoolean(array?.SelectToken(path), out result);
114+
}
115+
116+
/// <summary>
117+
/// Attempts to get the <see cref="bool"/> value of the token matching the specified <paramref name="path"/>.
118+
/// </summary>
119+
/// <param name="array">The parent array.</param>
120+
/// <param name="path">A <see cref="string"/> that contains a JPath expression.</param>
121+
/// <param name="result">When this method returns, holds the <see cref="bool"/> value if successful; otherwise, <see langword="null"/>.</param>
122+
/// <returns><see langword="true"/> if a matching token is found, and the value matches a <see cref="bool"/>; otherwise, <see langword="null"/>.</returns>
123+
public static bool TryGetBooleanByPath(this JArray? array, string path, [NotNullWhen(true)] out bool? result) {
124+
return TryParseBoolean(array?.SelectToken(path), out result);
125+
}
126+
127+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
using System.Diagnostics.CodeAnalysis;
2+
using Newtonsoft.Json.Linq;
3+
using static Skybrud.Essentials.Json.Newtonsoft.Parsing.JsonTokenUtils;
4+
5+
namespace Skybrud.Essentials.Json.Newtonsoft.Extensions;
6+
7+
public static partial class NewtonsoftJsonArrayExtensions {
8+
9+
/// <summary>
10+
/// Returns the <see cref="double"/> value of the item at the specified <paramref name="index"/> in the array. If an
11+
/// item at <paramref name="index"/> isn&apos;t found, or the value cannot be converted to a <see cref="double"/>,
12+
/// <c>0</c> is returned instead.
13+
/// </summary>
14+
/// <param name="array">The parent array.</param>
15+
/// <param name="index">The index of the item.</param>
16+
/// <returns>An instance of <see cref="double"/> if successful; otherwise, <c>0</c>.</returns>
17+
public static double GetDouble(this JArray? array, int index) {
18+
return TryParseDouble(GetToken(array, index), out double result) ? result : 0;
19+
}
20+
21+
/// <summary>
22+
/// Returns the <see cref="double"/> value of the item at the specified <paramref name="index"/> in the array. If an
23+
/// item at <paramref name="index"/> isn&apos;t found, or the value cannot be converted to a <see cref="double"/>,
24+
/// <paramref name="fallback"/> is returned instead.
25+
/// </summary>
26+
/// <param name="array">The parent array.</param>
27+
/// <param name="index">The index of the item.</param>
28+
/// <param name="fallback">The fallback value.</param>
29+
/// <returns>An instance of <see cref="double"/> if successful; otherwise, <paramref name="fallback"/>.</returns>
30+
public static double? GetDouble(this JArray? array, int index, double fallback) {
31+
return TryParseDouble(GetToken(array, index), out double result) ? result : fallback;
32+
}
33+
34+
/// <summary>
35+
/// Returns the <see cref="double"/> value of the item at the specified <paramref name="index"/> in the array. If an
36+
/// item at <paramref name="index"/> isn&apos;t found, or the value cannot be converted to a <see cref="double"/>,
37+
/// <see langword="null"/> is returned instead.
38+
/// </summary>
39+
/// <param name="array">The parent array.</param>
40+
/// <param name="index">The index of the item.</param>
41+
/// <returns>An instance of <see cref="double"/> if successful; otherwise, <see langword="null"/>.</returns>
42+
public static double? GetDoubleOrNull(this JArray? array, int index) {
43+
return TryParseDouble(GetToken(array, index), out double result) ? result : null;
44+
}
45+
46+
/// <summary>
47+
/// Returns the <see cref="double"/> value of the token matching the specified <paramref name="path"/>. If a
48+
/// matching token isn&apos;t found, or the value cannot be converted to a <see cref="double"/>, <c>0</c> is
49+
/// returned instead.
50+
/// </summary>
51+
/// <param name="array">The parent array.</param>
52+
/// <param name="path">A <see cref="string"/> that contains a JPath expression.</param>
53+
/// <returns>An instance of <see cref="double"/> if successful; otherwise, <c>0</c>.</returns>
54+
public static double GetDoubleByPath(this JArray? array, string path) {
55+
return ParseDouble(array?.SelectToken(path));
56+
}
57+
58+
/// <summary>
59+
/// Returns the <see cref="double"/> value of the token matching the specified <paramref name="path"/>. If a
60+
/// matching token isn&apos;t found, or the value cannot be converted to a <see cref="double"/>, <paramref name="fallback"/> is
61+
/// returned instead.
62+
/// </summary>
63+
/// <param name="array">The parent array.</param>
64+
/// <param name="path">A <see cref="string"/> that contains a JPath expression.</param>
65+
/// <param name="fallback">The fallback value.</param>
66+
/// <returns>An instance of <see cref="double"/> if successful; otherwise, <paramref name="fallback"/>.</returns>
67+
public static double GetDoubleByPath(this JArray? array, string path, double fallback) {
68+
return ParseDouble(array?.SelectToken(path), fallback);
69+
}
70+
71+
/// <summary>
72+
/// Returns the <see cref="double"/> value of the token matching the specified <paramref name="path"/>. If a
73+
/// matching token isn&apos;t found, or the value cannot be converted to a <see cref="double"/>, <see langword="null"/> is
74+
/// returned instead.
75+
/// </summary>
76+
/// <param name="array">The parent array.</param>
77+
/// <param name="path">A <see cref="string"/> that contains a JPath expression.</param>
78+
/// <returns>An instance of <see cref="double"/> if successful; otherwise, <see langword="null"/>.</returns>
79+
public static double? GetDoubleByPathOrNull(this JArray? array, string path) {
80+
return ParseDoubleOrNull(array?.SelectToken(path));
81+
}
82+
83+
/// <summary>
84+
/// Attempts to get the <see cref="double"/> value of the item at the specified <paramref name="index"/> in the array.
85+
/// </summary>
86+
/// <param name="array">The parent array.</param>
87+
/// <param name="index">The index of the item.</param>
88+
/// <param name="result">When this method returns, holds the <see cref="double"/> value if successful; otherwise, <c>0</c>.</param>
89+
/// <returns><see langword="true"/> if a matching item is found, and the value matches a <see cref="double"/>; otherwise, <c>0</c>.</returns>
90+
public static bool TryGetDouble(this JArray? array, int index, out double result) {
91+
return TryParseDouble(GetToken(array, index), out result);
92+
}
93+
94+
/// <summary>
95+
/// Attempts to get the <see cref="double"/> value of the item at the specified <paramref name="index"/> in the array.
96+
/// </summary>
97+
/// <param name="array">The parent array.</param>
98+
/// <param name="index">The index of the item.</param>
99+
/// <param name="result">When this method returns, holds the <see cref="double"/> value if successful; otherwise, <see langword="null"/>.</param>
100+
/// <returns><see langword="true"/> if a matching item is found, and the value matches a <see cref="double"/>; otherwise, <see langword="null"/>.</returns>
101+
public static bool TryGetDouble(this JArray? array, int index, [NotNullWhen(true)] out double? result) {
102+
return TryParseDouble(GetToken(array, index), out result);
103+
}
104+
105+
/// <summary>
106+
/// Attempts to get the <see cref="double"/> value of the token matching the specified <paramref name="path"/>.
107+
/// </summary>
108+
/// <param name="array">The parent array.</param>
109+
/// <param name="path">A <see cref="string"/> that contains a JPath expression.</param>
110+
/// <param name="result">When this method returns, holds the <see cref="double"/> value if successful; otherwise, <c>0</c>.</param>
111+
/// <returns><see langword="true"/> if a matching token is found, and the value matches a <see cref="double"/>; otherwise, <c>0</c>.</returns>
112+
public static bool TryGetDoubleByPath(this JArray? array, string path, out double result) {
113+
return TryParseDouble(array?.SelectToken(path), out result);
114+
}
115+
116+
/// <summary>
117+
/// Attempts to get the <see cref="double"/> value of the token matching the specified <paramref name="path"/>.
118+
/// </summary>
119+
/// <param name="array">The parent array.</param>
120+
/// <param name="path">A <see cref="string"/> that contains a JPath expression.</param>
121+
/// <param name="result">When this method returns, holds the <see cref="double"/> value if successful; otherwise, <see langword="null"/>.</param>
122+
/// <returns><see langword="true"/> if a matching token is found, and the value matches a <see cref="double"/>; otherwise, <see langword="null"/>.</returns>
123+
public static bool TryGetDoubleByPath(this JArray? array, string path, [NotNullWhen(true)] out double? result) {
124+
return TryParseDouble(array?.SelectToken(path), out result);
125+
}
126+
127+
}

0 commit comments

Comments
 (0)