Skip to content

Commit 743ba7a

Browse files
committed
Added "Get{ValueType}OrNull" and "Get{ValueType}OrNullByPath" extension methods to the "NewtonsoftJsonObjectExtensions" class
1 parent 82b8381 commit 743ba7a

21 files changed

+300
-26
lines changed

src/Skybrud.Essentials/Json/Newtonsoft/Extensions/NewtonsoftJsonObjectExtensions.Boolean.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,18 @@ public static bool GetBoolean(this JObject? json, string propertyName, bool fall
3131
return JsonTokenUtils.GetBoolean(json?[propertyName], fallback);
3232
}
3333

34+
/// <summary>
35+
/// Returns the <see cref="bool"/> value of the property with the specified <paramref name="propertyName"/>. If
36+
/// a matching property can not be found or the value can not be successfully converted to a <see cref="bool"/>
37+
/// value, <see langword="null"/> is returned instead.
38+
/// </summary>
39+
/// <param name="json">The parent JSON object.</param>
40+
/// <param name="propertyName">The name of the property.</param>
41+
/// <returns>An instance of <see cref="bool"/> if successful; otherwise, <see langword="null"/>.</returns>
42+
public static bool? GetBooleanOrNull(this JObject? json, string propertyName) {
43+
return JsonTokenUtils.GetBooleanOrNull(json?[propertyName]);
44+
}
45+
3446
/// <summary>
3547
/// Returns the <see cref="bool"/> value of the token matching the specified <paramref name="path"/>.
3648
/// If a matching property can not be found or the value can not be successfully converted to a
@@ -56,6 +68,18 @@ public static bool GetBooleanByPath(this JObject? json, string path, bool fallba
5668
return JsonTokenUtils.GetBoolean(json?.SelectToken(path), fallback);
5769
}
5870

71+
/// <summary>
72+
/// Returns the <see cref="bool"/> value of the token matching the specified <paramref name="path"/>.
73+
/// If a matching property can not be found or the value can not be successfully converted to a
74+
/// <see cref="bool"/> value, <see langword="null"/> is returned instead.
75+
/// </summary>
76+
/// <param name="json">The parent JSON object.</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? GetBooleanOrNullByPath(this JObject? json, string path) {
80+
return JsonTokenUtils.GetBooleanOrNull(json?.SelectToken(path));
81+
}
82+
5983
/// <summary>
6084
/// Attempts to get a boolean value from the property with the specified <paramref name="propertyName"/>.
6185
/// </summary>

src/Skybrud.Essentials/Json/Newtonsoft/Extensions/NewtonsoftJsonObjectExtensions.Double.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ public static double GetDouble(this JObject? json, string propertyName, double f
4747
return JsonTokenUtils.GetDouble(json?[propertyName], callback);
4848
}
4949

50+
/// <summary>
51+
/// Returns the <see cref="double"/> value of the property with the specified <paramref name="propertyName"/>.
52+
/// If a matching property can not be found or the value can not be successfully converted to a
53+
/// <see cref="double"/> value, <see langword="null"/> is returned instead.
54+
/// </summary>
55+
/// <param name="json">The parent JSON object.</param>
56+
/// <param name="propertyName">The name of the property.</param>
57+
/// <returns>An instance of <see cref="double"/> if successful; otherwise, <see langword="null"/>.</returns>
58+
public static double? GetDoubleOrNull(this JObject? json, string propertyName) {
59+
return JsonTokenUtils.GetDoubleOrNull(json?[propertyName]);
60+
}
61+
5062
/// <summary>
5163
/// Returns the <see cref="double"/> value of the token matching the specified <paramref name="path"/>.
5264
/// If a matching token can not be found or the value can not be successfully converted to a
@@ -86,6 +98,18 @@ public static double GetDoubleByPath(this JObject? json, string path, double fal
8698
return JsonTokenUtils.GetDouble(json?.SelectToken(path), callback);
8799
}
88100

101+
/// <summary>
102+
/// Returns the <see cref="double"/> value of the token matching the specified <paramref name="path"/>.
103+
/// If a matching token can not be found or the value can not be successfully converted to a
104+
/// <see cref="double"/> value,<see langword="null"/> is returned instead.
105+
/// </summary>
106+
/// <param name="json">The parent JSON object.</param>
107+
/// <param name="path">A <see cref="string"/> that contains a JPath expression.</param>
108+
/// <returns>An instance of <see cref="double"/> if successful; otherwise, <see langword="null"/>.</returns>
109+
public static double? GetDoubleOrNullByPath(this JObject? json, string path) {
110+
return JsonTokenUtils.GetDoubleOrNull(json?.SelectToken(path));
111+
}
112+
89113
/// <summary>
90114
/// Attempts to get a <see cref="double"/> value from the property with the specified <paramref name="propertyName"/>.
91115
/// </summary>

src/Skybrud.Essentials/Json/Newtonsoft/Extensions/NewtonsoftJsonObjectExtensions.Float.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ public static float GetFloat(this JObject? json, string propertyName, float fall
4747
return JsonTokenUtils.GetFloat(json?[propertyName], callback);
4848
}
4949

50+
/// <summary>
51+
/// Returns the <see cref="float"/> value of the property with the specified <paramref name="propertyName"/>.
52+
/// If a matching property can not be found or the value can not be successfully converted to a
53+
/// <see cref="float"/> value, <see langword="null"/> is returned instead.
54+
/// </summary>
55+
/// <param name="json">The parent JSON object.</param>
56+
/// <param name="propertyName">The name of the property.</param>
57+
/// <returns>An instance of <see cref="float"/> if successful; otherwise, <see langword="null"/>.</returns>
58+
public static float? GetFloatOrNull(this JObject? json, string propertyName) {
59+
return JsonTokenUtils.GetFloatOrNull(json?[propertyName]);
60+
}
61+
5062
/// <summary>
5163
/// Returns the <see cref="float"/> value of the token matching the specified <paramref name="path"/>.
5264
/// If a matching token can not be found or the value can not be successfully converted to a
@@ -86,6 +98,18 @@ public static float GetFloatByPath(this JObject? json, string path, float fallba
8698
return JsonTokenUtils.GetFloat(json?.SelectToken(path), callback);
8799
}
88100

101+
/// <summary>
102+
/// Returns the <see cref="float"/> value of the token matching the specified <paramref name="path"/>.
103+
/// If a matching token can not be found or the value can not be successfully converted to a
104+
/// <see cref="float"/> value,<see langword="null"/> is returned instead.
105+
/// </summary>
106+
/// <param name="json">The parent JSON object.</param>
107+
/// <param name="path">A <see cref="string"/> that contains a JPath expression.</param>
108+
/// <returns>An instance of <see cref="float"/> if successful; otherwise, <see langword="null"/>.</returns>
109+
public static float? GetFloatOrNullByPath(this JObject? json, string path) {
110+
return JsonTokenUtils.GetFloatOrNull(json?.SelectToken(path));
111+
}
112+
89113
/// <summary>
90114
/// Attempts to get a <see cref="float"/> value from the property with the specified <paramref name="propertyName"/>.
91115
/// </summary>

src/Skybrud.Essentials/Json/Newtonsoft/Extensions/NewtonsoftJsonObjectExtensions.Guid.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ public static Guid GetGuid(this JObject? json, string propertyName, Guid fallbac
4747
return JsonTokenUtils.GetGuid(json?[propertyName], callback);
4848
}
4949

50+
/// <summary>
51+
/// Returns the <see cref="Guid"/> value of the property with the specified <paramref name="propertyName"/>.
52+
/// If a matching property can not be found or the value can not be successfully converted to a
53+
/// <see cref="Guid"/> value, <see langword="null"/> is returned instead.
54+
/// </summary>
55+
/// <param name="json">The parent JSON object.</param>
56+
/// <param name="propertyName">The name of the property.</param>
57+
/// <returns>An instance of <see cref="Guid"/> if successful; otherwise, <see langword="null"/>.</returns>
58+
public static Guid? GetGuidOrNull(this JObject? json, string propertyName) {
59+
return JsonTokenUtils.GetGuidOrNull(json?[propertyName]);
60+
}
61+
5062
/// <summary>
5163
/// Returns the <see cref="Guid"/> value of the token matching the specified <paramref name="path"/>. If
5264
/// a matching token can not be found or the value can not be successfully converted to an <see cref="Guid"/>
@@ -87,6 +99,18 @@ public static Guid GetGuidByPath(this JObject? json, string path, Guid fallback)
8799
return JsonTokenUtils.GetGuid(json?.SelectToken(path), callback);
88100
}
89101

102+
/// <summary>
103+
/// Returns the <see cref="Guid"/> value of the token matching the specified <paramref name="path"/>.
104+
/// If a matching token can not be found or the value can not be successfully converted to a
105+
/// <see cref="Guid"/> value,<see langword="null"/> is returned instead.
106+
/// </summary>
107+
/// <param name="json">The parent JSON object.</param>
108+
/// <param name="path">A <see cref="string"/> that contains a JPath expression.</param>
109+
/// <returns>An instance of <see cref="Guid"/> if successful; otherwise, <see langword="null"/>.</returns>
110+
public static Guid? GetGuidOrNullByPath(this JObject? json, string path) {
111+
return JsonTokenUtils.GetGuidOrNull(json?.SelectToken(path));
112+
}
113+
90114
/// <summary>
91115
/// Attempts to get an <see cref="Guid"/> value from the property with the specified <paramref name="propertyName"/>.
92116
/// </summary>

src/Skybrud.Essentials/Json/Newtonsoft/Extensions/NewtonsoftJsonObjectExtensions.Int16.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ public static short GetInt16(this JObject? json, string propertyName, short fall
4747
return JsonTokenUtils.GetInt16(json?[propertyName], callback);
4848
}
4949

50+
/// <summary>
51+
/// Returns the <see cref="short"/> value of the property with the specified <paramref name="propertyName"/>.
52+
/// If a matching property can not be found or the value can not be successfully converted to a
53+
/// <see cref="short"/> value, <see langword="null"/> is returned instead.
54+
/// </summary>
55+
/// <param name="json">The parent JSON object.</param>
56+
/// <param name="propertyName">The name of the property.</param>
57+
/// <returns>An instance of <see cref="short"/> if successful; otherwise, <see langword="null"/>.</returns>
58+
public static short? GetInt16OrNull(this JObject? json, string propertyName) {
59+
return JsonTokenUtils.GetInt16OrNull(json?[propertyName]);
60+
}
61+
5062
/// <summary>
5163
/// Returns the <see cref="short"/> value of the token matching the specified <paramref name="path"/>.
5264
/// If a matching token can not be found or the value can not be successfully converted to a
@@ -86,6 +98,18 @@ public static short GetInt16ByPath(this JObject? json, string path, short fallba
8698
return JsonTokenUtils.GetInt16(json?.SelectToken(path), callback);
8799
}
88100

101+
/// <summary>
102+
/// Returns the <see cref="short"/> value of the token matching the specified <paramref name="path"/>.
103+
/// If a matching token can not be found or the value can not be successfully converted to a
104+
/// <see cref="short"/> value,<see langword="null"/> is returned instead.
105+
/// </summary>
106+
/// <param name="json">The parent JSON object.</param>
107+
/// <param name="path">A <see cref="string"/> that contains a JPath expression.</param>
108+
/// <returns>An instance of <see cref="short"/> if successful; otherwise, <see langword="null"/>.</returns>
109+
public static short? GetInt16OrNullByPath(this JObject? json, string path) {
110+
return JsonTokenUtils.GetInt16OrNull(json?.SelectToken(path));
111+
}
112+
89113
/// <summary>
90114
/// Attempts to get a <see cref="short"/> value from the property with the specified <paramref name="propertyName"/>.
91115
/// </summary>

src/Skybrud.Essentials/Json/Newtonsoft/Extensions/NewtonsoftJsonObjectExtensions.Int32.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ public static int GetInt32(this JObject? json, string propertyName, int fallback
4747
return JsonTokenUtils.GetInt32(json?[propertyName], callback);
4848
}
4949

50+
/// <summary>
51+
/// Returns the <see cref="int"/> value of the property with the specified <paramref name="propertyName"/>.
52+
/// If a matching property can not be found or the value can not be successfully converted to a
53+
/// <see cref="int"/> value, <see langword="null"/> is returned instead.
54+
/// </summary>
55+
/// <param name="json">The parent JSON object.</param>
56+
/// <param name="propertyName">The name of the property.</param>
57+
/// <returns>An instance of <see cref="int"/> if successful; otherwise, <see langword="null"/>.</returns>
58+
public static int? GetInt32OrNull(this JObject? json, string propertyName) {
59+
return JsonTokenUtils.GetInt32OrNull(json?[propertyName]);
60+
}
61+
5062
/// <summary>
5163
/// Returns the <see cref="int"/> value of the token matching the specified <paramref name="path"/>.
5264
/// If a matching token can not be found or the value can not be successfully converted to a
@@ -86,6 +98,18 @@ public static int GetInt32ByPath(this JObject? json, string path, int fallback)
8698
return JsonTokenUtils.GetInt32(json?.SelectToken(path), callback);
8799
}
88100

101+
/// <summary>
102+
/// Returns the <see cref="int"/> value of the token matching the specified <paramref name="path"/>.
103+
/// If a matching token can not be found or the value can not be successfully converted to a
104+
/// <see cref="int"/> value,<see langword="null"/> is returned instead.
105+
/// </summary>
106+
/// <param name="json">The parent JSON object.</param>
107+
/// <param name="path">A <see cref="string"/> that contains a JPath expression.</param>
108+
/// <returns>An instance of <see cref="int"/> if successful; otherwise, <see langword="null"/>.</returns>
109+
public static int? GetInt32OrDefaultByNull(this JObject? json, string path) {
110+
return JsonTokenUtils.GetInt32OrNull(json?.SelectToken(path));
111+
}
112+
89113
/// <summary>
90114
/// Attempts to get an <see cref="int"/> value from the property with the specified <paramref name="propertyName"/>.
91115
/// </summary>

src/Skybrud.Essentials/Json/Newtonsoft/Extensions/NewtonsoftJsonObjectExtensions.Int64.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ public static long GetInt64(this JObject? json, string propertyName, long fallba
4747
return JsonTokenUtils.GetInt64(json?[propertyName], callback);
4848
}
4949

50+
/// <summary>
51+
/// Returns the <see cref="long"/> value of the property with the specified <paramref name="propertyName"/>.
52+
/// If a matching property can not be found or the value can not be successfully converted to a
53+
/// <see cref="long"/> value, <see langword="null"/> is returned instead.
54+
/// </summary>
55+
/// <param name="json">The parent JSON object.</param>
56+
/// <param name="propertyName">The name of the property.</param>
57+
/// <returns>An instance of <see cref="long"/> if successful; otherwise, <see langword="null"/>.</returns>
58+
public static long? GetInt64OrNull(this JObject? json, string propertyName) {
59+
return JsonTokenUtils.GetInt64OrNull(json?[propertyName]);
60+
}
61+
5062
/// <summary>
5163
/// Returns the <see cref="long"/> value of the token matching the specified <paramref name="path"/>.
5264
/// If a matching token can not be found or the value can not be successfully converted to a
@@ -86,6 +98,18 @@ public static long GetInt64ByPath(this JObject? json, string path, long fallback
8698
return JsonTokenUtils.GetInt64(json?.SelectToken(path), callback);
8799
}
88100

101+
/// <summary>
102+
/// Returns the <see cref="long"/> value of the token matching the specified <paramref name="path"/>.
103+
/// If a matching token can not be found or the value can not be successfully converted to a
104+
/// <see cref="long"/> value,<see langword="null"/> is returned instead.
105+
/// </summary>
106+
/// <param name="json">The parent JSON object.</param>
107+
/// <param name="path">A <see cref="string"/> that contains a JPath expression.</param>
108+
/// <returns>An instance of <see cref="long"/> if successful; otherwise, <see langword="null"/>.</returns>
109+
public static long? GetInt64OrNullByPath(this JObject? json, string path) {
110+
return JsonTokenUtils.GetInt64OrNull(json?.SelectToken(path));
111+
}
112+
89113
/// <summary>
90114
/// Attempts to get a <see cref="long"/> value from the property with the specified <paramref name="propertyName"/>.
91115
/// </summary>

src/Skybrud.Essentials/Json/Newtonsoft/Extensions/NewtonsoftJsonObjectExtensions.UInt16.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ public static ushort GetUInt16(this JObject? json, string propertyName, ushort f
4747
return JsonTokenUtils.GetUInt16(json?[propertyName], callback);
4848
}
4949

50+
/// <summary>
51+
/// Returns the <see cref="ushort"/> value of the property with the specified <paramref name="propertyName"/>.
52+
/// If a matching property can not be found or the value can not be successfully converted to a
53+
/// <see cref="ushort"/> value, <see langword="null"/> is returned instead.
54+
/// </summary>
55+
/// <param name="json">The parent JSON object.</param>
56+
/// <param name="propertyName">The name of the property.</param>
57+
/// <returns>An instance of <see cref="ushort"/> if successful; otherwise, <see langword="null"/>.</returns>
58+
public static ushort? GetUInt16OrNull(this JObject? json, string propertyName) {
59+
return JsonTokenUtils.GetUInt16OrNull(json?[propertyName]);
60+
}
61+
5062
/// <summary>
5163
/// Returns the <see cref="ushort"/> value of the token matching the specified <paramref name="path"/>.
5264
/// If a matching token can not be found or the value can not be successfully converted to a
@@ -86,6 +98,18 @@ public static ushort GetUInt16ByPath(this JObject? json, string path, ushort fal
8698
return JsonTokenUtils.GetUInt16(json?.SelectToken(path), callback);
8799
}
88100

101+
/// <summary>
102+
/// Returns the <see cref="ushort"/> value of the token matching the specified <paramref name="path"/>.
103+
/// If a matching token can not be found or the value can not be successfully converted to a
104+
/// <see cref="ushort"/> value,<see langword="null"/> is returned instead.
105+
/// </summary>
106+
/// <param name="json">The parent JSON object.</param>
107+
/// <param name="path">A <see cref="string"/> that contains a JPath expression.</param>
108+
/// <returns>An instance of <see cref="ushort"/> if successful; otherwise, <see langword="null"/>.</returns>
109+
public static ushort? GetUInt16OrNullByPath(this JObject? json, string path) {
110+
return JsonTokenUtils.GetUInt16OrNull(json?.SelectToken(path));
111+
}
112+
89113
/// <summary>
90114
/// Attempts to get an <see cref="ushort"/> value from the property with the specified <paramref name="propertyName"/>.
91115
/// </summary>

0 commit comments

Comments
 (0)