-
Notifications
You must be signed in to change notification settings - Fork 0
/
Json.fs
35 lines (29 loc) · 1.39 KB
/
Json.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
module Json
open System.Collections.Generic
open FSharp.Data
open LiteDB
let toBson (value) =
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 ()
toBsonValue value :?> BsonDocument