JavaScriptSerializer 在子类中使用“new”时抛出 AmbiguousMatchException



我知道很多人都以某种方式面临同样的问题,但我感到困惑的是,为什么Newtonsoft JSON Serializer能够正确处理这种情况,而JavaScriptSerializer却无法做到这一点。

我将使用与其他堆栈溢出线程之一中使用的相同代码示例(在子类中使用"new"时,JavascriptSerializer 序列化属性两次)

void Main()
{
    System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
    var json = serializer.Serialize(new Limited());
    Limited status = serializer.Deserialize<Limited>(json); --> throws AmbiguousMatchException
}
public class Full
{
    public String Stuff { get { return "Common things"; }  }
    public FullStatus Status { get; set; }
    public Full(bool includestatus)
    {
        if(includestatus)
            Status = new FullStatus();
    }
}
public class Limited : Full
{
    public new LimitedStatus Status { get; set; }
    public Limited() : base(false)
    {
        Status = new LimitedStatus();
    }
}
public class FullStatus 
{
    public String Text { get { return "Loads and loads and loads of things"; } }
}
public class LimitedStatus 
{
    public String Text { get { return "A few things"; } }
}

但是如果我使用Newtonsoft Json Serializer,一切正常。为什么?使用JavaScriptSerializer是否有可能实现相同的目标?

void Main()
{
    var json = JsonConvert.SerializeObject(new Limited());
    Limited status = JsonConvert.DeserializeObject<Limited>(json);  ----> Works fine.
}

这在 Json.NET 中起作用的原因是它有特定的代码来处理这种情况。 来自 JsonPropertyCollection.cs:

/// <summary>
/// Adds a <see cref="JsonProperty"/> object.
/// </summary>
/// <param name="property">The property to add to the collection.</param>
public void AddProperty(JsonProperty property)
{
    if (Contains(property.PropertyName))
    {
        // don't overwrite existing property with ignored property
        if (property.Ignored)
            return;
        JsonProperty existingProperty = this[property.PropertyName];
        bool duplicateProperty = true;
        if (existingProperty.Ignored)
        {
            // remove ignored property so it can be replaced in collection
            Remove(existingProperty);
            duplicateProperty = false;
        }
        else
        {
            if (property.DeclaringType != null && existingProperty.DeclaringType != null)
            {
                if (property.DeclaringType.IsSubclassOf(existingProperty.DeclaringType))
                {
                    // current property is on a derived class and hides the existing
                    Remove(existingProperty);
                    duplicateProperty = false;
                }
                if (existingProperty.DeclaringType.IsSubclassOf(property.DeclaringType))
                {
                    // current property is hidden by the existing so don't add it
                    return;
                }
            }
        }
        if (duplicateProperty)
            throw new JsonSerializationException("A member with the name '{0}' already exists on '{1}'. Use the JsonPropertyAttribute to specify another name.".FormatWith(CultureInfo.InvariantCulture, property.PropertyName, _type));
    }
    Add(property);
}

正如您在上面看到的,这里有特定的代码来优先选择派生类属性而不是具有相同名称和可见性的基类属性。

JavaScriptSerializer没有这样的逻辑。 它只是调用Type.GetProperty(string, flags)

        PropertyInfo propInfo = serverType.GetProperty(memberName,
            BindingFlags.Instance | BindingFlags.IgnoreCase | BindingFlags.Public);

记录了此方法以在以下情况下引发异常:

发生歧义匹配异常的情况包括:

  • 类型包含两个索引属性,这两个属性具有相同的名称,但参数数不同。若要解决歧义,请使用指定参数类型的 GetProperty 方法的重载。

  • 派生类型声明一个属性,该属性使用新修饰符(Visual Basic 中的阴影)隐藏具有相同名称的继承属性。若要解决歧义,请包括 BindingFlags.DeclaredOnly 以将搜索限制为未继承的成员。

我不知道为什么Microsoft没有为此添加逻辑JavaScriptSerializer. 这实际上是一段非常简单的代码;也许它被DataContractJsonSerializer黯然失色?

您确实有一个解决方法,即编写自定义JavaScriptConverter

public class LimitedConverter : JavaScriptConverter
{
    const string StuffName = "Stuff";
    const string StatusName = "Status";
    public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
    {
        var limited = new Limited();
        object value;
        if (dictionary.TryGetValue(StuffName, out value))
        {
            // limited.Stuff = serializer.ConvertToType<string>(value); // Actually it's get only.
        }
        if (dictionary.TryGetValue(StatusName, out value))
        {
            limited.Status = serializer.ConvertToType<LimitedStatus>(value);
        }
        return limited;
    }
    public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
    {
        var limited = (Limited)obj;
        if (limited == null)
            return null;
        var dict = new Dictionary<string, object>();
        if (limited.Stuff != null)
            dict.Add(StuffName, limited.Stuff);
        if (limited.Status != null)
            dict.Add(StatusName, limited.Status);
        return dict;
    }
    public override IEnumerable<Type> SupportedTypes
    {
        get { return new [] { typeof(Limited) } ; }
    }
}

然后像这样使用它:

        try
        {
            System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
            serializer.RegisterConverters(new JavaScriptConverter[] { new LimitedConverter() });
            var json = serializer.Serialize(new Limited());
            Debug.WriteLine(json);
            var status = serializer.Deserialize<Limited>(json);
            var json2 = serializer.Serialize(status);
            Debug.WriteLine(json2);
        }
        catch (Exception ex)
        {
            Debug.Assert(false, ex.ToString()); // NO ASSERT.
        }

相关内容

  • 没有找到相关文章

最新更新