C
C#13mo ago
Shiv

✅ @ 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
Angius
Angius13mo ago
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
Shiv
ShivOP13mo ago
I need to send the same object back to an library which is expecting an @
Angius
Angius13mo ago
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
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
Angius
Angius13mo ago
Json has a property named @context from what I gather
Unknown User
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
Angius
Angius13mo ago
Probably Idk, detail is scarce lol
Shiv
ShivOP13mo ago
So it is conflicting?
Unknown User
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
Angius
Angius13mo ago
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?
Shiv
ShivOP13mo ago
It is an json LD object , if i send without @ it is throwing an error saying invalid object https://json-ld.org/
Angius
Angius13mo ago
[JsonProperty] handles the conversion to Json
MODiX
MODiX13mo ago
Angius
REPL Result: Success
using System.Text.Json;

public class Foo
{
[JsonProperty("@context")]
public string UngaBunga { get; set; }
}

var foo = new Foo { UngaBunga = "hello world" };

var json = JsonSerializer.Serialize(foo);

Console.WriteLine(json);

var newFoo = JsonSerializer.Deserialize<Foo>(json);

return newFoo;
using System.Text.Json;

public class Foo
{
[JsonProperty("@context")]
public string UngaBunga { get; set; }
}

var foo = new Foo { UngaBunga = "hello world" };

var json = JsonSerializer.Serialize(foo);

Console.WriteLine(json);

var newFoo = JsonSerializer.Deserialize<Foo>(json);

return newFoo;
Result: Foo
{
"ungaBunga": "hello world"
}
{
"ungaBunga": "hello world"
}
Console Output
{"UngaBunga":"hello world"}
{"UngaBunga":"hello world"}
Compile: 660.550ms | Execution: 56.581ms | React with ❌ to remove this embed.
Angius
Angius13mo ago
Huh, what? [JsonProperty] doesn't work for serialization?
Unknown User
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
Angius
Angius13mo ago
Ah Well that's the issue lmao
Unknown User
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX13mo ago
Angius
REPL Result: Success
using System.Text.Json;
using System.Text.Json.Serialization;

public class Foo
{
[JsonPropertyName("@context")]
public string UngaBunga { get; set; }
}

var foo = new Foo { UngaBunga = "hello world" };

var json = JsonSerializer.Serialize(foo);

Console.WriteLine(json);

var newFoo = JsonSerializer.Deserialize<Foo>(json);

return newFoo;
using System.Text.Json;
using System.Text.Json.Serialization;

public class Foo
{
[JsonPropertyName("@context")]
public string UngaBunga { get; set; }
}

var foo = new Foo { UngaBunga = "hello world" };

var json = JsonSerializer.Serialize(foo);

Console.WriteLine(json);

var newFoo = JsonSerializer.Deserialize<Foo>(json);

return newFoo;
Result: Foo
{
"@context": "hello world"
}
{
"@context": "hello world"
}
Console Output
{"@context":"hello world"}
{"@context":"hello world"}
Compile: 657.888ms | Execution: 96.639ms | React with ❌ to remove this embed.
Angius
Angius13mo ago
There
Shiv
ShivOP13mo ago
Should I use System.Text.Json; and not Newtonsoft.Json; ? @TeBeCo
Unknown User
Unknown User13mo ago
Message Not Public
Sign In & Join Server To View
Angius
Angius13mo ago
STJ is easier, better, faster, stronger
Shiv
ShivOP13mo ago
Got it. Thanks for the input @ZZZZZZZZZZZZZZZZZZZZZZZZZ @TeBeCo
Want results from more Discord servers?
Add your server