An error occurs during parsing JSON data

LikeVBDotNet

Member
Joined
Aug 2, 2022
Messages
5
Programming Experience
Beginner
Hi All,

I encountered an error during parsing the JSON data received from the external device through the serial port.
Could you advise what went wrong with my script? Anyway to resolve the issue? Thus, I have pasted the script and error message below.

Data received from an external device through the serial port
Serial CMD Received: r
Main State: Idle --> read_temperatures
Read Sensors State: Not running --> start
Sensor chain initialized
Sensor Conversion Started
Read Sensors State: start --> continue
Address Temperature (deg C)
4247B561000000CF 23.44
4282D40F000000E2 24.53
Read Sensors State: continue --> not running
{"num_sensors":2,"addresses":[14915921967490352962,16285016252837298754],"data":[23.4375,24.528]}
Main State: Read_Temp --> Idle

VB.NET:
Class Temperature_Data
        Public Property data As Double()
    End Class

Private Sub btnRead_Sensors_Click(sender As Object, e As EventArgs) Handles btnRead_Sensors.Click

        Dim Json_Strings_Length As Integer

        ' Sending r inquiry data from an external device
        Arduino_Return = ""
        Arduino_VCOM.DiscardInBuffer()
        Delay(0.1)
        Arduino_Send_Text("r")
        Delay(1)

        ' Data received from an external device through the serial port and stored to Arduino_Return buffer
        If Arduino_Return.IndexOf("data") <> -1 Then
            Json_Strings_Length = Arduino_Return.IndexOf("}") - Arduino_Return.IndexOf("{")
            Json_Data = Arduino_Return.Substring(Arduino_Return.IndexOf("{"), Json_Strings_Length + 1)
                       
            Dim d As Temperature_Data = System.Text.Json.JsonSerializer.Deserialize(Of Temperature_Data)(Json_Data)
            Dim firstValue As Double = d.data(0)
            Dim secondValue As Double = d.data(1)

            tbxReference_Temperature.Text = firstValue.ToString
            tbxDUT_Temperature.Text = secondValue.ToString
           
        End If
       
    End Sub

Error Message:
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.IO.FileLoadException: Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
File name: 'System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
at System.ReadOnlySpan`1..ctor(T[] array)
at System.Text.Json.Serialization.Metadata.JsonPropertyInfo.DeterminePropertyName()
at System.Text.Json.Serialization.Metadata.JsonPropertyInfo.GetPolicies(Nullable`1 ignoreCondition, Nullable`1 declaringTypeNumberHandling)
at System.Text.Json.Serialization.Metadata.JsonPropertyInfo`1.Initialize(Type parentClassType, Type declaredPropertyType, Type runtimePropertyType, ConverterStrategy runtimeClassType, MemberInfo memberInfo, Boolean isVirtual, JsonConverter converter, Nullable`1 ignoreCondition, Nullable`1 parentTypeNumberHandling, JsonSerializerOptions options)
at System.Text.Json.Serialization.Metadata.JsonTypeInfo.CreateProperty(Type declaredPropertyType, Type runtimePropertyType, MemberInfo memberInfo, Type parentClassType, Boolean isVirtual, JsonConverter converter, JsonSerializerOptions options, Nullable`1 parentTypeNumberHandling, Nullable`1 ignoreCondition)
at System.Text.Json.Serialization.Metadata.JsonTypeInfo.AddProperty(MemberInfo memberInfo, Type memberType, Type parentClassType, Boolean isVirtual, Nullable`1 parentTypeNumberHandling, JsonSerializerOptions options)
at System.Text.Json.Serialization.Metadata.JsonTypeInfo.CacheMember(Type declaringType, Type memberType, MemberInfo memberInfo, Boolean isVirtual, Nullable`1 typeNumberHandling, Boolean& propertyOrderSpecified, Dictionary`2& ignoredMembers)
at System.Text.Json.Serialization.Metadata.JsonTypeInfo..ctor(Type type, JsonConverter converter, Type runtimeType, JsonSerializerOptions options)
at System.Text.Json.JsonSerializerOptions.<InitializeForReflectionSerializer>g__CreateJsonTypeInfo|112_0(Type type, JsonSerializerOptions options)
at System.Text.Json.JsonSerializerOptions.GetClassFromContextOrCreate(Type type)
at System.Text.Json.JsonSerializerOptions.GetOrAddClass(Type type)
at System.Text.Json.JsonSerializer.GetTypeInfo(JsonSerializerOptions options, Type runtimeType)
at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, JsonSerializerOptions options)
at Walalight_PCBA_FCT.frmWalalight_PCBA_FCT.btnRead_Sensors_Click(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog].



************** Loaded Assemblies **************
mscorlib
Assembly Version: 4.0.0.0
Win32 Version: 4.8.4510.0 built by: NET48REL1LAST_B
CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
----------------------------------------
Walalight_PCBA_FCT
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///C:/Walalight_PCBA_FCT/Walalight_PCBA_FCT.exe
----------------------------------------
Microsoft.VisualBasic
Assembly Version: 10.0.0.0
Win32 Version: 14.8.4084.0 built by: NET48REL1
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/Microsoft.VisualBasic/v4.0_10.0.0.0__b03f5f7f11d50a3a/Microsoft.VisualBasic.dll
----------------------------------------
System
Assembly Version: 4.0.0.0
Win32 Version: 4.8.4488.0 built by: NET48REL1LAST_C
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Core
Assembly Version: 4.0.0.0
Win32 Version: 4.8.4470.0 built by: NET48REL1LAST_C
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Core/v4.0_4.0.0.0__b77a5c561934e089/System.Core.dll
----------------------------------------
System.Windows.Forms
Assembly Version: 4.0.0.0
Win32 Version: 4.8.4488.0 built by: NET48REL1LAST_C
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
Assembly Version: 4.0.0.0
Win32 Version: 4.8.4390.0 built by: NET48REL1LAST_C
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Configuration
Assembly Version: 4.0.0.0
Win32 Version: 4.8.4190.0 built by: NET48REL1LAST_B
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
System.Xml
Assembly Version: 4.0.0.0
Win32 Version: 4.8.4084.0 built by: NET48REL1
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
System.Runtime.Remoting
Assembly Version: 4.0.0.0
Win32 Version: 4.8.4084.0 built by: NET48REL1
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Runtime.Remoting/v4.0_4.0.0.0__b77a5c561934e089/System.Runtime.Remoting.dll
----------------------------------------
System.Management
Assembly Version: 4.0.0.0
Win32 Version: 4.8.4435.0 built by: NET48REL1LAST_C
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Management/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Management.dll
----------------------------------------
System.Text.Json
Assembly Version: 6.0.0.5
Win32 Version: 6.0.622.26707
CodeBase: file:///C:/Walalight_PCBA_FCT/System.Text.Json.DLL
----------------------------------------
System.Memory
Assembly Version: 4.0.1.1
Win32 Version: 4.6.28619.01
CodeBase: file:///C:/Walalight_PCBA_FCT/System.Memory.DLL
----------------------------------------
System.ValueTuple
Assembly Version: 4.0.0.0
Win32 Version: 4.8.4084.0
CodeBase: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.ValueTuple/v4.0_4.0.0.0__cc7b13ffcd2ddd51/System.ValueTuple.dll
----------------------------------------
Microsoft.Bcl.AsyncInterfaces
Assembly Version: 6.0.0.0
Win32 Version: 6.0.21.52210
CodeBase: file:///C:/Walalight_PCBA_FCT/Microsoft.Bcl.AsyncInterfaces.DLL
----------------------------------------
System.Text.Encodings.Web
Assembly Version: 6.0.0.0
Win32 Version: 6.0.21.52210
CodeBase: file:///C:/Walalight_PCBA_FCT/System.Text.Encodings.Web.DLL
----------------------------------------

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
<system.windows.forms jitDebugging="true" />
</configuration>

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.

Thank you.
 
Last edited by a moderator:
Solution
I tried to reproduce with a .Net Framework 4.8 project and added Nuget package System.Text.Json version 6.0.5 which has the same System.Text.Json as yours.
Testing the relevant code was successful:
VB.NET:
Dim Json_Data = "{""num_sensors"":2,""addresses"":[14915921967490352962,16285016252837298754],""data"":[23.4375,24.528]}"
Dim d As Temperature_Data = System.Text.Json.JsonSerializer.Deserialize(Of Temperature_Data)(Json_Data)
The System.Runtime.CompilerServices.Unsafe.dll that was loaded was assembly version 6.0.0.0 (fileversion 6.0.21.52210). If you have a different file in your output folder maybe there is installed a package with a conflicting version? Although I don't understand why the exception says it wants 4.0.4.1.
I tried to reproduce with a .Net Framework 4.8 project and added Nuget package System.Text.Json version 6.0.5 which has the same System.Text.Json as yours.
Testing the relevant code was successful:
VB.NET:
Dim Json_Data = "{""num_sensors"":2,""addresses"":[14915921967490352962,16285016252837298754],""data"":[23.4375,24.528]}"
Dim d As Temperature_Data = System.Text.Json.JsonSerializer.Deserialize(Of Temperature_Data)(Json_Data)
The System.Runtime.CompilerServices.Unsafe.dll that was loaded was assembly version 6.0.0.0 (fileversion 6.0.21.52210). If you have a different file in your output folder maybe there is installed a package with a conflicting version? Although I don't understand why the exception says it wants 4.0.4.1.
 
Solution
Hi John,

Thank you so much for validating the code.
The code works for me now. It has to copy all the files that are generated in Debug folder when I move the executable file to another folder.

Thank you so much.
 
It has to copy all the files that are generated in Debug folder when I move the executable file to another folder.
As the name suggests, the Debug folder is for debugging. If you want to deploy your app then create a Release build and copy the files from the Release folder, unless you specifically want to deploy a Debug build in order to track down an issue.
 
Back
Top