Skip to content

Commit

Permalink
#952 Enable decimal remoting
Browse files Browse the repository at this point in the history
  • Loading branch information
Jand42 committed May 19, 2018
1 parent 9106ea2 commit 29fe7b7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/compiler/WebSharper.Core/Json.fs
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,21 @@ let serializers =
| _ -> raise (DecoderException(String g, typeof<System.Guid>))
| x -> raise (DecoderException(x, typeof<System.Guid>))
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<decimal>))
| x -> raise (DecoderException(x, typeof<decimal>))
add encDecimal decDecimal d
d

let tupleEncoder dE (i: FormatSettings) (ta: TAttrs) =
Expand Down
14 changes: 12 additions & 2 deletions src/stdlib/WebSharper.Main/Json.fs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ let shallowMap (f: obj -> obj) (x: obj) : obj =
| _ ->
x

type SpecialTypes =
| List = 1
| Decimal = 2

[<JavaScript>]
[<Require(typeof<Resource>)>]
let Activate<'T> (json: obj) : 'T =
Expand All @@ -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<string[]> 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
Expand All @@ -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<obj[]> 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)
Expand Down

0 comments on commit 29fe7b7

Please sign in to comment.