0 people like it.

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: 
53: 
54: 
open FSharp.Data
open Newtonsoft.Json
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
Multiple items
namespace FSharp

--------------------
namespace Microsoft.FSharp
Multiple items
namespace FSharp.Data

--------------------
namespace Microsoft.FSharp.Data
namespace Newtonsoft
namespace Newtonsoft.Json
namespace Newtonsoft.Json.Linq
Multiple items
type JsonValueConverter =
  inherit JsonConverter
  new : unit -> JsonValueConverter
  override CanConvert : objectType:Type -> bool
  override ReadJson : reader:JsonReader * objectType:Type * existingValue:obj * serializer:JsonSerializer -> obj
  override WriteJson : writer:JsonWriter * value:obj * serializer:JsonSerializer -> unit

Full name: Script.JsonValueConverter

--------------------
new : unit -> JsonValueConverter
Multiple items
type JsonConverter =
  member CanConvert : objectType:Type -> bool
  member CanRead : bool
  member CanWrite : bool
  member ReadJson : reader:JsonReader * objectType:Type * existingValue:obj * serializer:JsonSerializer -> obj
  member WriteJson : writer:JsonWriter * value:obj * serializer:JsonSerializer -> unit

Full name: Newtonsoft.Json.JsonConverter

--------------------
type JsonConverter<'T> =
  inherit JsonConverter
  member CanConvert : objectType:Type -> bool
  member ReadJson : reader:JsonReader * objectType:Type * existingValue:obj * serializer:JsonSerializer -> obj + 1 overload
  member WriteJson : writer:JsonWriter * value:obj * serializer:JsonSerializer -> unit + 1 overload

Full name: Newtonsoft.Json.JsonConverter<_>

--------------------
type JsonConverterAttribute =
  inherit Attribute
  new : converterType:Type -> JsonConverterAttribute + 1 overload
  member ConverterParameters : obj[]
  member ConverterType : Type

Full name: Newtonsoft.Json.JsonConverterAttribute

--------------------
JsonConverter() : unit

--------------------
JsonConverterAttribute(converterType: System.Type) : unit
JsonConverterAttribute(converterType: System.Type, [<System.ParamArray>] converterParameters: obj []) : unit
override JsonValueConverter.CanConvert : objectType:System.Type -> bool

Full name: Script.JsonValueConverter.CanConvert
val objectType : System.Type
val typeof<'T> : System.Type

Full name: Microsoft.FSharp.Core.Operators.typeof
type JsonValue =
  | String of string
  | Number of decimal
  | Float of float
  | Record of properties: (string * JsonValue) []
  | Array of elements: JsonValue []
  | Boolean of bool
  | Null
  member Request : uri:string * ?httpMethod:string * ?headers:seq<string * string> -> HttpResponse
  member RequestAsync : uri:string * ?httpMethod:string * ?headers:seq<string * string> -> Async<HttpResponse>
  override ToString : unit -> string
  member ToString : saveOptions:JsonSaveOptions -> string
  member WriteTo : w:TextWriter * saveOptions:JsonSaveOptions -> unit
  static member AsyncLoad : uri:string * ?cultureInfo:CultureInfo -> Async<JsonValue>
  static member private JsonStringEncodeTo : w:TextWriter -> value:string -> unit
  static member Load : uri:string * ?cultureInfo:CultureInfo -> JsonValue
  static member Load : reader:TextReader * ?cultureInfo:CultureInfo -> JsonValue
  static member Load : stream:Stream * ?cultureInfo:CultureInfo -> JsonValue
  static member Parse : text:string * ?cultureInfo:CultureInfo -> JsonValue
  static member ParseMultiple : text:string * ?cultureInfo:CultureInfo -> seq<JsonValue>
  static member ParseSample : text:string * ?cultureInfo:CultureInfo -> JsonValue

Full name: FSharp.Data.JsonValue
property System.Type.BaseType: System.Type
val __ : JsonValueConverter
override JsonValueConverter.WriteJson : writer:JsonWriter * value:obj * serializer:JsonSerializer -> unit

Full name: Script.JsonValueConverter.WriteJson
val writer : JsonWriter
val value : obj
val serializer : JsonSerializer
val jsonValue : JsonValue
val jToken : JToken
union case JsonValue.Null: JsonValue
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 JsonValue.ToString : unit -> string
member JsonValue.ToString : saveOptions:JsonSaveOptions -> string
JToken.WriteTo(writer: JsonWriter, [<System.ParamArray>] converters: JsonConverter []) : unit
override JsonValueConverter.ReadJson : reader:JsonReader * objectType:System.Type * existingValue:obj * serializer:JsonSerializer -> obj

Full name: Script.JsonValueConverter.ReadJson
val reader : JsonReader
val existingValue : obj
val toJsonValue : (JToken -> JsonValue)
property JToken.Type: JTokenType
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
val values : System.Collections.Generic.IEnumerable<JToken>
(extension) System.Collections.Generic.IEnumerable.Values() : IJEnumerable<JToken>
(extension) System.Collections.Generic.IEnumerable.Values<'U>() : System.Collections.Generic.IEnumerable<'U>
JToken.Values<'T>() : System.Collections.Generic.IEnumerable<'T>
(extension) System.Collections.Generic.IEnumerable.Values(key: obj) : IJEnumerable<JToken>
(extension) System.Collections.Generic.IEnumerable.Values<'U>(key: obj) : System.Collections.Generic.IEnumerable<'U>
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
union case JsonValue.Array: elements: JsonValue [] -> JsonValue
field JTokenType.Object = 1
val value : string
JToken.ToString() : string
JToken.ToString(formatting: Formatting, [<System.ParamArray>] converters: JsonConverter []) : string
static member JsonValue.Parse : text:string * ?cultureInfo:System.Globalization.CultureInfo -> JsonValue
field JTokenType.Float = 7
val value : float
(extension) System.Collections.Generic.IEnumerable.Value<'U>() : 'U
(extension) System.Collections.Generic.IEnumerable.Value<'T,'U (requires 'T :> JToken)>() : 'U
JToken.Value<'T>(key: obj) : 'T
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<_>
union case JsonValue.Float: float -> JsonValue
field JTokenType.Null = 10
field JTokenType.Boolean = 9
val value : bool
type bool = System.Boolean

Full name: Microsoft.FSharp.Core.bool
union case JsonValue.Boolean: bool -> JsonValue
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
union case JsonValue.String: string -> JsonValue
field JTokenType.Integer = 6
val value : int64
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<_>
val d : decimal
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<_>
union case JsonValue.Number: decimal -> JsonValue
val failwithf : format:Printf.StringFormat<'T,'Result> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.failwithf
JToken.Load(reader: JsonReader) : JToken
JToken.Load(reader: JsonReader, settings: JsonLoadSettings) : JToken
val jv : JsonValue
type obj = System.Object

Full name: Microsoft.FSharp.Core.obj

More information

Link:http://fssnip.net/7Vn
Posted:6 days ago
Author:Nat Elkins
Tags: json , json.net , jsonvalue , jsonconverter , jsonvalueconverter