-
Notifications
You must be signed in to change notification settings - Fork 0
/
Json.fs
36 lines (28 loc) · 1.1 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
36
module Json
#nowarn "44" //need Jmes.Transform with JToken since we are annotating it with custom functions
open System
open DevLab.JmesPath
open Newtonsoft.Json.Linq
open FSharp.Interop.NullOptAble
type IJsonDocument = FSharp.Data.Runtime.BaseTypes.IJsonDocument
module JToken =
let isNullOrEmpty (token:JToken) =
(token = null) ||
(token.Type = JTokenType.Array && not token.HasValues) ||
(token.Type = JTokenType.Object && not token.HasValues) ||
(token.Type = JTokenType.String && token.ToString() = String.Empty) ||
(token.Type = JTokenType.Null)
let toIJsonDocOption target : IJsonDocument option =
target |> Option.map (fun x-> upcast x)
let jmes =
let jmes' = JmesPath()
jmes'.FunctionRepository
.Register<Console.Error>()
.Register<Console.Warn>()
.Register<Console.Info>()
.Register<Console.Progress>()
.Register<Console.Debug>()
.Register<Console.Trace>() |> ignore
jmes'
let queryJmesPath q (json:JToken) =
jmes.Transform(json, q) |> Option.ofObjWhenNot JToken.isNullOrEmpty