C
C#7mo 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
Angius7mo 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
Shiv7mo ago
I need to send the same object back to an library which is expecting an @
Angius
Angius7mo 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 User7mo ago
Message Not Public
Sign In & Join Server To View
Angius
Angius7mo ago
Json has a property named @context from what I gather
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
Angius
Angius7mo ago
Probably Idk, detail is scarce lol
Shiv
Shiv7mo ago
So it is conflicting?
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
Angius
Angius7mo 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
Shiv7mo ago
It is an json LD object , if i send without @ it is throwing an error saying invalid object https://json-ld.org/
Angius
Angius7mo ago
[JsonProperty] handles the conversion to Json
MODiX
MODiX7mo 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
Angius7mo ago
Huh, what? [JsonProperty] doesn't work for serialization?
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
Angius
Angius7mo ago
Ah Well that's the issue lmao
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
MODiX
MODiX7mo 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
Angius7mo ago
There
Shiv
Shiv7mo ago
Should I use System.Text.Json; and not Newtonsoft.Json; ? @TeBeCo
Unknown User
Unknown User7mo ago
Message Not Public
Sign In & Join Server To View
Angius
Angius7mo ago
STJ is easier, better, faster, stronger
Shiv
Shiv7mo ago
Got it. Thanks for the input @ZZZZZZZZZZZZZZZZZZZZZZZZZ @TeBeCo
Want results from more Discord servers?
Add your server
More Posts