当前位置: 首页 > 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;目标…...

Cilium动手实验室: 精通之旅---20.Isovalent Enterprise for Cilium: Zero Trust Visibility

Cilium动手实验室: 精通之旅---20.Isovalent Enterprise for Cilium: Zero Trust Visibility 1. 实验室环境1.1 实验室环境1.2 小测试 2. The Endor System2.1 部署应用2.2 检查现有策略 3. Cilium 策略实体3.1 创建 allow-all 网络策略3.2 在 Hubble CLI 中验证网络策略源3.3 …...

最新SpringBoot+SpringCloud+Nacos微服务框架分享

文章目录 前言一、服务规划二、架构核心1.cloud的pom2.gateway的异常handler3.gateway的filter4、admin的pom5、admin的登录核心 三、code-helper分享总结 前言 最近有个活蛮赶的&#xff0c;根据Excel列的需求预估的工时直接打骨折&#xff0c;不要问我为什么&#xff0c;主要…...

第一篇:Agent2Agent (A2A) 协议——协作式人工智能的黎明

AI 领域的快速发展正在催生一个新时代&#xff0c;智能代理&#xff08;agents&#xff09;不再是孤立的个体&#xff0c;而是能够像一个数字团队一样协作。然而&#xff0c;当前 AI 生态系统的碎片化阻碍了这一愿景的实现&#xff0c;导致了“AI 巴别塔问题”——不同代理之间…...

unix/linux,sudo,其发展历程详细时间线、由来、历史背景

sudo 的诞生和演化,本身就是一部 Unix/Linux 系统管理哲学变迁的微缩史。来,让我们拨开时间的迷雾,一同探寻 sudo 那波澜壮阔(也颇为实用主义)的发展历程。 历史背景:su的时代与困境 ( 20 世纪 70 年代 - 80 年代初) 在 sudo 出现之前,Unix 系统管理员和需要特权操作的…...

Maven 概述、安装、配置、仓库、私服详解

目录 1、Maven 概述 1.1 Maven 的定义 1.2 Maven 解决的问题 1.3 Maven 的核心特性与优势 2、Maven 安装 2.1 下载 Maven 2.2 安装配置 Maven 2.3 测试安装 2.4 修改 Maven 本地仓库的默认路径 3、Maven 配置 3.1 配置本地仓库 3.2 配置 JDK 3.3 IDEA 配置本地 Ma…...

IP如何挑?2025年海外专线IP如何购买?

你花了时间和预算买了IP&#xff0c;结果IP质量不佳&#xff0c;项目效率低下不说&#xff0c;还可能带来莫名的网络问题&#xff0c;是不是太闹心了&#xff1f;尤其是在面对海外专线IP时&#xff0c;到底怎么才能买到适合自己的呢&#xff1f;所以&#xff0c;挑IP绝对是个技…...

为什么要创建 Vue 实例

核心原因:Vue 需要一个「控制中心」来驱动整个应用 你可以把 Vue 实例想象成你应用的**「大脑」或「引擎」。它负责协调模板、数据、逻辑和行为,将它们变成一个活的、可交互的应用**。没有这个实例,你的代码只是一堆静态的 HTML、JavaScript 变量和函数,无法「活」起来。 …...

【堆垛策略】设计方法

堆垛策略的设计是积木堆叠系统的核心&#xff0c;直接影响堆叠的稳定性、效率和容错能力。以下是分层次的堆垛策略设计方法&#xff0c;涵盖基础规则、优化算法和容错机制&#xff1a; 1. 基础堆垛规则 (1) 物理稳定性优先 重心原则&#xff1a; 大尺寸/重量积木在下&#xf…...

GraphQL 实战篇:Apollo Client 配置与缓存

GraphQL 实战篇&#xff1a;Apollo Client 配置与缓存 上一篇&#xff1a;GraphQL 入门篇&#xff1a;基础查询语法 依旧和上一篇的笔记一样&#xff0c;主实操&#xff0c;没啥过多的细节讲解&#xff0c;代码具体在&#xff1a; https://github.com/GoldenaArcher/graphql…...

MeanFlow:何凯明新作,单步去噪图像生成新SOTA

1.简介 这篇文章介绍了一种名为MeanFlow的新型生成模型框架&#xff0c;旨在通过单步生成过程高效地将先验分布转换为数据分布。文章的核心创新在于引入了平均速度的概念&#xff0c;这一概念的引入使得模型能够通过单次函数评估完成从先验分布到数据分布的转换&#xff0c;显…...