I am trying to post a serialised object to a web service. The service requires the property names 'context' and 'type' to be formatted as '@context' and '@type' other wise it won't accept the request.
Newtonsoft JSON.NET is removing the '@' from the property names 'context' and 'type' and i need them to carry through into the JSON. Can anyone assist?
Here is the class I am using
public class PotentialAction
{
public string @context { get; set; }
public string @type { get; set; }
public string name { get; set; }
public IList<string> target { get; set; } = new List<string>();
}
Here is the JSON that it is being converted to:
{
"potentialAction": [
{
"context": "http://schema.org",
"type": "ViewAction",
"name": "View in Portal",
"target": [
"http://www.example.net"
]
}
]
}
But this is what I need it to serialise to:
{
"potentialAction": [
{
"@context": "http://schema.org",
"@type": "ViewAction",
"name": "View in Portal",
"target": [
"http://www.example.net"
]
}
]
}
@in the C# code is not actually considered to be part of the identifier, so the names of those property is actuallycontextandtype. The @ is used to tell the compiler that any syntax rules that would make the following word a keyword should be ignored and consider it as an identifier, but the@isn't added to the identifier. In other words, it isn't Json.Net that is removing the@, it is the C# compiler that isn't adding them because they mean something else.