Last active
February 10, 2020 21:51
-
-
Save mrange/988ffdbeb75568aa4ed9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2015 Mårten Rånge | |
// | |
// Licensed under the Apache License, Version 2.0 (the "License"); | |
// you may not use this file except in compliance with the License. | |
// You may obtain a copy of the License at | |
// | |
// http://www.apache.org/licenses/LICENSE-2.0 | |
// | |
// Unless required by applicable law or agreed to in writing, software | |
// distributed under the License is distributed on an "AS IS" BASIS, | |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
// See the License for the specific language governing permissions and | |
// limitations under the License. | |
module DynamicJson = | |
open System | |
open Newtonsoft.Json.Linq | |
exception JsonCastException of Type | |
type Json = | |
| JsonValue of JValue | |
| JsonObject of JObject | |
| JsonArray of JArray | |
| JsonMissingProperty of JToken*string | |
| JsonIndexOutOfRange of JToken*int | |
| JsonUnrecognizedType of obj | |
static member AsJson (o : obj) = | |
match o with | |
| :? JObject as jobj-> JsonObject jobj | |
| :? JArray as jarr -> JsonArray jarr | |
| :? JValue as jval -> JsonValue jval | |
| _ -> JsonUnrecognizedType o | |
member x.Path = | |
match x with | |
| JsonValue v -> v.Path | |
| JsonObject o -> o.Path | |
| JsonArray a -> a.Path | |
| JsonMissingProperty (t,key) -> t.Path + "." + key | |
| JsonIndexOutOfRange (t,idx) -> sprintf "%s.[%d]" t.Path idx | |
| JsonUnrecognizedType v -> "<NULL>" | |
override x.ToString () = | |
match x with | |
| JsonValue v -> sprintf "Value: %s" <| v.ToString () | |
| JsonObject o -> sprintf "Object: %s" <| o.ToString () | |
| JsonArray a -> sprintf "Array: %s" <| a.ToString () | |
| JsonMissingProperty (t,key) -> sprintf "Missing property: %s.%s" t.Path key | |
| JsonIndexOutOfRange (t,idx) -> sprintf "Out of range: %s.[%d]" t.Path idx | |
| JsonUnrecognizedType v -> sprintf "Value: %s" <| v.ToString () | |
static member (?) (json : Json, key : string) : Json = | |
match json with | |
| JsonValue jval -> JsonMissingProperty (jval, key) | |
| JsonArray jarr -> JsonMissingProperty (jarr, key) | |
| JsonObject jobj -> | |
let p = jobj.Property key | |
if p <> null then | |
Json.AsJson p.Value | |
else | |
JsonMissingProperty (jobj, key) | |
| _ -> json | |
member x.Item (idx : int) : Json = | |
match x with | |
| JsonValue jval -> JsonIndexOutOfRange (jval, idx) | |
| JsonObject jobj -> JsonIndexOutOfRange (jobj, idx) | |
| JsonArray jarr -> | |
if idx < 0 then | |
JsonIndexOutOfRange (jarr, idx) | |
elif idx >= jarr.Count then | |
JsonIndexOutOfRange (jarr, idx) | |
else | |
Json.AsJson jarr.[idx] | |
| _ -> x | |
member x.HasValue = | |
match x with | |
| JsonValue jval -> true | |
| _ -> false | |
member x.CastTo<'T> () = | |
match x with | |
| JsonValue jval -> jval.ToObject<'T>() | |
| JsonIndexOutOfRange (t, idx) -> | |
raise (IndexOutOfRangeException (x.ToString ())) | |
| _ -> | |
raise (InvalidCastException ()) | |
let asJson (o : obj) = Json.AsJson o | |
let hasValue (json : Json) = json.HasValue | |
let castTo<'T> (json : Json) = json.CastTo<'T> () | |
let describe (json : Json) = json.ToString () | |
module Test = | |
let json = """ | |
{ | |
"glossary": { | |
"title": "example glossary", | |
"GlossDiv": { | |
"title": "S", | |
"id": 24, | |
"GlossList": { | |
"GlossEntry": { | |
"ID": "SGML", | |
"SortAs": "SGML", | |
"GlossTerm": "Standard Generalized Markup Language", | |
"Acronym": "SGML", | |
"Abbrev": "ISO 8879:1986", | |
"GlossDef": { | |
"para": "A meta-markup language, used to create markup languages such as DocBook.", | |
"GlossSeeAlso": ["GML", "XML"] | |
}, | |
"GlossSee": "markup" | |
} | |
} | |
} | |
} | |
} | |
""" | |
open DynamicJson | |
open Newtonsoft.Json | |
open Test | |
[<EntryPoint>] | |
let main argv = | |
let json = asJson <| JsonConvert.DeserializeObject Test.json | |
let missing = describe json?glossary?missingProp?nextProp.[1] | |
let seeAlso = castTo<string> json?glossary?GlossDiv?GlossList?GlossEntry?GlossDef?GlossSeeAlso.[1] | |
printfn "%A" missing | |
printfn "%A" seeAlso | |
0 | |
Well, I thought I create repo around the F# navigate ideas. Feel free to have a look at the example programs and jot down some ideas if you have them
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Well that didn't work out as System.Text.Json doesn't have a writable DOM