Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public override void Read(ref Utf8JsonReader reader, IMessage obj, Type typeToCo
IFieldAccessor fieldAccessor)
{
_converter ??= GetConverter(ref options);
if (reader.TokenType == JsonTokenType.Null && !_converter.HandleNull)
{
return;
}

var read = _converter.Read(ref reader, typeToConvert, options);
if (read is { } value)
{
Expand Down
44 changes: 44 additions & 0 deletions test/Protobuf.System.Text.Json.Tests/SimpleMessageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,48 @@ public void Should_deserialize_message_with_primitive_types()
Assert.Equal(msg.Int32Property, deserialized.Int32Property);
Assert.Equal(msg.Int64Property, deserialized.Int64Property);
}

[Fact]
public void Should_deserialize_message_with_primitive_types_when_values_were_explicitly_set_to_nulls()
{
// Arrange
var serialized =
"""
{
"doubleProperty" : null,
"floatProperty" : null,
"int32Property" : null,
"int64Property" : null,
"uint32Property" : null,
"uint64Property" : null,
"sint32Property" : null,
"sint64Property" : null,
"fixed32Property" : null,
"fixed64Property" : null,
"sfixed32Property" : null,
"sfixed64Property" : null,
"boolProperty" : null
}
""";
var jsonSerializerOptions = TestHelper.CreateJsonSerializerOptions();

// Act
var deserialized = JsonSerializer.Deserialize<SimpleMessage>(serialized, jsonSerializerOptions);

// Assert
Assert.NotNull(deserialized);
Assert.Equal(0d, deserialized.DoubleProperty);
Assert.Equal(0f, deserialized.FloatProperty);
Assert.Equal(0, deserialized.Int32Property);
Assert.Equal(0L, deserialized.Int64Property);
Assert.Equal(0u, deserialized.Uint32Property);
Assert.Equal(0ul, deserialized.Uint64Property);
Assert.Equal(0, deserialized.Sint32Property);
Assert.Equal(0L, deserialized.Sint64Property);
Assert.Equal(0u, deserialized.Fixed32Property);
Assert.Equal(0ul, deserialized.Fixed64Property);
Assert.Equal(0, deserialized.Sfixed32Property);
Assert.Equal(0L, deserialized.Sfixed64Property);
Assert.False(deserialized.BoolProperty);
}
Copy link

Copilot AI Nov 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra trailing space after closing brace should be removed.

Suggested change
}
}

Copilot uses AI. Check for mistakes.
}