dotnet / dotnet/aspnetcore

MarkupString should support Json roundtrip

Open Beginner friendly
#67,608 1 comment 0 reactions 0 assignees View on GitHub
area-blazor
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

### Is there an existing issue for this?

- [x] I have searched the existing issues

### Is your feature request related to a problem? Please describe the problem.

I am trying to Serialize/Deserialize object that using JSON contains MarkupString as field and it fails.

```
public class ContentModel
{
public MarkupString HtmlContent { get; set; }
}

[Fact]
public void SimpleRoundTripTest()
{
var data = new ContentModel
{
HtmlContent = new MarkupString("

Hello World
")
};
var serialized = JsonSerializer.Serialize(data).ShouldNotBeNull();
var deserialized = JsonSerializer.Deserialize(serialized).ShouldNotBeNull();
deserialized.HtmlContent.ShouldBe("
Hello World
");
}
```

### Describe the solution you'd like

Add [JsonConstructor] to MarkupString

### Additional context

_No response_

Contributor guide

Open the contributing guide

Research direction

Start by locating the MarkupString type and reproduce the SimpleRoundTripTest shown in the issue with JsonSerializer. Verify that ContentModel.HtmlContent can be serialized and deserialized without failure and retains the original markup value.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
frontend
Issue type
Feature
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
72/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.