Skip to content

Commit 4cb08fc

Browse files
authored
fix json source generator serialize when writing (#116004)
* fix json source generator serialize when writing * Update JsonSerializerContextTests.cs * Update JsonSerializerContextTests.cs remove required modifier * test: update JsonIgnoreCondition_WhenWriting test case removing the serializer options
1 parent d709a73 commit 4cb08fc

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

src/libraries/System.Text.Json/gen/JsonSourceGenerator.Emitter.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,9 @@ private void GenerateFastPathFuncForObject(SourceWriter writer, ContextGeneratio
846846

847847
switch (defaultCheckType)
848848
{
849+
case SerializedValueCheckType.Ignore:
850+
break;
851+
849852
case SerializedValueCheckType.IgnoreWhenNull:
850853
writer.WriteLine($"if ({propValueExpr} is not null)");
851854
writer.WriteLine('{');
@@ -1053,12 +1056,14 @@ private enum SerializedValueCheckType
10531056
IgnoreWhenNull,
10541057
IgnoreWhenDefault,
10551058
DisallowNull,
1059+
Ignore
10561060
}
10571061

10581062
private static SerializedValueCheckType GetCheckType(ContextGenerationSpec contextSpec, PropertyGenerationSpec propertySpec)
10591063
{
10601064
return (propertySpec.DefaultIgnoreCondition ?? contextSpec.GeneratedOptionsSpec?.DefaultIgnoreCondition) switch
10611065
{
1066+
JsonIgnoreCondition.WhenWriting => SerializedValueCheckType.Ignore,
10621067
JsonIgnoreCondition.WhenWritingNull => propertySpec.PropertyType.CanBeNull ? SerializedValueCheckType.IgnoreWhenNull : SerializedValueCheckType.None,
10631068
JsonIgnoreCondition.WhenWritingDefault => propertySpec.PropertyType.CanBeNull ? SerializedValueCheckType.IgnoreWhenNull : SerializedValueCheckType.IgnoreWhenDefault,
10641069
_ when propertySpec.IsGetterNonNullableAnnotation && contextSpec.GeneratedOptionsSpec?.RespectNullableAnnotations is true => SerializedValueCheckType.DisallowNull,

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2168,11 +2168,9 @@ public async Task IgnoreConditionWhenWritingDefault_WinsOver_IgnoreReadOnlyField
21682168
[Fact]
21692169
public async Task JsonIgnoreCondition_WhenWriting()
21702170
{
2171-
var options = new JsonSerializerOptions { IgnoreReadOnlyProperties = true };
21722171
var json = await Serializer.SerializeWrapper
21732172
(
2174-
new JsonIgnoreCondition_WhenReadingWritingTestModel { Age = 10, Name = "Mike" },
2175-
options
2173+
new JsonIgnoreCondition_WhenReadingWritingTestModel { Age = 10, Name = "Mike" }
21762174
);
21772175
Assert.Equal("""{"Age":10}""", json);
21782176
}

src/libraries/System.Text.Json/tests/System.Text.Json.SourceGeneration.Tests/JsonSerializerContextTests.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,5 +937,45 @@ internal class SelfReference
937937
{
938938
public SelfReference Me { get; set; }
939939
}
940+
941+
[Fact]
942+
public static void SerializeIgnoreWhenWritingTest()
943+
{
944+
var p = new IgnoreWhenReadingWritingPerson
945+
{
946+
Id = 1,
947+
Name = "Alice",
948+
Description = "test"
949+
};
950+
var serializedJson = JsonSerializer.Serialize(p, SerializeIgnoreReadingWritingJsonSerializerContext.Default.IgnoreWhenReadingWritingPerson);
951+
Assert.Equal("""{"Name":"Alice","Description":"test"}""", serializedJson);
952+
}
953+
954+
[Fact]
955+
public static void SerializeIgnoreWhenReadingTest()
956+
{
957+
var json = """{"Id":1,"Name":"Alice","Description":"test"}""";
958+
var deserialized = JsonSerializer.Deserialize<IgnoreWhenReadingWritingPerson>(json, SerializeIgnoreReadingWritingJsonSerializerContext.Default.IgnoreWhenReadingWritingPerson);
959+
Assert.NotNull(deserialized);
960+
Assert.Equal(1, deserialized.Id);
961+
Assert.Equal("Alice", deserialized.Name);
962+
Assert.Null(deserialized.Description);
963+
}
964+
965+
internal sealed class IgnoreWhenReadingWritingPerson
966+
{
967+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWriting)]
968+
public int Id { get; set; }
969+
970+
public string Name { get; set; }
971+
972+
[JsonIgnore(Condition = JsonIgnoreCondition.WhenReading)]
973+
public string? Description { get; set; }
974+
}
975+
976+
[JsonSerializable(typeof(IgnoreWhenReadingWritingPerson))]
977+
internal partial class SerializeIgnoreReadingWritingJsonSerializerContext : JsonSerializerContext
978+
{
979+
}
940980
}
941981
}

0 commit comments

Comments
 (0)