2 people like it.
Like the snippet!
JsonValue JsonConverter
Not super efficient, and doesn't work on every case, but works most of the time. Json.NET JsonConverter for a JsonValue, i.e. JsonValueConverter. If you're using with a converter for discriminated unions, be sure to give this one higher precedence (since JsonValue is implemented as a DU).
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:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
|
open Newtonsoft.Json.Linq
type JsonValueConverter () =
inherit JsonConverter()
override __.CanConvert(objectType) =
typeof<JsonValue> = objectType.BaseType ||
typeof<JsonValue> = objectType
override __.WriteJson(writer, value, serializer) =
let jsonValue = value :?> JsonValue
let jToken =
match jsonValue with
| JsonValue.Null -> JValue.CreateNull () :> JToken
| _ -> JToken.Parse (jsonValue.ToString())
jToken.WriteTo writer
override __.ReadJson(reader, objectType, existingValue, serializer) =
let rec toJsonValue (jToken:JToken) =
match jToken.Type with
| JTokenType.Array ->
let values = jToken.Values<JToken>()
values
|> Seq.map toJsonValue
|> Seq.toArray
|> JsonValue.Array
| JTokenType.Object ->
let value = jToken.ToString()
(JsonValue.Parse value)
| JTokenType.Float ->
let value = jToken.Value<float>()
(JsonValue.Float value)
| JTokenType.Null -> (JsonValue.Null)
| JTokenType.Boolean ->
let value = jToken.Value<bool>()
(JsonValue.Boolean value)
| JTokenType.String ->
let value = jToken.Value<string>()
(JsonValue.String value)
| JTokenType.Integer ->
let value = jToken.Value<int64>()
let d = decimal value
(JsonValue.Number d)
| _ ->
failwithf
"Unable to deserialize into JsonValue. Unrecognized jToken type.|tokenType=%A|value=%s"
jToken.Type
(jToken.ToString())
let jToken = JToken.Load reader
let jv = toJsonValue jToken
jv :> obj
|
namespace Newtonsoft
namespace Newtonsoft.Json
namespace Newtonsoft.Json.Linq
Multiple items
type JsonValueConverter =
inherit obj
new : unit -> JsonValueConverter
override CanConvert : objectType:'a -> 'b
override ReadJson : reader:'a * objectType:'b * existingValue:'c * serializer:'d -> 'e
override WriteJson : writer:'a * value:'b * serializer:'c -> 'd
Full name: Script.JsonValueConverter
--------------------
new : unit -> JsonValueConverter
override JsonValueConverter.CanConvert : objectType:'a -> 'b
Full name: Script.JsonValueConverter.CanConvert
val typeof<'T> : System.Type
Full name: Microsoft.FSharp.Core.Operators.typeof
override JsonValueConverter.WriteJson : writer:'a * value:'b * serializer:'c -> 'd
Full name: Script.JsonValueConverter.WriteJson
Multiple items
type JValue =
inherit JToken
new : other:JValue -> JValue + 14 overloads
member CompareTo : obj:JValue -> int
member Equals : other:JValue -> bool + 1 overload
member GetHashCode : unit -> int
member HasValues : bool
member ToString : unit -> string + 3 overloads
member Type : JTokenType
member Value : obj with get, set
member WriteTo : writer:JsonWriter * [<ParamArray>] converters:JsonConverter[] -> unit
member WriteToAsync : writer:JsonWriter * cancellationToken:CancellationToken * [<ParamArray>] converters:JsonConverter[] -> Task
...
Full name: Newtonsoft.Json.Linq.JValue
--------------------
JValue(other: JValue) : unit
(+0 other overloads)
JValue(value: int64) : unit
(+0 other overloads)
JValue(value: decimal) : unit
(+0 other overloads)
JValue(value: char) : unit
(+0 other overloads)
JValue(value: uint64) : unit
(+0 other overloads)
JValue(value: float) : unit
(+0 other overloads)
JValue(value: float32) : unit
(+0 other overloads)
JValue(value: System.DateTime) : unit
(+0 other overloads)
JValue(value: System.DateTimeOffset) : unit
(+0 other overloads)
JValue(value: bool) : unit
(+0 other overloads)
JValue.CreateNull() : JValue
type JToken =
member AddAfterSelf : content:obj -> unit
member AddAnnotation : annotation:obj -> unit
member AddBeforeSelf : content:obj -> unit
member AfterSelf : unit -> IEnumerable<JToken>
member Ancestors : unit -> IEnumerable<JToken>
member AncestorsAndSelf : unit -> IEnumerable<JToken>
member Annotation<'T> : unit -> 'T + 1 overload
member Annotations<'T> : unit -> IEnumerable<'T> + 1 overload
member BeforeSelf : unit -> IEnumerable<JToken>
member Children : unit -> JEnumerable<JToken> + 1 overload
...
Full name: Newtonsoft.Json.Linq.JToken
JToken.Parse(json: string) : JToken
JToken.Parse(json: string, settings: JsonLoadSettings) : JToken
override JsonValueConverter.ReadJson : reader:'a * objectType:'b * existingValue:'c * serializer:'d -> 'e
Full name: Script.JsonValueConverter.ReadJson
type JTokenType =
| None = 0
| Object = 1
| Array = 2
| Constructor = 3
| Property = 4
| Comment = 5
| Integer = 6
| Float = 7
| String = 8
| Boolean = 9
...
Full name: Newtonsoft.Json.Linq.JTokenType
field JTokenType.Array = 2
module Seq
from Microsoft.FSharp.Collections
val map : mapping:('T -> 'U) -> source:seq<'T> -> seq<'U>
Full name: Microsoft.FSharp.Collections.Seq.map
val toArray : source:seq<'T> -> 'T []
Full name: Microsoft.FSharp.Collections.Seq.toArray
module Array
from Microsoft.FSharp.Collections
field JTokenType.Object = 1
field JTokenType.Float = 7
Multiple items
val float : value:'T -> float (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.float
--------------------
type float = System.Double
Full name: Microsoft.FSharp.Core.float
--------------------
type float<'Measure> = float
Full name: Microsoft.FSharp.Core.float<_>
field JTokenType.Null = 10
field JTokenType.Boolean = 9
type bool = System.Boolean
Full name: Microsoft.FSharp.Core.bool
field JTokenType.String = 8
Multiple items
val string : value:'T -> string
Full name: Microsoft.FSharp.Core.Operators.string
--------------------
type string = System.String
Full name: Microsoft.FSharp.Core.string
module String
from Microsoft.FSharp.Core
field JTokenType.Integer = 6
Multiple items
val int64 : value:'T -> int64 (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.int64
--------------------
type int64 = System.Int64
Full name: Microsoft.FSharp.Core.int64
--------------------
type int64<'Measure> = int64
Full name: Microsoft.FSharp.Core.int64<_>
Multiple items
val decimal : value:'T -> decimal (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.decimal
--------------------
type decimal = System.Decimal
Full name: Microsoft.FSharp.Core.decimal
--------------------
type decimal<'Measure> = decimal
Full name: Microsoft.FSharp.Core.decimal<_>
val failwithf : format:Printf.StringFormat<'T,'Result> -> 'T
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.failwithf
JToken.Load(reader: Newtonsoft.Json.JsonReader) : JToken
JToken.Load(reader: Newtonsoft.Json.JsonReader, settings: JsonLoadSettings) : JToken
type obj = System.Object
Full name: Microsoft.FSharp.Core.obj
More information