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.
Json namespace provides functionality for serializing to and deserializing from JavaScript Object Notation (JSON). Serialization is the process of converting the state of an object, that is, the values of its properties, into a form that can be stored or transmitted.
JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object). If you serialize this result it will generate a text with the structure and the record returned.
The Jackson Annotation @JsonProperty is used on a property or method during serialization or deserialization of JSON. It takes an optional 'name' parameter which is useful in case the property name is different than 'key' name in JSON.
The json module exposes two methods for serializing Python objects into JSON format. dump() will write Python data to a file-like object. We use this when we want to serialize our Python data to an external JSON file. dumps() will write Python data to a string in JSON format.
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;
}
}
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.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With