Skip to content
This repository has been archived by the owner on Aug 24, 2022. It is now read-only.

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

Open
Kyoril opened this issue Nov 28, 2017 · 1 comment
Open

Comments

@Kyoril
Copy link

Kyoril commented Nov 28, 2017

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
Copy link

kfina91 commented Nov 9, 2021

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?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants