To make WebSharper.Json.Encode/Decode (WebSharper.TypedJson from C#) functions be able to support custom de/serialization for your types, it should look for an EncodeJson/DecodeJson static method on the type.
This then can be used within WebSharper libs too, for example adding Json support for decimals, which are proxied in a separate optional WebSharper.MathJS.Extensions library.
Example:
type Vector2D(x: float, y: float) =
member thix.X = x
member this.Y = y
member this.Length = sqrt(x * x + y * y)
static member EncodeJson (v: Vector2D): obj = [| v.X, v.Y |]
static member DecodeJson (o: obj): Vector2D = Vector2D((As<float[]> o)[0], (As<float[]> o)[1])
Note: if you use generics, you must mark your EncodeJson/DecodeJson methods with the Inline attribute and call WebSharper.Json functions to handle the generic pieces of data.
To make
WebSharper.Json.Encode/Decode
(WebSharper.TypedJson
from C#) functions be able to support custom de/serialization for your types, it should look for anEncodeJson/DecodeJson
static method on the type.This then can be used within WebSharper libs too, for example adding Json support for decimals, which are proxied in a separate optional
WebSharper.MathJS.Extensions
library.Example:
Note: if you use generics, you must mark your EncodeJson/DecodeJson methods with the
Inline
attribute and callWebSharper.Json
functions to handle the generic pieces of data.