0

I receive the following error after deserializing some JSON and attempting to access root.items:

 root.items.Count |> should equal 2

System.NullReferenceException: 'Object reference not set to an instance of an object.'

root.items@ was null.

The JSON is the following:

{
    "items": [
        {
            "snippet": {
                "title": "Nikeza: F# Backwards Pipe Operator",
                "tags": [
                    "Elm",
                    "F#",
                    "Giraffe",
                    "Functional Programming",
                    "Software Development"
                ]
            }
        },
        {
            "snippet": {
                "title": "Giraffe: VS Code bug that doesn't show up in VS 20017 (3)",
                "tags": [
                    "Hangouts On Air",
                ]
            }
        },
        {
            "snippet": {
                "title": "Software Craftsmanship Conference - London",
                "tags": [
                    "Programming",
                    "Software Development",
                ]
            }
        }
    ]
}

Here's my code:

[<CLIMutable>]
type Snippet =    { title: string; tags: Collections.Generic.List<String> }

[<CLIMutable>]
type Item =       { snippet : Snippet }

[<CLIMutable>]
type Response =   { items : Collections.Generic.List<Item> }

[<Test>]
let ``Apply tags to videos`` () =
    let  delimitedIds = ["id1";"id2";"id3"] |> String.concat ","
    let  url = String.Format(requestTagsUrl, apiKey, delimitedIds)
    let response = httpClient.GetAsync(url) |> Async.AwaitTask |> Async.RunSynchronously

    if response.IsSuccessStatusCode
        then let root = response.Content.ReadAsAsync<Response>() |> Async.AwaitTask |> Async.RunSynchronously
             root.items.Count |> should equal 2
        else Assert.Fail()
3
  • Can you please add the Code of your model-class? Otherwise it's not possible to see, if the Json does match the model.
    – Nikolaus
    Oct 21, 2017 at 23:10
  • My guess is either the http request is not returning the response you expect, or that ReadAsAsync does not work how you expect. I would try replace the http request with a StringContent object with the json in it to help narrow down the problem.
    – Foole
    Oct 21, 2017 at 23:39
  • Hi. The Response record type defined above the test is the "model". response.Content.ReadAsAsync<Response>() Oct 21, 2017 at 23:40

2 Answers 2

1

I have just written a test for it, and I can confirm that the deserialization part of it is correct. Check if the request is returning the response correctly.

open System
open Expecto

let json = 
    """
    {
        "items": [
            {
                "snippet": {
                    "title": "Nikeza: F# Backwards Pipe Operator",
                    "tags": [
                        "Elm",
                        "F#",
                        "Giraffe",
                        "Functional Programming",
                        "Software Development"
                    ]
                }
            },
            {
                "snippet": {
                    "title": "Giraffe: VS Code bug that doesn't show up in VS 20017 (3)",
                    "tags": [
                        "Hangouts On Air",
                    ]
                }
            },
            {
                "snippet": {
                    "title": "Software Craftsmanship Conference - London",
                    "tags": [
                        "Programming",
                        "Software Development",
                    ]
                }
            }
        ]
    }
    """

[<CLIMutable>]
type Snippet =    { title: string; tags: Collections.Generic.List<String> }

[<CLIMutable>]
type Item =       { snippet : Snippet }

[<CLIMutable>]
type Response =   { items : Collections.Generic.List<Item> }

[<Tests>]
let tests =
    testList "Test" [
        test "Testing deserialization" {
            let result : Response = Json.deserialize json
            Expect.equal result.items.Count 3 "Should have 3 items"
        }
    ]                
1
  • I found a solution. Thanks again. Oct 22, 2017 at 10:31
0

The following worked for me:

In summary, I had to resort to retrieving the actual json string using the following:

json = response.Content.ReadAsStringAsync() |> Async.AwaitTask |> Async.RunSynchronously

Afterwards, I used JsonConvert.DeserializeObject:

JsonConvert.DeserializeObject<Response>(json)

Appendix:

if response.IsSuccessStatusCode
    then let json = response.Content.ReadAsStringAsync() |> Async.AwaitTask |> Async.RunSynchronously
         let result = JsonConvert.DeserializeObject<Response>(json);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.