JSONBuddyClassLibrary 1.1.5
dotnet add package JSONBuddyClassLibrary --version 1.1.5
NuGet\Install-Package JSONBuddyClassLibrary -Version 1.1.5
<PackageReference Include="JSONBuddyClassLibrary" Version="1.1.5" />
<PackageVersion Include="JSONBuddyClassLibrary" Version="1.1.5" />
<PackageReference Include="JSONBuddyClassLibrary" />
paket add JSONBuddyClassLibrary --version 1.1.5
#r "nuget: JSONBuddyClassLibrary, 1.1.5"
#:package JSONBuddyClassLibrary@1.1.5
#addin nuget:?package=JSONBuddyClassLibrary&version=1.1.5
#tool nuget:?package=JSONBuddyClassLibrary&version=1.1.5
JSONBuddyLibrary
JSONBuddyLibrary
Overview
JSONBuddyLibrary is a .NET wrapper for the jsonvalidator.dll, a powerful native library for validating, minifying, and prettifying JSON data. This package provides a simple and efficient way to leverage advanced JSON validation capabilities in your .NET applications, including streaming schema validation for huge data and customizable validation processes with callbacks.
Features
- JSON Validation: Validate JSON documents against various JSON Schema drafts.
- Generate JSON Schema documentation: Create self-contained, documentation-first HTML with property navigation, responsive and print layouts, and a secondary SVG schema diagram.
- Minification and Prettification: Easily minify or prettify JSON strings.
- JSON Patch: Apply JSON Patch operations to JSON documents.
- Schema Support: Supports multiple JSON Schema drafts, including Draft04, Draft06, Draft07, Draft201909, and Draft202012.
- Custom Callbacks: Define custom callbacks for document loading and validation reporting.
- License Support: Unlock enhanced functionality with a valid license key. You can obtain a full license from here JSONBuddy Online Shop.
Free versus full version
The following functionality is available in the free version:
- JSON Schema validation supporting all schema drafts.
- Prettify and minify JSON text.
The full version includes the following additional features:
- Streaming JSON data validation.
- JSON Schema documentation generation.
- Applying JSONPatch operations.
Installation
You can install the JSONBuddyLibrary package using any of the following methods:
NuGet Package Manager
PM> Install-Package JSONBuddyClassLibrary
.NET CLI
dotnet add package JSONBuddyClassLibrary
PackageReference
Add the following XML node into your project file to reference the package:
<PackageReference Include="JSONBuddyClassLibrary" Version="1.1.5" />
Usage
Basic JSON Validation
Here's an example of how to use the JSONValidatorWrapper to validate a JSON document against a schema:
using System;
using System.Runtime.InteropServices;
using JSONBuddyLibrary;
class Program
{
static void Main()
{
// Create a JSON Schema validator instance (using free version)
IntPtr validator = JSONValidatorWrapper.JB_NewJSONSchemaValidator("", "");
// Define your JSON Schema
string jsonSchema = @"
{
""$schema"": ""http://json-schema.org/draft-07/schema#"",
""type"": ""object"",
""properties"": {
""name"": { ""type"": ""string"" },
""age"": { ""type"": ""integer"" }
},
""required"": [""name"", ""age""]
}";
// Set the schema for the validator
bool schemaSet = JSONValidatorWrapper.JB_SetJSONSchemaFromString(validator, jsonSchema, 2); // Draft07
if (schemaSet)
{
// Validate a JSON instance against the schema
string jsonInstance = "{ \"name\": \"John Doe\", \"age\": 30 }";
IntPtr validationResult = JSONValidatorWrapper.JB_ValidateJSONDocument(validator, jsonInstance, null);
string resultString = Marshal.PtrToStringUni(validationResult);
Console.WriteLine($"Validation Result: {resultString}");
// Free the string allocated by the native library
JSONValidatorWrapper.JB_FreeString(validationResult);
}
// Free the validator instance
JSONValidatorWrapper.JB_FreeJSONSchemaValidator(validator);
}
}
Minifying and Prettifying JSON
The JSONValidatorWrapper also provides methods for minifying and prettifying JSON strings:
using System;
using System.Runtime.InteropServices;
using JSONBuddyLibrary;
class Program
{
static void Main()
{
IntPtr validator = JSONValidatorWrapper.JB_NewJSONSchemaValidator("", "");
string json = "{ \"name\": \"John Doe\", \"age\": 30, \"city\": \"New York\" }";
// Minify JSON
IntPtr minifiedJsonPtr = JSONValidatorWrapper.JB_MinifyJSONText(validator, json);
string minifiedJson = Marshal.PtrToStringUni(minifiedJsonPtr);
Console.WriteLine($"Minified JSON: {minifiedJson}");
JSONValidatorWrapper.JB_FreeString(minifiedJsonPtr);
// Prettify JSON
IntPtr prettifiedJsonPtr = JSONValidatorWrapper.JB_PrettifyJSONText(validator, json);
string prettifiedJson = Marshal.PtrToStringUni(prettifiedJsonPtr);
Console.WriteLine($"Prettified JSON: {prettifiedJson}");
JSONValidatorWrapper.JB_FreeString(prettifiedJsonPtr);
JSONValidatorWrapper.JB_FreeJSONSchemaValidator(validator);
}
}
Generating JSON Schema Documentation
JB_GenerateJSONSchemaDocumentation returns a complete HTML document. The generated file is self-contained and does not load external fonts, stylesheets, scripts, images, or CDNs. Its default Documentation view is optimized for reading, navigation, responsive display, accessibility, and printing; the structural Diagram remains available as a secondary section.
Documentation generation requires a valid license name and key:
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using JSONBuddyLibrary;
class Program
{
static void Main()
{
string jsonSchema = @"{
""$schema"": ""https://json-schema.org/draft/2020-12/schema"",
""title"": ""Customer"",
""type"": ""object"",
""properties"": {
""name"": {
""type"": ""string"",
""description"": ""The customer's display name.""
}
},
""required"": [""name""]
}";
IntPtr validator = JSONValidatorWrapper.JB_NewJSONSchemaValidator("license-name", "license-key");
if (validator == IntPtr.Zero)
throw new InvalidOperationException("Could not create the JSON Schema validator.");
try
{
IntPtr documentationPtr = JSONValidatorWrapper.JB_GenerateJSONSchemaDocumentation(validator, jsonSchema);
if (documentationPtr == IntPtr.Zero)
throw new InvalidOperationException("Could not generate the documentation.");
try
{
string html = Marshal.PtrToStringUni(documentationPtr) ?? "";
File.WriteAllText("customer-schema-documentation.html", html, new UTF8Encoding(false));
}
finally
{
JSONValidatorWrapper.JB_FreeString(documentationPtr);
}
}
finally
{
JSONValidatorWrapper.JB_FreeJSONSchemaValidator(validator);
}
}
}
Advanced Usage with Callbacks
You can define custom callbacks for loading documents and handling validation results. Please find below an example of how to use the streaming validation functionality with custom callbacks:
using System;
using System.Runtime.InteropServices;
using JSONBuddyLibrary;
class Program
{
static void Main()
{
IntPtr validator = JSONValidatorWrapper.JB_NewJSONSchemaValidator("", "");
// Define a document loader callback
JSONValidatorWrapper.LoadDocumentCallback documentLoader = (string path, out IntPtr content, out IntPtr error, long maxFileSize) =>
{
content = IntPtr.Zero;
error = IntPtr.Zero;
// Load your external document here, convert it to UTF-16LE, and assign it to content
// For simplicity, we'll just return false here
return false;
};
// Define a results entry added callback
JSONValidatorWrapper.ResultsEntryAddedCallback resultsCallback = (string jsonPointer, string jsonPointerSchema, string schemaKey, string pathToSchema, bool isValid, long validationStep, string message) =>
{
Console.WriteLine($"Validation Error: {message}");
return true; // Continue validation
};
string jsonInstancePath = "path_to_json_file.json";
bool validationSuccess = JSONValidatorWrapper.JB_ValidateJSONDocumentStream(validator, jsonInstancePath, documentLoader, resultsCallback, null);
Console.WriteLine($"Validation Success: {validationSuccess}");
JSONValidatorWrapper.JB_FreeJSONSchemaValidator(validator);
}
}
Supported Schema Drafts
The following JSON Schema drafts are supported:
- Draft04 (0)
- Draft06 (1)
- Draft07 (2)
- Draft201909 (3)
- Draft202012 (4)
Requirements
Platform: Windows (x64)
.NET 8 for Windows
Changelog
Version 1.1.5
- Modernized JSON Schema documentation as self-contained, documentation-first HTML.
- Added responsive property navigation, accessible native disclosure controls, and print styles.
- Retained and refreshed the structural SVG diagram as a secondary view.
- Preserved the existing native API and all supported JSON Schema drafts.
Version 1.1.2
- Added support for JSON Schema documentation and JSONPatch operations.
Version 1.0.0
- Initial release with standard and streaming JSON validation, minification, and prettification functionalities.
License
The JSONBuddyLibrary wrapper included in this NuGet package is free to use under the MIT license,
allowing developers to easily integrate standard JSON validation and manipulation capabilities into their .NET applications.
However, the underlying jsonvalidator.dll included in the package contains advanced functionality
that requires a commercial license for full access. Users can utilize basic features without a license, but to unlock enhanced capabilities,
a valid license key must be provided. For more details on the licensing terms, please refer to the license.txt file included in this package.
Support
For any issues or questions, please visit the github project pages at Clemens-U/jsonbuddy or contact us at office@xml-buddy.com.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0-windows7.0 is compatible. net9.0-windows was computed. net10.0-windows was computed. |
-
net8.0-windows7.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.