JsonExtensions
1.0.0
.NET System.Text.json的扩展集合
jsonConverter既可以将单个JSON对象又是JSON数组作为C#数组。
public sealed class Example
{
[ JsonConverter ( typeof ( SingleOrArrayJsonConverter ) ) ]
public string [ ] ? Array { get ; set ; }
} const string jsonSingle = """{"Array": "single"}""" ;
var deserializedSingle = JsonSerializer . Deserialize < Example > ( jsonSingle ) ;
Console . WriteLine ( $ " { deserializedSingle . Array . Length } : { deserializedSingle . Array [ 0 ] } " ) ;
// Output: "1: single"
const string jsonArray = """{"Array": ["first", "second"]}""" ;
var deserializedArray = JsonSerializer . Deserialize < Example > ( jsonArray ) ;
Console . WriteLine ( $ " { deserializedArray . Array . Length } : { deserializedArray . Array [ 0 ] } , { deserializedArray . Array [ 1 ] } " ) ;
// Output: "2: first, second" public sealed class Person
{
public string FirstName { get ; }
public string LastName { get ; }
public Person ( string firstName , string lastName )
{
FirstName = firstName ;
LastName = lastName ;
}
}
// Converts a Person object into a "${FirstName} ${LastName}" string
public sealed class PersonJsonConverter : JsonConverter < string >
{
public override string ? Read ( ref Utf8JsonReader reader , Type typeToConvert , JsonSerializerOptions options )
{
var personConverter = ( JsonConverter < Person > ) options . GetConverter ( typeof ( Person ) ) ;
Person ? person = personConverter . Read ( ref reader , typeof ( Person ) , options ) ;
return person != null ? $ " { person . FirstName } { person . LastName } " : null ;
}
public override void Write ( Utf8JsonWriter writer , string value , JsonSerializerOptions options )
{
var personConverter = ( JsonConverter < Person > ) options . GetConverter ( typeof ( Person ) ) ;
var split = value . Split ( ' ' ) ;
var firstName = split [ 0 ] ;
var lastName = split [ 1 ] ;
var person = new Person ( firstName , lastName ) ;
personConverter . Write ( writer , person , options ) ;
}
}
public sealed class Example
{
[ JsonConverter ( typeof ( SingleOrArrayJsonConverter < PersonJsonConverter > ) ) ]
public string [ ] ? Array { get ; set ; }
} const string jsonSingle = """{"Array": {"FirstName": "John", "LastName": "Smith"}}""" ;
var deserializedSingle = JsonSerializer . Deserialize < Example > ( json ) ;
Console . WriteLine ( $ " { deserializedSingle . Array . Length } : { deserializedSingle . Array [ 0 ] } " ) ;
// Output: "1: John Smith"
const string jsonArray = """{"Array": [{"FirstName": "John", "LastName": "Smith"}, {"FirstName": "John", "LastName": "Doe"}]}""" ;
var deserializedArray = JsonSerializer . Deserialize < Example > ( json ) ;
Console . WriteLine ( $ " { deserializedArray . Array . Length } : { deserializedArray . Array [ 0 ] } , { deserializedArray . Array [ 1 ] } " ) ;
// Output: "2: John Smith, John Doe" JSONPROPERTYNAMES属性和JSONMULTINAMEMEMEMEMEMEMEMODIFIER,允许将多个JSON键映射到一个C#属性。
public sealed class User
{
[ JsonPropertyNames ( "UserName" , "User" , "Name" ) ]
public string UserName { get ; set ; }
} JsonSerializerOptions jsonOptions = new ( )
{
TypeInfoResolver = new DefaultJsonTypeInfoResolver ( )
{
Modifiers = { Modifiers . JsonMultiNameModifier }
}
}
const string json1 = """{"UserName": "JohnSmith1"}""" ;
const string json2 = """{"User": "JohnSmith2"}""" ;
const string json3 = """{"Name": "JohnSmith3"}""" ;
var deserialized = JsonSerializer . Deserialize < User > ( json1 , jsonOptions ) ;
Console . WriteLine ( deserialized . UserName ) ;
// Output: "JohnSmith1"
deserialized = JsonSerializer . Deserialize < User > ( json2 , jsonOptions ) ;
Console . WriteLine ( deserialized . UserName ) ;
// Output: "JohnSmith2"
deserialized = JsonSerializer . Deserialize < User > ( json3 , jsonOptions ) ;
Console . WriteLine ( deserialized . UserName ) ;
// Output: "JohnSmith3"
string json = """{"UserName": "JohnSmith1", "User": "JohnSmith2", "Name": "JohnSmith3"}""" ;
deserialized = JsonSerializer . Deserialize < User > ( json , jsonOptions ) ;
Console . WriteLine ( deserialized . UserName ) ;
// Output: "JohnSmith3"
json = JsonSerializer . Serialize ( deserialized , jsonOptions ) ;
Console . WriteLine ( json ) ;
// Output: '{"UserName":"JohnSmith3"}' public sealed class User
{
[ JsonPropertyNames ( throwOnDuplicate : true , "UserName" , "User" , "Name" ) ]
public string UserName { get ; set ; }
} JsonSerializerOptions jsonOptions = new ( )
{
TypeInfoResolver = new DefaultJsonTypeInfoResolver ( )
{
Modifiers = { Modifiers . JsonMultiNameModifier }
}
}
const string json1 = """{"UserName": "JohnSmith1"}""" ;
const string json2 = """{"User": "JohnSmith2"}""" ;
const string json3 = """{"Name": "JohnSmith3"}""" ;
var deserialized = JsonSerializer . Deserialize < User > ( json1 , jsonOptions ) ;
Console . WriteLine ( deserialized . UserName ) ;
// Output: "JohnSmith1"
deserialized = JsonSerializer . Deserialize < User > ( json2 , jsonOptions ) ;
Console . WriteLine ( deserialized . UserName ) ;
// Output: "JohnSmith2"
deserialized = JsonSerializer . Deserialize < User > ( json3 , jsonOptions ) ;
Console . WriteLine ( deserialized . UserName ) ;
// Output: "JohnSmith3"
try {
const string json = """{"UserName": "JohnSmith1", "User": "JohnSmith2", "Name": "JohnSmith3"}""" ;
deserialized = JsonSerializer . Deserialize < User > ( json , jsonOptions ) ;
Console . WriteLine ( deserialized . UserName ) ;
} catch ( JsonException ) {
Console . WriteLine ( "Deserialize failed" ) ;
}
// Output: "Deserialize failed" public sealed class User
{
[ JsonPropertyNames ( serializationName : "NickName" , "UserName" , "User" , "Name" ) ]
public string UserName { get ; set ; }
} JsonSerializerOptions jsonOptions = new ( )
{
TypeInfoResolver = new DefaultJsonTypeInfoResolver ( )
{
Modifiers = { Modifiers . JsonMultiNameModifier }
}
}
string json = """{"UserName": "JohnSmith"}""" ;
var deserialized = JsonSerializer . Deserialize < User > ( json , jsonOptions ) ;
Console . WriteLine ( deserialized . UserName ) ;
// Output: "JohnSmith"
json = JsonSerializer . Serialize ( deserialized , jsonOptions ) ;
Console . WriteLine ( json ) ;
// Output: '{"NickName":"JohnSmith"}' 版权(C)2023 Guilgy
特此免费授予获得此软件副本和相关文档文件副本(“软件”)的任何人,以无限制处理该软件,包括无限制的使用权,复制,复制,修改,合并,合并,发布,分发,分发,分发,订婚,和/或允许软件的副本,并允许对以下条件提供以下条件,以下是以下条件。
上述版权通知和此许可通知应包含在软件的所有副本或大量部分中。
该软件是“原样”提供的,没有任何形式的明示或暗示保证,包括但不限于适销性,特定目的的适用性和非侵权的保证。在任何情况下,作者或版权持有人都不应对任何索赔,损害赔偿或其他责任责任,无论是在合同,侵权的诉讼中还是其他责任,是由软件,使用或与软件中的使用或其他交易有关的。