diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md
index c34eab1..7033e22 100644
--- a/docs/CHANGELOG.md
+++ b/docs/CHANGELOG.md
@@ -111,4 +111,8 @@
### **v1.2.0.0**
-> Add/adjust input validations in the `DataTypeExtensions` foler with extensions;
--> Add new string extensions: `IfNullThenEmpty`.
\ No newline at end of file
+-> Add new string extensions: `IfNullThenEmpty`.
+
+### **v1.3.0.0**
+-> Fix test for `CalculateAge`;
+-> Add new string extensions: `IsValidJson`, `IsValidJsonObject`, `IsValidJsonArray`.
\ No newline at end of file
diff --git a/src/DomainCommonExtensions/DataTypeExtensions/StringExtensions.cs b/src/DomainCommonExtensions/DataTypeExtensions/StringExtensions.cs
index 853774b..3af6aa1 100644
--- a/src/DomainCommonExtensions/DataTypeExtensions/StringExtensions.cs
+++ b/src/DomainCommonExtensions/DataTypeExtensions/StringExtensions.cs
@@ -29,6 +29,7 @@
#if NET || NETSTANDARD2_0_OR_GREATER
using System.Text.Encodings.Web;
+using System.Text.Json;
#endif
using System.Text.RegularExpressions;
@@ -1373,6 +1374,7 @@ public static string AddQueryString(this string url, string name, string value)
return url.AddQueryString(name + "=" + UrlEncoder.Default.Encode(value.IfNullThenEmpty()));
}
#endif
+
///
/// Add hash fragment to URL
///
@@ -1494,5 +1496,83 @@ public static string TrimSuffix(this string source, string suffix, StringCompari
///
///
public static string IfNullThenEmpty(this string source) => source ?? string.Empty;
+
+#if NET || NETSTANDARD2_0_OR_GREATER
+
+ ///
+ /// Check if provided source is as valid JSON
+ ///
+ /// Source string
+ ///
+ ///
+ public static bool IsValidJson(this string source)
+ {
+ try
+ {
+ if (source.IsMissing()) return false;
+ if ((source.TrimIfNotNull().StartsWith("{") && source.TrimIfNotNull().EndsWith("}")) ||
+ (source.TrimIfNotNull().StartsWith("[") && source.TrimIfNotNull().EndsWith("]")))
+ {
+ using var jsonDoc = JsonDocument.Parse(source);
+
+ return true;
+ }
+ else return false;
+ }
+ catch
+ {
+ return false;
+ }
+ }
+
+ ///
+ /// Check if provided source is as valid JSON object
+ ///
+ /// Source string
+ ///
+ ///
+ public static bool IsValidJsonObject(this string source)
+ {
+ try
+ {
+ if (source.IsMissing()) return false;
+ if (source.TrimIfNotNull().StartsWith("{")
+ && source.TrimIfNotNull().EndsWith("}"))
+ {
+ return source.IsValidJson();
+ }
+ else return false;
+ }
+ catch
+ {
+ return false;
+ }
+ }
+
+ ///
+ /// Check if provided source is as valid JSON array
+ ///
+ /// Source string
+ ///
+ ///
+ public static bool IsValidJsonArray(this string source)
+ {
+ try
+ {
+ if (source.IsMissing()) return false;
+ if (source.TrimIfNotNull().StartsWith("[")
+ && source.TrimIfNotNull().EndsWith("]"))
+ {
+ return source.IsValidJson();
+ }
+ else return false;
+ }
+ catch
+ {
+ return false;
+ }
+ }
+
+#endif
}
}
\ No newline at end of file
diff --git a/src/DomainCommonExtensions/DomainCommonExtensions.csproj b/src/DomainCommonExtensions/DomainCommonExtensions.csproj
index 34174c1..f80cdbc 100644
--- a/src/DomainCommonExtensions/DomainCommonExtensions.csproj
+++ b/src/DomainCommonExtensions/DomainCommonExtensions.csproj
@@ -61,6 +61,9 @@
4.7.2
+
+ 4.7.2
+
diff --git a/src/shared/GeneralAssemblyInfo.cs b/src/shared/GeneralAssemblyInfo.cs
index e233b4b..2c8f46a 100644
--- a/src/shared/GeneralAssemblyInfo.cs
+++ b/src/shared/GeneralAssemblyInfo.cs
@@ -47,6 +47,6 @@
[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.MainAssembly)]
#endif
-[assembly: AssemblyVersion("1.2.0.0")]
-[assembly: AssemblyFileVersion("1.2.0.0")]
-[assembly: AssemblyInformationalVersion("1.2.0.0")]
+[assembly: AssemblyVersion("1.3.0.0")]
+[assembly: AssemblyFileVersion("1.3.0.0")]
+[assembly: AssemblyInformationalVersion("1.3.0.0")]
diff --git a/src/tests/DataTypeTests/DateTimeTests.cs b/src/tests/DataTypeTests/DateTimeTests.cs
index 9764433..f87dfbc 100644
--- a/src/tests/DataTypeTests/DateTimeTests.cs
+++ b/src/tests/DataTypeTests/DateTimeTests.cs
@@ -164,8 +164,12 @@ public void CalculateAgeTest()
var date = new DateTime(1994, 8, 8, 13, 0, 0);
var res = date.CalculateAge();
+ var age = DateTime.Now.Date.Day >= date.Day
+ ? DateTime.Now.Year - 1994
+ : (DateTime.Now.Year - 1994) + 1;
+
Assert.IsNotNull(res);
- Assert.IsTrue(res.Equals(29));
+ Assert.IsTrue(res.Equals(age));
}
[TestMethod]
diff --git a/src/tests/DataTypeTests/StringTests.cs b/src/tests/DataTypeTests/StringTests.cs
index db7131f..30e9747 100644
--- a/src/tests/DataTypeTests/StringTests.cs
+++ b/src/tests/DataTypeTests/StringTests.cs
@@ -510,5 +510,30 @@ public void ValidatePullTest()
Assert.IsTrue(result);
}
+
+ [DataRow("{ \"key\": \"value\" }")]
+ [DataRow("[{ \"key\": \"value\" }]")]
+ [DataRow("{ \"key\": 1 }")]
+ [DataRow("[{ \"key\": 1 }]")]
+ [TestMethod]
+ public void IsValidJson_Success_Test(string source)
+ {
+ var result = source.IsValidJson();
+
+ Assert.IsTrue(result);
+ }
+
+ [DataRow("{ \"key\": \"value\"")]
+ [DataRow("{ \"key\": \"value\"")]
+ [DataRow("[{ key: \"value\" }]")]
+ [DataRow("{ \"key\": 1 }]")]
+ [DataRow("\"key\": 1")]
+ [TestMethod]
+ public void IsValidJson_Fail_Test(string source)
+ {
+ var result = source.IsValidJson();
+
+ Assert.IsFalse(result);
+ }
}
}
\ No newline at end of file