Reputation: 1198
I have a F# type which I deserialize to an object from the content of a HTTP web request. The API which I am calling uses an odata protocol and the content of that request has the following format with contains the key @odata.context
.
{
"@odata.context":"OData",
"Value":"token"
}
I am using Json.net to deserialize the content back to my F# type, the F# type is as follows
type Success = {
[<JsonProperty(PropertyName = "@odata.context")>]
``odata.context``: string;
Value: string; }
odata.context
is always null
in this situation.
I have tried both the following (with the @ symbol in the F# type property name) and the result is NULL
let test1 = JsonConvert.DeserializeObject<Success>("{\"@odata.context\": \"odata.context\", \"Value\": \"token\"}"))
(without the @ symbol in the F# type property name) This gets deserialized correctly.
let test2 = JsonConvert.DeserializeObject<Success>("{\"odata.context\": \"odata.context\", \"Value\": \"token\"}"))
I believe this could be to do with the @ symbol in the property name.
Any ideas on a solution would be great.
Upvotes: 6
Views: 1065
Reputation: 2220
If you don't have the opportunity to update Json.Net to a newer (e.g. 8.0.2), you can use a Newtonsoft.Json.Linq.
Example:
open System
open Newtonsoft.Json.Linq
type Success = {
``odata.context``: string;
Value: string; }
let json = "{\"@odata.context\":\"OData\",\"Value\":\"token\"}"
let p = JObject.Parse(json)
{``odata.context`` = p.["@odata.context"] |> string ;Value = p.["Value"] |> string}
|> printfn "%A"
Print:
{odata.context = "OData";
Value = "token";}
Link:
https://dotnetfiddle.net/SR16Ci
Upvotes: 3