Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit357b09d

Browse files
add new JsonIgnoreAttribute WhenWriting/WhenReading (#104562)
* feat: add new JsonIgnoreAttribute WhenWriting/WhenReading* remove empty line to align existing style* test: add test case* test: source generation test* update test case and fix some formatting---------Co-authored-by: Eirik Tsarpalis <eirik.tsarpalis@gmail.com>
1 parent598d5f7 commit357b09d

File tree

6 files changed

+53
-2
lines changed

6 files changed

+53
-2
lines changed

‎src/libraries/System.Text.Json/Common/JsonIgnoreCondition.cs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,13 @@ public enum JsonIgnoreCondition
3030
/// This is applied only to reference-type properties and fields.
3131
/// </summary>
3232
WhenWritingNull=3,
33+
/// <summary>
34+
/// Property is ignored during serialization
35+
/// </summary>
36+
WhenWriting=4,
37+
/// <summary>
38+
/// Property is ignored during deserialization
39+
/// </summary>
40+
WhenReading=5,
3341
}
3442
}

‎src/libraries/System.Text.Json/ref/System.Text.Json.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,6 +1028,8 @@ public enum JsonIgnoreCondition
10281028
Always=1,
10291029
WhenWritingDefault=2,
10301030
WhenWritingNull=3,
1031+
WhenWriting=4,
1032+
WhenReading=5,
10311033
}
10321034
[System.AttributeUsageAttribute(System.AttributeTargets.Field|System.AttributeTargets.Property,AllowMultiple=false)]
10331035
publicsealedpartialclassJsonIncludeAttribute:System.Text.Json.Serialization.JsonAttribute

‎src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Metadata/JsonPropertyInfoOfT.cs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,14 @@ private protected override void ConfigureIgnoreCondition(JsonIgnoreCondition? ig
429429
IgnoreDefaultValuesOnWrite=true;
430430
break;
431431

432+
caseJsonIgnoreCondition.WhenWriting:
433+
ShouldSerialize=ShouldSerializeIgnoreConditionAlways;
434+
break;
435+
436+
caseJsonIgnoreCondition.WhenReading:
437+
Set=null;
438+
break;
439+
432440
default:
433441
Debug.Fail($"Unknown value of JsonIgnoreCondition '{ignoreCondition}'");
434442
break;

‎src/libraries/System.Text.Json/tests/Common/PropertyVisibilityTests.cs‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2165,6 +2165,35 @@ public async Task IgnoreConditionWhenWritingDefault_WinsOver_IgnoreReadOnlyField
21652165
Assert.Equal(@"{}",json);
21662166
}
21672167

2168+
[Fact]
2169+
publicasyncTaskJsonIgnoreCondition_WhenWriting()
2170+
{
2171+
varoptions=newJsonSerializerOptions{IgnoreReadOnlyProperties=true};
2172+
varjson=awaitSerializer.SerializeWrapper
2173+
(
2174+
newJsonIgnoreCondition_WhenReadingWritingTestModel{Age=10,Name="Mike"},
2175+
options
2176+
);
2177+
Assert.Equal("""{"Age":10}""",json);
2178+
}
2179+
2180+
[Fact]
2181+
publicasyncTaskJsonIgnoreCondition_WhenReading()
2182+
{
2183+
varjson="""{"Age":10, "Name":"Mike"}""";
2184+
varmodel=awaitSerializer.DeserializeWrapper<JsonIgnoreCondition_WhenReadingWritingTestModel>(json);
2185+
Assert.Equal("Mike",model.Name);
2186+
Assert.Equal(0,model.Age);
2187+
}
2188+
2189+
publicclassJsonIgnoreCondition_WhenReadingWritingTestModel
2190+
{
2191+
[JsonIgnore(Condition=JsonIgnoreCondition.WhenReading)]
2192+
publicintAge{get;set;}
2193+
[JsonIgnore(Condition=JsonIgnoreCondition.WhenWriting)]
2194+
publicstring?Name{get;set;}
2195+
}
2196+
21682197
publicclassClassWithReadOnlyStringProperty
21692198
{
21702199
publicstringMyString{get;}

‎src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/Serialization/PropertyVisibilityTests.cs‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public override async Task HonorJsonPropertyName_PrivateGetter()
129129
Assert.Equal(MySmallEnum.AnotherValue,obj.GetProxy());
130130

131131
// JsonInclude for private members not supported in source gen
132-
awaitAssert.ThrowsAsync<InvalidOperationException>(async()=>awaitSerializer.SerializeWrapper(obj));
132+
awaitAssert.ThrowsAsync<InvalidOperationException>(async()=>awaitSerializer.SerializeWrapper(obj));
133133
}
134134

135135
[Fact]
@@ -331,7 +331,8 @@ public override async Task ClassWithIgnoredAndPrivateMembers_DoesNotIncludeIgnor
331331
[JsonSerializable(typeof(IDiamondInterfaceHierarchyWithNamingConflict.IJoinInterface),TypeInfoPropertyName="IDiamondInterfaceHierarchyWithNamingConflictIJoinInterface")]
332332
[JsonSerializable(typeof(IDiamondInterfaceHierarchyWithNamingConflictUsingAttribute.IJoinInterface),TypeInfoPropertyName="IDiamondInterfaceHierarchyWithNamingConflictUsingAttributeIJoinInterface")]
333333
[JsonSerializable(typeof(CollectionWithPrivateElementType))]
334-
[JsonSerializable(typeof(DictionaryWithPrivateKeyAndValueType))][JsonSerializable(typeof(ClassWithIgnoredAndPrivateMembers))]
334+
[JsonSerializable(typeof(DictionaryWithPrivateKeyAndValueType))]
335+
[JsonSerializable(typeof(ClassWithIgnoredAndPrivateMembers))]
335336
[JsonSerializable(typeof(ClassWithInternalJsonIncludeProperties))]
336337
[JsonSerializable(typeof(ClassWithIgnoredAndPrivateMembers))]
337338
[JsonSerializable(typeof(ClassUsingIgnoreWhenWritingDefaultAttribute))]
@@ -340,6 +341,7 @@ public override async Task ClassWithIgnoredAndPrivateMembers_DoesNotIncludeIgnor
340341
[JsonSerializable(typeof(ClassWithProperty_IgnoreConditionAlways_Ctor))]
341342
[JsonSerializable(typeof(ClassWithClassProperty_IgnoreConditionWhenWritingDefault_Ctor))]
342343
[JsonSerializable(typeof(StructWithStructProperty_IgnoreConditionWhenWritingDefault_Ctor))]
344+
[JsonSerializable(typeof(JsonIgnoreCondition_WhenReadingWritingTestModel))]
343345
[JsonSerializable(typeof(SmallStructWithValueAndReferenceTypes))]
344346
[JsonSerializable(typeof(WrapperForClassWithIgnoredUnsupportedDictionary))]
345347
[JsonSerializable(typeof(Class1))]
@@ -613,6 +615,7 @@ partial class DefaultContextWithGlobalIgnoreSetting : JsonSerializerContext;
613615
[JsonSerializable(typeof(ClassWithProperty_IgnoreConditionAlways_Ctor))]
614616
[JsonSerializable(typeof(ClassWithClassProperty_IgnoreConditionWhenWritingDefault_Ctor))]
615617
[JsonSerializable(typeof(StructWithStructProperty_IgnoreConditionWhenWritingDefault_Ctor))]
618+
[JsonSerializable(typeof(JsonIgnoreCondition_WhenReadingWritingTestModel))]
616619
[JsonSerializable(typeof(SmallStructWithValueAndReferenceTypes))]
617620
[JsonSerializable(typeof(WrapperForClassWithIgnoredUnsupportedDictionary))]
618621
[JsonSerializable(typeof(Class1))]

‎src/libraries/System.Text.Json/tests/System.Text.Json.Tests/Serialization/MetadataTests/DefaultJsonTypeInfoResolverTests.JsonPropertyInfo.cs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,7 @@ public enum ModifyJsonIgnore
908908
[InlineData(JsonIgnoreCondition.Never,ModifyJsonIgnore.DontModify)]
909909
[InlineData(JsonIgnoreCondition.WhenWritingDefault,ModifyJsonIgnore.NeverSerialize)]
910910
[InlineData(JsonIgnoreCondition.WhenWritingNull,ModifyJsonIgnore.NeverSerialize)]
911+
[InlineData(JsonIgnoreCondition.WhenWriting,ModifyJsonIgnore.NeverSerialize)]
911912
[InlineData(JsonIgnoreCondition.Never,ModifyJsonIgnore.NeverSerialize)]
912913
[InlineData(JsonIgnoreCondition.WhenWritingDefault,ModifyJsonIgnore.AlwaysSerialize)]
913914
[InlineData(JsonIgnoreCondition.WhenWritingNull,ModifyJsonIgnore.AlwaysSerialize)]

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp