Reputation: 7919
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.
Upvotes: 5
Views: 4476
Reputation: 129807
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:
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.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.Upvotes: 6
Reputation: 4274
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;
}
}
Upvotes: 4