sq / JSIL

CIL to Javascript Compiler
http://jsil.org/
Other
1.73k stars 241 forks source link

InvalidCastException when using Newtonsoft.Json.dll (10.0.1 .NET 4.0) #1050

Open Kyoril opened 6 years ago

Kyoril commented 6 years ago

I get an uncaught exception of type System.InvalidCastException when I try to execute the following:

            // Load the document
            using (Stream strm = archive.GetFileStream("package.json"))
            {
                using (StreamReader reader = new StreamReader(strm))
                {
                    // The following line crashes!
                    dynamic rootObject = JsonConvert.DeserializeObject(reader.ReadToEnd());
                }
             }

The generated JSIL line that causes the crash somewhere in the NewtonSoft.Json.dll js code looks like this:

var rootObject = $T0D().DeserializeObject(($S05().Construct(strm)).ReadToEnd());

Exception message in log div:

Unhandled exception at http://127.0.0.1/web/Libraries/JSIL.Core.js line 5034:
Uncaught System.InvalidCastException: Unable to cast object of type 'Newtonsoft.Json.JsonTextReader' to type 'Newtonsoft.Json.Serialization.TraceJsonReader'.

Edit: The C# code runs just fine in a compiled Windows application, so the JSON data isn't corrupted. Edit 2: I also checked in the javascript debugger that the stream contains the correct json text value, so everything got loaded correctly from the local file storage.

kfina91 commented 3 years ago

I have the same problem (using Newtonsoft.JSON 7.0.1, because modern ones do not even compile for JSIL). I am trying to call a C# method from JS like this

// index.html JS
window.asm = JSIL.GetAssembly("myproject", true);
// C#
private Dictionary<string, object> dict;
public MyClass(string json) {
    this.dict = Newtonsoft.Json.JsonConvert.DeserializeObject<Dictionary<string, object>>( json );
}
// Then in the browser console I try to instantiate MyClass
asm.mynamespace.MyClass(JSON.stringify({a:1, b: "hello"}))

This creates the same error message as shown by Kyoril: Unable to cast object of type 'Newtonsoft.Json.JsonTextReader' to type 'Newtonsoft.Json.Serialization.TraceJsonReader'. Stacktrace:

'Error\n    
at captureStackTrace (http://127.0.0.1:5500/Libraries/IgnoredBCL/JSIL.Bootstrap.js:1961:15)\n    
at System_InvalidCastException._ctor [as _ctor$304=void] (http://127.0.0.1:5500/Libraries/IgnoredBCL/JSIL.Bootstrap.js:1978:28)\n    
at System_InvalidCastException.System_Exception__ctor [as _ctor] (jsil://closure/System.Exception..ctor:12:25)\n    
at System_InvalidCastException._ctor$304=void (http://127.0.0.1:5500/Libraries/IgnoredBCL/JSIL.Bootstrap.js:2063:44)\n    
at System_InvalidCastException__ctor [as _ctor] (jsil://closure/System.InvalidCastException..ctor:12:25)\n    
at new System_InvalidCastException (jsil://closure/System.InvalidCastException:20:21)\n    
at throwCastError (http://127.0.0.1:5500/Libraries/JSIL.Core.js:5034:11)\n    
at Function.Cast [as $Cast] (http://127.0.0.1:5500/Libraries/JSIL.Core.js:5139:7)\n    
at Newtonsoft_Json_JsonSerializer.JsonSerializer_DeserializeInternal (http://127.0.0.1:5500/root/Newtonsoft.Json,%20Version=7.0.0.0,%20Culture=neutral,%20PublicKeyToken=30ad4fe6b2a6aeed.js:18658:97)\n    
at Newtonsoft_Json_JsonSerializer.JsonSerializer_Deserialize$06 [as Deserialize$769,301=300] (http://127.0.0.1:5500/root/Newtonsoft.Json,%20Version=7.0.0.0,%20Culture=neutral,%20PublicKeyToken=30ad4fe6b2a6aeed.js:18631:17)'

@Kyoril Did you find a workaround?