Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Can I Deserialize Json With F#?

Tags:

json.net

f#

I am attempting to deserialize some very simple json into F# with Newtonsoft.Json 5.0.4:

#if INTERACTIVE
#r "C:/Users/OCatenacci/fshacks/CreateWeeklySWEEventsEmail/packages/Newtonsoft.Json.5.0.4/lib/net40/Newtonsoft.Json.dll"
#endif 

open System
open Newtonsoft.Json

type meta() = class
    member val count = 0 with get, set
    end

let testjson = """{
    "meta": {
        "count": 15
    }
}"""


let o = JsonConvert.DeserializeObject<meta>(testjson)

meta always gets a 0 in the count. By the way, I was originally defining meta like this:

type meta = {
   count: int
}

And I changed to using the Automatic property because I thought that Newtonsoft.Json might not be able to construct the object correctly.

I would be surprised if my version of F#/Windows makes a difference in this case but just for sake of completeness: I'm trying this with the F# 3.0 Repl (11.0.60315.1) and I'm running on a Win7 x64 (SP1) box.

like image 757
Onorio Catenacci Avatar asked Apr 29 '13 19:04

Onorio Catenacci


People also ask

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.

How do you deserialize JSON into an object in Python?

To deserialize the string to a class object, you need to write a custom method to construct the object. You can add a static method to ImageLabelCollection inside of which you construct Label objects from the loaded JSON dictionary and then assign them as a list to the class variable bbox.

What is deserialize in JSON?

Deserialization is the process of reversing a String from a previously serialized format. This coverts the serialized String into a format that allows its Data Structure properties to be accessible to manipulation.

How do I decode a JSON file?

You just have to use json_decode() function to convert JSON objects to the appropriate PHP data type. Example: By default the json_decode() function returns an object. You can optionally specify a second parameter that accepts a boolean value. When it is set as “true”, JSON objects are decoded into associative arrays.


2 Answers

Try removing the outer-most set of curly braces from your JSON. Right now, you have an object containing an instance of meta. That's probably throwing off JSON.NET. In other words, I got it to run fine by using:

let testjson = "{ 'count' : 15 }"

[Updated base on comments]

Alternately, you could keep the JSON as-is, and provide a more complex type tree in F#. For example:

type Foo =
  { meta : Meta }
and Meta =
  { count : int }
like image 164
pblasucci Avatar answered Sep 18 '22 14:09

pblasucci


With FSharp.Json library you can use just record types - no need to define classes anymore. Structure of records provided above is just enough:

type Foo =
  { meta : Meta }
and Meta =
  { count : int }

let testjson = """{"meta": {"count": 15}}"""

open FSharp.Json
let data = Json.deserialize<Foo> testjson

Disclosure: I'm author of FSharp.Json library.

like image 39
vladimir Avatar answered Sep 21 '22 14:09

vladimir