Can JsonProvider deserialize to Generic.Dictionary?

I learn about type providers, and it looks like a ground break function. However, I cannot remove json deserialization from JsonProvider so that the target type has the Generic.Dictionary property. This can be done using Json.NET. Here is the code:

type ByProv = JsonProvider<"""{"dict":{"A":1,"B":2}}""">
type ByHand(d:Dictionary<string,int>) =
    member val dict = d with get,set

let text = """{"dict":{"C":3,"D":4}}"""
let newt = JsonConvert.DeserializeObject<ByHand> text
let prov = ByProv.Parse text
printfn "%A" newt.dict.["C"]
//Can only call statically inferred A or B (and it will throw at run-time)
printfn "%A" prov.Dict.A

Apparently dictdefined as a record type, not Dictionarythe default. Can this be overcome?

+4
source share
2 answers

JSON , JSON ( ) JSON . - , .

JSON , JsonValue, :

type ByProv = JsonProvider<"""{"dict":{"A":1,"B":2}}""">

let text = """{"dict":{"C":3,"D":4}}"""
let prov = ByProv.Parse text

prov.Dict.JsonValue.["C"].AsInteger()

, , JSON , ( ). , , F # Data GitHub!

+4

JSON .net, Properties() dict, :

type ByProv = JsonProvider<"""{"dict":{"A":1,"B":2}}""">

let text = """{"dict":{"C":3,"D":4}}"""

let prov = ByProv.Parse text

let d = prov.Dict.JsonValue.Properties() |> dict

d System.Collections.Generic.IDictionary<string,JsonValue>. , , Seq.map :

let d =
    prov.Dict.JsonValue.Properties()
    |> Seq.map (fun (k,v) -> k,v.AsInteger())
    |> dict
0

Source: https://habr.com/ru/post/1544311/


All Articles