views:

31

answers:

1

Im using DataContractJsonSerializer to serealize this class:

public class User
{

    public string id { get; set; }
    public string name { get; set; }
    public string password { get; set; }
    public string email { get; set; }
    public bool is_broker { get; set; }
    public string branch_id { get; set; }
    public string created_at { get; set; }
    public string updated_at { get; set; }
    public UserGroup UserGroup {get;set;}
    public UserAddress UserAddress { get; set; }
    public List<UserContact> UserContact {get; set;}

    public User()
    {
        UserGroup = new UserGroup();
        UserAddress = new UserAddress();
        UserContact = new List<UserContact>();
    }
}

The question is when i serealize to json , the property UserGroup is serealized, but i dont need this, i like to serealize to json whitout UserGroup property.

Obs: When Deserealize it´s all fine to have UserGroup, i need that!!

Any Trick ?????? Thanks!!!

A: 

You can decorate them like the following simple example

[DataContract]
public class Customer
{
    [IgnoreDataMember]
    public Age Age { get; set; }

    [DataMember]
    public string Name { get; set; }
}

However with this approach the Age will not set when deserializing (it will be null). So if you need it when you deserialize I would recommend not putting attributes on your class.

Jeppe Vammen Kristensen