Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 您会编写一个自定义Json.net序列化程序/反序列化程序吗?_C#_Json_Json.net - Fatal编程技术网

C# 您会编写一个自定义Json.net序列化程序/反序列化程序吗?

C# 您会编写一个自定义Json.net序列化程序/反序列化程序吗?,c#,json,json.net,C#,Json,Json.net,我开始认为我需要在Json.net中编写一个自定义转换器,因为到目前为止,常规转换器并没有对其进行裁剪。 我有一个班级比赛 class Game { [JsonProperty] public int Edition { get; private set; } [JsonProperty] public string Name { get { return

我开始认为我需要在Json.net中编写一个自定义转换器,因为到目前为止,常规转换器并没有对其进行裁剪。 我有一个班级比赛

class Game  
    {
        [JsonProperty]
        public int Edition { get; private set; }
        [JsonProperty]
        public string Name
    {
        get
        {
            return NameType.ToString();
        }
        private set
        {
            name = value;
        }
    }

    [JsonProperty]
    public string Date { get; private set; }

    [JsonConverter(typeof(StringEnumConverter))]  
    public GameType NameType { get; private set; } //enum

    public List<Question> Questions { get; private set; } //yes, Question is serializable

public Game(string Date, GameType t, int num, List<Question> q)
    {
        this.Date = Date;
        this.NameType = t;
        this.Edition = num;
        this.Questions = q;
    }
但当反序列化时,结果是:

日期“2016年”, 第1版, 名称“无”, 名称类型“无”, 问题无效

我在反序列化时是否犯了错误:

{
 "Edition": 1,
  "Name": "Regular",
  "Date": "2016",
  "NameType": "Regular",
  "Questions": [
    {
      "QuestionNumber": "1",
      "Round": 1,
      "Body": "1. A couple paragraphs",
      "Answer": " answers"
    },
  ]
}
 Game g = JsonConvert.DeserializeObject<Game>(File.ReadAllText(path));
Game g=JsonConvert.DeserializeObject(File.ReadAllText(path));
或者这是一种需要编写自定义序列化程序的情况


我目前还没有找到一个问题可以解决我遇到的这个bug,根据Json.net上的文档,大多数需要自定义序列化程序的奇怪边缘情况都是非IEnumerables和DateTimes

看起来好像您正试图创建一个具有一些不可变属性的类,即
Edition
Date
NameType
。此外,其中一个名为
NameType
,需要应用
JsonConverter
,以便通过Json.NET正确序列化。如果参数名称与c#属性名称相同,则可以为此类类指定一个构造函数,并将必要的属性作为参数传入,从而对其进行序列化和反序列化。您可以将属性
[JsonConverter(typeof(StringEnumConverter))]
应用于相应的参数以及相应的属性,但是如果未应用该属性,则将使用相应属性中的转换器,只要属性和参数具有相同的声明


这是完全合乎逻辑的:因为所有的setter都是private
private set
和name甚至没有setter,所以如果您尝试以同样的方式反序列化它,我也不会将其添加为json属性。“我开始认为我需要在json.net中编写一个自定义转换器,因为到目前为止,常规转换器没有剪切它。”:@MickyD,是的,我所做的是非常愚蠢的。所以我真的应该回顾一下我一个月前写的代码。。。。。很好。现在我的桌子上有一个大大的印花face@Alexandre英雄联盟不用担心,伙计:)
class Game  
{
    public Game(int edition, string date, [JsonConverter(typeof(StringEnumConverter))] GameType nameType)
    {
        this.Edition = edition;
        this.Date = date;
        this.NameType = nameType;
        this.Questions = new List<Question>();
    }

    public int Edition { get; private set; }

    [JsonIgnore]
    public string Name
    {
        get
        {
            return NameType.ToString();
        }
    }

    public string Date { get; private set; }

    [JsonConverter(typeof(StringEnumConverter))]  
    public GameType NameType { get; private set; } //enum

    public List<Question> Questions { get; private set; } //yes, Question is serializable
}
    public Game(int edition, string date, [JsonConverter(typeof(StringEnumConverter))] GameType nameType) 
        : this(edition, date, nameType, new List<Question>())
    {
    }

    [JsonConstructor]       
    public Game(int edition, string date, [JsonConverter(typeof(StringEnumConverter))] GameType nameType, List<Question> questions)
    {
        this.Edition = edition;
        this.Date = date;
        this.NameType = nameType;
        this.Questions = questions;
    }