Set Json Attribute on existing class - c#

I have an existing class in an external assembly which I can't change.
I would like to serialize an object from this class with Newtonsoft JSON.Net, but not all the properties.
Normally I can do this with the JsonIgnoreAttribute attribute like this:
public class TestJsonClass
{
public string PropA { get; set; }
[JsonIgnoreAttribute]
public string PropB { get; set; }
}
But since I can't change the class, is there a way to ignore a property without attributes?

Try inherit class and override property with appropriate annotations or copy property values in a completely new class.

Related

System.Json - custom rules for property serialization skipping

I am trying to migrate from Newtonsoft.Json to System.Text.Json
However, I ran into a problem since I was using DefaultContractResolver.
My "custom" behaviour have these rules for property serialization:
Skip property serialization if it is marked with ReadOnly attribute
Skip property serialization in case of null (this is supported)
Skip property serialization which would serialize into an empty object
Example:
class Car
{
[ReadOnly]
public string Id { get; set; }
public string Name { get; set; }
public Person Owner { get; set; }
}
class Person
{
[ReadOnly]
public string Id { get; set; }
public string Name { get; set; }
}
Now, imagine, we have this data if no rules would apply.
{
"Id":"1234",
"Name":"Skoda",
"Owner":{
"Id":"abcd",
"Name":null
}
}
Now, if I serialize the object, I would like to get this instead.
{
"Name":"Skoda"
}
In order to ignore individual properties, you need to use the [JsonIgnore] attribute along with one of these conditions:
Always;
Never;
WhenWritingDefault;
WhenWritingNull.
You can also define a default ignore condition through the JsonSerializerOptions type.
If additional behavior is needed, you should write a custom converter.
Example:
class Person
{
[JsonIgnore(Condition = JsonIgnoreCondition.Always)]
public string Id { get; set; }
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public string Name { get; set; }
}
More information:
How to ignore properties with System.Text.Json
How to write custom converters for JSON serialization (marshalling) in .NET

DataContractSerializer using default properties

Recently when I read the default behavior of DataContractSerializer, I get the rules from MSDN, however I do not understand the first rule which I extracted as below:
The DataContractSerializer infers a data contract from types without attributes using the default properties of the newly created types.
How do I interpret this statement, if some one has clear idea, could you help, I know that "without attributes", the attribute means DataContract attribute, however what does that "default properties" refer to. Is there something called "default properties" in a custom type?
If you a have type referenced within another class that has [DataContract] attribute, then DataContractSerializer will serialize the referenced type even if it is not attributed with [DataContract]. Serialization will happen on all public properties, unless the property is attributed with [IgnoreDataMember].
For example:
[DataContract]
public class ClassA
{
public ClassB MyData { get; set; }
public string SomeString { get; set; }
public int SomeNumber { get; set; }
}
public class ClassB
{
public string SomeOtherInfo { get; set; }
public int SomeOtherNumber { get; set; }
}
In the above code, ClassB will be serialized based on its default properties, which in this case are all the public properties: "SomeOtherInfo" and "SomeOtherNumber".

OOD in C# - Determine Which Properties are Inherited vs which are Declared in Current Class

Consider the following example:
public class BaseClass
{
public string StringInBaseClass {get;set;}
public int IntInBaseClass {get;set;}
}
public InheritingClass : BaseClass
{
public long LongInInheritingClass {get;set;}
public long ShortInInheritingClass {get;set;}
public long CharInInheritingClass {get;set;}
}
Now, what I want to do is serialize JUST the inheriting properties to a JSON string. For example, I want to somehow create a JSON object out of just the 3 properties in InheritingClass, but if I do:
InheritingClass a = new InheritingClass();
JavaScriptSerializer jss = new JavaScriptSerializer();
string jsonString = jss.Serialize(a);
The jsonString value will have all of the properties of the BaseClass as well as all of the properties of InheritingClass. I understand that is normal, because I am inheriting all of those properties. What I am looking to do is NOT include those inherited properties and build a JSON string out of ONLY the 3 properties in InheritingClass.
Is this possible?
Use the ScriptIgnoreAttribute on any property you don't want serialized with the JavaScriptSerializer. With this you can do the following in your inherited class to stop a property from the base class from being serialized:
public class BaseClass
{
public virtual string StringInBaseClass {get;set;}
public int IntInBaseClass {get;set;}
}
public class InheritingClass : BaseClass
{
[ScriptIgnore]
public override string StringInBaseClass
{
get
{
return base.StringInBaseClass;
}
set
{
base.StringInBaseClass = value;
}
}
public long LongInInheritingClass {get;set;}
public long ShortInInheritingClass {get;set;}
public long CharInInheritingClass {get;set;}
}
If you never want to serialize those properties (even when serializing the base class or other derived classes) you can add the ScriptIgnore attribute to the base class:
public class BaseClass
{
[ScriptIgnore]
public string StringInBaseClass {get;set;}
[ScriptIgnore]
public int IntInBaseClass {get;set;}
}
This code return properties(type of PropertyInfo[]) that inherited members are not considered:
typeof(InheritingClass).GetProperties(BindingFlags.DeclaredOnly)
Yuri is correct in how to get json to ignore the property.
However, there is no way to implicity ignore the properties of the base class, the inheriting class is the base class, that's how inheritance works.

How to deserialize XML attributes

I can create an object to hold a deserialized xml file. Mapping Xml elements to objects is easy, i just create properties in the in the class matching the name of the element. But how can i map Xml attributes to the class. For example, if i have this:
<Typestyle name="" location="" />
I want to deserialize the name and location attributes into properties on my class?
why not use the xsd.exe tool in the .NET framework SDK to create C# class code representing the schema. Then add those classes to your project and you can use XmlSerializer with those classes without needing to write the class code yourself.
Try this http://msdn.microsoft.com/en-us/library/x6c1kb0s.aspx
Look at XmlAttributeAttribute class.
public class TypeStyle
{
[XmlAttribute("name")]
public string Name { get; set; }
[XmlAttribute("location")]
public string Location{ get; set; }
}
public class Typestyle
{
[XmlAttribute]
public string name { get; set; }
[XmlAttribute]
public string location { get; set; }
}

Hide public Property when exposed through web service

I would like to prevent a property from being exposed via my WCF web service. I tried adding the XmlIgnore attribute bug that didn't work. We are using .NET 3.5. WCF.
This doesn't work:
public class MyClass
{
public string S1 { get; set; }
[XmlIgnore]
public string S2NotExposed { get; set; }
}
Mark your class with the [DataContract] attribute from the DataContractAttribute Class , then mark only the properties you want to expose with the [DataMember] attribute from the DataMemberAttribute Class.

Categories