JSONBuddyLibrary is a C# .NET wrapper for the jsonvalidator.dll, a powerful x64 native library for validating, formatting JSON text, JSON data patching, and JSON Schema documentation. This package provides a simple and efficient way to leverage advanced JSON validation capabilities in your .NET applications.
$ dotnet add package JSONBuddyClassLibraryJSONBuddyLibrary
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.
The following functionality is available in the free version:
The full version includes the following additional features:
You can install the JSONBuddyLibrary package using any of the following methods:
PM> Install-Package JSONBuddyLibrary
dotnet add package JSONBuddyLibrary
Add the following XML node into your project file to reference the package:
<PackageReference Include="JSONBuddyLibrary" Version="1.1.3" />
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);
}
}
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);
}
}
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);
}
}
The following JSON Schema drafts are supported:
Platform: Windows (x64)
.NET Standard 2.0 or higher
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.
For any issues or questions, please visit the github project pages at Clemens-U/jsonbuddy or contact us at office@xml-buddy.com.