当前位置: 首页 > news >正文

c#:System.Text.Json 的使用一

环境:

  • .net 6.0
  • vs2022

参考:
从 Newtonsoft.Json 迁移到 System.Text.Json
System.Text.Json 常规用法

一、写入时的控制

1.1 非ascii码转换

直接看代码:

var str = System.Text.Json.JsonSerializer.Serialize(new Model { Id = 1, Name = "小明" });
Console.WriteLine(str);
//out: {"Id":1,"Name":"\u5C0F\u660E"}public class Model
{public int Id { get; set; }public string Name { get; set; }
}

为了能输出 “小明” 而不是 “\u5C0F\u660E”,我们需要显示声明编码方法:

var str = System.Text.Json.JsonSerializer.Serialize(new Model { Id = 1, Name = "小明" }, new System.Text.Json.JsonSerializerOptions
{Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
});
//out: {"Id":1,"Name":"小明"}
public class Model
{public int Id { get; set; }public string Name { get; set; }
}

比对:Newtonsoft.Json 默认就是 “小明”

1.2 首字母小写

默认类属性和dictionary的key都是原样输出,如:

var str = System.Text.Json.JsonSerializer.Serialize(new Model
{Id = 1,Name = "小明",Properties = new Dictionary<string, object> { { "Age", 18 }, { "Other", new { Name = "小刚" } } }
}, new System.Text.Json.JsonSerializerOptions
{Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
});
Console.WriteLine(str);
//out: {"Id":1,"Name":"小明","Properties":{"Age":18,"Other":{"Name":"小刚"}}}public class Model
{public int Id { get; set; }public string Name { get; set; }public Dictionary<string, object> Properties { get; set; }
}

如果想首字母小写,则:

var str = System.Text.Json.JsonSerializer.Serialize(new Model
{Id = 1,Name = "小明",Properties = new Dictionary<string, object> { { "Age", 18 }, { "Other", new { Name = "小刚" } } }
}, new System.Text.Json.JsonSerializerOptions
{Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping,//属性名首字母小写PropertyNamingPolicy = System.Text.Json.JsonNamingPolicy.CamelCase,//字典key首字母小写DictionaryKeyPolicy = System.Text.Json.JsonNamingPolicy.CamelCase,
});
Console.WriteLine(str);
//out: {"id":1,"name":"小明","properties":{"age":18,"other":{"name":"小刚"}}}public class Model
{public int Id { get; set; }public string Name { get; set; }public Dictionary<string, object> Properties { get; set; }
}

比对:Newtonsoft.Json 默认也是原样,也可以控制属性首字母小写(但为找到控制字典key首字母小写的方法)

1.3 缩进格式输出

默认输出是一行(上面的效果),可以指定缩进输出,如下:

var str = System.Text.Json.JsonSerializer.Serialize(new Model
{Id = 1,Name = "小明",Properties = new Dictionary<string, object> { { "Age", 18 }, { "Other", new { Name = "小刚" } } }
}, new System.Text.Json.JsonSerializerOptions
{Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping,//缩进输出WriteIndented = true,
});
Console.WriteLine(str);
/* out:
{"Id": 1,"Name": "小明","Properties": {"Age": 18,"Other": {"Name": "小刚"}}
}
*/public class Model
{public int Id { get; set; }public string Name { get; set; }public Dictionary<string, object> Properties { get; set; }
}

比对:Newtonsoft.Json 默认也是一行,也可以控制缩进

1.4 将枚举转成字符串输出

默认枚举转为数字输出,如下:

var str = System.Text.Json.JsonSerializer.Serialize(new Model
{Id = 1,Name = "小明",Sex = EnumSex.Male,
}, new System.Text.Json.JsonSerializerOptions
{Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
});
Console.WriteLine(str);
//out: {"Id":1,"Name":"小明","Sex":0}public class Model
{public int Id { get; set; }public string Name { get; set; }public EnumSex Sex { get; set; }
}
public enum EnumSex
{Male,FeMale
}

可以添加转换器来转换成字符串:

var options = new System.Text.Json.JsonSerializerOptions
{Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
};
options.Converters.Add(new System.Text.Json.Serialization.JsonStringEnumConverter());
var str = System.Text.Json.JsonSerializer.Serialize(new Model
{Id = 1,Name = "小明",Sex = EnumSex.Male,
}, options);
Console.WriteLine(str);
//out: {"Id":1,"Name":"小明","Sex":"Male"}public class Model
{public int Id { get; set; }public string Name { get; set; }public EnumSex Sex { get; set; }
}
public enum EnumSex
{Male,FeMale
}

比对:Newtonsoft.Json 默认也是转为数字,也可以控制转为字符串

1.5 忽略null值属性的输出

默认情况下,如果某个属性值为null,也会被输出,如下:

var options = new System.Text.Json.JsonSerializerOptions
{Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping
};
var str = System.Text.Json.JsonSerializer.Serialize(new Model
{Id = 1,Name = null
}, options);
Console.WriteLine(str);
//out: {"Id":1,"Name":null}public class Model
{public int Id { get; set; }public string Name { get; set; }
}

如果不想输出null值属性,如下:

var options = new System.Text.Json.JsonSerializerOptions
{Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping,DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull,
};
var str = System.Text.Json.JsonSerializer.Serialize(new Model
{Id = 1,Name = null
}, options);
Console.WriteLine(str);
//out: {"Id":1}public class Model
{public int Id { get; set; }public string Name { get; set; }
}

比对:Newtonsoft.Json 默认也是输出null值属性,也可以控制不输出

1.6 忽略循环引用

默认情况下,system.text.json会直接报错,如下:

var options = new System.Text.Json.JsonSerializerOptions
{Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
};
var fu = new Model
{Id = 1,Name = "小明",Parent = null,Children = new List<Model> { new Model { Id = 2, Name = "小明2", Parent = null } }
};
fu.Children[0].Parent = fu;
var str = System.Text.Json.JsonSerializer.Serialize(fu, options);
Console.WriteLine(str);
/*expception:
Unhandled exception. System.Text.Json.JsonException: A possible object cycle was detected. This can either be due to a cycle or if the object depth is larger than the maximum allowed depth of 64. Consider using ReferenceHandler.Preserve on JsonSerializerOptions to support cycles. Path: $.Children.Parent.Children.Parent
*/public class Model
{public int Id { get; set; }public string Name { get; set; }public Model Parent { get; set; }public List<Model> Children { get; set; }
}

我们可以设置忽略循环引用,如下:

var options = new System.Text.Json.JsonSerializerOptions
{Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping,//忽略循环引用ReferenceHandler = System.Text.Json.Serialization.ReferenceHandler.IgnoreCycles,
};
var fu = new Model
{Id = 1,Name = "小明",Parent = null,Children = new List<Model> { new Model { Id = 2, Name = "小明2", Parent = null } }
};
fu.Children[0].Parent = fu;
var str = System.Text.Json.JsonSerializer.Serialize(fu, options);
Console.WriteLine(str);
//out: {"Id":1,"Name":"小明","Parent":null,"Children":[{"Id":2,"Name":"小明2","Parent":null,"Children":null}]}public class Model
{public int Id { get; set; }public string Name { get; set; }public Model Parent { get; set; }public List<Model> Children { get; set; }
}

比对:Newtonsoft.Json 默认也是检测到循环报错,也可以控制忽略循环引用

1.7 允许输出Field

默认情况下,Field不会被输出,如下:

var options = new System.Text.Json.JsonSerializerOptions
{Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
};
var str = System.Text.Json.JsonSerializer.Serialize(new Model
{Id = 1,Name = "小明",FieldAge = 18,
}, options);
Console.WriteLine(str);
//out: {"Id":1,"Name":"小明"}public class Model
{public int Id { get; set; }public string Name { get; set; }public int FieldAge;
}

为了输出Field,如下:

var options = new System.Text.Json.JsonSerializerOptions
{Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping,// 输出 FieldIncludeFields = true,
};
var str = System.Text.Json.JsonSerializer.Serialize(new Model
{Id = 1,Name = "小明",FieldAge = 18,
}, options);
Console.WriteLine(str);
//out: {{"Id":1,"Name":"小明","FieldAge":18}public class Model
{public int Id { get; set; }public string Name { get; set; }public int FieldAge;
}

比对:Newtonsoft.Json 默认输出Field,并且只读Field也会输出,这点需要注意!

1.8 禁止输出只读Field

当我们允许输出Field的时候,只读的Field也会被输出,如下:

var options = new System.Text.Json.JsonSerializerOptions
{Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping,//允许输出FieldIncludeFields = true,
};
var str = System.Text.Json.JsonSerializer.Serialize(new Model
{Id = 1,Name = "小明",FieldAge = 18,
}, options);
Console.WriteLine(str);
//out: {"Id":1,"Name":"小明","FieldAge":18,"ReadOnlyFieldAge":15}public class Model
{public int Id { get; set; }public string Name { get; set; }public int FieldAge;public readonly int ReadOnlyFieldAge = 15;
}

如果我们不想输出只读的Field,如下:

var options = new System.Text.Json.JsonSerializerOptions
{Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping,//允许输出FieldIncludeFields = true,//忽略只读FieldIgnoreReadOnlyFields = true,
};
var str = System.Text.Json.JsonSerializer.Serialize(new Model
{Id = 1,Name = "小明",FieldAge = 18,
}, options);
Console.WriteLine(str);
//out: {"Id":1,"Name":"小明","FieldAge":18}public class Model
{public int Id { get; set; }public string Name { get; set; }public int FieldAge;public readonly int ReadOnlyFieldAge = 15;
}

比对:Newtonsoft.Json 默认输出Field,并且只读Field也会输出,这点需要注意!

1.9 禁止输出只读属性

上面对只读的Field做了控制,也可以对只读属性做控制,默认情况下会输出只读属性:

var options = new System.Text.Json.JsonSerializerOptions
{Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
};
var str = System.Text.Json.JsonSerializer.Serialize(new Model
{Id = 1,Name = "小明",
}, options);
Console.WriteLine(str);
//out: {"Id":1,"Name":"小明","IsAdmin":true}public class Model
{public int Id { get; set; }public string Name { get; set; }public bool IsAdmin => Name == "小明";
}

在某些场景下,减少只读属性的输出会便于存储和数据传递(体积减少),但给前端输出数据慎用,如下:

var options = new System.Text.Json.JsonSerializerOptions
{Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping,//忽略只读属性IgnoreReadOnlyProperties = true,
};
var str = System.Text.Json.JsonSerializer.Serialize(new Model
{Id = 1,Name = "小明",
}, options);
Console.WriteLine(str);
//out: {"Id":1,"Name":"小明"}public class Model
{public int Id { get; set; }public string Name { get; set; }public bool IsAdmin => Name == "小明";
}

比对:Newtonsoft.Json 默认输出只读属性,没有找到禁止的方法。

1.10 自定义日期格式(DateTime、DateTimeOffset)

默认情况下输出的格式为 ISO 8601-1:2019 参考百度百科 ,默认类似:


var options = new System.Text.Json.JsonSerializerOptions
{Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping,WriteIndented = true,
};
var str = System.Text.Json.JsonSerializer.Serialize(new Model
{Id = 1,Name = "小明",sDateTime = DateTime.Parse("2023-03-04 18:36:12.1234567+08:00"),sDateTimeOffset = DateTime.Parse("2023-03-04 18:36:12.1234567+08:00"),sTimeSpan = new TimeSpan(1, 2, 3, 4, 5),
}, options);
Console.WriteLine(str);
/*out:
{"Id": 1,"Name": "小明","sDateTime": "2023-03-04T18:36:12.1234567+08:00","sDateTimeOffset": "2023-03-04T18:36:12.1234567+08:00","sTimeSpan": "1.02:03:04.0050000"
}
*/public class Model
{public int Id { get; set; }public string Name { get; set; }public DateTime sDateTime { get; set; }public DateTimeOffset sDateTimeOffset { get; set; }public TimeSpan sTimeSpan { get; set; }
}

如果我们想支持自定义的格式,如下:

var options = new System.Text.Json.JsonSerializerOptions
{Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping,WriteIndented = true,
};
options.Converters.Add(new JsonConverterDatetime("yyyy-MM-dd HH:mm:ss.fff zz"));
options.Converters.Add(new JsonConverterDateTimeOffset("yyyy-MM-dd HH:mm:ss.fff zz"));
var str = System.Text.Json.JsonSerializer.Serialize(new Model
{Id = 1,Name = "小明",sDateTime = DateTime.Parse("2023-03-04 18:36:12.1234567+08:00"),sDateTimeOffset = DateTime.Parse("2023-03-04 18:36:12.1234567+08:00"),sTimeSpan = new TimeSpan(1, 2, 3, 4, 5),
}, options);
Console.WriteLine(str);
/*out:
{"Id": 1,"Name": "小明","sDateTime": "2023-03-04 18:36:12.123 +08","sDateTimeOffset": "2023-03-04 18:36:12.123 +08","sTimeSpan": "1.02:03:04.0050000"
}
*/public class Model
{public int Id { get; set; }public string Name { get; set; }public DateTime sDateTime { get; set; }public DateTimeOffset sDateTimeOffset { get; set; }//public DateOnly sDateOnly { get; set; }//public TimeOnly sTimeOnly { get; set; }public TimeSpan sTimeSpan { get; set; }
}public class JsonConverterDatetime : System.Text.Json.Serialization.JsonConverter<DateTime>
{public string Format { get; set; }public JsonConverterDatetime(string format){if (format == null) throw new ArgumentNullException("format");Format = format;}public override DateTime Read(ref System.Text.Json.Utf8JsonReader reader, Type typeToConvert, System.Text.Json.JsonSerializerOptions options){return DateTime.Parse(reader.GetString());}public override void Write(System.Text.Json.Utf8JsonWriter writer, DateTime value, System.Text.Json.JsonSerializerOptions options){writer.WriteStringValue(value.ToString(Format));}
}public class JsonConverterDateTimeOffset : System.Text.Json.Serialization.JsonConverter<DateTimeOffset>
{public string Format { get; set; }public JsonConverterDateTimeOffset(string format){if (format == null) throw new ArgumentNullException("format");Format = format;}public override DateTimeOffset Read(ref System.Text.Json.Utf8JsonReader reader, Type typeToConvert, System.Text.Json.JsonSerializerOptions options){return DateTimeOffset.Parse(reader.GetString());}public override void Write(System.Text.Json.Utf8JsonWriter writer, DateTimeOffset value, System.Text.Json.JsonSerializerOptions options){writer.WriteStringValue(value.ToString(Format));}
}

顺便说下:ISO的这种时间格式兼容性还是比较好的,比如:mysql中可以插入 '2023-03-04T18:36:12.1234567+08:00',但不能插入 '2023-03-04 18:36:12.1234567 +08:00',原因是因为后面的 “+08:00” 前不能有空格。

比对:Newtonsoft.Json 默认也是ISO的格式,也支持自定义格式。

1.11 支持输出 DateOnly、TimeOnly

默认.net 6 (.net7可以)不支持 DateOnly、TimeOnly,报错如下:

  • Unhandled exception. System.NotSupportedException: Serialization and deserialization of ‘System.DateOnly’ instances are not supported
  • Unhandled exception. System.NotSupportedException: Serialization and deserialization of ‘System.TimeOnly’ instances are not supported

可以自定义转换器支持,如下:

var options = new System.Text.Json.JsonSerializerOptions
{Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping,WriteIndented = true,
};
options.Converters.Add(new JsonConverterDateOnly("yyyy-MM-dd"));
options.Converters.Add(new JsonConverterTimeOnly("HH:mm:ss.fffffff"));
var str = System.Text.Json.JsonSerializer.Serialize(new Model
{Id = 1,Name = "小明",sDateOnly = DateOnly.Parse("2023-03-04"),sTimeOnly = TimeOnly.Parse("18:36:12.1234567"),
}, options);
Console.WriteLine(str);
/*out:
{"Id": 1,"Name": "小明","sDateOnly": "2023-03-04","sTimeOnly": "18:36:12.1234567"
}
*/public class Model
{public int Id { get; set; }public string Name { get; set; }public DateOnly sDateOnly { get; set; }public TimeOnly sTimeOnly { get; set; }
}public class JsonConverterDateOnly : System.Text.Json.Serialization.JsonConverter<DateOnly>
{public string Format { get; set; }public JsonConverterDateOnly(string format){if (format == null) throw new ArgumentNullException("format");Format = format;}public override DateOnly Read(ref System.Text.Json.Utf8JsonReader reader, Type typeToConvert, System.Text.Json.JsonSerializerOptions options){return DateOnly.Parse(reader.GetString());}public override void Write(System.Text.Json.Utf8JsonWriter writer, DateOnly value, System.Text.Json.JsonSerializerOptions options){writer.WriteStringValue(value.ToString(Format));}
}public class JsonConverterTimeOnly : System.Text.Json.Serialization.JsonConverter<TimeOnly>
{public string Format { get; set; }public JsonConverterTimeOnly(string format){if (format == null) throw new ArgumentNullException("format");Format = format;}public override TimeOnly Read(ref System.Text.Json.Utf8JsonReader reader, Type typeToConvert, System.Text.Json.JsonSerializerOptions options){return TimeOnly.Parse(reader.GetString());}public override void Write(System.Text.Json.Utf8JsonWriter writer, TimeOnly value, System.Text.Json.JsonSerializerOptions options){writer.WriteStringValue(value.ToString(Format));}
}

比对:新版本的Newtonsoft.Json 支持DateOnly 和 TimeOnly

1.12 将数字输出为字符串

有的时候,我们需要将数字输出为字符串,如下:

var options = new System.Text.Json.JsonSerializerOptions
{//输出是 number => string, 读取时允许 string => numberNumberHandling = System.Text.Json.Serialization.JsonNumberHandling.AllowReadingFromString | System.Text.Json.Serialization.JsonNumberHandling.WriteAsString,
};var json = System.Text.Json.JsonSerializer.Serialize(new Model
{Id = 1,Score = 99.5f
}, options);
Console.WriteLine(json);
//out: {"Id":"1","Score":"99.5"}
public class Model
{public int Id { get; set; }public float Score { get; set; }
}

比对:Newtonsoft.Json 可以通过转换器将数字转为字符串。

1.13 忽略默认值的属性

默认情况下:属性有默认值也会被输出,我们可以控制当属性/字段有默认值的时候不再输出(传递给前端时慎用):

var options = new System.Text.Json.JsonSerializerOptions
{DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingDefault,IncludeFields = true,IgnoreReadOnlyFields = false,IgnoreReadOnlyProperties = false,
};var json = System.Text.Json.JsonSerializer.Serialize(new Model(), options);
Console.WriteLine(json);
//out: {"Id1":1,"IdR1":1,"IdF1":1,"IdFR1":1}
public class Model
{public int Id0 { get; set; } = 0;public int Id1 { get; set; } = 1;public int IdR0 => Id0;public int IdR1 => Id1;public int IdF0 = 0;public int IdF1 = 1;public readonly int IdFR0 = 0;public readonly int IdFR1 = 1;
}

比对:Newtonsoft.Json 默认也会输出默认值的属性,也可以设置禁止输出。

1.14 在类定义时声明忽略某些属性

[System.Text.Json.Serialization.JsonIgnore]
public int Age { get; set; }

1.15 在类定义时声明控制属性输出的顺序

[Newtonsoft.Json.JsonProperty(Order = 0)]
public int Age { get; set; }

1.16 在类定义时声明控制属性输出的名称

[System.Text.Json.Serialization.JsonPropertyName("catAge")]
public int Age { get; set; }

二、读取时控制(反序列化为模型)

2.1 允许json中多余的逗号

默认情况下,不允许json中存在多余的逗号,如:

{"Id": 1,"Name": "小明",
}

这本身并没有错,因为标准json格式是严格的,不允许这个。。。
如果我们读取上面字符串,就会报错如下:

Unhandled exception. System.Text.Json.JsonException: The JSON object contains a trailing comma at the end which is not supported in this mode. Change the reader options. Path: $ | LineNumber: 4 | BytePositionInLine: 0.
—> System.Text.Json.JsonReaderException: The JSON object contains …

为了能顺利读取到,我们可以使用如下方式:

var options = new System.Text.Json.JsonSerializerOptions
{AllowTrailingCommas = true,
};
var json = @"
{""Id"": 1,""Name"": ""小明"",
}
";
var model = System.Text.Json.JsonSerializer.Deserialize<Model>(json, options);Console.WriteLine($"model.Id={model.Id},model.Id={model.Name}");
//out: model.Id=1,model.Id=小明public class Model
{public int Id { get; set; }public string Name { get; set; }
}

2.2 允许json中存在注释

默认情况下,不允许json中存在注释,如:

//注释
/*
注释
*/
{//注释"Id": 1,"Name": "小明",
}

这本身并没有错,因为标准json格式是严格的,不允许这个。。。
如果我们读取上面字符串,就会报错如下:

Unhandled exception. System.Text.Json.JsonException: ‘/’ is an invalid start of a value. Path: $ | LineNumber: 1 | BytePositionInLine: 0.
—> System.Text.Json.JsonReaderException: ‘/’ is an invalid start of a value. …

为了能顺利读取到,我们可以使用如下方式:

var options = new System.Text.Json.JsonSerializerOptions
{//因为我们是直接反序列化为模型,所以这里 跳过注释ReadCommentHandling = System.Text.Json.JsonCommentHandling.Skip,
};
var json = @"
//注释
/*
注释
*/
{//注释""Id"": 1,""Name"": ""小明""
}
";
var model = System.Text.Json.JsonSerializer.Deserialize<Model>(json, options);Console.WriteLine($"model.Id={model.Id},model.Name={model.Name}");
//out: model.Id=1,model.Name=小明

2.3 允许将json中的 “123” => 123,“NaN” => float.NaN

默认情况下,json中的字符串不能转为数字,如下,会报错:

var json = @"
{""Id"": ""123"",
}
";
var model = System.Text.Json.JsonSerializer.Deserialize<Model>(json);
/*exception:
Unhandled exception. System.Text.Json.JsonException: The JSON value could not be converted to System.Int32. Path: $.Id | LineNumber: 2 | BytePositionInLine: 13.
*/public class Model
{public int Id { get; set; }
}

如果我们想实现自动转换,如下:

var options = new System.Text.Json.JsonSerializerOptions
{//允许将 "123" 转为数字 123NumberHandling = System.Text.Json.Serialization.JsonNumberHandling.AllowReadingFromString,
};
var json = @"
{""Id"": ""123"",""Score"":""NaN""
}
";
var model = System.Text.Json.JsonSerializer.Deserialize<Model>(json, options);Console.WriteLine($"model.Id={model.Id},model.Score={model.Score}");
//model.Id=123,model.Score=NaNpublic class Model
{public int Id { get; set; }public float Score { get; set; }
}

顺便说一下:float中的NaN,Infinity,-Infinity 都是有意义的。

相关文章:

c#:System.Text.Json 的使用一

环境&#xff1a; .net 6.0vs2022 参考&#xff1a; 从 Newtonsoft.Json 迁移到 System.Text.Json System.Text.Json 常规用法 一、写入时的控制 1.1 非ascii码转换 直接看代码&#xff1a; var str System.Text.Json.JsonSerializer.Serialize(new Model { Id 1, Name …...

kaggle数据集下载当中所遇到的问题

kaggle数据集下载当中所遇到的问题报错分析pip install kagglethe SSL module is not available解决方法pip的版本升级解决办法下载kaggle包kaggle数据集下载问题解决参考内容报错分析 今天在尝试使用pip install kaggle的方法去下载我需要的数据集的时候遇到了一些报错的问题…...

TEX:高阶用法

文章目录定制LATEX记数器创建记数器改变记数器的值显示记数器的值长度橡皮长度用户定义命令用户定义的环境标题定制正文中标题设置使用titlesec宏包设置标题格式目录中标题设置LATEX 2ε\varepsilonε程序设计语言命令的层次文件识别上载其他类和宏包输入文件检测文件选项的处理…...

UML 类图

车的类图结构为<>&#xff0c;表示车是一个抽象类&#xff1b; 它有两个继承类&#xff1a;小汽车和自行车&#xff1b;它们之间的关系为实现关系&#xff0c;使用带空心箭头的虚线表示&#xff1b; 小汽车为与SUV之间也是继承关系&#xff0c;它们之间的关系为泛化关系…...

项目实战典型案例1——redis只管存不管删除 让失效时间删除的问题

redis只管存不管删除 让失效时间删除的问题一&#xff1a;背景介绍二&#xff1a;思路&方案三&#xff1a;代码模拟1.错误示范通过班级id查询课程名称执行结果通过班级id修改课程名称&#xff08;并没有删除对应缓存&#xff09;执行结果2.正确示范在错误示范的更新接口上添…...

@RequestParam和@PathVariable的用法与区别

PathVariable PathVariable 映射 URL 绑定的占位符带占位符的 URL 是 Spring3.0 新增的功能&#xff0c;该功能在SpringMVC 向 REST 目标挺进发展过程中具有里程碑的意义通过 PathVariable 可以将 URL 中占位符参数绑定到控制器处理方法的入参中&#xff1a;URL 中的 {xxx} 占…...

【大数据 AI 人工智能】数据科学家必学的 9 个核心机器学习算法

如今,机器学习正改变着我们的世界。借助机器学习(ML),谷歌在为我们推荐搜索结果,奈飞在为我们推荐观看影片,脸书在为我们推荐可能认识的朋友。 机器学习从未像在今天这样重要。但与此同时,机器学习这一领域也充斥着各种术语,晦涩难懂,各种机器学习的算法每年层出不穷…...

IronPDF for .NET 2023.2.4 Crack

适用于 .NET 2023.2.4 的 IronPDF 添加对增量 PDF 保存的支持。 2023 年 3 月 2 日 - 10:23新版本 特征 添加了对 IronPdfEngine Docker 的支持。 添加了对增量 PDF 保存的支持。 重新设计了 PDF 签名和签名。 删除了 iTextSharp 依赖项。 在文本页眉/页脚中添加了 DrawDivider…...

3.4-前端的10个问题

01、null和undefined undefined是全局对象的一个属性&#xff0c;当一个变量没有赋值或者访问一个对象不存在的属性&#xff0c;这时候都是undefined。 null&#xff1a;表示是一个空对象。在需要释放一个对象的时候&#xff0c;直接赋值为null即可。 02、箭头函数 箭头函数…...

开发手册——一、编程规约_9.其他

这篇文章主要梳理了在java的实际开发过程中的编程规范问题。本篇文章主要借鉴于《阿里巴巴java开发手册终极版》 下面我们一起来看一下吧。 1. 【强制】在使用正则表达式时&#xff0c;利用好其预编译功能&#xff0c;可以有效加快正则匹配速度。 说明&#xff1a;不要在方法…...

23.3.4打卡 AtCoder Beginner Contest 291(Sponsored by TOYOTA SYSTEMS)A~E

F题题面都看不懂嘞!开摆! 没找到合适的markdown, 截图网页翻译了我真是天才 比赛链接: https://atcoder.jp/contests/abc291 A题 题意 给出一个字符串, 找到第一个大写字母的下标 简单题就不多说了, 直接放代码 代码 void solve() {cin>>str;nstr.size();str"…...

Gem5模拟器,一些运行的小tips(十一)

一些基础知识&#xff0c;下面提到的东西与前面的文章有一定的关系&#xff0c;感兴趣的小伙伴可以看一下&#xff1a; (21条消息) Gem5模拟器&#xff0c;全流程运行Chiplet-Gem5-SharedMemory-main&#xff08;十&#xff09;_好啊啊啊啊的博客-CSDN博客 Gem5模拟器&#xf…...

【JAVA】List接口

&#x1f3c6;今日学习目标&#xff1a;List接口 &#x1f603;创作者&#xff1a;颜颜yan_ ✨个人主页&#xff1a;颜颜yan_的个人主页 ⏰本期期数&#xff1a;第四期 &#x1f389;专栏系列&#xff1a;JAVA List接口一、ArrayList二、LinkedList总结一、ArrayList ArrayLis…...

Hbase RegionServer的核心模块

RegionServer是HBase系统中最核心的组件&#xff0c;主要负责用户数据写入、读取等基础操作。RegionServer组件实际上是一个综合体系&#xff0c;包含多个各司其职的核心模块&#xff1a;HLog、MemStore、HFile以及BlockCache。 一、RegionServer内部结构 RegionServer是HBas…...

【Java开发】JUC进阶 01:Lock锁详解

1 Lock锁介绍已经在【JUC基础】04简单介绍过了&#xff0c;本文做进一步的拓展&#xff0c;比如公平锁和非公平锁、&#x1f4cc; 明白锁的核心四个对象&#xff1a;线程&#xff0c;共享资源&#xff0c;锁&#xff0c;锁操作包括线程如何操作资源&#xff0c;使用锁锁哪个资源…...

关于登录校验的解决方案以及原理(回顾知识点)--项目开发那点事(自问自答版本)

开始前奏&#xff1a; 嘻嘻&#x1f604; 通常一个完整的系统&#xff0c;需要安全性的保证。如登录校验&#xff0c;登录成功后&#xff0c;才可以访问服务资源。在服务端渲染项目中&#xff0c;我们通常使用 session来进行登录校验。在前后端分离的场景中&#xff0c;很多时…...

【数据结构】邻接矩阵和邻接图的遍历

写在前面 本篇文章开始学习数据结构的图的相关知识&#xff0c;涉及的基本概念还是很多的。本文的行文思路:学习图的基本概念学习图的存储结构——本文主要介绍邻接矩阵和邻接表对每种结构进行深度优先遍历和广度优先遍历先识概念话不多说&#xff0c;狠活献上学习思想等等&…...

设计跳表(动态设置节点高度)

最近学习redis的zset时候&#xff0c;又看到跳表的思想&#xff0c;突然对跳表的设置有了新的思考 这是19年设计的跳表&#xff0c;在leetcode的执行时间是200ms 现在我对跳表有了新的想法 1、跳表的设计&#xff0c;类似二分查找&#xff0c;但是不是二分查找&#xff0c;比较…...

基于神经辐射场(Neural Radiance Fileds, NeRF)的三维重建- 简介(1)

Nerf简介 Nerf&#xff08;neural Radiance Fileds&#xff09; 为2020年ICCV上提出的一个基于隐式表达的三维重建方法&#xff0c;使用2D的 Posed Imageds 来生成&#xff08;表达&#xff09;复杂的三维场景。现在越来越多的研究人员开始关注这个潜力巨大的领域&#xff0c;也…...

【AI面试】NMS 与 Soft NMS 的辨析

往期文章&#xff1a; AI/CV面试&#xff0c;直达目录汇总【AI面试】L1 loss、L2 loss和Smooth L1 Loss&#xff0c;L1正则化和L2正则化 一、NMS 非极大值抑制&#xff08;Non-Maximum Suppression&#xff0c;NMS&#xff09;&#xff0c;并不是深度学习时期&#xff0c;目标…...

一文让你彻底理解Linux内核多线程(互斥锁、条件变量、读写锁、自旋锁、信号量)

一、互斥锁&#xff08;同步&#xff09; 在多任务操作系统中&#xff0c;同时运行的多个任务可能都需要使用同一种资源。这个过程有点类似于&#xff0c;公司部门里&#xff0c;我在使用着打印机打印东西的同时&#xff08;还没有打印完&#xff09;&#xff0c;别人刚好也在…...

利用python写一个gui小公举--环境搭建

文章目录背景搭建环境安装必要库添加工具快捷方式检验背景 在实习过程中遇到一个问题&#xff0c;某项目是通过python代码实现的&#xff0c;而且需要一直修改参数实现功能&#xff0c;过程有些繁琐。虽然师兄用PHP study搭了一个网站用于查看结果&#xff0c;但是还是过于繁琐…...

英飞凌Tricore实战系列02_ENDINIT属性看门狗原理及应用

目录 1.概述2.ENDINIT功能及使用2.1 ENDINIT属性2.2 改写受ENDINIT保护寄存器的步骤3. Tricore 看门狗介绍及使用3.1 看门狗系统介绍3.1.1 安全看门狗介绍3.1.2 CPU看门狗介绍3.2 看门狗模式介绍3.2.1 Time-out模式3.2.2 正常模式(Normal Mode)3.2.3 禁用模式(Disabled Mode…...

Java Number类

Java Number 类是一个抽象类&#xff0c;它是所有数字类的基类。Java 中的数字类包括 Byte、Short、Integer、Long、Float 和 Double&#xff0c;它们都继承自 Number 类。Java Number 类提供了一些常用的方法&#xff0c;可以用于将数字类型转换为不同的格式&#xff0c;以及进…...

C++构造和析构

欢迎来观看温柔了岁月.c的博客 目前 设有C学习专栏 C语言项目专栏 数据结构与算法专栏 目前主要更新C学习专栏&#xff0c;C语言项目专栏不定时更新 待C专栏完毕&#xff0c;会陆续更新C项目专栏和数据结构与算法专栏 一周主要三更&#xff0c;星期三&#xff0c;星期五&#x…...

docker安装即docker连接mysql(window)

一 安装docker 1.什么是docker Docker容器与虚拟机类似&#xff0c;但二者在原理上不同。容器是将操作系统层虚拟化&#xff0c;虚拟机则是虚拟化硬件&#xff0c;因此容器更具有便携性、高效地利用服务器。 2.WSL2 WSL&#xff0c;即Windows Subsystem on Linux&#xff0c;中…...

HMM-维特比算法

HMM-维特比算法&#xff08;viterbi&#xff09;HMM回顾隐马科夫链解法&#xff1a;维特比算法&#xff08;Viterbi&#xff09;HMM回顾 最终的公式可以解释主要分为两个部分&#xff1a; P(xi|yi)&#xff0c;发射概率&#xff0c;字面意思是从一个词性中发射/生成出某一个单…...

【C++初阶】2. 类和对象_1

1. 面向过程和面向对象的初步认识 2. 类的引入 C语言结构体中只能定义变量&#xff0c;在C中&#xff0c;结构体内不仅可以定义变量&#xff0c;也可以定义函数。比如&#xff1a; 之前在数据结构初阶中&#xff0c;用C语言方式实现的栈&#xff0c;结构体中只能定义变量&#…...

kotlin把函数作为参数转递给另一个函数

kotlin把函数作为参数转递给另一个函数 fun say(s: String, foo: (String) -> Unit) {print("hello")foo(s) }fun hi(str: String) {println(str) }fun main(args: Array<String>) {say("hello", ::hi) } 输出&#xff1a; hellohello...

海思嵌入式开发-005-OpenHarmony源码编译问题

海思嵌入式开发-005-OpenHarmony源码编译问题一、问题描述二、解决方案2.1解决原理2.2获取OpenHarmony 3.1.1 Release源码2.3最后解决问题&#xff0c;编译成功。一、问题描述 按照链接拉取master源码&#xff0c;出现如下问题&#xff0c;打开build.log文件 提示相应位置的文…...