Set requried field while Deserializing using Json.Net - c#

I have a class Foo as follows
public class Foo
{
public ClassA A {get;set;}
public string B {get;set;}
}
public class ClassA
{
public string C {get;set;}
}
When I get a Json string (say fooJson), I want to deserialize it to a Foo object with following conditions
The object must have the property Foo.A
Foo.B is optional
Foo.A.C is optional
I tried using MissingMemberHandling = MissingMemberHandling.Error as a part of my JsonSerializerSettings. but that throws error even when Foo.B is missing.

If you want some properties to be optional and some required, the easiest way to achieve this is to mark up your classes with [JsonProperty] attributes indicating which properties are required, e.g.:
public class Foo
{
[JsonProperty(Required = Required.Always)]
public ClassA A { get; set; }
public string B { get; set; }
}
public class ClassA
{
public string C { get; set; }
}

Related

Required Property and JSON PropertyName

I want to create a class with an required attribute and a mapped JSON name like the following:
class MyClass {
[Required]
public string Foo {get; set;}
}
This works all fine. But combining this with an JSON annotation like the following, breaks the validation
class MyClass {
[Required]
[JsonProperty(PropertyName = "bar")]
public string Foo {get; set;}
}
Why does the behavior change here and how can I fix that?
Try this:
class MyClass
{
[JsonProperty(PropertyName = "bar", Required = Required.Always)]
public string Foo { get; set; }
}

Property overriden with "new" gets dupped in WCF Webservice

I have a problem with a Webservice I'm creating, the thing is, I have a class, name it ClassA with PropertyA, he na ClassB inheriting ClassA that overrides PropertyA using "new" like this:
[DataContract]
public class ClassA
{
[DataMember]
public string PropertyA { get; set; }
[DataMember]
public string PropertyB { get; set; }
}
[DataContract]
public class ClassB : ClassA
{
[DataMember]
public new int PropertyA { get; set; }
}
This should make ClassB "replace" the string PropertyA from ClassA for a int PropertyA in ClassB.
I expose ClassB in a service method like this:
public bool TellMeSomething(ClassB param) { .... whatever .... }
When I add the service reference in the client application, the classes imported show something like this:
public class ClassA
{
public string PropertyA { get; set; }
public string PropertyB { get; set; }
}
public class ClassB : ClassA
{
public int PropertyA1 { get; set; }
}
As you can notice, ClassB has no "new int PropertyA" anymore, but a "int PropertyA1". I guess the serializer finds the ancestor has a PropertyA already and doesn't know how to handle the "new" keyword to hide the "string PropertyA", so it creates a new name for the PropertyA in the inherited class...
Is there any way to avoid this behavior? I would need that property to be called like it should...
You should use generics instead of identifier re-using:
[DataContract]
public class ClassA<T>
{
[DataMember]
public T PropertyA { get; set; }
[DataMember]
public int PropertyB { get; set; }
}
// No need to override PropertyA since T is int and ClassB
// inherits both PropertyA and PropertyB...
[DataContract]
public class ClassB : ClassA<int>
{
}

How to define behave during serialization an object to attribute in XML

I have some problem with serialization an object to XML. At the beggining, this is class Order which have some properties of types like string, int...
public class Order
{
[XmlAttribute("ObjectType")]
public string TypeName
{
get; set;
}
[XmlAttribute("ID")]
public string ID
{
get; set;
}
[XmlAttribute("TID")]
public string TID
{
get; set;
}
[XmlAttribute("Command")]
public Command Command
{
get; set;
}
public Order()
{
}
}
As you can see, this class has also one property of type Command. Command class has one string property called Name:
public class Command
{
[XmlAttribute("Name")]
public string Name
{
get; set;
}
}
What's the problem? I need to get result like this:
<Order ObjectType="TestCase" ID="2" TID="W404" Command="SomeCommand" />
Where 'SomeCommand' is value of 'Name' property of this Command object.
With simple words: i need to define behave of how Command object should be serialized to XML attribute - return Name string. Very important thing is that also I have to be able to deserialize this XML to objects structure.
Hope my post is understandable and my problem is explained clearly. ;)
Best regards!
Assuming you have some way to reconstruct the command from the command name, you can introduce a derived property CommandName to serialize the name, and use xml serialization attributes to make XmlSerializer output the desired properties as attributes with the desired names:
public class Order
{
[XmlAttribute("ObjectType")]
public string TypeName
{
get;
set;
}
[XmlAttribute("ID")]
public string ID
{
get;
set;
}
[XmlAttribute("TID")]
public string TID
{
get;
set;
}
[XmlIgnore]
public Command Command
{
get;
set;
}
[XmlAttribute("Command")]
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
[Browsable(false)]
public string CommandName
{
get
{
return Command == null ? null : Command.Name;
}
set
{
// Logic to convert CommandName to Command, e.g.:
Command = Command.FindByName(value); // or whatever.
}
}
public Order()
{
}
}
The CommandName property must be public in order for this to work.

DataContract and inheritance?

How to use DataContract with inheritance? Will code below work?
[DataContract]
public class ConsoleData
{
[DataMember]
public String Description { get; set; }
}
[DataContract]
public class SomeData : ConsoleData
{
[DataMember]
public int Volume { get; set; }
......
Yes, that would work.
The DataContractAttribute has Inherited set to false, so it is necessary to apply the attribute to both the child class and the parent class (as you have done in the question).
You would need to use the KnownType attribute if you want to use your data contracts with polymorphism.
For example
[ServiceContract]
interface MyWcfContract
{
[OperationContract]
HandleData(ConsoleData contractData);
}
If you invoked the method like so:
SomeData someData = new SomeData { Description = "Test", Volume = 30 };
// The method is expecting a ConsoleData instance,
// I'm passing a SomeData instance instead
myWcfProxy.HandleData(someData);
Then the deserializer on the service end will not know that it's an instance of SomeData, just an instance of ConsoleData which it was expecting.
The way to fix this is to register the SomeData class as a known type of the ConsoleData.
[DataContract]
[KnownType(typeof(SomeData))]
public class ConsoleData
{
[DataMember]
public String Description { get; set; }
}
[DataContract]
public class SomeData : ConsoleData
{
[DataMember]
public int Volume { get; set; }
......
You'll need to use the KnownType attribute if you are using the XmlSerializerFormat for your ServiceContract:
[DataContract]
public class ConsoleData
{
[DataMember]
public String Description { get; set; }
}
[DataContract, KnownType(typeof(ConsoleData))]
public class SomeData : ConsoleData
{
[DataMember]
public int Volume { get; set; }
}

C# serialization dropping property in subclass?

When I try to serialize a populated instance of type List<C>() where:
public class A<T> : List<T>
{
[XmlAttribute("Name")]
public string Name {get; set;}
public A() {}
}
public class B
{
[XmlAttribute("Other")]
public string OtherPCO { get; set:}
}
public class C : A<B>
{
}
The serialization drops the Name property of class A but does create an array of type B with the OtherPCO property. How can I get the serializer to include Name?
Collections are serialized in a specific manner, which takes into account only the items of the collection, not the extra properties you added to the class. You need to wrap the collection in another class that is not a collection.
This should give you the desired result :
public class A<T>
{
[XmlAttribute("Name")]
public string Name {get; set;}
[XmlElement(typeof(T))]
public List<T> Items { get; set; }
}

Categories

Resources