C# JsonConvert.SerializeObject在windows Xp sp3中崩溃
我正在使用Newtonsoft json工具进行序列化和反序列化C# JsonConvert.SerializeObject在windows Xp sp3中崩溃,c#,json,c#-4.0,json.net,C#,Json,C# 4.0,Json.net,我正在使用Newtonsoft json工具进行序列化和反序列化 JsonConvert.SerializeObject(NodeConfigurator.Instance, Formatting.None, new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore }); 它在windows 7及以上版本中运行良好,但在windows xp sp3中,它给出了“Newton
JsonConvert.SerializeObject(NodeConfigurator.Instance, Formatting.None, new JsonSerializerSettings()
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
它在windows 7及以上版本中运行良好,但在windows xp sp3中,它给出了“Newtonsoft.Json.dll中发生的类型为'System.NotSupportedException'的未处理异常”。异常消息是“'CommandConverter'无法将'abc.RelayCommand'转换为'System.String'”。InnerException为空
stacktrace消息是
at System.ComponentModel.TypeConverter.GetConvertToException(Object value, Type destinationType)
at System.Windows.Input.CommandConverter.ConvertTo(ITypeDescriptorContext context, CultureInfo culture, Object value, Type destinationType)
at System.ComponentModel.TypeConverter.ConvertToString(ITypeDescriptorContext context, CultureInfo culture, Object value)
at System.ComponentModel.TypeConverter.ConvertToInvariantString(Object value)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.TryConvertToString(Object value, Type type, String& s)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeString(JsonWriter writer, Object value, JsonStringContract contract)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)
at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)
at Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter, Object value, Type objectType)
at Newtonsoft.Json.JsonConvert.SerializeObjectInternal(Object value, Type type, JsonSerializer jsonSerializer)
at Newtonsoft.Json.JsonConvert.SerializeObject(Object value, Type type, Formatting formatting, JsonSerializerSettings settings)
at Newtonsoft.Json.JsonConvert.SerializeObject(Object value, Formatting formatting, JsonSerializerSettings settings)
at uOPCDA.ViewModel.ShellVM.SaveToXmlFile(Nullable`1 result) in
我用简单的对象进行了测试,它在xp中也运行良好。但如果结构包含循环引用,则给出上述例外。帮我解决这个问题
在Windows7中,我的Json输出如下所示
{
"LastSavedPath":"C:\Program Files (x86)\abc\uOPC Server\Lastclosed.uopf",
"Channels":[
{
"NewDeviceCommand":{
},
"DeleteChannelCommand":{
},
"ChannelDoubleClickCommand":{
},
"Device":[
{
"NewTagCommand":{
},
"DeleteDevice":{
},
"IsNewTagCommandVisible":true,
"IsDeleteDeviceVisible":true,
"IsNewDeviceCommandVisible":true,
"Tags":[
{
"IsEnableApplyBtn":false,
"TagName":"Tag1",
"TagAddress":"102364",
"TagNameimg":null,
"TagRightImg":null,
"TagDescription":"New",
"DataType":"Bool",
"ClientAccess":"Read Only",
"ScanRate":0,
"NewTag":null,
"NewGrpTag":null,
"DeleteCurGrpTag":null,
"MultiTagGen":null,
"ScalingNone":true,
"ScalingLinear":false,
"ScalingSquareRoot":false,
"RVRHigh":0,
"RVRLow":0,
"SVRData":"Double",
"SVRHigh":0,
"SVRHighClamp":false,
"SVRLow":0,
"SVRLowClamp":true,
"SVRUnits":null,
"NegateScalVal":true,
"Location":"DiscreteInputs",
"Address":"02364",
"Length":1
},
{
"IsEnableApplyBtn":false,
"TagName":"Tag2",
"TagAddress":"102365",
"TagNameimg":null,
"TagRightImg":null,
"TagDescription":"dfdfdf",
"DataType":"Bool",
"ClientAccess":"Read Only",
"ScanRate":0,
"NewTag":null,
"NewGrpTag":null,
"DeleteCurGrpTag":null,
"MultiTagGen":null,
"ScalingNone":true,
"ScalingLinear":false,
"ScalingSquareRoot":false,
"RVRHigh":0,
"RVRLow":0,
"SVRData":"Double",
"SVRHigh":0,
"SVRHighClamp":false,
"SVRLow":0,
"SVRLowClamp":true,
"SVRUnits":null,
"NegateScalVal":true,
"Location":"DiscreteInputs",
"Address":"02365",
"Length":1
}
],
"DeviceModel":"Modbus",
"DeviceName":"Device1",
"AsciiDeviceID":"5",
"AsciiDeviceIDType":"Decimal",
"EtherNetDeviceID":null,
"ScanMode":"",
"ConnectionTimeout":3,
"ScanRate":0,
"IsAddedFirstDevice":false,
"RequestTimeout":1000,
"FailAfterSuccTimeout":3,
"InterReqDelay":0,
"EnableAutoDeviceDemotion":false,
"DemotAftSuccFailure":0,
"DemotForMS":0,
"DiscardWriteReq":true,
"StartUp":"Do not generate on startup",
"Action":"Do not overwrite, log error",
"PortNumber":"502",
"IPProtocol":"",
"AddtoGroup":0,
"AllowAutoGenSubGrp":true,
"UseZeroBasAdd":true,
"UseZeroBitAddReg":false,
"UseHoldRegBitMask":false,
"UseModFun06RegWrite":false,
"SingleRegisterWrites":"0",
"UseModFun05CoilWrite":false,
"singleCoilWrites":"0",
"ClientPrivilege":"Read/Write",
"ModbusDeviceBaseAddress":"1",
"DefaultModByteOrd":true,
"FirstWrdLowin32BitDT":true,
"firstWordLowIn32Bit":"1",
"ByteOrder":"Big-endian",
"FirstDWrdLowin64bitDT":false,
"firstDwordLowIn64Bit":"0",
"ModiconbitOrd":true,
"LongasDblePrsUD":false,
"CoilOutput":32,
"CoilInput":32,
"RegInternal":32,
"RegHolding":32,
"PerBlockRead":false,
"CloseTCPSocTimeout":false,
"VariableImport":null,
"VariableImportBTN":null,
"IncludeDescription":true,
"DeactivateTagIllAddrExe":true,
"RejectRepMsg":false,
"UseModTCPFrame":true,
"LeadingByte":0,
"TrailingByte":0,
"ImportFile":null,
"ImportFileBTN":null,
"SelectObjs":null,
"FilterOptProp":false,
"CreateTagasRWAllowed":false,
"USeObjNameTagGrpName":false,
"BitsperRegister":"16",
"IsModBusTcpIpSelected":false,
"IsScanModeTextBoxEnable":true,
"IsRTUSelected":true,
"IsSelected":true
}
],
"PageVisite":false,
"ChannelName":"Channel1",
"DeviceDriver":"Modbus ASCII Serial",
"EnableDiagnostic":true,
"VirtualNetwork":"None",
"TransactionsPerCycle":0,
"NetworkMode":"Load Balanced",
"PhysicalMedium":"COM Port",
"ReadProcessing":"Ignore",
"ComID":"6",
"BaudRate":"9600",
"Databit":"7",
"Parity":"Even",
"Stopbit":"1",
"FlowControl":"None",
"NetworkAdapter":"Default",
"ReportConnErr":true,
"reportConnectionErrors":"1",
"CloseConnWnIdle":true,
"closeConnectionWhenIdle":"1",
"SecofIdleTime":0,
"WriteAllTags":false,
"LatesValNonboolTags":false,
"LatesvalAllTags":true,
"DutyCycle":0,
"NonNormalizedValue":"",
"SingleSocket":true,
"MoreSocket":false,
"MaxSocketsperDevice":0,
"PortNumber":"",
"IPProtocol":"",
"SerializePortsetting":true,
"OperationCommunications":false,
"EtherNetSettings":false,
"IsTagListViewVisible":true,
"IsChannelListViewVisible":true,
"ChannelSelectedItem":null,
"IsChannelSelected":false
}
]
}
我的班级如下
公共类频道:BaseViewModel
{
RelayCommand _newDevice;
public RelayCommand NewDeviceCommand
{
get
{
if (_newDevice == null)
_newDevice = new RelayCommand(value => this.NewDevice(value as Channel));
return _newDevice;
}
}
}
这里是属性…我认为您应该修改您的类,将其序列化并向导致循环引用的成员添加
[JsonIgnore]
属性
但当对象被反序列化时,您需要重新分配此引用
有关
[JsonIgnore]
的更多信息,请参见官方文档:消息是“CommandConverter”无法将“abc.RelayCommand”转换为“System.String”。Innerexception为空。此旧计算机上安装了什么版本的Json.NET?要了解这一点,您可以执行Console.WriteLine(“Json.NET版本:”+typeof(JsonSerializer.Assembly.FullName)代码>。我已经执行了&它是Newtonsoft.Json,版本=7.0.0.0,区域性=neutral,PublicKeyToken=null。在“修复-修复了使用有效类型转换器反序列化接口的错误”中修复了一个错误。因此,如果您在XP机器上使用的版本比该版本旧,则可能导致崩溃。@dbc我已通过将JsonIgnore放入relaycommand中修复了此问题。现在它没有崩溃。我也尝试过此操作,但仍然会遇到相同的异常。可能这不是循环引用问题。您是否检查了两个操作系统上的库版本?你能输出你在W7上收到的序列化字符串吗?我已经在windows 7Ya中发布了输出,我已经检查过了。我已经使用.net 4.0开发了应用程序,我已经添加了相应的dll 7.0.0.0版本&运行时版本是v4.0.30319