(De) Serialize DynamicObject with Jil?

I have a DynamicObject problem (serialization) with another json library that is not Newtownsoft.Json. (Jil, NetJSON, ServiceStack.Text ...)

This is my extensible class of objects:

public class ExpandableObject : DynamicObject
{
    private readonly Dictionary<string, object> _fields = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
    [JsonIgnore]
    public Dictionary<string, object> Extra { get { return _fields; } }

    public override IEnumerable<string> GetDynamicMemberNames()
    {
        var membersNames = GetType().GetProperties().Where(propInfo => propInfo.CustomAttributes
            .All(ca => ca.AttributeType != typeof (JsonIgnoreAttribute)))
            .Select(propInfo => propInfo.Name);
        return Extra.Keys.Union(membersNames);
    }

    public override bool TryGetMember(GetMemberBinder binder, out object result)
    {
        return _fields.TryGetValue(binder.Name, out result);
    }

    public override bool TrySetMember(SetMemberBinder binder, object value)
    {
        _fields[binder.Name] = value;
        return true;
    }
}

The problem with other libraries (like Jil) is that overridden methods are not called. It works just fine with Newtonsoft.Json, but its performance is poor.

For example, deserializing a test of a derived class:

public class Person : ExpandableObject
{
    public int Id { get; set; }
}

public class Program
{
    public static void Main(string[] args)
    {
        var json = "{ \"Id\": 12 , \"SomeFiled\" : \"hello\" }";
        var person = Jil.JSON.Deserialize<Person>(json);            
    }
}

There is no exception .. He just ignored the "SomeFiled" field (should be in "Extra")

1. Is there any solution?

2. Newtonsoft.Json, , JIL ? ( ...). , DLR. ?

.

EDIT:

DeserilizeDynamic Deserialize (T). , DLR. - DeserilizeDynamic "dynamic" (T). - -API can not POST, . mabye ...

+4
1

Common Pitfalls (https://github.com/kevin-montrose/Jil/wiki/Common-Pitfalls), :

FooBase Bar: FooBase JSON.Serialize(myBar), Jil , FooBase . , .

, Options ShouldIncludeInherited, .

0

All Articles