JsonConvert.SerializeObject在windows Xp sp3中崩溃

本文关键字:sp3 崩溃 Xp windows SerializeObject JsonConvert | 更新日期: 2023-09-27 18:26:50

我正在使用Newtonsoft json工具进行序列化和反序列化。之前我得到了循环引用json错误,以解决我使用的问题

JsonConvert.SerializeObject(NodeConfigurator.Instance, Formatting.None, new JsonSerializerSettings()
{
    ReferenceLoopHandling = ReferenceLoopHandling.Ignore
}); 

它在windows 7及更高版本中运行良好,但在windows xp sp3中,它给出了"Newtonsoft.Json.dll中发生类型为System.NotSupportedException的未处理异常"。异常消息为"'CommonConverter'无法将'abc.RelayCommand'转换为'System.String'"。InnerException为null。

堆栈跟踪消息是

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;
        }
    }
}

这是属性。。。

JsonConvert.SerializeObject在windows Xp sp3中崩溃

我认为您应该修改您的类,即序列化并向导致循环引用的成员添加[JsonIgnore]属性。

但当对象被反序列化时,您将需要将这个引用分配回。

有关[JsonIgnore]的更多信息,请参阅官方文档:http://www.newtonsoft.com/json/help/html/t_newtonsoft_json_jsonignoreattribute.htm