✅ @ in the property name c#
Team , Is there any way to get @ in the property name like this
public class TestClass
{
[JsonProperty("@context)]
public string @context { get; set; }
}
Adding Jsonproperty , I am able to deserialize . but I need @context as output property,right now it is coming as just context
But Underscore is working
public class TestClass
{
[JsonProperty("@context)]
public string _context { get; set; }
}
Please guide on this
24 Replies
Why would you want the property to be called that, and not
Context
?
[JsonProperty]
handles the serialization and deserialization
The name in code can just as well be UngaBunga
I need to send the same object back to an library which is expecting an @
It's not a valid identifier, so no library can use it either
Not to mention, unless using reflections, the names of properties don't matter
Unknown User•13mo ago
Message Not Public
Sign In & Join Server To View
Json has a property named
@context
from what I gatherUnknown User•13mo ago
Message Not Public
Sign In & Join Server To View
Probably
Idk, detail is scarce lol
So it is conflicting?
Unknown User•13mo ago
Message Not Public
Sign In & Join Server To View
What is conflicting with what?
Handling a Json property named
@context
is easy with [JsonProperty]
attribute
The property itself cannot begin witn @
No library will ever expect a property starting with @
Why do you need specifically the property of your class to be named @Context
and not Context
or UngaBunga
?It is an json LD object , if i send without @ it is throwing an error saying invalid object https://json-ld.org/
[JsonProperty]
handles the conversion to JsonAngius
REPL Result: Success
Result: Foo
Console Output
Compile: 660.550ms | Execution: 56.581ms | React with ❌ to remove this embed.
Huh, what?
[JsonProperty]
doesn't work for serialization?Unknown User•13mo ago
Message Not Public
Sign In & Join Server To View
Ah
Well that's the issue lmao
Unknown User•13mo ago
Message Not Public
Sign In & Join Server To View
Angius
REPL Result: Success
Result: Foo
Console Output
Compile: 657.888ms | Execution: 96.639ms | React with ❌ to remove this embed.
There
Should I use System.Text.Json; and not Newtonsoft.Json; ? @TeBeCo
Unknown User•13mo ago
Message Not Public
Sign In & Join Server To View
STJ is easier, better, faster, stronger
Got it. Thanks for the input @ZZZZZZZZZZZZZZZZZZZZZZZZZ @TeBeCo