Getting Started

This guide will help you start using Solace Schema Registry JSON Schema SERDES for .NET in your applications.

Installation

Install the NuGet package:

dotnet add package Solace.SchemaRegistry.Serdes.JsonSchema

This package includes the Core package as a dependency.

Basic Usage

Creating a JsonSchemaSerializer

using System.Text.Json.Nodes;
using Solace.SchemaRegistry.Serdes.JsonSchema;
using Solace.SchemaRegistry.Serdes.Core.Resolver;

// Create the serializer
using var serializer = new JsonSchemaSerializer<JsonNode>();

// Configure with Solace Schema Registry credentials
var config = new Dictionary<string, object>
{
    { SchemaResolverPropertyKeys.RegistryUrl, "https://your-registry.solace.cloud/apis/registry/v3" },
    { SchemaResolverPropertyKeys.AuthUsername, "your-username" },
    { SchemaResolverPropertyKeys.AuthPassword, "your-password" }
};

serializer.Configure(config);

Creating a JsonSchemaDeserializer

using System.Text.Json.Nodes;
using Solace.SchemaRegistry.Serdes.JsonSchema;
using Solace.SchemaRegistry.Serdes.Core.Resolver;

// Create the deserializer
using var deserializer = new JsonSchemaDeserializer<JsonNode>();

// Configure with Solace Schema Registry credentials
var config = new Dictionary<string, object>
{
    { SchemaResolverPropertyKeys.RegistryUrl, "https://your-registry.solace.cloud/apis/registry/v3" },
    { SchemaResolverPropertyKeys.AuthUsername, "your-username" },
    { SchemaResolverPropertyKeys.AuthPassword, "your-password" }
};

deserializer.Configure(config);

Integration with Solace Messaging API for .NET

When using with the Solace Messaging API for .NET:

using System.Text.Json.Nodes;
using Solace.SchemaRegistry.Serdes.JsonSchema;
using Solace.SchemaRegistry.Serdes.Core.Resolver;
using SolaceSystems.Solclient.Messaging;
using SolaceSystems.Solclient.Messaging.Serialization;
using Solace.Serdes;

// Create and configure JSON Schema serializer and deserializer
using var deserializer = new JsonSchemaDeserializer<JsonNode>();
using var serializer = new JsonSchemaSerializer<JsonNode>();

var config = new Dictionary<string, object>
{
    { SchemaResolverPropertyKeys.RegistryUrl, "https://your-registry.solace.cloud/apis/registry/v3" },
    { SchemaResolverPropertyKeys.AuthUsername, "your-username" },
    { SchemaResolverPropertyKeys.AuthPassword, "your-password" }
};

deserializer.Configure(config);
serializer.Configure(config);

// Wrap async serializer/deserializer with synchronous adapters for use with the API's synchronous callbacks
var syncDeserializer = deserializer.AsSyncOverAsync();
var syncSerializer = serializer.AsSyncOverAsync();

// Create a session with the message handler
using ISession session = context.CreateSession(
    sessionProps,
    (source, msgArgs) => HandleMessage(source, msgArgs, syncDeserializer),
    null);

// Publishing a message
using (var message = ContextFactory.Instance.CreateMessage())
{
    message.Destination = topic;

    var data = new JsonObject
    {
        ["name"] = "Test",
        ["value"] = 42
    };

    // Serialize the data to the message using JSON Schema serialization
    message.Serialize(syncSerializer, data);
    session.Send(message);
}

// Receiving a message
void HandleMessage(object source, MessageEventArgs args, IDeserializer<JsonNode> deserializer)
{
    // Deserialize the message payload using JSON Schema validation
    JsonNode data = args.Message.Deserialize(deserializer);
    Console.WriteLine($"Received: {data.ToJsonString()}");
}

Additional Examples

For complete working examples, including integration with Solace messaging, visit the Solace Samples for .NET repository.