-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add PATCH mode for JSON deserialization #47841
Comments
Tagging subscribers to this area: @eiriktsarpalis, @layomia Issue DetailsBackground and MotivationFor most scenarios, we will deserialize a JSON object to a new object in .NET. But for some others, we may just need patch the memberwise delta changes to an existing object from the JSON properties. That means we may already have an object with properties initialized or set by other places, and we can still have a way to continue to set a number of specific properties from a JSON object. Proposed APInamespace System.Text.Json
{
public static class JsonSerializer
{
public static object Deserialize(string json, Type returnType, JsonSerializerOptions options = null);
public static object Deserialize(ReadOnlySpan<byte> utf8Json, Type returnType, JsonSerializerOptions options = null);
public static object Deserialize(ref Utf8JsonReader reader, Type returnType, JsonSerializerOptions options = null);
public static TValue Deserialize<TValue>(string json, JsonSerializerOptions options = null);
public static TValue Deserialize<TValue>(ReadOnlySpan<byte> utf8Json, JsonSerializerOptions options = null);
public static TValue Deserialize<TValue>(ref Utf8JsonReader reader, JsonSerializerOptions options = null);
+ public static void PatchDeserialize(object value, string json, Type returnType, JsonSerializerOptions options = null);
+ public static void PatchDeserialize(object value, ReadOnlySpan<byte> utf8Json, Type returnType, JsonSerializerOptions options = null);
+ public static void PatchDeserialize(object value, ref Utf8JsonReader reader, Type returnType, JsonSerializerOptions options = null);
+ public static void PatchDeserialize<TValue>(TValue value, string json, JsonSerializerOptions options = null) where TValue : class;
+ public static void PatchDeserialize<TValue>(TValue value, ReadOnlySpan<byte> utf8Json, JsonSerializerOptions options = null) where TValue : class;
+ public static void PatchDeserialize<TValue>(TValue value, ref Utf8JsonReader reader, JsonSerializerOptions options = null) where TValue : class;
public static string Serialize(object value, Type inputType, JsonSerializerOptions options = null);
public static void Serialize(Utf8JsonWriter writer, object value, Type inputType, JsonSerializerOptions options = null);
public static string Serialize<TValue>(TValue value, JsonSerializerOptions options = null);
public static void Serialize<TValue>(Utf8JsonWriter writer, Type inputType, JsonSerializerOptions options = null);
public static Task SerializeAsync(Stream utf8Json, object value, Type inputType, JsonSerializerOptions options = null);
public static void SerializeAsync<TValue>(Stream utf8Json, TValue value, JsonSerializerOptions options = null);
public static byte[] SerializeToUtf8Bytes(object value, Type inputType, JsonSerializerOptions options = null);
public static byte[] SerializeToUtf8Bytes<TValue>(TValue value, JsonSerializerOptions options = null);
}
} The static methods Usage Examplespublic class Model
{
public string Name { get; set; }
public string Value { get; set; }
public string Other { get; set; }
public string TestForNull { get; set; }
} var json = "{ \"Name\": \"Abcdefg\", \"Value\": \"Hijklmn\", \"TestForNull\": null }";
var m = new Model
{
Name = "Opq Rst",
Other = "Uvw Xyz",
TestForNull = "0123456789"
};
JsonSerializer.PatchDeserialize(m, json);
Assert.AreEqual("Abcdefg", m.Name);
Assert.AreEqual("Hijklmn", m.Value);
Assert.AreEqual("Uvw Xyz", m.Other);
Assert.IsNull(m.TestForNull);
|
Hi @kingcean, that's an interesting proposal. I don't believe I've seen such a feature before, do you know of any serializer libraries that already offer this? |
Hi @eiriktsarpalis , No. But I think it will be very useful when we build a web service and provide an API so that the client just need to PUT the delta update (only the properties needed to change) in the payload of the HTTP request to modify the entity in database. |
I believe this is a duplicate of #29538. |
Background and Motivation
For most scenarios, we will deserialize a JSON object to a new object in .NET. But for some others, we may just need patch the memberwise delta changes to an existing object from the JSON properties. That means we may already have an object with properties initialized or set by other places, and we can still have a way to continue to set a number of specific properties from a JSON object.
Proposed API
The static methods
PatchDeserialize
are used to delta update the object with the JSON data.Usage Examples
The text was updated successfully, but these errors were encountered: