JsonExtensions
1.0.0
.NET System.text.jsonの拡張機能のコレクション
単一のJSONオブジェクトとJSONアレイの両方をC#アレイとして脱出するJSONConverter。
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" 1つのC#プロパティへの複数のJSONキーをマッピングできるJSONPropertyNames属性とJSonMultinAmemodifier。
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ガオリー
このソフトウェアと関連するドキュメントファイル(「ソフトウェア」)のコピーを入手して、制限なしにソフトウェアを扱うために、このソフトウェアを制限する権利を含め、ソフトウェアのコピーをコピー、変更、公開、配布、販売する、ソフトウェアのコピーを許可する人を許可する人を許可することを含めて、許可が無料で許可されます。
上記の著作権通知とこの許可通知は、ソフトウェアのすべてのコピーまたはかなりの部分に含まれるものとします。
このソフトウェアは、商品性、特定の目的への適合性、および非侵害の保証を含むがこれらに限定されない、明示的または黙示的なものを保証することなく、「現状のまま」提供されます。いかなる場合でも、著者または著作権所有者は、契約、不法行為、またはその他の訴訟、ソフトウェアまたはソフトウェアの使用またはその他の取引に関連する、またはその他の契約、またはその他の請求、またはその他の責任について責任を負いません。