Set the comparer for Newtonsoft.Json.JsonConvert to use for HashSet/Dictionary

為{幸葍}努か 提交于 2019-12-10 18:23:30

问题


I have a HashSet<string> that JsonConvert.SerializeObject serialises to an array.

When I deserialise using JsonConvert.DeserializeObject<HashSet<string>> I get a new HashSet<string> with the same values. However, the Comparer has been reset.

// JSON settings
var settings = new JsonSerializerSettings
{
    ContractResolver = new CamelCasePropertyNamesContractResolver()
};

// Create a case insensitive hashset
var h = new HashSet<string>(new string[] {"A", "b"}, StringComparer.OrdinalIgnoreCase);
h.Contains("a"); // TRUE

// Serialise and deserialise with Newtonsoft.Json
string s = JsonConvert.SerializeObject(h, settings);
// s = ["A", "b"]
var newH = JsonConvert.DeserializeObject<HashSet<string>>(s, settings);

// Result is now case-sensitive
newH.Contains("a"); // FALSE
newH.Contains("A"); // TRUE

This is because JsonConvert uses EqualityComparer<string>.Default, which is case sensitive.

How do I tell it to use StringComparer.OrdinalIgnoreCase instead?

I don't want to include the HashSet<string>.Comparer property in the serialised data (I think it should be a simple array in JSON), I want to specify it at the point of deserialisation.


回答1:


You could use JsonConvert.PopulateObject() instead, when the hash set is the root object:

var newH = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
JsonConvert.PopulateObject(s, newH);

When the hash set is not the root object, Json.NET will populate it if preallocated unless ObjectCreationHandling.Replace is enabled. This allows the containing type to pre-allocate the hash set with the required comparer, e.g.:

public class RootObject
{
    public RootObject() { this.Collection = new HashSet<string>(StringComparer.OrdinalIgnoreCase); }

    public HashSet<string> Collection { get; private set; }
}

Alternatively, you could subclass CustomCreationConverter<HashSet<T>> and allocate the hash set with the required comparer within the converter's Create() method:

public class HashSetCreationConverter<T> : CustomCreationConverter<HashSet<T>>
{
    public IEqualityComparer<T> Comparer { get; private set; }

    public HashSetCreationConverter(IEqualityComparer<T> comparer)
    {
        this.Comparer = comparer;
    }

    public override HashSet<T> Create(Type objectType)
    {
        return new HashSet<T>(Comparer);
    }
}

And then do:

var newH = JsonConvert.DeserializeObject<HashSet<string>>(s, new HashSetCreationConverter<string>(StringComparer.OrdinalIgnoreCase));


来源:https://stackoverflow.com/questions/41875114/set-the-comparer-for-newtonsoft-json-jsonconvert-to-use-for-hashset-dictionary

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!