1

I have an odd problem. I use WCF RIA with Entity Framework. I've implemented a generic search functionality, which relies on sending back the resulting entities as byte[] (enter Json.Net) and I'm able to get around all sorts of limitations of strong typedness of RIA. But when I'm deserializing back in the client, my object is not properly assembled. Now what do I mean by that?

The json, technically a string, converted to byte[] by me and back on the client, contains the related entity information I need. So let's suppose the entity is called Account and it has a related Person object. The json string , even the deserialized jobject, has this Person object and it's details. However, when I deserialize like JsonConvert.DeserializeObject<Account>(jdata, settings) - Person is null with no errors.

The settings I'm trying are here:

settings = new JsonSerializerSettings()
{
    //CheckAdditionalContent = true,
    PreserveReferencesHandling = PreserveReferencesHandling.All,
    //ReferenceLoopHandling = ReferenceLoopHandling.Serialize
    NullValueHandling = NullValueHandling.Ignore,
    DefaultValueHandling = DefaultValueHandling.Ignore,
    ObjectCreationHandling = ObjectCreationHandling.Replace,
    TypeNameHandling = TypeNameHandling.Auto 
}; 

Any ideas?

4

1 回答 1

2

好的,想通了 - 所以在反序列化之前,我将下面的解析器附加到我的设置中,比如settings.ContractResolver = new DynamicContractResolver();

    public class DynamicContractResolver : DefaultContractResolver
    {
        protected override JsonProperty CreateProperty(System.Reflection.MemberInfo member, MemberSerialization memberSerialization)
        {
            var r = base.CreateProperty(member, memberSerialization);
            r.Ignored = false;
            return r;
        }
    }

现在 Json 中的所有内容都完全反序列化到对象中。我不知道为什么这不是默认行为。

于 2012-06-13T18:40:20.033 回答