0

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.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
baddrivers
  • 25
  • 4
  • Does this answer your question? [What does the @ symbol before a variable name mean in C#?](https://stackoverflow.com/questions/429529/what-does-the-symbol-before-a-variable-name-mean-in-c) – quaabaam Jun 15 '21 at 16:51
  • 2
    is this using .net core 3.0? if so try `[JsonPropertyName("@Scope")]` ? e.g see https://stackoverflow.com/a/58299628/15410 – Rippo Jun 15 '21 at 16:52
  • 1
    `@` is not a valid character in C# identifiers itself; it's used as an escape sequence to allow keywords to serve as identifiers. So writing `@Scope` has the same meaning as just writing `Scope`; you need to use whatever you'd normally use to override serialization to get such names as literals. – Jeroen Mostert Jun 15 '21 at 16:52
  • TFM - [How to customize property names and values with System.Text.Json](https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-customize-properties) "To set the name of individual properties, use the [`JsonPropertyName`](https://learn.microsoft.com/en-us/dotnet/api/system.text.json.serialization.jsonpropertynameattribute) attribute" – Alexei Levenkov Jun 15 '21 at 16:56

1 Answers1

1

Try using

[JsonPropertyName("@Scope")]

Instead of

[JsonProperty("@Scope")]

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

golakwer
  • 355
  • 1
  • 4