question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to deserialize JsonProvider types

See original GitHub issue

Hi, thank you for your library.

I successfully serialized the type generated by the JsonProvider type provider, but when I try to deserialize I get an exception:

Could not create an instance of type FSharp.Data.Runtime.BaseTypes.IJsonDocument. Type is an interface or abstract class and cannot be instantiated. Path ‘folders[0].JsonValue’, line 1, position 25.

use db = new LiteDatabase("wunderlist.db", FSharpBsonMapper())

type F = JsonProvider<"FoldersJsonSample.json",  RootName = "Folder">
let folders = F.Load(....)
{ folders = folders } |> db.GetCollection<BackUp>().Insert |> ignore

let get = db.GetCollection<BackUp>().FindAll() // <- exception

would you be able to suggest a workaround or fix it?

Thank you.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
UnoSDcommented, Aug 6, 2018

Here it is:

let rec toBsonValue value =
    let toBsonDictionary (record : (string * JsonValue)[]) =
        record |>
        Array.map (fun (name, value) -> 
                        KeyValuePair((if name = "id" then "_id" else name), toBsonValue value)) |>
        (fun x -> new Dictionary<string, BsonValue>(x))

    let toBsonArray (array : JsonValue[]) =
        array |>
        Array.map (fun value -> toBsonValue value)

    let (|Integer|Decimal|) input =
        match System.Int64.TryParse (input.ToString()) with
        | true, value -> Integer value
        | false, _ -> Decimal input

    match value with
        | JsonValue.String  value -> BsonValue    (value)
        | JsonValue.Number  value -> match value with
                                        | Integer i -> BsonValue(i)
                                        | Decimal d -> BsonValue(d)
        | JsonValue.Float   value -> BsonValue    (value)
        | JsonValue.Boolean value -> BsonValue    (value)
        | JsonValue.Record  value -> BsonDocument (toBsonDictionary value) :> BsonValue
        | JsonValue.Array   value -> BsonArray    (toBsonArray      value) :> BsonValue
        | JsonValue.Null          -> BsonValue    ()

from https://github.com/UnoSD/WunderlistBackup

and deserializing is easy… JsonValue.Parse(bson.ToString()) although you may have to add some converters for types like long that get serialized as objects.

0reactions
Zaid-Ajajcommented, Aug 6, 2018

@UnoSD I am glad you could figure it out, maybe you could share it for others who come across this issue 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to deserialize json file
To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or...
Read more >
Unable To Deserialize Object - Forums - Liferay
Locally on my own enviroment i've been able to serialize and deserialize data. To achieve this i used the JSONFactoryUtil.serialize & deserialize methods....
Read more >
Failed to deserialize JSON
The error is correct, the built-in JSON deserializer does not allow you to turn an array of simple types into an Object. You...
Read more >
JAX-RS Data Bindings - Apache CXF
The "convertTypesToStrings" property can be used to configure JSONProvider to convert all the types to strings. Jackson. If you prefer working with Jackson...
Read more >
Jackson JSON Java Parser API Example Tutorial
Jackson JSON Parser API provides easy way to convert JSON to POJO Object and supports easy conversion to Map from JSON data.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found