Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserializing to an F# type using json.net and a json property containing an @ symbol

Tags:

json.net

f#

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.

like image 530
Alan Mulligan Avatar asked Feb 03 '16 08:02

Alan Mulligan


People also ask

What does deserialize mean?

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.

What is deserializing a JSON?

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.

What is the difference between serialize and deserialize?

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.

How do I deserialize a JSON file?

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.


1 Answers

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

like image 151
FoggyFinder Avatar answered Nov 12 '22 10:11

FoggyFinder