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.
Deserialization is the process of reconstructing a data structure or object from a series of bytes or a string in order to instantiate the object for consumption. This is the reverse process of serialization, i.e., converting a data structure or object into a series of bytes for storage or transmission across devices.
The process whereby a lower-level format (e.g. that has been transferred over a network, or stored in a data store) is translated into a readable object or other data structure. In JavaScript, for example, you can deserialize a JSON string to an object by calling the function JSON.
Serialization is a mechanism of converting the state of an object into a byte stream. Deserialization is the reverse process where the byte stream is used to recreate the actual Java object in memory.
A common way to deserialize JSON is to first create a class with properties and fields that represent one or more of the JSON properties. Then, to deserialize from a string or a file, call the JsonSerializer. Deserialize method.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With