serialize different properties with conditions

Feb 19, 2012 at 10:13 PM

Hi all,

I'm using Json.net a few months and I like it. I have a question / problem about serialization for my new project. Let me introduce my class structure a bit;

Promotion.cs

public Guid Id { get; set; }
public string Name { get; set; }

[JsonConverter(typeof(IsoDateTimeConverter))]
public DateTime StartDate { get; set; }

[JsonConverter(typeof(IsoDateTimeConverter))]
public DateTime EndDate { get; set; }

public string Details { get; set; }
public Category Category { get; set; }
public PromotionType TypeId { get; set; }
public Company.Company CompanyId { get; set; }
public List<PromotionFile> Files { get; set; }
public List<Checkin> Checkins { get; set; }
public List<Like> Likes { get; set; }
public Status Status { get; set; }
public string ApiLink { get; set; }
public int TotalCheckins { get; set; }
public int TotalLikes { get; set; }
public int TotalShares { get; set; }

and PromotionFile.cs

public Guid Id { get; set; }
public Promotion PromotionId { get; set; }
public string Name { get; set; }
public FileType FileTypeId { get; set; }

 

as you can see I return "PromotionId" property as a Promotion class. There is no problem, this is normal flow for my project. This classes have

[JsonObject(Newtonsoft.Json.MemberSerialization.OptOut)] attribute for serialization and return all properties (exclude Json-Ignore properties).

When I serialize PromotionFile.cs, it returns all Promotion.cs properties becase of this 

public Promotion PromotionId { get; set; }

Can I return some properties that I need? For exaple when I want serialize Promotion.cs, I use

JsonConvert.SerializeObject(Promotion, Formatting.Indented);

but when I want serialize PromotionFile.cs, should not return all Promotion.cs 's properties, only Id and name

 

Can you help me about this issue?

 

Regars 

Emre