5

This is the code:

public class ParameterDictionary : Dictionary<HydroObjectIdentifier, string>
{

    public void WriteToJson(string jsonFilePath)
    {
        string json = Newtonsoft.Json.JsonConvert.SerializeObject(this, formatting: Newtonsoft.Json.Formatting.Indented);
        System.IO.File.WriteAllText(jsonFilePath, json);
    }

}

public struct HydroObjectIdentifier
{

    public string Name { get; set; }
    public string TypeName { get; set; }

    public HydroObjectIdentifier(string name, string typeName)
    {
        this.Name = name;
        this.TypeName = typeName;
    }

}

...and this is the Json result. Notice that it shows the class name RSEngine.HydroObjectIdentifier instead of its parameters, which was not intended in my code.

{
  "RSEngine.HydroObjectIdentifier": [
    {
      "myString"
    },
    ...

As explained in the comments, the intended behavior is to write Name and TypeName into the json, instead of the name of the class.

Xavier Peña
  • 7,399
  • 9
  • 57
  • 99
  • 2
    well what was your intention then? – TheVillageIdiot Dec 01 '15 at 13:16
  • @TheVillageIdiot The intended behavior is the seralizytion of its parameters, that is to say, something like `{"Name": "myName", "TypeName": "typeName"}`. – Xavier Peña Dec 01 '15 at 13:19
  • What is `Parameters`? What are the members of `ParameterDictionary`? Read [mcve] first. – Arghya C Dec 01 '15 at 13:22
  • @ArghyaC Since the Parameters class was correctly serialized, I thought it was not necessary to describe it in the post. Furthermore, it is a nasty piece of heritage code which would not provide much to the current question. I hope this helps. – Xavier Peña Dec 01 '15 at 13:24
  • @ArghyaC Ok, now I get it. If it really didn't matter, I could have written `string` or whatever to make anybody copy the code and paste it into their IDE. I'm going to edit the post. – Xavier Peña Dec 01 '15 at 13:38

2 Answers2

6

The reason you are seeing this behavior is because you are using your complex object (HydroObjectIdentifier) as the key in a Dictionary. In JSON, object keys must always be strings, per the spec. When Json.Net tries to serialize the dictionary, it sees that your keys are not strings. Since it needs a string, it simply calls ToString() on your class. The default implementation of ToString() in C# returns the name of the type, which in your case is RSEngine.HydroObjectIdentifier.

If you implement your own ToString() method, as was suggested in another answer, then you can make the key whatever you want to get around the issue. However, the downside of this approach is that you will not be able to deserialize the JSON back into your dictionary. That is because there is no converse "FromString" method that Json.Net can use to convert the serialized key from a string back into your identifier class. If you need to be able to go the full round trip with your JSON (serialize and deserialize) then you will need a different solution.

There are a couple of possible ways to handle complex dictionary keys in Json.Net:

  1. Implement a TypeConverter for your identifier class, as is mentioned in the Json.Net Serialization Guide. See How to: Implement a Type Converter in MSDN for details. After implementing the type converter, you will need to mark your class with a [TypeConverter] attribute so Json.Net knows to use it.
  2. Use a custom JsonConverter for the dictionary which changes how the key-value pairs are written to the JSON. See How To Serialize a class that derives from a Dictionary for an example of that approach.
Brian Rogers
  • 125,747
  • 31
  • 299
  • 300
  • This is the answer I was looking for. Very thorough and clear, thanks. I changed my choice and marked this as the accepted answer. As a side note: I don't quite understand why the question was downvoted, since I think your answer shows that the issue is not trivial. I did my research before asking, and it was difficult to find the key words that could lead to a solution. Thanks again for taking the time to answer, and I hope the answer will help other people looking for the same solution. – Xavier Peña Dec 01 '15 at 20:14
  • Glad my answer helped you. As to why the question was downvoted, who can say? I felt it was a reasonable question. I wouldn't worry about it too much. – Brian Rogers Dec 01 '15 at 21:11
4

Try to override the ToString() method:

public struct HydroObjectIdentifier
{

    public string Name { get; set; }
    public string TypeName { get; set; }

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

    public HydroObjectIdentifier(string name, string typeName)
    {
        this.Name = name;
        this.TypeName = typeName;
    }

}
dariogriffo
  • 4,148
  • 3
  • 17
  • 34
  • 1
    This certainly does the trick. Any ideas about why it needs to be done like this in this particular case? I have used Json.Net in quite a large number of projects and it is the first time I encounter this problem. – Xavier Peña Dec 01 '15 at 13:47
  • 2
    @XavierPeña It is because you are using your object as a key in a dictionary. I added an answer explaining this in more detail. – Brian Rogers Dec 01 '15 at 17:49