JsonScrutinize 1.0.4

There is a newer version of this package available.
See the version list below for details.
dotnet add package JsonScrutinize --version 1.0.4
                    
NuGet\Install-Package JsonScrutinize -Version 1.0.4
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="JsonScrutinize" Version="1.0.4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="JsonScrutinize" Version="1.0.4" />
                    
Directory.Packages.props
<PackageReference Include="JsonScrutinize" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add JsonScrutinize --version 1.0.4
                    
#r "nuget: JsonScrutinize, 1.0.4"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=JsonScrutinize&version=1.0.4
                    
Install JsonScrutinize as a Cake Addin
#tool nuget:?package=JsonScrutinize&version=1.0.4
                    
Install JsonScrutinize as a Cake Tool

JSONScrutinize

JSONScrutinize is a powerful .NET library designed to sanitize JSON strings by comparing them against a predefined standard class. It recursively traverses the JSON structure to detect mismatches in data types, missing or null keys, and validation errors based on regular expressions.

Features

  • Type Validation:
    Compares each JSON property type with the corresponding property in a standard class. If a type mismatch is found (for example, an integer instead of a string), the key is flagged.

  • Required Keys Check:
    Utilizes the [Required] attribute on the standard class properties. Keys marked as required must be present and non-null in the JSON string.

  • Regex Validation:
    Uses the [RegularExpression("<pattern>")] attribute on the standard class properties to enforce specific formatting rules. If the JSON property value does not match the specified regex, the key is flagged.

  • Nested Objects and Arrays:
    Supports deep validation by recursively traversing nested JSON objects and arrays, ensuring that all levels of the JSON hierarchy are validated against the standard definitions.

Error Codes

JSONScrutinize returns a list of strings where each string is either a key that failed validation or a code indicating the result. The error and success codes include:

  • E6001:
    Description: Indicates that the JSON input is either empty, null, or malformed.
    Message Examples:

    • "JSON cannot be empty or null"
    • JSON parsing error details (e.g., invalid format)
  • E6002:
    Description: Indicates that the provided type for comparison is null or an unexpected exception occurred during processing.
    Message Examples:

    • "Type to compare cannot be null"
    • Exception message details from processing errors
  • S200:
    Description: Indicates successful validation with no mismatches, missing keys, or regex errors detected.

Usage Example

Standard Class Definition

Define your standard class with the appropriate validation attributes:

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

Consider the following JSON string:

{
    "key1": "value1",
    "key2": "123",
    "key3": 123
}

In this example, key3 is an integer, whereas the standard class expects it to be a string.

Code Sample

The following code demonstrates how to sanitize the JSON string:

StandardClass standard = new StandardClass();
string json = "{\"key1\": \"value1\", \"key2\": \"123\", \"key3\": 123}";
Type typeToCheck = standard.GetType();
List<string> result = await JSONScrutinize.JSONScrutinize.SanitizeJSON(json, typeToCheck);

Expected Output

[
    "key3"
]

This output indicates that the value of key3 does not match the expected type defined in the StandardClass.

Nested Objects and Arrays

JSONScrutinize also handles nested JSON structures. Consider the following example:

Standard Class for Nested Objects

public class NestedStandardClass
{
    [Required]
    public string parentKey { get; set; }
    
    public ChildStandardClass child { get; set; }
}

public class ChildStandardClass
{
    [RegularExpression(@"^[A-Za-z]{3}$")]
    public string childKey { get; set; }
}

JSON Input for Nested Objects

{
    "parentKey": "ParentValue",
    "child": {
        "childKey": "AB12"
    }
}

In this JSON, childKey is expected to be a three-letter string (as per the regex ^[A-Za-z]{3}$), but it is provided as "AB12", which does not match the pattern.

Expected Output

[
    "child.childKey"
]

This output indicates that the nested key child.childKey failed validation because its value does not match the required regex pattern.

Conclusion

JSONScrutinize offers a robust and flexible way to ensure the integrity of your JSON data by validating it against a standard class definition. Whether you need to enforce data types, required fields, or specific formats using regex, this library provides a comprehensive solution for JSON sanitization in your .NET applications.

Feel free to contribute, report issues, or suggest improvements on our GitHub repository.

Happy Coding!

Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.0.1 103 2/4/2025
2.0.0 83 2/4/2025
1.0.4 96 2/3/2025
1.0.3 95 2/3/2025
1.0.2 82 2/3/2025
1.0.1 101 2/3/2025
1.0.0 98 2/3/2025