Deserialize a property, but do not serialize back again C# Asp.net - c#

I have an xml that contains certain information that was serialized in a certain version. Now in next version, I want that information to be deserialized, but dont serialize it back in the same property/element. That information will be handled in a child class and that will serialize it. I know I can use XmlIgnore but that will ignore both Serialization and deserialization.
P.S: I could make the string attributes null, but what about the doubles, int?

Related

Ignore a property from being serialized when trying to serialize an object to Json using JIL (like [JsonIgnore] in newtonsoft)

I've recently changed my Json serialization/deserialization logic from Newtonsoft to JIL,it(Jil) works with better performance than newtonsoft (as expected) but I have one major problem with it
My model objects are usually big objects with lots of properties including their own props and their parent props, so I can't pass the whole object to the client on every request, I usually exclude some of their properties (unnecessary ones).
In newtonsoft I easily used JsonIgnore attribute and my need was satisfied but as I googled a bit I haven't found it's (JsonIgnore) equivalent attribute in JIL.
Does this attribute exists in JIL or I should write it myself??
if the answer is no how can I write an attribute that Ignores property from being serialized with JIL because I have no access to JIL JSON.Serialize mechanism
any help would be hugely appreciated
I found the answer myself I should use JilDirective attribute with first property of false see below
[JilDirective(true)]

How to deserialize JSON into a List<KeyValuePair<string,string>> set

I have some JSON data :-
{
"mail":"mitch#domain.com",
"givenName":"User",
"sn":"Name",
"uid":"mitch",
"gecos":"User Name"
}
What I'm trying to do is de-serialize this into a List<KeyValuePair<string,string>>
I would normally do a dictionary, however some key's may be duplicated - this is the representation that is automatically generated by .NET when I pass a List<KeyValuePair<string,string>> object into the System.Web.Script.Serialization.JavaScriptSerializer class.
When I just plug the serialized object into the System.Web.Script.Serialization.JavaScriptDeserializer I get a empty response back.
From what I can see it should not be possible using the JavaScriptSerializer. The only way for customizing its behavior is by means of a JavaScriptConverter class, that will allow you to customize the serialization/deserialization process. Unfortunately both methods will pass an IDictionary for the properties, therefore the duplicated names are already merged. You might want to look into either a different format for your JSON or a different serialization library such as JSON.net which is way more customizable.

XmlSerializer.Deserialize ignore unnecessary elements

I have used xsd.exe to generate .cs file from my XML response.
In my XML response there are few elements which are not required in app, I want to omit those, so I removed them from response xml file and regenerated the .cs file.
I use this .cs file to deserialize from my xml server response (which has those unnecessary elements), which causes System.InvalidOperationException.
Please let me know how can I parse or deserialize the server response to the class which has only those properties which are required.
I wok for Windows Phone app.
Thanks,
Kavit.
Try decorating the properties you no longer want with the XmlIgnoreAttribute attribute.
By default, all public fields and public read/write properties are
serialized by the XmlSerializer. That is, the value of each public
field or property is persisted as an XML element or XML attribute in
an XML-document instance.
To override the default serialization of a field or property, create
an XmlAttributes object, and set its XmlIgnore property to true. Add
the object to an XmlAttributeOverrides object and specify the type of
the object that contains the field or property to ignore, and the name
of the field or property to ignore.
http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlattributes.xmlignore.aspx

How to rename and remap fields with illegal json field names

I have used json2csharp to produce some nice c# class into which I can deserialize json.
It is actually working as expected EXCEPT that json2csharp named some of the fields invalid_name . I renamed those to valid csharp names but when serialized those class are null.
I found this other SO post... where one of the answerers said the following...
Keep in mind the class I have pasted below will not work directly,
because of the naming of some of the fields in the json. You may have
to rename them manually and map them.
This exactly describes my problem. Unfortunately, the answer gives no clues on actually HOW to "map them." So can someone tell me how to manually map some json arrays to c# classes.
I am using RestSharp deserializers, btw.
Any ideas?
If you are using JSON.NET as the basis or JSON parsing, you can rename your properties and then decorate them with attributes to align them back to the original JSON object.
An example is a property in JSON called 1 which is invalid in C#. This can be corrected using the following:
[JsonProperty("1")]
public int? One { get; set; }
There is also a JsonObject attribute if you want to edit at the class level, and also a JsonIgnore object to ignore serialisation of properties.

Is there an XML specific object (like XElement) that is binary serializable?

I have a use case where I am serializing objects over the wire via MSMQ (mostly strings). When I read the object off the queue I want to be able to tell if the user meant for the object to be a XML or a string. I was thinking a good way to do this would just be to check the type. If it's XmlElement than it becomes XML data otherwise it becomes string or CDATA. The reason I don't want to just check to see if the data is valid XML is that sometimes data will be provided that is supposed to be serialized as a string but is in fact valid XML. I want the caller to be able to control the de-serialization into string or XML.
Are there any types that are marked as serializable in the .NET Framework like XElement or XmlElement (both which are not marked serializable)?
Why don't you just add a property to the class of the serialized object that tells you what it is? I'd propose IsXml.

Categories