Custom JSON Serializer
You can replace the serializer used by SetJson / GetJson by implementing IJobMasterMessageJsonSerializer. Define it before AddJobMasterCluster:
using JobMaster.Serialization;
services.DefineJobMasterMessageJsonSerializer(new MyNewtonsoftSerializer());
services.AddJobMasterCluster(...);
Things to know before changing it:
- It applies to the whole process, every cluster included, and only to
SetJson/GetJsonvalues. - Every app that schedules or runs these jobs must use the same serializer with the same settings. Otherwise a value written by one can fail, or be read wrongly, in another.
- Values already stored aren't converted. Jobs scheduled before the change keep the old format.
The default serializer is new SystemTextJsonMessageSerializer(). It keeps the same format across JobMaster versions, so it's always safe to list as a fallback.
To change serializer while old jobs still exist, use FallbackMessageJsonSerializer. It writes with the first serializer and, when reading, tries the others in order whenever one throws.
A fallback is only tried when a serializer throws, so every serializer except the last must reject JSON it doesn't own. A serializer that reads another format without throwing returns a wrong object instead, and the next one is never tried. For example, the default serializer reading camelCase JSON into PascalCase properties gives an object with empty properties. Pass rejectUnknownProperties: true to make it throw instead; writes are unchanged.
If your apps update one by one, deploy in two steps:
// Step 1, on every app: still writes the default format, already reads the new one.
services.DefineJobMasterMessageJsonSerializer(new FallbackMessageJsonSerializer(
new SystemTextJsonMessageSerializer(rejectUnknownProperties: true),
new MyNewtonsoftSerializer()));
// Step 2, once every app runs step 1: writes the new format, still reads the old one.
services.DefineJobMasterMessageJsonSerializer(new FallbackMessageJsonSerializer(
new MyStrictNewtonsoftSerializer(), // e.g. MissingMemberHandling.Error
new SystemTextJsonMessageSerializer()));
During step 2, apps still on step 1 read JSON from apps already on step 2. That's why the first serializer of each step must be strict.
Don't use rejectUnknownProperties: true on the last or only serializer: a stored value with a property you've since removed from the class would fail instead of being read.
Message data can be written by anyone who can schedule jobs, or who has write access to the job storage or transport (database, NATS). It is deserialized on your workers. If your serializer creates the type named in the JSON, a crafted payload can run code on every worker.
With Newtonsoft.Json, keep TypeNameHandling at None (the default). If you need polymorphism, restrict the allowed types with a SerializationBinder.