aspnet / JsonPatch

[Archived] JSON PATCH library. Project moved to https://github.com/aspnet/AspNetCore
Apache License 2.0
103 stars 48 forks source link

It should be possible to provide a custom JSON serializer #120

Closed MovGP0 closed 6 years ago

MovGP0 commented 7 years ago

Patching does not work when the entity uses custom datatypes that are not serializeable by default.

In my case, I use NodaTime to provide time-dependent information in a save manner:

public sealed class MyEntity 
{
    [JsonProperty("created")]
    public Instant Created { get; set; }
}
[
    { "op": "replace", "path": "/created", "value": "2017-10-25T12:34:56.000Z" }
]

Patching fails, since it cannot convert the ISO date string to an Instant, unless the global JSON serializer is set up to support those datatypes:

JsonConvert.DefaultSettings = () => new JsonSerializerSettings()
     .ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);

However, one might need a different global configuration than I want to use for patching. Therefore, it should be possible to provide a custom JSON converter for patching, rather than the global one:

var settings = new JsonSerializerSettings().ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);
var myJsonSerializer = JsonSerializer.Create(settings);

patch.ApplyTo(myEntity, myJsonSerializer);
aspnet-hello commented 6 years ago

This issue was moved to aspnet/Home#2425