tishma
tishma

Reputation: 1885

Is it possible to serialize different property sets using DataContractJsonSerializer?

I'm building a (hopefully) RESTful service using WCF and JSON.

For example:

[DataContract]
public class Person
{
    // This member is serialized.
    [DataMember]
    internal string FullName;

    // This is serialized even though it is private.
    [DataMember]
    private int Age;

    // This is not serialized because the DataMemberAttribute 
    // has not been applied.
    private string MailingAddress;

    // This is not serialized, but the property is.
    private string telephoneNumberValue;

    [DataMember]
    public string TelephoneNumber
    {
        get { return telephoneNumberValue; }
        set { telephoneNumberValue = value; }
    }
}

I need to return a Person object without telephone number (i.e. w/ FullName and Age only) from a WCF method, so I was wondering if it's somehow possible to do it using only attribute properties of Person class and WFC method?

Thanks

v.

Upvotes: 0

Views: 315

Answers (2)

Vitaly
Vitaly

Reputation: 2125

Alternately, you could use [DataMember( EmitDefaultValue=false)] and set fields you don't want to serialize to null or not initialize at all

E.g.

[DataContract]
public class Person
{
    // This member is serialized.
    [DataMember]
    internal string FullName;

    [DataMember( EmitDefaultValue=false)]
    public string TelephoneNumber
    {
        get { return telephoneNumberValue; }
        set { telephoneNumberValue = value; }
    }
}

Then you may have 2 methods that load\create Person in 2 different ways

public Person LoadWithPhone() 
    {
        return new Person() { FullName = "Name", TelephoneNumber = "123456" };
    }

 public Person LoadWithoutPhone() 
    {
        return new Person() { FullName = "Name" }; //TelephoneNumber is null
    }

Then when serialize person created by second method, TelephoneNumber will not be serialized This might be useful when load object from DB, you can have several methods that load different columns or have one that is constructing query on the fly and initialized object only with requested fields.

Upvotes: 2

sanpaco
sanpaco

Reputation: 815

The simple answer is you can't do that, but here is an example of a way you can accomplish the same goal without a lot of work.

You will need to have two Person classes, one that has PhoneNumber with the DataMember attribute and one that doesn't. The best way to do this would probably be to create two sub classes from the main Person class

[DataContract]
public class PersonWithoutPhone
{
    private Person _p;

    public PersonWithoutPhone(Person p)
    {
        _p = p;
    }

    [DataMember]
    internal string FullName
    {
        get { return _p.FullName; }
    }

    [DataMember]
    private int Age
    {
        get { return _p.Age; }
    }

    public string TelephoneNumber
    {
        get { return _p.TelephonNumber; }
    }
}

[DataContract]
public class PersonWithPhone
{
    private Person _p;

    public PersonWithoutPhone(Person p)
    {
        _p = p;
    }

    [DataMember]
    internal string FullName
    {
        get { return _p.FullName; }
    }

    [DataMember]
    private int Age
    {
        get { return _p.Age; }
    }

    [DataMember]
    public string TelephoneNumber
    {
        get { return _p.TelephonNumber; }
    }
}

Upvotes: 1

Related Questions