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.