Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 反序列化为NameValueCollection时Json.NET 6.0.7中的ArgumentNullException_C#_Json_Json.net - Fatal编程技术网

C# 反序列化为NameValueCollection时Json.NET 6.0.7中的ArgumentNullException

C# 反序列化为NameValueCollection时Json.NET 6.0.7中的ArgumentNullException,c#,json,json.net,C#,Json,Json.net,我已经编写了一些自定义json转换器,将json文本反序列化为System.Net.Mail.MailMessage对象。下面是完整的代码,可以在LINQPad中运行。有趣的是,这段代码在Json.NET 4.5.11中按预期运行: void Main() { const string JsonMessage = @"{ ""From"": { ""Address"": ""askywalker@theEmpire.gov"", ""DisplayName"": ""Dart

我已经编写了一些自定义json转换器,将json文本反序列化为
System.Net.Mail.MailMessage
对象。下面是完整的代码,可以在LINQPad中运行。有趣的是,这段代码在Json.NET 4.5.11中按预期运行:

void Main()
{
const string JsonMessage = @"{
  ""From"": {
    ""Address"": ""askywalker@theEmpire.gov"",
    ""DisplayName"": ""Darth Vader""
  },
  ""Sender"": null,
  ""ReplyTo"": null,
  ""ReplyToList"": [],
  ""To"": [
    {
      ""Address"": ""lskywalker@theRebellion.org"",
      ""DisplayName"": ""Luke Skywalker""
    }
  ],
  ""Bcc"": [],
  ""CC"": [
    {
      ""Address"": ""lorgana@alderaan.gov"",
      ""DisplayName"": ""Princess Leia""
    }
  ],
  ""Priority"": 0,
  ""DeliveryNotificationOptions"": 0,
  ""Subject"": ""Family tree"",
  ""SubjectEncoding"": null,
  ""Headers"": [],
  ""HeadersEncoding"": null,
  ""Body"": ""<strong>I am your father!</strong>"",
  ""BodyEncoding"": ""US-ASCII"",
  ""BodyTransferEncoding"": -1,
  ""IsBodyHtml"": true,
  ""Attachments"": [
    {
      ""FileName"": ""skywalker family tree.jpg"",
      ""ContentBase64"": ""AQIDBAU=""
    }
  ],
  ""AlternateViews"": []
}";
    JsonConvert.DeserializeObject<MailMessage>(JsonMessage, 
    new MailAddressReadConverter(), new AttachmentReadConverter(), new EncodingReadConverter()).Dump();

}

    public class MailAddressReadConverter : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            return objectType == typeof(MailAddress);
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            var messageJObject = serializer.Deserialize<JObject>(reader);
            if (messageJObject == null)
            {
                return null;
            }

            var address = messageJObject.GetValue("Address", StringComparison.OrdinalIgnoreCase).ToObject<string>();

            JToken displayNameToken;
            string displayName;
            if (messageJObject.TryGetValue("DisplayName", StringComparison.OrdinalIgnoreCase, out displayNameToken)
                && !string.IsNullOrEmpty(displayName = displayNameToken.ToObject<string>()))
            {
                return new MailAddress(address, displayName);
            }

            return new MailAddress(address);
        }

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

        public class AttachmentReadConverter : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            return objectType == typeof(Attachment);
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            var info = serializer.Deserialize<AttachmentInfo>(reader);

            var attachment = info != null
                ? new Attachment(new MemoryStream(Convert.FromBase64String(info.ContentBase64)), "application/octet-stream")
                {
                    ContentDisposition = { FileName = info.FileName }
                }
                : null;
            return attachment;
        }

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

        private class AttachmentInfo
        {
            [JsonProperty(Required = Required.Always)]
            public string FileName { get; set; }

            [JsonProperty(Required = Required.Always)]
            public string ContentBase64 { get; set; }
        }
    }

        public class EncodingReadConverter : JsonConverter
    {
        public override bool CanConvert(Type objectType)
        {
            return typeof(Encoding).IsAssignableFrom(objectType);
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            var encodingName = serializer.Deserialize<string>(reader);
            return encodingName.NullSafe(s => Encoding.GetEncoding(s));
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            throw new NotImplementedException();
        }
    }
这是JSON 6中的错误吗?我做错什么了吗


编辑:通过进一步调试,我确定问题出在
Headers
属性上。

这里的基本问题是返回a,这有点像字典,但没有实现
IDictionary
或非泛型
IDictionary
。相反,它实现了非通用接口
ICollection
IEnumerable
。这些接口实际做的是只在集合的内部循环,完全忽略这些值

因此,如果我使用
NameValueCollection
这样:

    public static NameValueCollection CreateCollection()
    {
        NameValueCollection collection = new NameValueCollection();
        FillCollection(collection);
        return collection;
    }

    private static void FillCollection(NameValueCollection collection)
    {
        collection.Add("Sam", "Dot Net Perls");
        collection.Add("Bill", "Microsoft");
        collection.Add("Bill", "White House");
        collection.Add("Sam", "IBM");
    }
并使用Json.NET 6.0.7对其进行序列化,它会看到传入类是一个非泛型集合,并且:

制作:

        ["Sam","Bill"]
如您所见,这些值已被剥离

然后,在反序列化时,Json.NET尝试将字符串数组转换回
NameValueCollection
,但无法这样做。特别是,它试图构造一个临时列表来保存正在读取的数据,但对列表的基本类型感到困惑,并引发异常。这可能是Json.NET中的一个bug,但即使它没有抛出异常,数据也已经在存储中丢失了。这可以通过以下简单的测试类重现:

public class NameValueCollectionWrapper
{
    public NameValueCollectionWrapper()
    {
        this.Collection = new NameValueCollection();
    }

    public NameValueCollection Collection { get; private set; }
}
所以,问题是,您是想阅读标题,还是想忽略它们?如果你想阅读它们,你会以什么格式接收它们?如果要成功发送和接收它们,需要编写一个自定义的
JsonConverter
。这样做有点棘手,因为
NameValueCollection
几乎就像一个
字典,但它保留了键的添加顺序,而不是。理想情况下,序列化应该保持这种顺序。这可以通过创建和序列化包装器
IDictionary
来实现,例如从到:

最后,应用
NameValueJsonConverter
,就像应用其他转换器一样。这将以Json字典样式生成输出,同时保留顺序,例如:

{"Sam":["Dot Net Perls","IBM"],"Bill":["Microsoft","White House"]}
我没有Json.NET 4.x可供测试,但我怀疑它是否正确序列化了
NameValueCollection
的键和值。您可能希望安装该版本以双重检查其功能

更新

刚刚检查了Json.NET4.5.11。在该版本中,my
NameValueCollectionWrapper
测试类中的
NameValueCollection
属性被序列化为键字符串数组,然后在反序列化时被忽略(集合返回为空)。因此,Json.NET版本6抛出异常而不是忽略属性可能是一种回归

public class NameValueCollectionWrapper
{
    public NameValueCollectionWrapper()
    {
        this.Collection = new NameValueCollection();
    }

    public NameValueCollection Collection { get; private set; }
}
public class NameValueCollectionDictionaryAdapter<TNameValueCollection> : IDictionary<string, string[]>
    where TNameValueCollection : NameValueCollection, new()
{
    readonly TNameValueCollection collection;

    public NameValueCollectionDictionaryAdapter() : this(new TNameValueCollection()) { }

    public NameValueCollectionDictionaryAdapter(TNameValueCollection collection)
    {
        this.collection = collection;
    }

    // Method instead of a property to guarantee that nobody tries to serialize it.
    public TNameValueCollection GetCollection() { return collection; }

    #region IDictionary<string,string[]> Members

    public void Add(string key, string[] value)
    {
        if (collection.GetValues(key) != null)
            throw new ArgumentException("Duplicate key " + key);
        if (value == null)
            collection.Add(key, null);
        else
            foreach (var str in value)
                collection.Add(key, str);
    }

    public bool ContainsKey(string key) { return collection.GetValues(key) != null; }

    public ICollection<string> Keys { get { return collection.AllKeys; } }

    public bool Remove(string key)
    {
        bool found = ContainsKey(key);
        if (found)
            collection.Remove(key);
        return found;
    }

    public bool TryGetValue(string key, out string[] value)
    {
        return (value = collection.GetValues(key)) != null;
    }

    public ICollection<string[]> Values
    {
        get
        {
            return new ReadOnlyCollectionAdapter<KeyValuePair<string, string[]>, string[]>(this, p => p.Value);
        }
    }

    public string[] this[string key]
    {
        get
        {
            var value = collection.GetValues(key);
            if (value == null)
                throw new KeyNotFoundException(key);
            return value;
        }
        set
        {
            Remove(key);
            Add(key, value);
        }
    }

    #endregion

    #region ICollection<KeyValuePair<string,string[]>> Members

    public void Add(KeyValuePair<string, string[]> item) { Add(item.Key, item.Value); }

    public void Clear() { collection.Clear(); }

    public bool Contains(KeyValuePair<string, string[]> item)
    {
        string[] value;
        if (!TryGetValue(item.Key, out value))
            return false;
        return EqualityComparer<string[]>.Default.Equals(item.Value, value); // Consistent with Dictionary<TKey, TValue>
    }

    public void CopyTo(KeyValuePair<string, string[]>[] array, int arrayIndex)
    {
        foreach (var item in this)
            array[arrayIndex++] = item;
    }

    public int Count { get { return collection.Count; } }

    public bool IsReadOnly { get { return false; } }

    public bool Remove(KeyValuePair<string, string[]> item)
    {
        if (Contains(item))
            return Remove(item.Key);
        return false;
    }

    #endregion

    #region IEnumerable<KeyValuePair<string,string[]>> Members

    public IEnumerator<KeyValuePair<string, string[]>> GetEnumerator()
    {
        foreach (string key in collection)
            yield return new KeyValuePair<string, string[]>(key, collection.GetValues(key));
    }

    #endregion

    #region IEnumerable Members

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return GetEnumerator(); }

    #endregion
}

public static class NameValueCollectionExtensions
{
    public static NameValueCollectionDictionaryAdapter<TNameValueCollection> ToDictionaryAdapter<TNameValueCollection>(this TNameValueCollection collection)
        where TNameValueCollection : NameValueCollection, new()
    {
        if (collection == null)
            throw new ArgumentNullException();
        return new NameValueCollectionDictionaryAdapter<TNameValueCollection>(collection);
    }
}

public class ReadOnlyCollectionAdapter<TIn, TOut> : CollectionAdapterBase<TIn, TOut, ICollection<TIn>>
{
    public ReadOnlyCollectionAdapter(ICollection<TIn> collection, Func<TIn, TOut> toOuter)
        : base(() => collection, toOuter)
    {
    }

    public override void Add(TOut item) { throw new NotImplementedException(); }

    public override void Clear() { throw new NotImplementedException(); }

    public override bool IsReadOnly { get { return true; } }

    public override bool Remove(TOut item) { throw new NotImplementedException(); }
}

public abstract class CollectionAdapterBase<TIn, TOut, TCollection> : ICollection<TOut> 
    where TCollection : ICollection<TIn>
{
    readonly Func<TCollection> getCollection;
    readonly Func<TIn, TOut> toOuter;

    public CollectionAdapterBase(Func<TCollection> getCollection, Func<TIn, TOut> toOuter)
    {
        if (getCollection == null || toOuter == null)
            throw new ArgumentNullException();
        this.getCollection = getCollection;
        this.toOuter = toOuter;
    }

    protected TCollection Collection { get { return getCollection(); } }

    protected TOut ToOuter(TIn inner) { return toOuter(inner); }

    #region ICollection<TOut> Members

    public abstract void Add(TOut item);

    public abstract void Clear();

    public virtual bool Contains(TOut item)
    {
        var comparer = EqualityComparer<TOut>.Default;
        foreach (var member in Collection)
            if (comparer.Equals(item, ToOuter(member)))
                return true;
        return false;
    }

    public void CopyTo(TOut[] array, int arrayIndex)
    {
        foreach (var item in this)
            array[arrayIndex++] = item;
    }

    public int Count { get { return Collection.Count; } }

    public abstract bool IsReadOnly { get; }

    public abstract bool Remove(TOut item);

    #endregion

    #region IEnumerable<TOut> Members

    public IEnumerator<TOut> GetEnumerator()
    {
        foreach (var item in Collection)
            yield return ToOuter(item);
    }

    #endregion

    #region IEnumerable Members

    IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }

    #endregion
}
public class NameValueJsonConverter<TNameValueCollection> : JsonConverter
    where TNameValueCollection : NameValueCollection, new()
{
    public override bool CanConvert(Type objectType)
    {
        return typeof(TNameValueCollection).IsAssignableFrom(objectType);
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        if (reader.SkipComments().TokenType == JsonToken.Null)
            return null;

        var collection = (TNameValueCollection)existingValue ?? new TNameValueCollection();
        var dictionaryWrapper = collection.ToDictionaryAdapter();

        if (reader.TokenType != JsonToken.StartObject)
        {
            // Old buggy name value collection format in which the values were not written and so cannot be recovered.
            // Skip the token and all its children.
            reader.Skip();
        }
        else
        {
            serializer.Populate(reader, dictionaryWrapper);
        }

        return collection;
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        var collection = (TNameValueCollection)value;
        var dictionaryWrapper = new NameValueCollectionDictionaryAdapter<TNameValueCollection>(collection);
        serializer.Serialize(writer, dictionaryWrapper);
    }
}

public static partial class JsonExtensions
{
    public static JsonReader SkipComments(this JsonReader reader)
    {
        while (reader.TokenType == JsonToken.Comment && reader.Read())
            ;
        return reader;
    }
}
{"Sam":["Dot Net Perls","IBM"],"Bill":["Microsoft","White House"]}