简体   繁体   English

JsonConvert.SerializeObject在Windows XP SP3中崩溃

[英]JsonConvert.SerializeObject is crashing in windows Xp sp3

I am using Newtonsoft json tool to serialize and de serialize.Previously i got circular reference json error, to resolve that i have used 我正在使用Newtonsoft json工具进行序列化和反序列化。以前我遇到了循环引用json错误,以解决我使用过的问题

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

and it is working good in windows 7 and above , but in windows xp sp3 it is giving " An unhandled exception of type 'System.NotSupportedException' occurred in Newtonsoft.Json.dll ". 并且它在Windows 7及更高版本中运行良好,但是在Windows XP SP3中,它给出了“ Newtonsoft.Json.dll中发生了'System.NotSupportedException'类型的未处理异常 ”。 The exception message is "'CommandConverter' is unable to convert 'abc.RelayCommand' to 'System.String'" . 异常消息是“'CommandConverter'无法将'abc.RelayCommand'转换为'System.String'” The InnerException is null. InnerException为null。

The stacktrace message is 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 

I have tested with simple object and it is working good in xp also. 我已经测试了简单的对象,并且在xp中也能正常工作。 But if the structure contains cyclic reference it is giving the above exception. 但是,如果结构包含循环引用,则会给出上述异常。 Help me to solve this problem. 帮我解决这个问题。

My Json output is like below in Windows 7. 我的Json输出类似于Windows 7中的以下内容。

{
   "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
      }
   ]
}

My class is like below 我的课如下

public class Channel : BaseViewModel 公共类Channel:BaseViewModel

 {

     RelayCommand _newDevice;

    public RelayCommand NewDeviceCommand
    {
        get
        {

            if (_newDevice == null)
                _newDevice = new RelayCommand(value => this.NewDevice(value as Channel));
            return _newDevice;
        }

    }
}

Here is properties... 这是属性...

I think you should amend your class, that you serializing and add [JsonIgnore] attribute to a member that leads to cyclic reference. 我认为您应该修改您的类,进行序列化并将[JsonIgnore]属性添加到导致循环引用的成员。

But you will need to assign this reference back when object is deserialized. 但是当对象反序列化时,您将需要重新分配该引用。

More info on [JsonIgnore] can be found in the official documentation: http://www.newtonsoft.com/json/help/html/t_newtonsoft_json_jsonignoreattribute.htm 可以在官方文档中找到有关[JsonIgnore]更多信息: http : //www.newtonsoft.com/json/help/html/t_newtonsoft_json_jsonignoreattribute.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM