baddrivers
baddrivers

Reputation: 25

Setting property name to include @ in JSON using System.Text.Json

I need to send via REST (not including some other sensitive information):

{"@Scope": "Local"}

In the class file I JsonProperty for Json.Net serialization:

[JsonProperty("@Scope")]
public string Scope { get; set; }

When putting together the json message to be sent via WebRequest, I have

{@Scope = "Local"}

But after running:

string jsonString = System.Text.Json.JsonSerializer.Serialize(message);

The message being sent out is

{"Scope": "Local"}

For some reason I though System.Text.Json will pick up JsonProperty attributes but apparently there should be some other way.

Upvotes: 0

Views: 1194

Answers (1)

golakwer
golakwer

Reputation: 373

Try using

[JsonPropertyName("@Scope")]

Instead of

[JsonProperty("@Scope")]

JsonProperty might be for Newtonsoft which the built in serialization library probably does not recognize.

Upvotes: 1

Related Questions