JsonScrutinize 2.0.1
dotnet add package JsonScrutinize --version 2.0.1
NuGet\Install-Package JsonScrutinize -Version 2.0.1
<PackageReference Include="JsonScrutinize" Version="2.0.1" />
paket add JsonScrutinize --version 2.0.1
#r "nuget: JsonScrutinize, 2.0.1"
// Install JsonScrutinize as a Cake Addin #addin nuget:?package=JsonScrutinize&version=2.0.1 // Install JsonScrutinize as a Cake Tool #tool nuget:?package=JsonScrutinize&version=2.0.1
JSONScrutinize
JSONScrutinize is a powerful .NET library designed to validate and scrutinize JSON strings against predefined C# class definitions. It provides deep validation of JSON structures, detecting mismatches in data types, missing or null keys, and enforcing validation rules using attributes like [Required]
and [RegularExpression]
.
Features
- Type Validation: Compares JSON property types with their corresponding C# class property types and flags any mismatches.
- Missing Keys Detection: Identifies missing properties based on
[Required]
attributes. - Null Key Detection: Flags properties with
null
values unless explicitly ignored. - Regex Validation: Ensures properties adhere to specified formats using
[RegularExpression]
attributes. - Nested Objects & Arrays: Recursively validates nested structures, ensuring type integrity at all levels.
Installation
To install JSONScrutinize, add the required package via NuGet:
Install-Package Newtonsoft.Json
Usage
Standard Class Definition
Define a C# class with validation attributes:
using System.ComponentModel.DataAnnotations;
public class StandardClass
{
[Required]
public string key1 { get; set; }
[RegularExpression(@"^[0-9]{3}$")]
public string key2 { get; set; }
public string key3 { get; set; }
}
JSON Input Example
{
"key1": "value1",
"key2": "123",
"key3": 123
}
In this case, key3
should be a string, but an integer is provided, which will be flagged as an error.
Running Validation
using JsonScrutinize;
StandardClass standard = new StandardClass();
string json = "{\"key1\": \"value1\", \"key2\": \"123\", \"key3\": 123}";
Type typeToCheck = standard.GetType();
JsonScrutinizer jsonScrutinize = new JsonScrutinize();
var result = await jsonScrutinize.KeyTypeScrutinize(json, typeToCheck);
Expected Output
{
"StatusCode": "S201",
"Description": "Found 1 mismatched keys in the provided JSON",
"MismatchedKeys": [
"key3"
]
}
This indicates that key3
does not match the expected type.
Nested Objects & Arrays
Standard Class for Nested Objects
public class ParentClass
{
[Required]
public string parentKey { get; set; }
public ChildClass child { get; set; }
}
public class ChildClass
{
[RegularExpression(@"^[A-Za-z]{3}$")]
public string childKey { get; set; }
}
JSON Input for Nested Objects
{
"parentKey": "ParentValue",
"child": {
"childKey": "AB12"
}
}
Expected Output
{
"StatusCode": "S201",
"Description": "Found 1 mismatched keys in the provided JSON",
"MismatchedKeys": [
"child.childKey"
]
}
Status Codes & Their Meanings
Status Code | Meaning |
---|---|
S200 |
Success - No issues found |
S201 |
Type mismatch found |
S202 |
Null keys detected |
S203 |
Missing keys detected |
E6001 |
JSON input is null or empty |
E6002 |
Type to compare is null |
E6005 |
JSON parsing error or unexpected failure |
Additional Validation Functions
Missing Key Check
var missingKeysResult = await jsonScrutinize.KeyMissingScrutinize(json, typeToCheck);
Null Key Check (Ignoring Certain Keys)
var nullKeysResult = await jsonScrutinize.KeyNullScrutinize(json, typeToCheck, "key3");
Version Information
Release Date | Version | Changes Done | Submitted By |
---|---|---|---|
2025-02-04 | v2.0.1 | Changed JsonScrutinize namespace to JsonScrutinizer for better readability | Core Team |
2025-02-04 | v2.0.0 | New release with type validation, null and missing key detection, and regex validation support | Core Team |
2025-02-03 | v1.0.0 | Initial release with type validation only | Core Team |
Conclusion
JSONScrutinize offers a comprehensive way to validate JSON structures against standard class definitions in .NET applications. Whether enforcing data types, required fields, or specific formats, this library ensures data integrity effectively.
For contributions, issues, or improvements, feel free to submit to the GitHub repository.
Happy Coding!
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
-
net8.0
- Newtonsoft.Json (>= 13.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.