diff --git a/src/compiler/WebSharper.Core/Json.fs b/src/compiler/WebSharper.Core/Json.fs index 301e97137..ab46bf996 100644 --- a/src/compiler/WebSharper.Core/Json.fs +++ b/src/compiler/WebSharper.Core/Json.fs @@ -601,6 +601,21 @@ let serializers = | _ -> raise (DecoderException(String g, typeof)) | x -> raise (DecoderException(x, typeof)) add encGuid decGuid d + let encDecimal (d: decimal) = + let b = System.Decimal.GetBits(d) + EncodedInstance ( + AST.Address [ "Decimal"; "WebSharper" ], + [ + "bits", EncodedArray (b |> Seq.map (string >> EncodedNumber) |> List.ofSeq) + ] + ) + let decDecimal = function + | Object [ "mathjs", String "BigNumber"; "value", String d ] as x -> + match System.Decimal.TryParse d with + | true, d -> d + | _ -> raise (DecoderException(x, typeof)) + | x -> raise (DecoderException(x, typeof)) + add encDecimal decDecimal d d let tupleEncoder dE (i: FormatSettings) (ta: TAttrs) = diff --git a/src/stdlib/WebSharper.Main/Json.fs b/src/stdlib/WebSharper.Main/Json.fs index 06d841a1b..c16c8a1c2 100644 --- a/src/stdlib/WebSharper.Main/Json.fs +++ b/src/stdlib/WebSharper.Main/Json.fs @@ -82,6 +82,10 @@ let shallowMap (f: obj -> obj) (x: obj) : obj = | _ -> x +type SpecialTypes = + | List = 1 + | Decimal = 2 + [] [)>] let Activate<'T> (json: obj) : 'T = @@ -91,7 +95,11 @@ let Activate<'T> (json: obj) : 'T = json else for i = 0 to types.Length - 1 do - types.[i] <- lookup (As types.[i]) + types.[i] <- + match As types.[i] with + | [| "WebSharper"; "List"; "T" |] -> box SpecialTypes.List + | [| "WebSharper"; "Decimal" |] -> box SpecialTypes.Decimal + | t -> lookup t json?("$DATA") let rec decode (x: obj) : obj = if x = null then x else @@ -104,8 +112,10 @@ let Activate<'T> (json: obj) : 'T = let ti = x?("$T") if ti ===. JS.Undefined then o else let t = types.[ti] - if t ===. JS.Global?WebSharper?List?T then + if t ===. SpecialTypes.List then box (List.ofArray (As o)) + elif t ===. SpecialTypes.Decimal then + JS.Global?WebSharper?MathJS?Extensions?Extensions?CreateDecimalBits(o?bits) else let r = JS.New types.[ti] JS.ForEach o (fun k -> (?<-) r k ((?) o k); false)