2

Pertinent sections of the class I'm trying to serialize/deserialize:

public class SampleClass
{
    [JsonPropertyName("DimensionValueDictionary")]
    public Dictionary<string, List<string>> DimensionValueDictionary { get; set; }

    [JsonPropertyName("DimensionValueContentMappings")]
    public Dictionary<HashSet<string>, string> DimensionValueMappings { get; set; }

    public override string ToString()
    {   
        return Newtonsoft.Json.JsonConvert.SerializeObject(this);
    }

}

I'm able to successfully serialize and deserialize the first dictionary (with string as key), but I'm not able to figure out how to get the dictionary with hashset as key to serialize correctly.

var serializedSampleClass = sampleClassInstance.ToString();
var deSerializedSampleClassInstance = JsonConvert.DeserializeObject<SampleClass>(serializedSampleClass);
stuartd
  • 70,509
  • 14
  • 132
  • 163
hexcode
  • 393
  • 1
  • 4
  • 13
  • _I'm not able to figure out how to get the dictionary with hashset as key to serialize correctly._ - can you show an example of the serialized class, and explain why it isn't correct? – stuartd May 25 '21 at 21:15
  • 1
    Side note: JsonPropertyNameAttribute does not seem to be related to the way you serialize the object... Somewhat does not matter for [MCVE] so just making sure you have no idea know what you are doing. – Alexei Levenkov May 25 '21 at 21:18
  • [How can I serialize/deserialize a dictionary with custom keys using Json.Net?](https://stackoverflow.com/q/24681873/3744182) is another viable duplicate. – dbc May 25 '21 at 21:21

0 Answers0