core icon indicating copy to clipboard operation
core copied to clipboard

Serialization of NaN value.

Open V0d01ey opened this issue 7 years ago • 1 comments

As for now following remote method gives WriteException on sending response.

        [Remote]
        public static Task<float> GetNaN()
        {
            return Task.FromResult(float.NaN);
        }
[WriteException: Выдано исключение типа &quot;WebSharper.Core.Json+WriteException&quot;.]
   WebSharper.Core.Json.wN@284(TextWriter writer, String x) +105
   WebSharper.Core.Json.Stringify(Value v) +57
   [email protected](Value _arg1) +12
   [email protected](a a) +63
   Microsoft.FSharp.Control.AsyncIAsyncResult`1.GetResult() +356
   Microsoft.FSharp.Control.AsBeginEndHelpers.endAction(IAsyncResult iar) +64
   &lt;StartupCode$FSharp-Core&gt;[email protected](IAsyncResult iar) +40
   WebSharper.Web.RpcHandler.System-Web-IHttpAsyncHandler-EndProcessRequest(IAsyncResult res) +13
   System.Web.CallHandlerExecutionStep.InvokeEndHandler(IAsyncResult ar) +152
   System.Web.CallHandlerExecutionStep.OnAsyncHandlerCompletion(IAsyncResult ar) +126

This exception has no message about the value that caused the exception. So in more complicated structures there is a challenge to find out what was the real problem:

[WriteException: Выдано исключение типа &quot;WebSharper.Core.Json+WriteException&quot;.]
   WebSharper.Core.Json.wN@284(TextWriter writer, String x) +105
   WebSharper.Core.Json.wA@274(TextWriter writer, FSharpList`1 x) +77
   WebSharper.Core.Json.wO@308(TextWriter writer, FSharpList`1 x) +50
   WebSharper.Core.Json.wO@308(TextWriter writer, FSharpList`1 x) +115
   WebSharper.Core.Json.wO@308(TextWriter writer, FSharpList`1 x) +50
   WebSharper.Core.Json.wO@308(TextWriter writer, FSharpList`1 x) +115
   WebSharper.Core.Json.wA@274(TextWriter writer, FSharpList`1 x) +77
   WebSharper.Core.Json.wO@308(TextWriter writer, FSharpList`1 x) +50
   WebSharper.Core.Json.wO@308(TextWriter writer, FSharpList`1 x) +115
   WebSharper.Core.Json.wA@274(TextWriter writer, FSharpList`1 x) +41
   WebSharper.Core.Json.wO@308(TextWriter writer, FSharpList`1 x) +50
   WebSharper.Core.Json.wO@308(TextWriter writer, FSharpList`1 x) +115
   WebSharper.Core.Json.wO@308(TextWriter writer, FSharpList`1 x) +115
   WebSharper.Core.Json.Stringify(Value v) +57

How this problem will be resolved? Will NaN be serialized in future?

V0d01ey avatar Mar 05 '18 16:03 V0d01ey

Thanks for the report!

Currently RPC results are parsed with a JSON.parse on the client and then have object prototypes restored which are also returned by the RPC. A simple way to include some special values would be (like NaN and Infinity) is to encode them as special objects (for example returning { "$Val": "NaN" }) that a reviver function added to JSON.parse can restore to the values.

Similarly the JSON.stringify used to send RPC argument values need a replacer, transforming these values in the other direction.

Jand42 avatar Mar 05 '18 20:03 Jand42