Quartz.Serialization.Json 3.8.1

JSON is recommended persistent format to store data in database for greenfield projects. You should also strongly consider setting useProperties to true to restrict key-values to be strings.

Quartz.Serialization.Json provides JSON serialization support for job stores using Json.NET to handle the actual serialization process.

Installation

You need to add NuGet package reference to your project which uses Quartz.

Install-Package Quartz.Serialization.Json

Configuring

Classic property-based configuration

var properties = new NameValueCollection
{
	["quartz.jobStore.type"] = "Quartz.Impl.AdoJobStore.JobStoreTX, Quartz",
	// "json" is alias for "Quartz.Simpl.JsonObjectSerializer, Quartz.Serialization.Json" 
	["quartz.serializer.type"] = "json"
};
ISchedulerFactory schedulerFactory = new StdSchedulerFactory(properties);

Configuring using scheduler builder

var config = SchedulerBuilder.Create();
config.UsePersistentStore(store =>
{
    // it's generally recommended to stick with
    // string property keys and values when serializing
    store.UseProperties = true;
    store.UseGenericDatabase(dbProvider, db =>
        db.ConnectionString = "my connection string"
    );

    store.UseJsonSerializer();
});
ISchedulerFactory schedulerFactory = config.Build();

Migrating from binary serialization

There's now official solution for migration as there can be quirks in every setup, but there's a recipe that can work for you.

  • Configure custom serializer like MigratorSerializer below that can read binary serialization format and writes JSON format
  • Either let system gradually migrate as it's running or create a program which loads and writes back to DB all relevant serialized assets

Example hybrid serializer

public class MigratorSerializer : IObjectSerializer
{
    private BinaryObjectSerializer binarySerializer;
    private JsonObjectSerializer jsonSerializer;

    public MigratorSerializer()
    {
        this.binarySerializer = new BinaryObjectSerializer();
        // you might need custom configuration, see sections about customizing
        // in documentation
        this.jsonSerializer = new JsonObjectSerializer();
    }

    public T DeSerialize<T>(byte[] data) where T : class
    {
        try
        {
            // Attempt to deserialize data as JSON
            var result = this.jsonSerializer.DeSerialize<T>(data);
            return result;
        }
        catch (JsonReaderException)
        {
            // Presumably, the data was not JSON, we instead use the binary serializer
            return this.binarySerializer.DeSerialize<T>(data);
        }
    }

    public void Initialize()
    {
        this.binarySerializer.Initialize();
        this.jsonSerializer.Initialize();
    }

    public byte[] Serialize<T>(T obj) where T : class
    {
        return this.jsonSerializer.Serialize<T>(obj);
    }
}

Customizing JSON.NET

If you need to customize JSON.NET settings, you need to inherit custom implementation and override CreateSerializerSettings.

class CustomJsonSerializer : JsonObjectSerializer
{
   protected override JsonSerializerSettings CreateSerializerSettings()
   {
       var settings = base.CreateSerializerSettings();
       settings.Converters.Add(new MyCustomConverter());
       return settings;
   }
} 

And then configure it to use

store.UseSerializer<CustomJsonSerializer>();
// or 
"quartz.serializer.type" = "MyProject.CustomJsonSerializer, MyProject"

Customizing calendar serialization

If you have implemented a custom calendar, you need to implement a ICalendarSerializer for it. There's a convenience base class CalendarSerializer that you can use the get strongly-typed experience.

Custom calendar and serializer

[Serializable]
class CustomCalendar : BaseCalendar
{
    public CustomCalendar()
    {
    }

    // binary serialization support
    protected CustomCalendar(SerializationInfo info, StreamingContext context) : base(info, context)
    {
        SomeCustomProperty = info?.GetBoolean("SomeCustomProperty") ?? true;
    }

    public bool SomeCustomProperty { get; set; } = true;

    // binary serialization support
    public override void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        base.GetObjectData(info, context);
        info?.AddValue("SomeCustomProperty", SomeCustomProperty);
    }
}

// JSON serialization support
class CustomCalendarSerializer : CalendarSerializer<CustomCalendar>
{
    protected override CustomCalendar Create(JObject source)
    {
        return new CustomCalendar();
    }

    protected override void SerializeFields(JsonWriter writer, CustomCalendar calendar)
    {
        writer.WritePropertyName("SomeCustomProperty");
        writer.WriteValue(calendar.SomeCustomProperty);
    }

    protected override void DeserializeFields(CustomCalendar calendar, JObject source)
    {
        calendar.SomeCustomProperty = source["SomeCustomProperty"]!.Value<bool>();
    }
}

Configuring custom calendar serializer

var config = SchedulerBuilder.Create();
config.UsePersistentStore(store =>
{
    store.UseJsonSerializer(json =>
    {
        json.AddCalendarSerializer<CustomCalendar>(new CustomCalendarSerializer());
    });
});

// or just globally which is what above code calls
JsonObjectSerializer.AddCalendarSerializer<CustomCalendar>(new CustomCalendarSerializer());

Showing the top 20 packages that depend on Quartz.Serialization.Json.

Packages Downloads
Quartz.Extensions.DependencyInjection
Extension methods to integrate Quartz scheduler and the jobs with the generic host in net core.
15

https://github.com/quartznet/quartznet/releases

.NET Framework 4.6.2

.NET Framework 4.7.2

.NET Standard 2.0

Version Downloads Last updated
3.8.1 3 02/17/2024
3.8.0 4 11/19/2023
3.7.0 5 09/07/2023
3.6.3 4 06/28/2023
3.6.2 38 05/09/2023
3.6.1 18 06/21/2023
3.6.0 24 05/12/2023
3.5.0 39 09/25/2022
3.4.0 28 09/11/2022
3.3.3 10 09/23/2022
3.3.2 40 09/12/2022
3.3.1 37 09/17/2022
3.3.0 34 09/23/2022
3.2.4 58 10/15/2022
3.2.3 39 09/12/2022
3.2.2 17 09/12/2022
3.2.1 23 09/23/2022
3.2.0 42 09/12/2022
3.1.0 21 09/12/2022
3.0.7 25 09/12/2022
3.0.6 33 09/12/2022
3.0.5 15 09/25/2022
3.0.4 53 09/12/2022
3.0.3 15 09/12/2022
3.0.2 20 09/21/2022
3.0.1 18 09/12/2022
3.0.0 11 09/12/2022
3.0.0-beta1 15 09/12/2022
3.0.0-alpha3 20 09/12/2022
3.0.0-alpha2 12 09/12/2022
3.0.0-alpha1 50 09/12/2022