Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
256 views
in Technique[技术] by (71.8m points)

c# - .Net Core 3.0 JsonSerializer populate existing object

I'm preparing a migration from ASP.NET Core 2.2 to 3.0.

As I don't use more advanced JSON features (but maybe one as described below), and 3.0 now comes with a built-in namespace/classes for JSON, System.Text.Json, I decided to see if I could drop the previous default Newtonsoft.Json.
Do note, I'm aware that System.Text.Json will not completely replace Newtonsoft.Json.

I managed to do that everywhere, e.g.

var obj = JsonSerializer.Parse<T>(jsonstring);

var jsonstring = JsonSerializer.ToString(obj);

but in one place, where I populate an existing object.

With Newtonsoft.Json one can do

JsonConvert.PopulateObject(jsonstring, obj);

The built-in System.Text.Json namespace has some additional classes, like JsonDocumnet, JsonElement and Utf8JsonReader, though I can't find any that take an existing object as a parameter.

Nor am I experienced enough to see how to make use of the existing one's.

There might be a possible upcoming feature in .Net Core (thanks to Mustafa Gursel for the link), but meanwhile (and what if it doesn't),...

...I now wonder, is it possible to achieve something similar as what one can do with PopulateObject?

I mean, is it possible with any of the other System.Text.Json classes to accomplish the same, and update/replace only the properties set?,... or some other clever workaround?


Here is a sample input/output of what I am looking for, and it need to be generic as the object passed into the deserialization method is of type <T>). I have 2 Json string's to be parsed into an object, where the first have some default properties set, and the second some, e.g.

Note, a property value can be of any other type than a string.

Json string 1:

{
  "Title": "Startpage",
  "Link": "/index",
}

Json string 2:

{
  "Head": "Latest news"
  "Link": "/news"
}

Using the 2 Json strings above, I want an object resulting in:

{
  "Title": "Startpage",
  "Head": "Latest news",
  "Link": "/news"
}

As seen in above sample, if properties in the 2nd has values/is set, it replace values in the 1st (as with "Head" and "Link"), if not, existing value persist (as with "Title")

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

So assuming that Core 3 doesn't support this out of the box, let's try to work around this thing. So, what's our problem?

We want a method that overwrites some properties of an existing object with the ones from a json string. So our method will have a signature of:

void PopulateObject<T>(T target, string jsonSource) where T : class

We don't really want any custom parsing as it's cumbersome, so we'll try the obvious approach - deserialize jsonSource and copy the result properties into our object. We cannot, however, just go

T updateObject = JsonSerializer.Parse<T>(jsonSource);
CopyUpdatedProperties(target, updateObject);

That's because for a type

class Example
{
    int Id { get; set; }
    int Value { get; set; }
}

and a JSON

{
    "Id": 42
}

we will get updateObject.Value == 0. Now we don't know if 0 is the new updated value or if it just wasn't updated, so we need to know exactly which properties jsonSource contains.

Fortunately, the System.Text.Json API allows us to examine the structure of the parsed JSON.

var json = JsonDocument.Parse(jsonSource).RootElement;

We can now enumerate over all properties and copy them.

foreach (var property in json.EnumerateObject())
{
    OverwriteProperty(target, property);
}

We will copy the value using reflection:

void OverwriteProperty<T>(T target, JsonProperty updatedProperty) where T : class
{
    var propertyInfo = typeof(T).GetProperty(updatedProperty.Name);

    if (propertyInfo == null)
    {
        return;
    }

    var propertyType = propertyInfo.PropertyType;
    v?a?r? ?p?a?r?s?e?d?V?a?l?u?e? ?=? ?J?s?o?n?S?e?r?i?a?l?i?z?e?r?.?P?a?r?s?e?(?u?p?d?a?t?e?d?P?r?o?p?e?r?t?y?.?V?a?l?u?e?,? ?p?r?o?p?e?r?t?y?T?y?p?e?)?;?
    var parsedValue = JsonSerializer.Deserialize(
        updatedProperty.Value.GetRawText(), 
        propertyType);

    propertyInfo.SetValue(target, parsedValue);
} 

We can see here that what we're doing is a shallow update. If the object contains another complex object as its property, that one will be copied and overwritten as a whole, not updated. If you require deep updates, this method needs to be changed to extract the current value of the property and then call the PopulateObject recursively if the property's type is a reference type (that will also require accepting Type as a parameter in PopulateObject).

Joining it all together we get:

void PopulateObject<T>(T target, string jsonSource) where T : class
{
    var json = JsonDocument.Parse(jsonSource).RootElement;

    foreach (var property in json.EnumerateObject())
    {
        OverwriteProperty(target, property);
    }
}

void OverwriteProperty<T>(T target, JsonProperty updatedProperty) where T : class
{
    var propertyInfo = typeof(T).GetProperty(updatedProperty.Name);

    if (propertyInfo == null)
    {
        return;
    }

    var propertyType = propertyInfo.PropertyType;
    v?a?r? ?p?a?r?s?e?d?V?a?l?u?e? ?=? ?J?s?o?n?S?e?r?i?a?l?i?z?e?r?.?P?a?r?s?e?(?u?p?d?a?t?e?d?P?r?o?p?e?r?t?y?.?V?a?l?u?e?,? ?p?r?o?p?e?r?t?y?T?y?p?e?)?;?
    var parsedValue = JsonSerializer.Deserialize(
        updatedProperty.Value.GetRawText(), 
        propertyType);

    propertyInfo.SetValue(target, parsedValue);
} 

How robust is this? Well, it certainly won't do anything sensible for a JSON array, but I'm not sure how you'd expect a PopulateObject method to work on an array to begin with. I don't know how it compares in performance to the Json.Net version, you'd have to test that by yourself. It also silently ignores properties that are not in the target type, by design. I thought it was the most sensible approach, but you might think otherwise, in that case the property null-check has to be replaced with an exception throw.

EDIT:

I went ahead and implemented a deep copy:

void PopulateObject<T>(T target, string jsonSource) where T : class => 
    PopulateObject(target, jsonSource, typeof(T));

void OverwriteProperty<T>(T target, JsonProperty updatedProperty) where T : class =>
    OverwriteProperty(target, updatedProperty, typeof(T));

void PopulateObject(object target, string jsonSource, Type type)
{
    var json = JsonDocument.Parse(jsonSource).RootElement;

    foreach (var property in json.EnumerateObject())
    {
        OverwriteProperty(target, property, type);
    }
}

void OverwriteProperty(object target, JsonProperty updatedProperty, Type type)
{
    var propertyInfo = type.GetProperty(updatedProperty.Name);

    if (propertyInfo == null)
    {
        return;
    }

    var propertyType = propertyInfo.PropertyType;
    object parsedValue;

    if (propertyType.IsValueType)
    {
        ?p?a?r?s?e?d?V?a?l?u?e? ?=? ?J?s?o?n?S?e?r?i?a?l?i?z?e?r?.?P?a?r?s?e?(?u?p?d?a?t?e?d?P?r?o?p?e?r?t?y?.?V?a?l?u?e?,? ?p?r?o?p?e?r?t?y?T?y?p?e?)?;?
        parsedValue = JsonSerializer.Deserialize(
            updatedProperty.Value.GetRawText(),
            propertyType);
    }
    else
    {
        parsedValue = propertyInfo.GetValue(target);
        P?o?p?u?l?a?t?e?O?b?j?e?c?t?(?p?a?r?s?e?d?V?a?l?u?e?,? ?u?p?d?a?t?e?d?P?r?o?p?e?r?t?y?.?V?a?l?u?e?,? ?p?r?o?p?e?r?t?y?T?y?p?e?)?;?
        PopulateObject(
            parsedValue, 
            updatedProperty.Value.GetRawText(), 
            propertyType);
    }

    propertyInfo.SetValue(target, parsedValue);
}

To make this more robust you'd either have to have a separate PopulateObjectDeep method or pass PopulateObjectOptions or something similar with a deep/shallow flag.

EDIT 2:

The point of deep-copying is so that if we have an object

{
    "Id": 42,
    "Child":
    {
        "Id": 43,
        "Value": 32
    },
    "Value": 128
}

and populate it with

{
    "Child":
    {
        "Value": 64
    }
}

we'd get

{
    "Id": 42,
    "Child":
    {
        "Id": 43,
        "Value": 64
    },
    "Value": 128
}

In case of a shallow copy we'd get Id = 0 in the copied child.

EDIT 3:

As @ldam pointed out, this no longer works in stable .NET Core 3.0, because the API was changed. The Parse method is now Deserialize and you have to dig deeper to get to a JsonElement's value. There is an active issue in the corefx repo to allow direct deserialization of a JsonElement. Right now the closest solution is to use GetRawText(). I went ahead and edited the code above to work, leaving the old version struck-through.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...