Json.Net serializing the class name instead of the internal properties

ε祈祈猫儿з 提交于 2019-12-19 20:47:26

问题


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.


回答1:


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.



回答2:


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;
    }

}


来源:https://stackoverflow.com/questions/34021338/json-net-serializing-the-class-name-instead-of-the-internal-properties

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