3

I´m trying to use the JsonConverter from this answer to Can I specify a path in an attribute to map a property in my class to a child property in my JSON? by Brian Rogers to map nested properties in JSON to a flat object.

The converter works well, but I need to fire the OnDeserialized callback to fill other properties and it´s not fired. If I don´t use the converter, the callback is fired.

Examples:

string json = @"{
    'response': {
        'code': '000',
        'description': 'Response success',
    },
    'employee': {
        'name': 'Test',
        'surname': 'Testing',
        'work': 'At office'
    }
}";
// employee.cs

public class EmployeeStackoverflow
{
    [JsonProperty("response.code")]
    public string CodeResponse { get; set; }

    [JsonProperty("employee.name")]
    public string Name { get; set; }

    [JsonProperty("employee.surname")]
    public string Surname { get; set; }

    [JsonProperty("employee.work")]
    public string Workplace { get; set; }

    [OnDeserialized]
    internal void OnDeserializedMethod(StreamingContext context)
    {
        Workplace = "At Home!!";
    }
}
// employeeConverter.cs
public class EmployeeConverter : JsonConverter
{
    public override object ReadJson(JsonReader reader, Type objectType,
                                object existingValue, JsonSerializer serializer)
    {
        JObject jo = JObject.Load(reader);
        object targetObj = Activator.CreateInstance(objectType);

        foreach (PropertyInfo prop in objectType.GetProperties()
                                                .Where(p => p.CanRead && p.CanWrite))
        {
            JsonPropertyAttribute att = prop.GetCustomAttributes(true)
                                            .OfType<JsonPropertyAttribute>()
                                            .FirstOrDefault();

            string jsonPath = (att != null ? att.PropertyName : prop.Name);
            JToken token = jo.SelectToken(jsonPath);

            if (token != null && token.Type != JTokenType.Null)
            {
                object value = token.ToObject(prop.PropertyType, serializer);
                prop.SetValue(targetObj, value, null);
            }
        }

        return targetObj;
    }

    public override bool CanConvert(Type objectType)
    {
        // CanConvert is not called when [JsonConverter] attribute is used
        return false;
    }

    public override bool CanWrite
    {
        get { return false; }
    }

    public override void WriteJson(JsonWriter writer, object value,
                                    JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }

}

If I add [JsonConverter(typeof(EmployeeConverter))] in the Employee class I obtain:

=== With Converter ===
Code: 000
Name: Test
Surname: Testing
Workplace: At office

If I remove[JsonConverter(typeof(EmployeeConverter))] from the Employee class I obtain:

=== With Converter ===
Code:
Name:
Surname:
Workplace: At Home!!

My goal is to obtain:

=== With Converter ===
Code: 000
Name: Test
Surname: Testing
Workplace: At Home!!

Is the converter missing something?

dbc
  • 104,963
  • 20
  • 228
  • 340
Randolf
  • 367
  • 1
  • 14

1 Answers1

3

Once you have created a custom JsonConverter for a type, it is incumbent on the converter to handle everything that needs to be done during deserialization -- including

  • Calling serialization callbacks.
  • Skipping ignored properties.
  • Invoking JsonConverter.ReadJson() for converters attached via attributes to members of the type.
  • Setting default values, skipping null values, resolving references, etc etc.

The complete logic can be seen in JsonSerializerInternalReader.PopulateObject(), and in theory you might need to make your ReadJson() method duplicate this method. (But in practice you will likely only implement a small, necessary subset of the logic.)

One way to make this task easier is to use Json.NET's own JsonObjectContract type metadata, as returned by JsonSerializer.ContractResolver.ResolveContract(objectType). This information contains the list of serialization callbacks and JsonpropertyAttribute property data used by Json.NET during deserialization. A modified version of the converter that uses this information would be as follows:

// Modified from this answer https://stackoverflow.com/a/33094930
// To https://stackoverflow.com/questions/33088462/can-i-specify-a-path-in-an-attribute-to-map-a-property-in-my-class-to-a-child-pr/
// By https://stackoverflow.com/users/10263/brian-rogers
// By adding handling of deserialization callbacks and some JsonProperty attributes.
public override object ReadJson(JsonReader reader, Type objectType,
                            object existingValue, JsonSerializer serializer)
{
    var contract = serializer.ContractResolver.ResolveContract(objectType) as JsonObjectContract ?? throw new JsonException(string.Format("{0} is not a JSON object", objectType));

    var jo = JToken.Load(reader);
    if (jo.Type == JTokenType.Null)
        return null;
    else if (jo.Type != JTokenType.Object)
        throw new JsonSerializationException(string.Format("Unexpected token {0}", jo.Type));

    var targetObj = contract.DefaultCreator();
    
    // Handle deserialization callbacks
    foreach (var callback in contract.OnDeserializingCallbacks)
        callback(targetObj, serializer.Context);

    foreach (var property in contract.Properties)
    {
        // Check that property isn't ignored, and can be deserialized.
        if (property.Ignored || !property.Writable)
            continue;
        if (property.ShouldDeserialize != null && !property.ShouldDeserialize(targetObj))
            continue;
        var jsonPath = property.PropertyName;
        var token = jo.SelectToken(jsonPath);
        // TODO: default values, skipping nulls, PreserveReferencesHandling, ReferenceLoopHandling, ...
        if (token != null && token.Type != JTokenType.Null)
        {
            object value;
            // Call the property's converter if present, otherwise deserialize directly.
            if (property.Converter != null && property.Converter.CanRead)
            {
                using (var subReader = token.CreateReader())
                {
                    if (subReader.TokenType == JsonToken.None)
                        subReader.Read();
                    value = property.Converter.ReadJson(subReader, property.PropertyType, property.ValueProvider.GetValue(targetObj), serializer);
                }
            }
            // TODO: property.ItemConverter != null
            else
            {
                value = token.ToObject(property.PropertyType, serializer);
            }
            property.ValueProvider.SetValue(targetObj, value);
        }
    }
    
    // Handle deserialization callbacks
    foreach (var callback in contract.OnDeserializedCallbacks)
        callback(targetObj, serializer.Context);
        
    return targetObj;
}

Demo fiddle here.

dbc
  • 104,963
  • 20
  • 228
  • 340
  • I understand everything you tell me, I thought that the converter was only for properties. I thought it was going to be easier to get nested properties and in the end the converter has been very difficult for someone as novice as me. I have tested your implementation and it works perfectly. For my use of case, it is enough even if TODO are missing. Thank you very much for taking the trouble to implement it. – Randolf Jun 09 '20 at 09:03