草庐IT

SERIALIZATION

全部标签

c# - 序列化属性,但不要反序列化 Json.Net 中的属性

虽然我发现了很多方法来反序列化特定属性同时防止它们序列化,但我正在寻找相反的行为。我发现很多问题都在问相反的问题:Makingapropertydeserializebutnotserializewithjson.netCanIinstructJson.NETtodeserialize,butnotserialize,specificproperties?JSON.Net-UseJsonIgnoreAttributeonlyonserialization(Butnotwhendeserialzing)我怎样才能序列化一个特定的属性,同时防止它反序列化回POCO?有没有我可以用来装饰特定

c# - 不可打印字符的序列化

以下代码;varc=(char)1;varserializer=newXmlSerializer(typeof(string));varwriter=newStringWriter();serializer.Serialize(writer,c.ToString());varserialized=writer.ToString();vardc=serializer.Deserialize(newStringReader(serialized));在.NET4中抛出此异常。InvalidOperationException-ThereisanerrorinXMLdocument(2,12

c# - 如何对 Object 类型的属性使用 ShouldSerialize[MemberName]() 方法?

我尝试使用Newtonsoft.Json中的ShouldSerialize方法来阻止没有为其属性分配新值的类型对象的属性。但我不知道如何实现它,所以请帮我解决这个问题......这是示例代码publicclassSample1{publicStringname{get;set;}publicintId{get;set;};}这是我的类,包含上述类作为其属性之一publicclassContainer{publicStringCname{get;set;}publicSample1Sample{get;set;};publicboolShouldSerializeSample(){//W

C# - Web API - 将枚举序列化为带空格的字符串

我的问题很简单,但比其他有关将枚举类型序列化为字符串的问题更具体一些。考虑以下代码:usingNewtonsoft.Json;usingNewtonsoft.Json.Converters;publicenumMyEnum{TypeOne,TypeTwo,TypeThree}publicclassFoo{[JsonConverter(typeof(StringEnumConverter))]publicMyEnumTypes{get;set;}}当WebAPIController发送序列化的Foo对象时,它们可能看起来像这样:{"Type":"TypeTwo"}我的问题:是否可以将序列

c# - ASP.net Web API : change class name when serializing

我有一个产品的数据传输对象类publicclassProductDTO{publicGuidId{get;set;}publicstringName{get;set;}//Otherproperties}当Asp.net序列化JSON(使用JSON.NET)或XML中的对象时,它会生成ProductDTO对象。但是,我想在序列化期间更改名称,从ProductDTO到Product,使用某种属性:[Name("Product")]publicclassProductDTO{[Name("ProductId")]publicGuidId{get;set;}publicstringName{

c# - Newtonsoft.JSON 无法转换具有 TypeConverter 属性的模型

我有一个C#MVC应用程序,它将数据作为JSON字符串存储在XML文档以及MySQL数据库表中。最近我收到了在MySQL数据库字段中存储JSON字符串的需求,通过Newtonsoft.Json转换成C#对象,所以我决定实现一个TypeConverter将JSON字符串转换为自定义C#模型。不幸的是,当TypeConverter属性添加到我的C#模型时,我无法在我的解决方案中的任何地方使用以下命令来反序列化我的JSON字符串:JsonConvert.DeserializeObject(json);删除属性可以解决问题,但这会阻止我将MySQL数据库字段转换为自定义C#对象。这是我添加了T

c# - 更改属性名称以进行序列化

我的类有一个属性“Property”,我希望它在序列化时在JSON对象中显示为“Property”。有什么我可以使用的属性吗? 最佳答案 对于Json.NET和DataContractJsonSerializer使用DataMemberAttribute:[DataMember(Name="PropertyB")]TPropertyA{...}确保您的类也装饰有[DataContract]属性。如果您使用的是JavaScriptSerializer,则需要创建派生实现,如下所述:JavaScriptSerializer.Deseri

c# - Json Serialize Dictionary<Enum, Int32> 的问题

每当我尝试序列化字典时,我都会得到异常:System.ArgumentException:Type'System.Collections.Generic.Dictionary`2[[Foo.DictionarySerializationTest+TestEnum,Foo,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null],[System.Int32,mscorlib,Version=2.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089]]'isnotsupportedfors

c# - 在不实现任何自定义序列化/反序列化时是否需要实现 ISerializable 接口(interface)

我正在查看实现ISerializable接口(interface)的解决方案中的类。它有一个GetObjectData方法,用于根据接口(interface)的要求进行序列化。这里没有发生任何自定义序列化,它只是用类属性的名称及其值填充SerializationInfo对象。[Serializable]publicclassPersonName:ISerializable{[DataMember]publicstringNamePrefix{get;set;}[DataMember]publicstringGivenName{get;set;}[DataMember]publicstr

c# - 如何在 C# 中使用继承类实现 Xml 序列化

我有两个类:基类名称Component和继承类名称DBComponent[Serializable]publicclassComponent{privatestringname=string.Empty;privatestringdescription=string.Empty;}[Serializable]publicclassDBComponent:Component{privateListspFiles=newList();//StorageProcedureFiles[XmlArrayItem("SPFile",typeof(string))][XmlArray("SPFile